Tulip 6.1.0
Large graphs analysis and drawing
Loading...
Searching...
No Matches
ParallelTools.h
1/*
2 *
3 * This file is part of Tulip (https://tulip.labri.fr)
4 *
5 * Authors: David Auber and the Tulip development Team
6 * from LaBRI, University of Bordeaux
7 *
8 * Tulip is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * Tulip is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
17 *
18 */
19
20///@cond DOXYGEN_HIDDEN
21
22#ifndef TLP_PARALLEL_TOOLS_H
23#define TLP_PARALLEL_TOOLS_H
24
25#include <tulip/tulipconf.h>
26#include <vector>
27
28#ifndef TLP_NO_THREADS
29
30#ifdef _OPENMP
31typedef size_t OMP_ITER_TYPE;
32#define OMP(x) _Pragma(STRINGIFY(omp x))
33#define OMP_CRITICAL_SECTION(x) _Pragma(STRINGIFY(omp critical(x)))
34
35#ifdef __APPLE__
36struct omp_lock_t;
37#endif
38
39#else
40
41// OpenMP not available use C++11 threads
42#include <iostream>
43#include <algorithm>
44#include <mutex>
45#include <thread>
46
47#endif
48
49#endif
50
51namespace tlp {
52
53class TlpThread;
54// ===================================================================================
55
56/**
57 * @brief Static wrapper class around std::thread
58 *
59 * @since Tulip 5.2
60 */
61class TLP_SCOPE ThreadManager {
62
63 static unsigned int maxNumberOfThreads;
64
65#if !defined(TLP_NO_THREADS) && !defined(_OPENMP)
66
67 // allocate a number for the calling thread
68 static void allocateThreadNumber();
69
70 // deallocate the number of the calling thread
71 static void freeThreadNumber();
72
73 // create a thread dedicated to the execution of a function
74 // used to iterate between begin and end indices
75 template <typename runFunction>
76 static inline std::thread *launchThread(const runFunction &f, uint begin, uint end) {
77 auto thrdFunction = [&](uint begin, uint end) {
78 allocateThreadNumber();
79 f(begin, end);
80 freeThreadNumber();
81 };
82 return new std::thread(thrdFunction, begin, end);
83 }
84
85#endif
86
87public:
88#if !defined(TLP_NO_THREADS) && !defined(_OPENMP)
89
90 // create a thread dedicated to the execution of a function
91 // with no arguments
92 template <typename runFunction>
93 static inline std::thread *launchThread(const runFunction &f) {
94 auto thrdFunction = [&]() {
95 allocateThreadNumber();
96 f();
97 freeThreadNumber();
98 };
99 return new std::thread(thrdFunction);
100 }
101
102#endif
103
104 /**
105 * Returns the number of processors on the host system.
106 */
107 static unsigned int getNumberOfProcs();
108
109 /**
110 * Returns the number of threads used by default in subsequent OpenMP parallel sections.
111 */
112 static unsigned int getNumberOfThreads();
113
114 /**
115 * Sets the number of threads used by default in subsequent parallel sections.
116 */
117 static void setNumberOfThreads(unsigned int nbThreads);
118
119 /**
120 * Returns the current thread number.
121 */
122 static unsigned int getThreadNumber();
123
124#ifndef _OPENMP
125
126 /**
127 * Parallel iteration of the same function over partitioned ranges
128 * between 0 and maxId
129 */
130 template <typename ThreadFunction>
131 static inline void iterate(size_t maxId, const ThreadFunction &threadFunction) {
132#ifndef TLP_NO_THREADS
133 if (maxId == 0)
134 return;
135
136 // compute the size of range indices for each thread
137 size_t nbPerThread = maxId == 1 ? 1 : std::max(maxId / (maxNumberOfThreads - 1), size_t(2));
138 // begin and end indices of thread partition
139 size_t begin = 0, end = nbPerThread;
140 maxId -= end - begin;
141 // create threads
142 std::vector<std::thread *> threads;
143 threads.reserve(maxNumberOfThreads - 1);
144 while (maxId) {
145 threads.push_back(launchThread(threadFunction, begin, end));
146 if (nbPerThread > 1) {
147 if (maxNumberOfThreads - threads.size() == maxId)
148 nbPerThread = 1;
149 }
150 begin = end;
151 end += std::min(nbPerThread, maxId);
152 maxId -= end - begin;
153 }
154 // iterate on last partition
155 threadFunction(begin, end);
156 // wait for threads
157 for (auto thrd : threads) {
158 thrd->join();
159 delete thrd;
160 }
161#else
162 threadFunction(0, maxId);
163#endif
164 }
165
166#endif
167};
168
169#ifndef TLP_NO_THREADS
170
171#define TLP_MAX_NB_THREADS 128
172
173#define TLP_NB_THREADS tlp::ThreadManager::getNumberOfThreads()
174
175#ifdef _OPENMP
176
177#ifdef __APPLE__
178
179// In order to workaround an odd phenomenon on some MacOS runtimes when an application
180// shutdowns, use OpenMP C API to declare critical sections instead of directives.
181// https://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=29031&start=15#p129707
182// gives more details about the issue
183
184class OpenMPLock {
185public:
186 OpenMPLock();
187 ~OpenMPLock();
188 void lock();
189 void unlock();
190
191private:
192 omp_lock_t *_lock;
193};
194
195#define TLP_LOCK_SECTION(mtx) \
196 static tlp::OpenMPLock mtx; \
197 mtx.lock();
198#define TLP_UNLOCK_SECTION(mtx) mtx.unlock();
199#define TLP_DECLARE_GLOBAL_LOCK(mtx) extern tlp::OpenMPLock mtx
200#define TLP_DEFINE_GLOBAL_LOCK(mtx) tlp::OpenMPLock mtx
201#define TLP_GLOBALLY_LOCK_SECTION(mtx) mtx.lock();
202#define TLP_GLOBALLY_UNLOCK_SECTION(mtx) mtx.unlock()
203
204#else // __APPLE__
205
206#define TLP_LOCK_SECTION(mtx) OMP_CRITICAL_SECTION(mtx)
207#define TLP_UNLOCK_SECTION(mtx)
208#define TLP_DECLARE_GLOBAL_LOCK(mtx) extern void mtx()
209#define TLP_DEFINE_GLOBAL_LOCK(mtx) extern void mtx()
210#define TLP_GLOBALLY_LOCK_SECTION(mtx) OMP_CRITICAL_SECTION(mtx)
211#define TLP_GLOBALLY_UNLOCK_SECTION(mtx)
212
213#endif // __APPLE__
214
215#else
216
217#define TLP_LOCK_SECTION(mtx) \
218 static std::mutex mtx; \
219 mtx.lock();
220#define TLP_UNLOCK_SECTION(mtx) mtx.unlock()
221#define TLP_DECLARE_GLOBAL_LOCK(mtx) extern std::mutex mtx
222#define TLP_DEFINE_GLOBAL_LOCK(mtx) std::mutex mtx
223#define TLP_GLOBALLY_LOCK_SECTION(mtx) mtx.lock();
224#define TLP_GLOBALLY_UNLOCK_SECTION(mtx) mtx.unlock()
225
226#endif
227
228#else
229
230// no multi-threading
231#define TLP_MAX_NB_THREADS 1
232
233#define TLP_NB_THREADS 1
234
235#define TLP_LOCK_SECTION(mtx)
236#define TLP_UNLOCK_SECTION(mtx)
237#define TLP_DECLARE_GLOBAL_LOCK(mtx) extern void mtx()
238#define TLP_DEFINE_GLOBAL_LOCK(mtx) extern void mtx()
239#define TLP_GLOBALLY_LOCK_SECTION(mtx)
240#define TLP_GLOBALLY_UNLOCK_SECTION(mtx)
241
242#endif
243
244// ===================================================================================
245
246/**
247 * Template function to ease the creation of parallel threads taking
248 * an index as parameter (0 <= index < maxIdx).
249 *
250 * @since Tulip 5.2
251 *
252 * @param maxIdx the upper bound exclusive of the indices range
253 * @param idxFunction callable object (e.g. lambda function) taking an unsigned integer as parameter
254 *
255 * Example of use:
256 *
257 * @code
258 * auto computationIntensiveTask = [&](unsigned int id) {
259 * double result = 0;
260 * ...
261 * return result;
262 * };
263 * const unsigned int N = ... ;
264 * std::vector<double> result(N);
265 * TLP_PARALLEL_MAP_INDICES(N, [&](unsigned int i) {
266 * // run task in a thread
267 * result[i] = computationIntensiveTask(i);
268 * });
269 * @endcode
270 */
271template <typename IdxFunction>
272void inline TLP_PARALLEL_MAP_INDICES(size_t maxIdx, const IdxFunction &idxFunction) {
273#ifdef _OPENMP
274 OMP(parallel for)
275 for (OMP_ITER_TYPE i = 0; i < OMP_ITER_TYPE(maxIdx); ++i) {
276 idxFunction(i);
277 }
278#else
279 auto threadFunction = [&](size_t begin, size_t end) {
280 for (; begin < end; ++begin) {
281 idxFunction(begin);
282 }
283 };
284 ThreadManager::iterate(maxIdx, threadFunction);
285#endif
286}
287
288template <typename EltType, typename IdxFunction>
289void inline TLP_PARALLEL_MAP_VECTOR(const std::vector<EltType> &vect,
290 const IdxFunction &idxFunction) {
291#ifdef _OPENMP
292 auto maxIdx = vect.size();
293 OMP(parallel for)
294 for (OMP_ITER_TYPE i = 0; i < OMP_ITER_TYPE(maxIdx); ++i) {
295 idxFunction(vect[i]);
296 }
297#else
298 auto threadFunction = [&](size_t begin, size_t end) {
299 for (; begin < end; ++begin) {
300 idxFunction(vect[begin]);
301 }
302 };
303
304 ThreadManager::iterate(vect.size(), threadFunction);
305#endif
306}
307
308template <typename EltType, typename IdxFunction>
309void inline TLP_PARALLEL_MAP_VECTOR_AND_INDICES(const std::vector<EltType> &vect,
310 const IdxFunction &idxFunction) {
311#ifdef _OPENMP
312 auto maxIdx = vect.size();
313 OMP(parallel for)
314 for (OMP_ITER_TYPE i = 0; i < OMP_ITER_TYPE(maxIdx); ++i) {
315 idxFunction(vect[i], i);
316 }
317#else
318 auto threadFunction = [&](size_t begin, size_t end) {
319 for (; begin < end; ++begin) {
320 idxFunction(vect[begin], begin);
321 }
322 };
323
324 ThreadManager::iterate(vect.size(), threadFunction);
325#endif
326}
327
328template <typename F1, typename F2>
329void inline TLP_PARALLEL_SECTIONS(const F1 &f1, const F2 &f2) {
330#ifndef TLP_NO_THREADS
331#ifdef _OPENMP
332 OMP(parallel) {
333 OMP(sections nowait) {
334 OMP(section) {
335 f1();
336 }
337 }
338 OMP(masked) {
339 f2();
340 }
341 }
342#else
343 auto thrd = ThreadManager::launchThread(f1);
344 f2();
345 thrd->join();
346 delete thrd;
347#endif
348#else
349 f1();
350 f2();
351#endif
352}
353
354template <typename F1, typename F2, typename F3>
355void inline TLP_PARALLEL_SECTIONS(const F1 &f1, const F2 &f2, const F3 &f3) {
356#ifndef TLP_NO_THREADS
357#ifdef _OPENMP
358 OMP(parallel) {
359 OMP(sections nowait) {
360 OMP(section) {
361 f1();
362 }
363 OMP(section) {
364 f2();
365 }
366 }
367 OMP(masked) {
368 f3();
369 }
370 }
371#else
372 auto thrd1 = ThreadManager::launchThread(f1);
373 auto thrd2 = ThreadManager::launchThread(f2);
374 f3();
375 thrd1->join();
376 thrd2->join();
377 delete thrd1;
378 delete thrd2;
379#endif
380#else
381 f1();
382 f2();
383 f3();
384#endif
385}
386
387template <typename F1, typename F2, typename F3, typename F4>
388void inline TLP_PARALLEL_SECTIONS(const F1 &f1, const F2 &f2, const F3 &f3, const F4 &f4) {
389#ifndef TLP_NO_THREADS
390#ifdef _OPENMP
391 OMP(parallel) {
392 OMP(sections nowait) {
393 OMP(section) {
394 f1();
395 }
396 OMP(section) {
397 f2();
398 }
399 OMP(section) {
400 f3();
401 }
402 }
403 OMP(masked) {
404 f4();
405 }
406 }
407#else
408 auto thrd1 = ThreadManager::launchThread(f1);
409 auto thrd2 = ThreadManager::launchThread(f2);
410 auto thrd3 = ThreadManager::launchThread(f3);
411 f4();
412 thrd1->join();
413 thrd2->join();
414 thrd3->join();
415 delete thrd1;
416 delete thrd2;
417 delete thrd3;
418#endif
419#else
420 f1();
421 f2();
422 f3();
423 f4();
424#endif
425}
426} // namespace tlp
427
428#endif // TLP_PARALLEL_TOOLS_H
429
430///@endcond