aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
databaseTable_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
49
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52namespace gum {
53
54 namespace learning {
55
56 // a method to process the rows of the database in multithreading
57 template < typename Functor1, typename Functor2 >
58 void DatabaseTable::_threadProcessDatabase_(Functor1& exec_func, Functor2& undo_func) {
59 // compute the number of threads to execute the code
60 const std::size_t nb_threads = this->nbProcessingThreads_();
61
62 // if there is just one thread, let it process all the rows
63 if (nb_threads == 1) {
64 const std::size_t db_size = this->nbRows();
65 exec_func(std::size_t(0), db_size, 0);
66 return;
67 }
68
69 // if there are multiple threads, compute the ranges of rows they should process
70 const std::vector< std::pair< std::size_t, std::size_t > > ranges
71 = this->rangesProcessingThreads_(nb_threads);
72
73 // here, we shall create the threads, but also one std::exception_ptr
74 // for each thread. This will allow us to catch the exception raised
75 // by the threads
76 std::vector< std::thread > threads;
77 threads.reserve(nb_threads);
78 std::vector< std::exception_ptr > func_exceptions(nb_threads, nullptr);
79
80 // create a lambda that will execute exec_func while catching its exceptions
81 auto real_exec_func = [&exec_func](std::size_t begin,
82 std::size_t end,
83 std::size_t index,
84 std::exception_ptr& exc) -> void {
85 try {
86 exec_func(begin, end, index);
87 } catch (...) { exc = std::current_exception(); }
88 };
89
90 // launch the threads
91 for (std::size_t i = std::size_t(0); i < nb_threads; ++i) {
92 threads.push_back(std::thread(std::ref(real_exec_func),
93 ranges[i].first,
94 ranges[i].second,
95 i,
96 std::ref(func_exceptions[i])));
97 }
98
99 // wait for the threads to complete their executions
100 std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
101
102 // now, check if one exception has been raised
103 bool exception_raised = false;
104 for (const auto& exc: func_exceptions) {
105 if (exc != nullptr) {
106 exception_raised = true;
107 break;
108 }
109 }
110
111 if (exception_raised) {
112 // create a lambda that will execute undo_func while catching
113 // its exceptions
114 auto real_undo_func = [&undo_func](std::size_t begin,
115 std::size_t end,
116 std::size_t index,
117 std::exception_ptr& exc) -> void {
118 try {
119 undo_func(begin, end, index);
120 } catch (...) { exc = std::current_exception(); }
121 };
122
123 // launch the repair threads
124 threads.clear();
125 std::vector< std::exception_ptr > undo_func_exceptions(nb_threads, nullptr);
126 for (std::size_t i = std::size_t(0); i < nb_threads; ++i) {
127 // we just need to repair the threads that did not raise exceptions
128 if (func_exceptions[i] == nullptr)
129 threads.push_back(std::thread(std::ref(real_undo_func),
130 ranges[i].first,
131 ranges[i].second,
132 i,
133 std::ref(undo_func_exceptions[i])));
134 }
135
136 // wait for the threads to complete their executions
137 std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
138
139 // rethrow the exception
140 for (const auto& exc: func_exceptions) {
141 if (exc != nullptr) { std::rethrow_exception(exc); }
142 }
143 }
144 }
145
146 } /* namespace learning */
147
148} /* namespace gum */
149
150#endif /* DOXYGEN_SHOULD_SKIP_THIS */
const iterator & end() const noexcept
std::vector< std::pair< std::size_t, std::size_t > > rangesProcessingThreads_(const std::size_t nb_threads) const
The class representing a tabular database stored in RAM.
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46