libMesh
Classes | Typedefs | Functions | Variables
libMesh::Threads Namespace Reference

The Threads namespace is for wrapper functions for common general multithreading algorithms and tasks. More...

Classes

class  atomic
 Defines atomic operations which can only be executed on a single thread at a time. More...
 
class  BlockedRange
 Blocked range which can be subdivided and executed in parallel. More...
 
class  BoolAcquire
 We use a class to turn Threads::in_threads on and off, to be exception-safe. More...
 
class  DisablePerfLogInScope
 We use a class to turn perf logging off and on within threads, to be exception-safe and to avoid forcing indirect inclusion of libmesh_logging.h everywhere. More...
 
class  NonConcurrentThread
 Simple compatibility class for std::thread 'concurrent' execution. More...
 
class  RangeBody
 
class  recursive_mutex
 Recursive mutex. More...
 
class  scalable_allocator
 Scalable allocator to be used in multithreaded code chunks which allocate a lot of dynamic memory. More...
 
class  spin_mutex
 Spin mutex. More...
 
class  split
 Dummy "splitting object" used to distinguish splitting constructors from copy constructors. More...
 
class  task_scheduler_init
 Scheduler to manage threads. More...
 

Typedefs

typedef NonConcurrentThread Thread
 Use the non-concurrent placeholder. More...
 
typedef tbb::task_scheduler_init task_scheduler_init
 Scheduler to manage threads. More...
 
typedef tbb::split split
 Dummy "splitting object" used to distinguish splitting constructors from copy constructors. More...
 
typedef tbb::spin_mutex spin_mutex
 Spin mutex. More...
 
typedef tbb::recursive_mutex recursive_mutex
 Recursive mutex. More...
 

Functions

void lock_singleton_spin_mutex ()
 
void unlock_singleton_spin_mutex ()
 
template<typename Range , typename Body >
void parallel_for (const Range &range, const Body &body)
 Execute the provided function object in parallel on the specified range. More...
 
template<typename Range , typename Body , typename Partitioner >
void parallel_for (const Range &range, const Body &body, const Partitioner &)
 Execute the provided function object in parallel on the specified range with the specified partitioner. More...
 
template<typename Range , typename Body >
void parallel_reduce (const Range &range, Body &body)
 Execute the provided reduction operation in parallel on the specified range. More...
 
template<typename Range , typename Body , typename Partitioner >
void parallel_reduce (const Range &range, Body &body, const Partitioner &)
 Execute the provided reduction operation in parallel on the specified range with the specified partitioner. More...
 
template<typename Range >
unsigned int num_pthreads (Range &range)
 
template<typename Range , typename Body >
void * run_body (void *args)
 

Variables

bool in_threads = false
 A boolean which is true iff we are in a Threads:: function It may be useful to assert(!Threadsin_threads) in any code which is known to not be thread-safe. More...
 
spin_mutex spin_mtx
 A convenient spin mutex object which can be used for obtaining locks. More...
 
recursive_mutex recursive_mtx
 A convenient recursive mutex object which can be used for obtaining locks. More...
 

Detailed Description

The Threads namespace is for wrapper functions for common general multithreading algorithms and tasks.

Typedef Documentation

◆ recursive_mutex

typedef tbb::recursive_mutex libMesh::Threads::recursive_mutex

Recursive mutex.

Implements mutual exclusion by busy-waiting in user space for the lock to be acquired. The same thread can acquire the same lock multiple times

Definition at line 174 of file threads_tbb.h.

◆ spin_mutex

typedef tbb::spin_mutex libMesh::Threads::spin_mutex

Spin mutex.

Implements mutual exclusion by busy-waiting in user space for the lock to be acquired.

Definition at line 167 of file threads_tbb.h.

◆ split

typedef tbb::split libMesh::Threads::split

Dummy "splitting object" used to distinguish splitting constructors from copy constructors.

Definition at line 77 of file threads_tbb.h.

◆ task_scheduler_init

typedef tbb::task_scheduler_init libMesh::Threads::task_scheduler_init

Scheduler to manage threads.

Definition at line 71 of file threads_tbb.h.

◆ Thread

typedef tbb::tbb_thread libMesh::Threads::Thread

Use the non-concurrent placeholder.

Thread object abstraction that provides a basic constructor and support for join().

