aGrUM 2.3.2
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-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
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53
54namespace gum {
55
56 namespace threadsOMP {
57
59 template < typename FUNCTION, typename... ARGS >
60 void ThreadExecutor::execute(std::size_t nb_threads, FUNCTION exec_func, ARGS&&... func_args) {
61# ifndef _OPENMP
62 // without openMP we only have one thread available
63 exec_func(0, 1, std::forward< ARGS >(func_args)...);
64# else
65 if (nb_threads <= 1) {
66 exec_func(0, 1, std::forward< ARGS >(func_args)...);
67 } else {
68 // indicate that we start a new threadExecutor
70
71 // here, we shall create one std::exception_ptr for each openMP thread
72 // that will be created. This will allow us to catch the exception raised
73 // by the threads
74 std::vector< std::exception_ptr > func_exceptions(nb_threads, nullptr);
75
76 // launch the threads and wait for their completion
77# pragma omp parallel num_threads(int(nb_threads))
78 {
79 // get the number of the thread
80 const std::size_t this_thread = omp_get_thread_num();
81
82 try {
83 exec_func(this_thread, nb_threads, std::forward< ARGS >(func_args)...);
84 } catch (...) { func_exceptions[this_thread] = std::current_exception(); }
85 }
86
87 // now, we have completed the execution of the ThreadExecutor
89
90 // now, check if one exception has been raised
91 for (const auto& exc: func_exceptions) {
92 if (exc != nullptr) { std::rethrow_exception(exc); }
93 }
94 }
95# endif // _OPENMP
96 }
97
99 template < typename FUNC1, typename FUNC2, typename... ARGS >
100 void ThreadExecutor::executeOrUndo(std::size_t nb_threads,
101 FUNC1 exec_func,
102 FUNC2 undo_func,
103 ARGS&&... func_args) {
104# ifndef _OPENMP
105 // without openMP we only have one thread available
106 try {
107 exec_func(0, 1, std::forward< ARGS >(func_args)...);
108 } catch (...) {
109 undo_func(0, 1, std::forward< ARGS >(func_args)...);
110 throw;
111 }
112# else
113 if (nb_threads <= 1) {
114 try {
115 exec_func(0, 1, std::forward< ARGS >(func_args)...);
116 } catch (...) {
117 undo_func(0, 1, std::forward< ARGS >(func_args)...);
118 throw;
119 }
120 } else {
121 // indicate that we start a new threadExecutor
123
124 // here, we shall create one std::exception_ptr for each thread openMP
125 // that will be created. This will allow us to catch the exception raised
126 // by the threads
127 std::vector< std::exception_ptr > func_exceptions(nb_threads, nullptr);
128
129 // launch the threads and waith for their completion
130# pragma omp parallel num_threads(int(nb_threads))
131 {
132 // get the number of the thread
133 const std::size_t this_thread = getThreadNumber();
134
135 try {
136 exec_func(this_thread, nb_threads, std::forward< ARGS >(func_args)...);
137 } catch (...) { func_exceptions[this_thread] = std::current_exception(); }
138 }
139
140 // now, check if one exception has been raised
141 bool exception_raised = false;
142 for (const auto& exc: func_exceptions) {
143 if (exc != nullptr) {
144 exception_raised = true;
145 break;
146 }
147 }
148
149 if (exception_raised) {
150 // create the exceptions to catch during the repair threads executions
151 std::vector< std::exception_ptr > undo_func_exceptions(nb_threads, nullptr);
152
153 // launch the repair threads
154# pragma omp parallel num_threads(int(nb_threads))
155 {
156 // get the number of the thread
157 const std::size_t this_thread = getThreadNumber();
158
159 try {
160 undo_func(this_thread, nb_threads, std::forward< ARGS >(func_args)...);
161 } catch (...) { undo_func_exceptions[this_thread] = std::current_exception(); }
162 }
163
164 // now, we have completed the execution of the ThreadExecutor
166
167 // rethrow the exception
168 for (const auto& exc: func_exceptions) {
169 if (exc != nullptr) { std::rethrow_exception(exc); }
170 }
171 } else {
172 // now, we have completed the execution of the ThreadExecutor
174 }
175 }
176# endif // _OPENMP
177 }
178
179 } /* namespace threadsOMP */
180
181} /* namespace gum */
182
183#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.