dune-fem  2.4.1-rc
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 
7 #if USE_PTHREADS && HAVE_PTHREAD == 0
8 #warning "pthreads were not found!"
9 #undef USE_PTHREADS
10 #endif
11 
12 #if defined _OPENMP || USE_PTHREADS == 1
13 #ifndef USE_SMP_PARALLEL
14 #define USE_SMP_PARALLEL
15 #endif
16 #endif
17 
18 #if USE_PTHREADS
19 #include <pthread.h>
20 #endif
21 
22 #ifdef _OPENMP
23 #include <omp.h>
24 #endif
25 
26 
27 namespace Dune
28 {
29 
30  namespace Fem
31  {
32 
33 #ifdef _OPENMP
34  struct ThreadManager
35  {
39  static inline int maxThreads()
40  {
41  return omp_get_max_threads();
42  }
43 
45  static inline int currentThreads()
46  {
47  return omp_get_num_threads();
48  }
49 
51  static inline int thread()
52  {
53  return omp_get_thread_num();
54  }
55 
57  static inline bool isMaster()
58  {
59  return thread() == 0 ;
60  }
61 
63  static inline void setMaxNumberThreads( const int numThreads )
64  {
65  omp_set_num_threads( numThreads );
66  }
67 
69  static inline bool singleThreadMode()
70  {
71  return currentThreads() == 1 ;
72  }
73  }; // end class ThreadManager
74 #elif USE_PTHREADS
75 #warning "ThreadManager: using pthreads"
76 
77  struct ThreadManager
78  {
79  private:
80  struct Manager
81  {
82  static inline Manager& instance()
83  {
84  static thread_local Manager mg ;
85  return mg ;
86  }
87 
88  inline void initThread( const int maxThreads, const int threadNum )
89  {
90  // thread number 0 is reserved for the master thread
91  maxThreads_ = maxThreads;
92  threadNum_ = threadNum ;
93  }
94 
95  inline void singleThreadMode()
96  {
97  activeThreads_ = 1;
98  }
99 
100  inline void multiThreadMode(const int nThreads )
101  {
102  activeThreads_ = nThreads;
103  }
104 
105  inline int maxThreads() const { return maxThreads_; }
106  inline int currentThreads() const { return activeThreads_; }
107  inline int thread()
108  {
109  assert( threadNum_ >= 0 );
110  return threadNum_;
111  }
112 
113  private:
114  int threadNum_;
115  int maxThreads_;
116  int activeThreads_;
117 
118  Manager()
119  : threadNum_( -1 ), maxThreads_( 1 ), activeThreads_( 1 )
120  {}
121  };
122 
123  static inline Manager& manager()
124  {
125  return Manager :: instance();
126  }
127 
128  public:
130  // begin of pthread specific interface
133  static inline void initSingleThreadMode()
134  {
135  manager().singleThreadMode();
136  }
137 
139  static inline void initMultiThreadMode( const int nThreads )
140  {
141  manager().multiThreadMode( nThreads );
142  }
143 
145  static inline void initThread( const int maxThreads, const int threadNum )
146  {
147  manager().initThread( maxThreads, threadNum );
148  }
149 
151  // INTERFACE
154  static inline int maxThreads()
155  {
156  return manager().maxThreads();
157  }
158 
160  static inline int currentThreads()
161  {
162  return manager().currentThreads();
163  }
164 
166  static inline int thread()
167  {
168  return manager().thread();
169  }
170 
172  static inline void setMaxNumberThreads( const int numThreads )
173  {
174  // this call also initiates the master thread
175  manager().initThread( numThreads, 0 );
176  }
177 
179  static inline bool isMaster()
180  {
181  return thread() == 0;
182  }
183 
185  static inline bool singleThreadMode()
186  {
187  return currentThreads() == 1 ;
188  }
189  }; // end class ThreadManager (pthreads)
190 
191 #else
192 
200  {
202  static inline int maxThreads() { return 1; }
203 
205  static inline int currentThreads() { return 1; }
206 
208  static inline int thread() { return 0; }
209 
211  static inline bool isMaster() { return true ; }
212 
214  static inline void setMaxNumberThreads( const int numThreads ) { }
215 
217  static inline bool singleThreadMode() { return true ; }
218  }; // end class ThreadManager
219 #endif
220 
221  } // namespace Fem
222 
223 } // namespace Dune
224 
225 #endif // #ifndef DUNE_FEM_OMPMANAGER_HH
static bool isMaster()
return true if the current thread is the master thread (i.e. thread 0)
Definition: threadmanager.hh:211
The ThreadManager wrapps basic shared memory functionality provided by OpenMP or pthreads such as thr...
Definition: threadmanager.hh:199
static void setMaxNumberThreads(const int numThreads)
set maximal number of threads available during run
Definition: threadmanager.hh:214
static int thread()
return thread number
Definition: threadmanager.hh:208
Definition: coordinate.hh:4
static int currentThreads()
return number of current threads
Definition: threadmanager.hh:205
static int maxThreads()
return maximal number of threads possbile in the current run
Definition: threadmanager.hh:202
static bool singleThreadMode()
returns true if program is operating on one thread currently
Definition: threadmanager.hh:217