dune-fem  2.8-git
threadmanager.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_OMPMANAGER_HH
2 #define DUNE_FEM_OMPMANAGER_HH
3 
4 #include <cassert>
5 #include <cstdlib>
6 #include <thread>
7 
8 #ifdef USE_PTHREADS
9 #if HAVE_PTHREAD == 0
10 #warning "pthreads were not found!"
11 #undef USE_PTHREADS
12 #endif
13 #endif
14 
15 #if defined _OPENMP || defined(USE_PTHREADS)
16 #ifndef USE_SMP_PARALLEL
17 #define USE_SMP_PARALLEL
18 #endif
19 #endif
20 
21 #if HAVE_PTHREAD
22 #include <pthread.h>
23 #endif
24 
25 #ifdef _OPENMP
26 #include <omp.h>
27 #endif
28 
29 #include <dune/common/visibility.hh>
30 
31 namespace Dune
32 {
33 
34  namespace Fem
35  {
36 
45  {
47  static constexpr bool pthreads = false ;
48 
50  static inline void initSingleThreadMode() {}
51 
53  static inline void initMultiThreadMode( const int nThreads ) {}
54 
56  static inline void initThread( const int maxThreads, const int threadNum ) {}
57 
59  static inline int maxThreads() { return 1; }
60 
62  static inline int currentThreads() { return 1; }
63 
65  static inline int thread() { return 0; }
66 
68  static inline bool isMaster() { return true ; }
69 
71  static inline void setMaxNumberThreads( const int numThreads ) { }
72 
74  static inline bool singleThreadMode() { return true ; }
75  }; // end class ThreadManager
76 
77 #ifdef _OPENMP
78  struct OpenMPThreadManager : public EmptyThreadManager
79  {
81  static constexpr bool pthreads = false ;
82 
86  static inline int maxThreads()
87  {
88  return omp_get_max_threads();
89  }
90 
92  static inline int currentThreads()
93  {
94  return omp_get_num_threads();
95  }
96 
98  static inline int thread()
99  {
100  return omp_get_thread_num();
101  }
102 
104  static inline bool isMaster()
105  {
106  return thread() == 0 ;
107  }
108 
110  static inline void setMaxNumberThreads( const int numThreads )
111  {
112  omp_set_num_threads( numThreads );
113  }
114 
115  static inline void initMultiThreadMode( const int numThreads )
116  {
117  omp_set_num_threads( numThreads );
118  }
119 
121  static inline bool singleThreadMode()
122  {
123  return currentThreads() == 1 ;
124  }
125  }; // end class ThreadManager
126 #endif
127 
128 
129 #if HAVE_PTHREAD
130  struct PThreadsManager
131  {
133  static constexpr bool pthreads = true ;
134 
135  private:
136  struct Manager
137  {
138  DUNE_EXPORT static inline Manager& instance()
139  {
140  static thread_local Manager mg ;
141  return mg ;
142  }
143  DUNE_EXPORT int &maxThreads_()
144  {
145  static int maxThreads = std::max(1u, std::thread::hardware_concurrency());
146  return maxThreads;
147  }
148 
149  inline void initThread( const int maxThreads, const int threadNum )
150  {
151  // thread number 0 is reserved for the master thread
152  maxThreads_() = maxThreads;
153  threadNum_ = threadNum ;
154  }
155 
156  inline void singleThreadMode()
157  {
158  activeThreads_ = 1;
159  }
160 
161  inline void multiThreadMode(const int nThreads )
162  {
163  activeThreads_ = nThreads;
164  }
165 
166  inline int maxThreads() { return maxThreads_(); }
167  inline int currentThreads() const { return activeThreads_; }
168  inline int thread()
169  {
170  assert( threadNum_ >= 0 );
171  return threadNum_;
172  }
173 
174  private:
175  int threadNum_;
176  int activeThreads_;
177 
178  Manager()
179  : threadNum_( 0 ), activeThreads_( 1 )
180  {}
181  };
182 
183  static inline Manager& manager()
184  {
185  return Manager :: instance();
186  }
187 
188  public:
190  // begin of pthread specific interface
193  static inline void initSingleThreadMode()
194  {
195  manager().singleThreadMode();
196  }
197 
199  static inline void initMultiThreadMode( const int nThreads )
200  {
201  manager().multiThreadMode( nThreads );
202  }
203 
205  static inline void initThread( const int maxThreads, const int threadNum )
206  {
207  manager().initThread( maxThreads, threadNum );
208  }
209 
211  // INTERFACE
214  static inline int maxThreads()
215  {
216  return manager().maxThreads();
217  }
218 
220  static inline int currentThreads()
221  {
222  return manager().currentThreads();
223  }
224 
226  static inline int thread()
227  {
228  return manager().thread();
229  }
230 
232  static inline void setMaxNumberThreads( const int numThreads )
233  {
234  // this call also initiates the master thread
235  manager().initThread( numThreads, 0 );
236  }
237 
239  static inline bool isMaster()
240  {
241  return thread() == 0;
242  }
243 
245  static inline bool singleThreadMode()
246  {
247  return currentThreads() == 1 ;
248  }
249  }; // end class ThreadManager (pthreads)
250 #endif
251 
252 #ifdef _OPENMP
253 // in debug mode show which threading model is used
254 #ifndef NDEBUG
255 #warning "ThreadManager: using OpenMP"
256 #endif
257  using ThreadManager = OpenMPThreadManager;
258 #elif defined(USE_PTHREADS)
259 // in debug mode show which threading model is used
260 #ifndef NDEBUG
261 #warning "ThreadManager: using pthreads"
262 #endif
263  using ThreadManager = PThreadsManager;
264 #else
266 #endif
267 
268  } // namespace Fem
269 
270 } // namespace Dune
271 
272 #endif // #ifndef DUNE_FEM_OMPMANAGER_HH
Definition: bindguard.hh:11
static constexpr T max(T a)
Definition: utility.hh:77
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...