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  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  RAIIAcquire
 We use a class to turn Threads::in_threads on and off, to be exception-safe. 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 RAIIAcquire< bool, true, true > BoolAcquire
 
typedef NonConcurrentThread Thread
 Use the non-concurrent placeholder. More...
 
typedef tbb::spin_mutex spin_mutex
 Spin mutex. More...
 
typedef tbb::task_scheduler_init task_scheduler_init
 Scheduler to manage the TBB thread pool. More...
 
typedef tbb::split split
 Dummy "splitting object" used to distinguish splitting constructors from copy constructors. 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, unsigned int n_threads=libMesh::n_threads())
 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 &, unsigned int n_threads=libMesh::n_threads())
 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, unsigned int n_threads=libMesh::n_threads())
 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 &, unsigned int n_threads=libMesh::n_threads())
 Execute the provided reduction operation in parallel on the specified range with the specified partitioner. More...
 
template<typename Range >
unsigned int num_pthreads (const Range &range, unsigned int requested=libMesh::n_threads())
 
template<typename Range , typename Body >
void * run_body (void *args)
 

Variables

int active_threads = 1
 An integer which is set to the number of active threads when we are in a Threads:: parallel operation. More...
 
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

◆ BoolAcquire

typedef RAIIAcquire<bool, true, true> libMesh::Threads::BoolAcquire

Definition at line 84 of file threads.h.

◆ recursive_mutex

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

Recursive mutex.

Implements mutual exclusion allowing the same thread to acquire the lock multiple times.

Legacy TBB: thin typedef around tbb::recursive_mutex. oneTBB: tbb::recursive_mutex was removed; this wrapper provides the same scoped_lock interface using std::recursive_mutex underneath.

Definition at line 287 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.

tbb::spin_mutex (and its scoped_lock) is present in both legacy and oneTBB.

Definition at line 44 of file threads_spin_mutex_forward.h.

◆ split

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

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

Definition at line 136 of file threads_tbb.h.

◆ task_scheduler_init

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

Scheduler to manage the TBB thread pool.

Legacy TBB: thin typedef around tbb::task_scheduler_init. oneTBB: task_scheduler_init was removed; this wrapper uses tbb::global_control to set the same global parallelism limit.

Definition at line 102 of file threads_tbb.h.

◆ Thread

typedef std::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.

Legacy TBB: tbb::tbb_thread | oneTBB: std::thread

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 35 of file threads.C.

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

35 { 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 ( const Range &  range,
unsigned int  requested = libMesh::n_threads() 
)

Definition at line 214 of file threads_pthread.h.

216 {
217  std::size_t mn = std::min((std::size_t)requested, range.size()/range.grainsize());
218  return mn > 0 ? cast_int<unsigned int>(mn) : 1;
219 }

◆ parallel_for() [1/2]

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

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

Definition at line 73 of file threads_none.h.

References in_threads, and libMesh::n_threads().

Referenced by libMesh::EquationSystems::_add_system_to_nodes_and_elems(), libMesh::EquationSystems::allgather(), libMesh::FEMSystem::assemble_qoi_derivative(), libMesh::FEMSystem::assembly(), libMesh::MeshCommunication::assign_global_indices(), libMesh::System::boundary_project_vector(), libMesh::EquationSystems::build_parallel_elemental_solution_vector(), libMesh::MeshBase::change_elemset_code(), libMesh::MeshTools::Modification::change_subdomain_id(), libMesh::MeshCommunication::check_for_duplicate_global_indices(), libMesh::MeshRefinement::coarsen_elements(), libMesh::UnstructuredMesh::copy_nodes_and_elements(), libMesh::DofMap::create_dof_constraints(), libMesh::MeshRefinement::create_parent_error_vector(), libMesh::WeightedPatchRecoveryErrorEstimator::estimate_error(), libMesh::PatchRecoveryErrorEstimator::estimate_error(), libMesh::SmoothnessEstimator::estimate_smoothness(), libMesh::UnstructuredMesh::find_neighbors(), libMesh::ParmetisPartitioner::initialize(), libMesh::FEMSystem::postprocess(), libMesh::MeshRefinement::refine_and_coarsen_elements(), libMesh::MeshRefinement::refine_elements(), libMesh::DofMap::reinit(), libMesh::EquationSystems::reinit_mesh(), libMesh::EquationSystems::reinit_solutions(), libMesh::MeshBase::size_elem_extra_integers(), ParallelTest::testParallelForThreadSubset(), ParallelTest::testRequestedThreadCountExceedsGlobal(), and libMesh::System::zero_variable().

75 {
76  libmesh_error_msg_if(n_threads > libMesh::n_threads(),
77  "Requested n_threads (" << n_threads << ") exceeds the "
78  "global thread count (" << libMesh::n_threads() << ").");
79  BoolAcquire set_in_threads(in_threads);
80  body(range);
81 }
unsigned int n_threads()
Definition: libmesh_base.h:109
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
RAIIAcquire< bool, true, true > BoolAcquire
Definition: threads.h:84

◆ 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,
unsigned int  n_threads = libMesh::n_threads() 
)
inline

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

