aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53namespace gum {
54
55 namespace learning {
56
57 // a method to process the rows of the database in multithreading
58 template < typename Functor1, typename Functor2 >
59 void DatabaseTable::_threadProcessDatabase_(Functor1& exec_func, Functor2& undo_func) {
60 // compute the number of threads to execute the code
61 const std::size_t nb_threads = this->nbProcessingThreads_();
62
63 // if there is just one thread, let it process all the rows
64 if (nb_threads == 1) {
65 const std::size_t db_size = this->nbRows();
66 exec_func(std::size_t(0), db_size, 0);
67 return;
68 }
69
70 // if there are multiple threads, compute the ranges of rows they should process
71 const std::vector< std::pair< std::size_t, std::size_t > > ranges
72 = this->rangesProcessingThreads_(nb_threads);
73
74 // here, we shall create the threads, but also one std::exception_ptr
75 // for each thread. This will allow us to catch the exception raised
76 // by the threads
77 std::vector< std::thread > threads;
78 threads.reserve(nb_threads);
79 std::vector< std::exception_ptr > func_exceptions(nb_threads, nullptr);
80
81 // create a lambda that will execute exec_func while catching its exceptions
82 auto real_exec_func = [&exec_func](std::size_t begin,
83 std::size_t end,
84 std::size_t index,
85 std::exception_ptr& exc) -> void {
86 try {
87 exec_func(begin, end, index);
88 } catch (...) { exc = std::current_exception(); }
89 };
90
91 // launch the threads
92 for (std::size_t i = std::size_t(0); i < nb_threads; ++i) {
93 threads.push_back(std::thread(std::ref(real_exec_func),
94 ranges[i].first,
95 ranges[i].second,
96 i,
97 std::ref(func_exceptions[i])));
98 }
99
100 // wait for the threads to complete their executions
101 std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
102
103 // now, check if one exception has been raised
104 bool exception_raised = false;
105 for (const auto& exc: func_exceptions) {
106 if (exc != nullptr) {
107 exception_raised = true;
108 break;
109 }
110 }
111
112 if (exception_raised) {
113 // create a lambda that will execute undo_func while catching
114 // its exceptions
115 auto real_undo_func = [&undo_func](std::size_t begin,
116 std::size_t end,
117 std::size_t index,
118 std::exception_ptr& exc) -> void {
119 try {
120 undo_func(begin, end, index);
121 } catch (...) { exc = std::current_exception(); }
122 };
123
124 // launch the repair threads
125 threads.clear();
126 std::vector< std::exception_ptr > undo_func_exceptions(nb_threads, nullptr);
127 for (std::size_t i = std::size_t(0); i < nb_threads; ++i) {
128 // we just need to repair the threads that did not raise exceptions
129 if (func_exceptions[i] == nullptr)
130 threads.push_back(std::thread(std::ref(real_undo_func),
131 ranges[i].first,
132 ranges[i].second,
133 i,
134 std::ref(undo_func_exceptions[i])));
135 }
136
137 // wait for the threads to complete their executions
138 std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
139
140 // rethrow the exception
141 for (const auto& exc: func_exceptions) {
142 if (exc != nullptr) { std::rethrow_exception(exc); }
143 }
144 }
145 }
146
147 } /* namespace learning */
148
149} /* namespace gum */
150
151#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:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46