threads.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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 
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),
169  {}
170 
177  _end(r._end),
178  _begin(r._begin),
180  {
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;
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
BlockedRange(const const_iterator first, const const_iterator last, const unsigned int new_grainsize=1000)
Definition: threads.h:144
BlockedRange(const unsigned int new_grainsize=1000)
Definition: threads.h:134
const_iterator end() const
Definition: threads.h:207
void reset(const const_iterator first, const const_iterator last)
Definition: threads.h:192
bool in_threads
Definition: threads.C:31
unsigned int grainsize() const
Definition: threads.h:213
void grainsize(const unsigned int &gs)
Definition: threads.h:218
const_iterator begin() const
Definition: threads.h:202
spin_mutex spin_mtx
Definition: threads.C:29
BlockedRange(const BlockedRange< T > &r)
Definition: threads.h:165
BlockedRange(BlockedRange< T > &r, Threads::split)
Definition: threads.h:176
recursive_mutex recursive_mtx
Definition: threads.C:30