libMesh
threads.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 
72 class [[maybe_unused]] DisablePerfLogInScope
73 {
74 public:
75 #ifndef LIBMESH_ENABLE_PERFORMANCE_LOGGING
76  DisablePerfLogInScope() = default;
77  ~DisablePerfLogInScope() = default;
78 #else
81 private:
83 #endif
84 };
85 
86 
92 {
93 public:
99  template <typename Callable>
100  NonConcurrentThread (Callable f) { f(); }
101 
105  void join() {}
106 
110  bool joinable() const { return true; }
111 };
112 
113 } // namespace Threads
114 
115 } // namespace libMesh
116 
117 
118 
119 // Include thread-model specific algorithms and objects. These
120 // headers include headers of their own and handle their own
121 // namespacing.
122 #define LIBMESH_SQUASH_HEADER_WARNING
123 #ifdef LIBMESH_HAVE_TBB_API
124 # include "libmesh/threads_tbb.h"
125 #elif LIBMESH_HAVE_PTHREAD
126 # include "libmesh/threads_pthread.h"
127 #else
128 # include "libmesh/threads_none.h"
129 #endif
130 
131 
132 
133 namespace libMesh
134 {
135 
136 namespace Threads
137 {
138 
142 template <typename T>
144 {
145 public:
149  typedef T const_iterator;
150 
156  explicit BlockedRange (const unsigned int new_grainsize = 1000) :
157  _grainsize(new_grainsize)
158  {}
159 
167  const const_iterator last,
168  const unsigned int new_grainsize = 1000) :
169  _grainsize(new_grainsize)
170  {
171  this->reset(first, last);
172  }
173 
188  _end(r._end),
189  _begin(r._begin),
191  {}
192 
199  _end(r._end),
200  _begin(r._begin),
202  {
204  beginning = r._begin,
205  ending = r._end,
206  middle = beginning + (ending - beginning)/2u;
207 
208  r._end = _begin = middle;
209  }
210 
214  void reset (const const_iterator first,
215  const const_iterator last)
216  {
217  _begin = first;
218  _end = last;
219  }
220 
224  const_iterator begin () const { return _begin; }
225 
229  const_iterator end () const { return _end; }
230 
235  unsigned int grainsize () const {return _grainsize;}
236 
240  void grainsize (const unsigned int & gs) {_grainsize = gs;}
241 
245  int size () const { return (_end -_begin); }
246 
247  //------------------------------------------------------------------------
248  // Methods that implement Range concept
249  //------------------------------------------------------------------------
250 
254  bool empty() const { return (_begin == _end); }
255 
259  bool is_divisible() const { return ((_begin + this->grainsize()) < _end); }
260 
261 private:
262 
263  const_iterator _end;
265  unsigned int _grainsize;
266 };
267 
268 
269 
273 extern spin_mutex spin_mtx;
274 
279 
280 } // namespace Threads
281 
282 } // namespace libMesh
283 
284 #endif // LIBMESH_THREADS_H
Simple compatibility class for std::thread &#39;concurrent&#39; execution.
Definition: threads.h:91
NonConcurrentThread(Callable f)
Constructor.
Definition: threads.h:100
BlockedRange(const const_iterator first, const const_iterator last, const unsigned int new_grainsize=1000)
Constructor.
Definition: threads.h:166
BlockedRange(const unsigned int new_grainsize=1000)
Constructor.
Definition: threads.h:156
void join()
Join is a no-op, since the constructor blocked until completion.
Definition: threads.h:105
const_iterator end() const
End of the range.
Definition: threads.h:229
recursive_mutex recursive_mtx
A convenient recursive mutex object which can be used for obtaining locks.
Definition: threads.C:31
Dummy "splitting object" used to distinguish splitting constructors from copy constructors.
Definition: threads_none.h:63
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:214
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:32
We use a class to turn perf logging off and on within threads, to be exception-safe and to avoid forc...
Definition: threads.h:72
Blocked range which can be subdivided and executed in parallel.
Definition: threads.h:143
unsigned int grainsize() const
The grain size for the range.
Definition: threads.h:235
void grainsize(const unsigned int &gs)
Set the grain size.
Definition: threads.h:240
const_iterator begin() const
Beginning of the range.
Definition: threads.h:224
bool joinable() const
Always joinable.
Definition: threads.h:110
T const_iterator
Allows an StoredRange to behave like an STL container.
Definition: threads.h:149
libmesh_assert(ctx)
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:187
BlockedRange(BlockedRange< T > &r, Threads::split)
Splits the range r.
Definition: threads.h:198
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition: threads.C:30