libMesh
Loading...
Searching...
No Matches
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)
29MULTIPLE THREADING MODELS CANNOT BE SIMULTANEOUSLY ACTIVE
30#endif
31
32namespace libMesh
33{
34
39namespace Threads
40{
41
46extern int active_threads;
47
55extern bool in_threads;
56
64template <typename T, T new_x_default = T(), bool assert_change = false>
66{
67public:
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; }
77private:
78 T & _x;
81};
82
83// We'll only acquire in_threads to turn it from false to true
85
94class [[maybe_unused]] DisablePerfLogInScope
95{
96public:
97#ifndef LIBMESH_ENABLE_PERFORMANCE_LOGGING
98 DisablePerfLogInScope() = default;
99 ~DisablePerfLogInScope() = default;
100#else
103private:
105#endif
106};
107
108
114{
115public:
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
155namespace libMesh
156{
157
158namespace Threads
159{
160
164template <typename T>
166{
167public:
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
283private:
284
285 const_iterator _end;
287 unsigned int _grainsize;
288};
289
290
291
295extern spin_mutex spin_mtx;
296
301
302} // namespace Threads
303
304} // namespace libMesh
305
306#endif // LIBMESH_THREADS_H
Blocked range which can be subdivided and executed in parallel.
Definition threads.h:166
void reset(const const_iterator first, const const_iterator last)
Resets the StoredRange to contain [first,last).
Definition threads.h:236
const_iterator end() const
End of the range.
Definition threads.h:251
unsigned int grainsize() const
The grain size for the range.
Definition threads.h:257
const_iterator begin() const
Beginning of the range.
Definition threads.h:246
BlockedRange(const const_iterator first, const const_iterator last, const unsigned int new_grainsize=libMesh::default_grainsize())
Constructor.
Definition threads.h:188
void grainsize(const unsigned int &gs)
Set the grain size.
Definition threads.h:262
BlockedRange(const BlockedRange< T > &r)
Copy constructor.
Definition threads.h:209
BlockedRange(const unsigned int new_grainsize=libMesh::default_grainsize())
Constructor.
Definition threads.h:178
BlockedRange(BlockedRange< T > &r, Threads::split)
Splits the range r.
Definition threads.h:220
T const_iterator
Allows an StoredRange to behave like an STL container.
Definition threads.h:171
We use a class to turn perf logging off and on within threads, to be exception-safe and to avoid forc...
Definition threads.h:95
Simple compatibility class for std::thread 'concurrent' execution.
Definition threads.h:114
bool joinable() const
Always joinable.
Definition threads.h:132
void join()
Join is a no-op, since the constructor blocked until completion.
Definition threads.h:127
NonConcurrentThread(Callable f)
Constructor.
Definition threads.h:122
We use a class to turn Threads::in_threads on and off, to be exception-safe.
Definition threads.h:66
RAIIAcquire(T &x, T new_x=new_x_default)
Definition threads.h:69
Dummy "splitting object" used to distinguish splitting constructors from copy constructors.
bool in_threads
A boolean which is true iff we are in a Threads:: function It may be useful to assert(!...
Definition threads.C:33
recursive_mutex recursive_mtx
A convenient recursive mutex object which can be used for obtaining locks.
Definition threads.C:31
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition threads.C:30
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
The libMesh namespace provides an interface to certain functionality in the library.
unsigned int default_grainsize()
libmesh_assert(ctx)