libMesh
threads.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2026 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 
46 extern int active_threads;
47 
55 extern bool in_threads;
56 
64 template <typename T, T new_x_default = T(), bool assert_change = false>
66 {
67 public:
68  explicit
69  RAIIAcquire(T & x, T new_x = new_x_default) :
70  _x(x), _old_x(x), _new_x(new_x)
71  {
72  libmesh_assert(!assert_change || _x != _new_x);
73  _x = _new_x;
74  }
75 
76  ~RAIIAcquire() { libmesh_exceptionless_assert(_x == _new_x); _x = _old_x; }
77 private:
78  T & _x;
79  T _old_x;
80  T _new_x;
81 };
82 
83 // We'll only acquire in_threads to turn it from false to true
85 
94 class [[maybe_unused]] DisablePerfLogInScope
95 {
96 public:
97 #ifndef LIBMESH_ENABLE_PERFORMANCE_LOGGING
98  DisablePerfLogInScope() = default;
99  ~DisablePerfLogInScope() = default;
100 #else
103 private:
105 #endif
106 };
107 
108 
114 {
115 public:
121  template <typename Callable>
122  NonConcurrentThread (Callable f) { f(); }
123 
127  void join() {}
128 
132  bool joinable() const { return true; }
133 };
134 
135 } // namespace Threads
136 
137 } // namespace libMesh
138 
139 
140 
141 // Include thread-model specific algorithms and objects. These
142 // headers include headers of their own and handle their own
143 // namespacing.
144 #define LIBMESH_SQUASH_HEADER_WARNING
145 #ifdef LIBMESH_HAVE_TBB_API
146 # include "libmesh/threads_tbb.h"
147 #elif LIBMESH_HAVE_PTHREAD
148 # include "libmesh/threads_pthread.h"
149 #else
150 # include "libmesh/threads_none.h"
151 #endif
152 
153 
154 
155 namespace libMesh
156 {
157 
158 namespace Threads
159 {
160 
164 template <typename T>
166 {
167 public:
171  typedef T const_iterator;
172 
178  explicit BlockedRange (const unsigned int new_grainsize = libMesh::default_grainsize()) :
179  _grainsize(new_grainsize)
180  {}
181 
189  const const_iterator last,
190  const unsigned int new_grainsize = libMesh::default_grainsize()) :
191  _grainsize(new_grainsize)
192  {
193  this->reset(first, last);
194  }
195 
210  _end(r._end),
211  _begin(r._begin),
213  {}
214 
221  _end(r._end),
222  _begin(r._begin),
224  {
226  beginning = r._begin,
227  ending = r._end,
228  middle = beginning + (ending - beginning)/2u;
229 
230  r._end = _begin = middle;
231  }
232 
236  void reset (const const_iterator first,
237  const const_iterator last)
238  {
239  _begin = first;
240  _end = last;
241  }
242 
246  const_iterator begin () const { return _begin; }
247 
251  const_iterator end () const { return _end; }
252 
257  unsigned int grainsize () const {return _grainsize;}
258 
262  void grainsize (const unsigned int & gs) {_grainsize = gs;}
263 
267  int size () const { return (_end -_begin); }
268 
269  //------------------------------------------------------------------------
270  // Methods that implement Range concept
271  //------------------------------------------------------------------------
272 
276  bool empty() const { return (_begin == _end); }
277 
281  bool is_divisible() const { return ((_begin + this->grainsize()) < _end); }
282 
283 private:
284 
285  const_iterator _end;
287  unsigned int _grainsize;
288 };
289 
290 
291 
295 extern spin_mutex spin_mtx;
296 
301 
302 } // namespace Threads
303 
304 } // namespace libMesh
305 
306 #endif // LIBMESH_THREADS_H
Simple compatibility class for std::thread &#39;concurrent&#39; execution.
Definition: threads.h:113
NonConcurrentThread(Callable f)
Constructor.
Definition: threads.h:122
void join()
Join is a no-op, since the constructor blocked until completion.
Definition: threads.h:127
const_iterator end() const
End of the range.
Definition: threads.h:251
recursive_mutex recursive_mtx
A convenient recursive mutex object which can be used for obtaining locks.
Definition: threads.C:31
We use a class to turn Threads::in_threads on and off, to be exception-safe.
Definition: threads.h:65
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:236
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:33
We use a class to turn perf logging off and on within threads, to be exception-safe and to avoid forc...
Definition: threads.h:94
Blocked range which can be subdivided and executed in parallel.
Definition: threads.h:165
unsigned int grainsize() const
The grain size for the range.
Definition: threads.h:257
void grainsize(const unsigned int &gs)
Set the grain size.
Definition: threads.h:262
const_iterator begin() const
Beginning of the range.
Definition: threads.h:246
bool joinable() const
Always joinable.
Definition: threads.h:132
T const_iterator
Allows an StoredRange to behave like an STL container.
Definition: threads.h:171
RAIIAcquire(T &x, T new_x=new_x_default)
Definition: threads.h:69
libmesh_assert(ctx)
unsigned int default_grainsize()
Definition: libmesh_base.h:117
BlockedRange(const unsigned int new_grainsize=libMesh::default_grainsize())
Constructor.
Definition: threads.h:178
BlockedRange(const BlockedRange< T > &r)
Copy constructor.
Definition: threads.h:209
int active_threads
An integer which is set to the number of active threads when we are in a Threads:: parallel operation...
Definition: threads.C:32
RAIIAcquire< bool, true, true > BoolAcquire
Definition: threads.h:84
BlockedRange(BlockedRange< T > &r, Threads::split)
Splits the range r.
Definition: threads.h:220
BlockedRange(const const_iterator first, const const_iterator last, const unsigned int new_grainsize=libMesh::default_grainsize())
Constructor.
Definition: threads.h:188
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition: threads.C:30