libMesh
threads.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 #ifndef LIBMESH_THREADS_H
20 #define LIBMESH_THREADS_H
21 
22 // Local includes
23 #include "libmesh/libmesh_config.h"
24 #include "libmesh/libmesh_common.h" // for libmesh_assert
25 
26 
27 // Compile-time check: TBB and pthreads are now mutually exclusive.
28 #if defined(LIBMESH_HAVE_TBB_API) && defined(LIBMESH_HAVE_PTHREAD)
29 MULTIPLE THREADING MODELS CANNOT BE SIMULTANEOUSLY ACTIVE
30 #endif
31 
32 namespace libMesh
33 {
34 
39 namespace Threads
40 {
41 
47 extern bool in_threads;
48 
54 {
55 public:
56  explicit
57  BoolAcquire(bool & b) : _b(b) { libmesh_assert(!_b); _b = true; }
58 
59  ~BoolAcquire() { libmesh_exceptionless_assert(_b); _b = false; }
60 private:
61  bool & _b;
62 };
63 
64 
70 {
71 public:
77  template <typename Callable>
78  NonConcurrentThread (Callable f) { f(); }
79 
83  void join() {}
84 
88  bool joinable() const { return true; }
89 };
90 
91 } // namespace Threads
92 
93 } // namespace libMesh
94 
95 
96 
97 // Include thread-model specific algorithms and objects. These
98 // headers include headers of their own and handle their own
99 // namespacing.
100 #define LIBMESH_SQUASH_HEADER_WARNING
101 #ifdef LIBMESH_HAVE_TBB_API
102 # include "libmesh/threads_tbb.h"
103 #elif LIBMESH_HAVE_PTHREAD
104 # include "libmesh/threads_pthread.h"
105 #else
106 # include "libmesh/threads_none.h"
107 #endif
108 
109 
110 
111 namespace libMesh
112 {
113 
114 namespace Threads
115 {
116 
120 template <typename T>
122 {
123 public:
127  typedef T const_iterator;
128 
134  explicit BlockedRange (const unsigned int new_grainsize = 1000) :
135  _grainsize(new_grainsize)
136  {}
137 
144  BlockedRange (const const_iterator first,
145  const const_iterator last,
146  const unsigned int new_grainsize = 1000) :
147  _grainsize(new_grainsize)
148  {
149  this->reset(first, last);
150  }
151 
166  _end(r._end),
167  _begin(r._begin),
168  _grainsize(r._grainsize)
169  {}
170 
177  _end(r._end),
178  _begin(r._begin),
179  _grainsize(r._grainsize)
180  {
181  const_iterator
182  beginning = r._begin,
183  ending = r._end,
184  middle = beginning + (ending - beginning)/2u;
185 
186  r._end = _begin = middle;
187  }
188 
192  void reset (const const_iterator first,
193  const const_iterator last)
194  {
195  _begin = first;
196  _end = last;
197  }
198 
202  const_iterator begin () const { return _begin; }
203 
207  const_iterator end () const { return _end; }
208 
213  unsigned int grainsize () const {return _grainsize;}
214 
218  void grainsize (const unsigned int & gs) {_grainsize = gs;}
219 
223  int size () const { return (_end -_begin); }
224 
225  //------------------------------------------------------------------------
226  // Methods that implement Range concept
227  //------------------------------------------------------------------------
228 
232  bool empty() const { return (_begin == _end); }
233 
237  bool is_divisible() const { return ((_begin + this->grainsize()) < _end); }
238 
239 private:
240 
241  const_iterator _end;
242  const_iterator _begin;
243  unsigned int _grainsize;
244 };
245 
246 
247 
251 extern spin_mutex spin_mtx;
252 
257 
258 } // namespace Threads
259 
260 } // namespace libMesh
261 
262 #endif // LIBMESH_THREADS_H
Simple compatibility class for std::thread &#39;concurrent&#39; execution.
Definition: threads.h:69
NonConcurrentThread(Callable f)
Constructor.
Definition: threads.h:78
BlockedRange(const const_iterator first, const const_iterator last, const unsigned int new_grainsize=1000)
Constructor.
Definition: threads.h:144
BlockedRange(const unsigned int new_grainsize=1000)
Constructor.
Definition: threads.h:134
void join()
Join is a no-op, since the constructor blocked until completion.
Definition: threads.h:83
Dummy "splitting object" used to distinguish splitting constructors from copy constructors.
Definition: threads_none.h:63
bool is_divisible() const
Definition: threads.h:237
const_iterator end() const
End of the range.
Definition: threads.h:207
The libMesh namespace provides an interface to certain functionality in the library.
void reset(const const_iterator first, const const_iterator last)
Resets the StoredRange to contain [first,last).
Definition: threads.h:192
bool in_threads
A boolean which is true iff we are in a Threads:: function It may be useful to assert(!Threadsin_thre...
Definition: threads.C:31
Blocked range which can be subdivided and executed in parallel.
Definition: threads.h:121
libmesh_assert(j)
const_iterator begin() const
Beginning of the range.
Definition: threads.h:202
void grainsize(const unsigned int &gs)
Set the grain size.
Definition: threads.h:218
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition: threads.C:29
T const_iterator
Allows an StoredRange to behave like an STL container.
Definition: threads.h:127
bool joinable() const
Always joinable.
Definition: threads.h:88
We use a class to turn Threads::in_threads on and off, to be exception-safe.
Definition: threads.h:53
BlockedRange(const BlockedRange< T > &r)
Copy constructor.
Definition: threads.h:165
unsigned int grainsize() const
The grain size for the range.
Definition: threads.h:213
BlockedRange(BlockedRange< T > &r, Threads::split)
Splits the range r.
Definition: threads.h:176
recursive_mutex recursive_mtx
A convenient recursive mutex object which can be used for obtaining locks.
Definition: threads.C:30