aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
threadExecutorOMP_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
52#ifndef DOXYGEN_SHOULD_SKIP_THIS
53
54
55namespace gum {
56
57 namespace threadsOMP {
58
60 template < typename FUNCTION, typename... ARGS >
61 void ThreadExecutor::execute(std::size_t nb_threads, FUNCTION exec_func, ARGS&&... func_args) {
62# ifndef _OPENMP
63 // without openMP we only have one thread available
64 exec_func(0, 1, std::forward< ARGS >(func_args)...);
65# else
66 if (nb_threads <= 1) {
67 exec_func(0, 1, std::forward< ARGS >(func_args)...);
68 } else {
69 // allocate before incrementing so a bad_alloc leaves the counter unchanged
70 std::vector< std::exception_ptr > func_exceptions(nb_threads, nullptr);
71
72 // indicate that we start a new threadExecutor
74
75 // launch the threads and wait for their completion
76# pragma omp parallel num_threads(int(nb_threads))
77 {
78 // get the number of the thread
79 const std::size_t this_thread = omp_get_thread_num();
80
81 try {
82 exec_func(this_thread, nb_threads, func_args...);
83 } catch (...) { func_exceptions[this_thread] = std::current_exception(); }
84 }
85
86 // now, we have completed the execution of the ThreadExecutor
88
89 // now, check if one exception has been raised
90 for (const auto& exc: func_exceptions) {
91 if (exc != nullptr) { std::rethrow_exception(exc); }
92 }
93 }
94# endif // _OPENMP
95 }
96
98 template < typename FUNC1, typename FUNC2, typename... ARGS >
99 void ThreadExecutor::executeOrUndo(std::size_t nb_threads,
100 FUNC1 exec_func,
101 FUNC2 undo_func,
102 ARGS&&... func_args) {
103# ifndef _OPENMP
104 // without openMP we only have one thread available
105 try {
106 exec_func(0, 1, std::forward< ARGS >(func_args)...);
107 } catch (...) {
108 undo_func(0, 1, std::forward< ARGS >(func_args)...);
109 throw;
110 }
111# else
112 if (nb_threads <= 1) {
113 try {
114 exec_func(0, 1, std::forward< ARGS >(func_args)...);
115 } catch (...) {
116 undo_func(0, 1, std::forward< ARGS >(func_args)...);
117 throw;
118 }
119 } else {
120 // indicate that we start a new threadExecutor
122
123 // here, we shall create one std::exception_ptr for each thread openMP
124 // that will be created. This will allow us to catch the exception raised
125 // by the threads
126 std::vector< std::exception_ptr > func_exceptions(nb_threads, nullptr);
127
128 // launch the threads and waith for their completion
129# pragma omp parallel num_threads(int(nb_threads))
130 {
131 // get the number of the thread
132 const std::size_t this_thread = getThreadNumber();
133
134 try {
135 exec_func(this_thread, nb_threads, func_args...);
136 } catch (...) { func_exceptions[this_thread] = std::current_exception(); }
137 }
138
139 // now, check if one exception has been raised
140 bool exception_raised = false;
141 for (const auto& exc: func_exceptions) {
142 if (exc != nullptr) {
143 exception_raised = true;
144 break;
145 }
146 }
147
148 if (exception_raised) {
149 // create the exceptions to catch during the repair threads executions
150 std::vector< std::exception_ptr > undo_func_exceptions(nb_threads, nullptr);
151
152 // launch the repair threads
153# pragma omp parallel num_threads(int(nb_threads))
154 {
155 // get the number of the thread
156 const std::size_t this_thread = getThreadNumber();
157
158 try {
159 undo_func(this_thread, nb_threads, func_args...);
160 } catch (...) { undo_func_exceptions[this_thread] = std::current_exception(); }
161 }
162
163 // now, we have completed the execution of the ThreadExecutor
165
166 // rethrow the exception
167 for (const auto& exc: func_exceptions) {
168 if (exc != nullptr) { std::rethrow_exception(exc); }
169 }
170 } else {
171 // now, we have completed the execution of the ThreadExecutor
173 }
174 }
175# endif // _OPENMP
176 }
177
178 } /* namespace threadsOMP */
179
180} /* namespace gum */
181
182#endif /* DOXYGEN_SHOULD_SKIP_THIS */
static std::atomic< int > nbRunningThreadsExecutors_
he number of currently running ThreadExecutors
unsigned int getThreadNumber()
Get the calling thread id.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
static void execute(std::size_t nb_threads, FUNCTION exec_func, ARGS &&... func_args)
executes a function using several threads
static void executeOrUndo(std::size_t nb_threads, FUNC1 exec_func, FUNC2 undo_func, ARGS &&... func_args)
executes in parallel a function and undoes it if execptions are raised
A class to execute several threads by exploiting openMP.