22#ifndef TLP_PARALLEL_TOOLS_H
23#define TLP_PARALLEL_TOOLS_H
25#include <tulip/tulipconf.h>
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)))
61class TLP_SCOPE ThreadManager {
63 static unsigned int maxNumberOfThreads;
65#if !defined(TLP_NO_THREADS) && !defined(_OPENMP)
68 static void allocateThreadNumber();
71 static void freeThreadNumber();
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();
82 return new std::thread(thrdFunction, begin, end);
88#if !defined(TLP_NO_THREADS) && !defined(_OPENMP)
92 template <
typename runFunction>
93 static inline std::thread *launchThread(
const runFunction &f) {
94 auto thrdFunction = [&]() {
95 allocateThreadNumber();
99 return new std::thread(thrdFunction);
107 static unsigned int getNumberOfProcs();
112 static unsigned int getNumberOfThreads();
117 static void setNumberOfThreads(
unsigned int nbThreads);
122 static unsigned int getThreadNumber();
130 template <
typename ThreadFunction>
131 static inline void iterate(
size_t maxId,
const ThreadFunction &threadFunction) {
132#ifndef TLP_NO_THREADS
137 size_t nbPerThread = maxId == 1 ? 1 : std::max(maxId / (maxNumberOfThreads - 1),
size_t(2));
139 size_t begin = 0, end = nbPerThread;
140 maxId -= end - begin;
142 std::vector<std::thread *> threads;
143 threads.reserve(maxNumberOfThreads - 1);
145 threads.push_back(launchThread(threadFunction, begin, end));
146 if (nbPerThread > 1) {
147 if (maxNumberOfThreads - threads.size() == maxId)
151 end += std::min(nbPerThread, maxId);
152 maxId -= end - begin;
155 threadFunction(begin, end);
157 for (
auto thrd : threads) {
162 threadFunction(0, maxId);
169#ifndef TLP_NO_THREADS
171#define TLP_MAX_NB_THREADS 128
173#define TLP_NB_THREADS tlp::ThreadManager::getNumberOfThreads()
195#define TLP_LOCK_SECTION(mtx) \
196 static tlp::OpenMPLock mtx; \
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()
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)
217#define TLP_LOCK_SECTION(mtx) \
218 static std::mutex mtx; \
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()
231#define TLP_MAX_NB_THREADS 1
233#define TLP_NB_THREADS 1
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)
271template <
typename IdxFunction>
272void inline TLP_PARALLEL_MAP_INDICES(
size_t maxIdx,
const IdxFunction &idxFunction) {
275 for (OMP_ITER_TYPE i = 0; i < OMP_ITER_TYPE(maxIdx); ++i) {
279 auto threadFunction = [&](
size_t begin,
size_t end) {
280 for (; begin < end; ++begin) {
284 ThreadManager::iterate(maxIdx, threadFunction);
288template <
typename EltType,
typename IdxFunction>
289void inline TLP_PARALLEL_MAP_VECTOR(
const std::vector<EltType> &vect,
290 const IdxFunction &idxFunction) {
292 auto maxIdx = vect.size();
294 for (OMP_ITER_TYPE i = 0; i < OMP_ITER_TYPE(maxIdx); ++i) {
295 idxFunction(vect[i]);
298 auto threadFunction = [&](
size_t begin,
size_t end) {
299 for (; begin < end; ++begin) {
300 idxFunction(vect[begin]);
304 ThreadManager::iterate(vect.size(), threadFunction);
308template <
typename EltType,
typename IdxFunction>
309void inline TLP_PARALLEL_MAP_VECTOR_AND_INDICES(
const std::vector<EltType> &vect,
310 const IdxFunction &idxFunction) {
312 auto maxIdx = vect.size();
314 for (OMP_ITER_TYPE i = 0; i < OMP_ITER_TYPE(maxIdx); ++i) {
315 idxFunction(vect[i], i);
318 auto threadFunction = [&](
size_t begin,
size_t end) {
319 for (; begin < end; ++begin) {
320 idxFunction(vect[begin], begin);
324 ThreadManager::iterate(vect.size(), threadFunction);
328template <
typename F1,
typename F2>
329void inline TLP_PARALLEL_SECTIONS(
const F1 &f1,
const F2 &f2) {
330#ifndef TLP_NO_THREADS
333 OMP(sections nowait) {
343 auto thrd = ThreadManager::launchThread(f1);
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
359 OMP(sections nowait) {
372 auto thrd1 = ThreadManager::launchThread(f1);
373 auto thrd2 = ThreadManager::launchThread(f2);
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
392 OMP(sections nowait) {
408 auto thrd1 = ThreadManager::launchThread(f1);
409 auto thrd2 = ThreadManager::launchThread(f2);
410 auto thrd3 = ThreadManager::launchThread(f3);