1#ifndef DUNE_FEM_OMPMANAGER_HH
2#define DUNE_FEM_OMPMANAGER_HH
10#warning "pthreads were not found!"
15#if defined _OPENMP || defined(USE_PTHREADS)
16#ifndef USE_SMP_PARALLEL
17#define USE_SMP_PARALLEL
29#include <dune/common/visibility.hh>
65 static inline int thread() {
return 0; }
68 static inline bool isMaster() {
return true ; }
78 struct OpenMPThreadManager :
public EmptyThreadManager
81 static constexpr bool pthreads = false ;
86 static inline int maxThreads()
88 return omp_get_max_threads();
92 static inline int currentThreads()
94 return omp_get_num_threads();
98 static inline int thread()
100 return omp_get_thread_num();
104 static inline bool isMaster()
106 return thread() == 0 ;
110 static inline void setMaxNumberThreads(
const int numThreads )
112 omp_set_num_threads( numThreads );
115 static inline void initMultiThreadMode(
const int numThreads )
117 omp_set_num_threads( numThreads );
121 static inline bool singleThreadMode()
123 return currentThreads() == 1 ;
130 struct PThreadsManager
133 static constexpr bool pthreads = true ;
138 DUNE_EXPORT
static inline Manager& instance()
140 static thread_local Manager mg ;
143 DUNE_EXPORT
int &maxThreads_()
145 static int maxThreads =
std::max(1u, std::thread::hardware_concurrency());
149 inline void initThread(
const int maxThreads,
const int threadNum )
152 maxThreads_() = maxThreads;
153 threadNum_ = threadNum ;
156 inline void singleThreadMode()
161 inline void multiThreadMode(
const int nThreads )
163 activeThreads_ = nThreads;
166 inline int maxThreads() {
return maxThreads_(); }
167 inline int currentThreads()
const {
return activeThreads_; }
170 assert( threadNum_ >= 0 );
179 : threadNum_( 0 ), activeThreads_( 1 )
183 static inline Manager& manager()
185 return Manager :: instance();
193 static inline void initSingleThreadMode()
195 manager().singleThreadMode();
199 static inline void initMultiThreadMode(
const int nThreads )
201 manager().multiThreadMode( nThreads );
205 static inline void initThread(
const int maxThreads,
const int threadNum )
207 manager().initThread( maxThreads, threadNum );
214 static inline int maxThreads()
216 return manager().maxThreads();
220 static inline int currentThreads()
222 return manager().currentThreads();
226 static inline int thread()
228 return manager().thread();
232 static inline void setMaxNumberThreads(
const int numThreads )
235 manager().initThread( numThreads, 0 );
239 static inline bool isMaster()
241 return thread() == 0;
245 static inline bool singleThreadMode()
247 return currentThreads() == 1 ;
255#warning "ThreadManager: using OpenMP"
258#elif defined(USE_PTHREADS)
261#warning "ThreadManager: using pthreads"
double max(const Dune::Fem::Double &v, const double p)
Definition: double.hh:965
Definition: bindguard.hh:11
Definition: threadmanager.hh:45
static void setMaxNumberThreads(const int numThreads)
set maximal number of threads available during run
Definition: threadmanager.hh:71
static bool isMaster()
return true if the current thread is the master thread (i.e. thread 0)
Definition: threadmanager.hh:68
static void initSingleThreadMode()
initialize single thread mode (when in multithread mode)
Definition: threadmanager.hh:50
static void initThread(const int maxThreads, const int threadNum)
set max number of threads and thread number for this thread
Definition: threadmanager.hh:56
static void initMultiThreadMode(const int nThreads)
initialize multi thread mode (when in single thread mode)
Definition: threadmanager.hh:53
static int currentThreads()
return number of current threads
Definition: threadmanager.hh:62
static int maxThreads()
return maximal number of threads possbile in the current run
Definition: threadmanager.hh:59
static bool singleThreadMode()
returns true if program is operating on one thread currently
Definition: threadmanager.hh:74
static constexpr bool pthreads
true if pthreads are used
Definition: threadmanager.hh:47
static int thread()
return thread number
Definition: threadmanager.hh:65
The ThreadManager wrapps basic shared memory functionality provided by OpenMP or pthreads such as thr...