Use std::thread when available.

Definition at line 43 of file threads_none.h.

Function Documentation

◆ lock_singleton_spin_mutex()

void libMesh::Threads::lock_singleton_spin_mutex ( )

Definition at line 34 of file threads.C.

References libMesh::Threads::spin_mutex::lock(), and spin_mtx.

34 { spin_mtx.lock(); }
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition: threads.C:30

◆ num_pthreads()

template<typename Range >
unsigned int libMesh::Threads::num_pthreads ( Range &  range)

Definition at line 213 of file threads_pthread.h.

References libMesh::n_threads().

214 {
215  std::size_t min = std::min((std::size_t)libMesh::n_threads(), range.size());
216  return min > 0 ? cast_int<unsigned int>(min) : 1;
217 }
unsigned int n_threads()
Definition: libmesh_base.h:96

◆ parallel_for() [1/2]

template<typename Range , typename Body >
void libMesh::Threads::parallel_for ( const Range &  range,
const Body &  body 
)
inline

Execute the provided function object in parallel on the specified range.

Definition at line 73 of file threads_none.h.

References in_threads.

Referenced by libMesh::FEMSystem::assemble_qoi_derivative(), libMesh::FEMSystem::assembly(), libMesh::MeshCommunication::assign_global_indices(), libMesh::System::boundary_project_vector(), libMesh::MeshCommunication::check_for_duplicate_global_indices(), libMesh::DofMap::create_dof_constraints(), libMesh::WeightedPatchRecoveryErrorEstimator::estimate_error(), libMesh::PatchRecoveryErrorEstimator::estimate_error(), and libMesh::FEMSystem::postprocess().

74 {
75  BoolAcquire b(in_threads);
76  body(range);
77 }
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

◆ parallel_for() [2/2]

template<typename Range , typename Body , typename Partitioner >
void libMesh::Threads::parallel_for ( const Range &  range,
const Body &  body,
const Partitioner partitioner 
)
inline

Execute the provided function object in parallel on the specified range with the specified partitioner.

Definition at line 87 of file threads_none.h.

References in_threads.

88 {
89  BoolAcquire b(in_threads);
90  body(range);
91 }
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

◆ parallel_reduce() [1/2]

template<typename Range , typename Body >
void libMesh::Threads::parallel_reduce ( const Range &  range,
Body &  body 
)
inline

◆ parallel_reduce() [2/2]

template<typename Range , typename Body , typename Partitioner >
void libMesh::Threads::parallel_reduce ( const Range &  range,
Body &  body,
const Partitioner partitioner 
)
inline

Execute the provided reduction operation in parallel on the specified range with the specified partitioner.

Definition at line 115 of file threads_none.h.

References in_threads.

116 {
117  BoolAcquire b(in_threads);
118  body(range);
119 }
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

◆ run_body()

template<typename Range , typename Body >
void* libMesh::Threads::run_body ( void *  args)

Definition at line 228 of file threads_pthread.h.

References libMesh::Threads::RangeBody< Range, Body >::body, and libMesh::Threads::RangeBody< Range, Body >::range.

229 {
230  RangeBody<Range, Body> * range_body = (RangeBody<Range, Body> *)args;
231 
232  Body & body = *range_body->body;
233  Range & range = *range_body->range;
234 
235  body(range);
236 
237  return nullptr;
238 }

◆ unlock_singleton_spin_mutex()

void libMesh::Threads::unlock_singleton_spin_mutex ( )

Definition at line 35 of file threads.C.

References spin_mtx, and libMesh::Threads::spin_mutex::unlock().

35 { spin_mtx.unlock(); }
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition: threads.C:30

Variable Documentation

◆ in_threads

bool libMesh::Threads::in_threads = false

A boolean which is true iff we are in a Threads:: function It may be useful to assert(!Threadsin_threads) in any code which is known to not be thread-safe.

Definition at line 32 of file threads.C.

Referenced by parallel_for(), parallel_reduce(), and libMesh::MeshBase::sub_point_locator().

◆ recursive_mtx

recursive_mutex libMesh::Threads::recursive_mtx

A convenient recursive mutex object which can be used for obtaining locks.

Definition at line 31 of file threads.C.

◆ spin_mtx

spin_mutex libMesh::Threads::spin_mtx