Definition at line 91 of file threads_none.h.

References in_threads, and libMesh::n_threads().

93 {
94  libmesh_error_msg_if(n_threads > libMesh::n_threads(),
95  "Requested n_threads (" << n_threads << ") exceeds the "
96  "global thread count (" << libMesh::n_threads() << ").");
97  BoolAcquire set_in_threads(in_threads);
98  body(range);
99 }
unsigned int n_threads()
Definition: libmesh_base.h:109
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
RAIIAcquire< bool, true, true > BoolAcquire
Definition: threads.h:84

◆ parallel_reduce() [1/2]

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

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

Definition at line 109 of file threads_none.h.

References in_threads, and libMesh::n_threads().

Referenced by libMesh::FEMSystem::assemble_qoi(), libMesh::DofMap::build_sparsity(), libMesh::MeshTetInterface::check_hull_integrity(), libMesh::MeshTools::create_bounding_box(), libMesh::MeshTools::create_local_bounding_box(), libMesh::MeshTools::create_nodal_bounding_box(), libMesh::MeshTools::create_processor_bounding_box(), libMesh::MeshTools::create_subdomain_bounding_box(), libMesh::NumericVector< Number >::l1_norm_diff(), libMesh::NumericVector< Number >::l2_norm_diff(), main(), libMesh::MeshTools::n_active_local_levels(), libMesh::GenericProjector< FFunctor, GFunctor, FValue, ProjectionAction >::project(), libMesh::System::project_vector(), libMesh::MeshBase::subdomain_ids(), ParallelTest::testParallelReduceThreadSubset(), ParallelTest::testRequestedThreadCountExceedsGlobal(), libMesh::MeshTools::total_weight(), and libMesh::MeshTools::weight().

111 {
112  libmesh_error_msg_if(n_threads > libMesh::n_threads(),
113  "Requested n_threads (" << n_threads << ") exceeds the "
114  "global thread count (" << libMesh::n_threads() << ").");
115  BoolAcquire set_in_threads(in_threads);
116  body(range);
117 }
unsigned int n_threads()
Definition: libmesh_base.h:109
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
RAIIAcquire< bool, true, true > BoolAcquire
Definition: threads.h:84

◆ parallel_reduce() [2/2]

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

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

Definition at line 127 of file threads_none.h.

References in_threads, and libMesh::n_threads().

129 {
130  libmesh_error_msg_if(n_threads > libMesh::n_threads(),
131  "Requested n_threads (" << n_threads << ") exceeds the "
132  "global thread count (" << libMesh::n_threads() << ").");
133  BoolAcquire set_in_threads(in_threads);
134  body(range);
135 }
unsigned int n_threads()
Definition: libmesh_base.h:109
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
RAIIAcquire< bool, true, true > BoolAcquire
Definition: threads.h:84

◆ run_body()

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

Definition at line 230 of file threads_pthread.h.

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

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

◆ unlock_singleton_spin_mutex()

void libMesh::Threads::unlock_singleton_spin_mutex ( )

Definition at line 36 of file threads.C.

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

36 { 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

◆ active_threads

int libMesh::Threads::active_threads = 1

An integer which is set to the number of active threads when we are in a Threads:: parallel operation.

Definition at line 32 of file threads.C.

Referenced by libMesh::PerfLog::fast_pop(), libMesh::PerfLog::fast_push(), libMesh::PerfLog::pop(), and libMesh::PerfLog::push().

◆ 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.

Code which is not thread-safe but which is specifically enabled only when multiple threads are active might test for (Threads::active_threads != 1) instead.

Definition at line 33 of file threads.C.

Referenced by libMesh::MeshBase::active_local_element_stored_range(), libMesh::MeshBase::element_stored_range(), parallel_for(), parallel_reduce(), libMesh::StaticCondensation::set_current_elem(), 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