$extrastylesheet
libMesh::Threads Namespace Reference

Classes

class  BoolAcquire
class  Thread
class  atomic
class  spin_mutex
class  recursive_mutex
class  RangeBody
class  task_scheduler_init
class  split
class  BlockedRange
class  scalable_allocator

Typedefs

typedef std::thread Thread
typedef tbb::task_scheduler_init task_scheduler_init
typedef tbb::split split
typedef tbb::spin_mutex spin_mutex
typedef tbb::recursive_mutex recursive_mutex

Functions

template<typename Range , typename Body >
void parallel_for (const Range &range, const Body &body)
template<typename Range , typename Body , typename Partitioner >
void parallel_for (const Range &range, const Body &body, const Partitioner &partitioner)
template<typename Range , typename Body >
void parallel_reduce (const Range &range, Body &body)
template<typename Range , typename Body , typename Partitioner >
void parallel_reduce (const Range &range, Body &body, const Partitioner &partitioner)
unsigned int pthread_unique_id ()
template<typename Range >
unsigned int num_pthreads (Range &range)
template<typename Range , typename Body >
void * run_body (void *args)

Variables

bool in_threads = false
std::map< pthread_t, unsigned int > _pthread_unique_ids
spin_mutex _pthread_unique_id_mutex
spin_mutex spin_mtx
recursive_mutex recursive_mtx

Detailed Description

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


Typedef Documentation

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 aquire the same lock multiple times

Definition at line 315 of file threads.h.

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 307 of file threads.h.

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

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

Definition at line 170 of file threads.h.

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

Scheduler to manage threads.

Definition at line 161 of file threads.h.

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

Use std::thread when available.

Fall back to tbb::tbb_thread when available.

Definition at line 115 of file threads.h.


Function Documentation

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

Definition at line 443 of file threads.h.

References std::min(), and libMesh::n_threads().

{
  unsigned int min = std::min((std::size_t)libMesh::n_threads(), range.size());
  return min > 0 ? min : 1;
}
template<typename Range , typename Body >
void libMesh::Threads::parallel_for ( const Range &  range,
const Body &  body 
) [inline]
template<typename Range , typename Body , typename Partitioner >
void libMesh::Threads::parallel_for ( const Range &  range,
const Body &  body,
const Partitioner &  partitioner 
) [inline]

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

Definition at line 213 of file threads.h.

References libMesh::PerfLog::disable_logging(), libMesh::PerfLog::enable_logging(), in_threads, libMesh::PerfLog::logging_enabled(), libMesh::n_threads(), parallel_for(), and libMesh::perflog.

{
  BoolAcquire b(in_threads);

#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
  const bool logging_was_enabled = libMesh::perflog.logging_enabled();

  if (libMesh::n_threads() > 1)
    libMesh::perflog.disable_logging();
#endif

  if (libMesh::n_threads() > 1)
    tbb::parallel_for (range, body, partitioner);

  else
    body(range);

#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
  if (libMesh::n_threads() > 1 && logging_was_enabled)
    libMesh::perflog.enable_logging();
#endif
}
template<typename Range , typename Body >
void libMesh::Threads::parallel_reduce ( const Range &  range,
Body &  body 
) [inline]

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

Definition at line 245 of file threads.h.

References libMesh::PerfLog::disable_logging(), libMesh::PerfLog::enable_logging(), in_threads, libMesh::PerfLog::logging_enabled(), libMesh::n_threads(), and libMesh::perflog.

Referenced by libMesh::FEMSystem::assemble_qoi(), libMesh::MeshTools::bounding_box(), libMesh::DofMap::build_sparsity(), parallel_reduce(), libMesh::MeshTools::processor_bounding_box(), libMesh::System::project_vector(), libMesh::MeshTools::total_weight(), and libMesh::MeshTools::weight().

{
  BoolAcquire b(in_threads);

#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
  const bool logging_was_enabled = libMesh::perflog.logging_enabled();

  if (libMesh::n_threads() > 1)
    libMesh::perflog.disable_logging();
#endif

  if (libMesh::n_threads() > 1)
    tbb::parallel_reduce (range, body, tbb::auto_partitioner());

  else
    body(range);

#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
  if (libMesh::n_threads() > 1 && logging_was_enabled)
    libMesh::perflog.enable_logging();
#endif
}
template<typename Range , typename Body , typename Partitioner >
void libMesh::Threads::parallel_reduce ( const Range &  range,
Body &  body,
const Partitioner &  partitioner 
) [inline]

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

Definition at line 277 of file threads.h.

References libMesh::PerfLog::disable_logging(), libMesh::PerfLog::enable_logging(), in_threads, libMesh::PerfLog::logging_enabled(), libMesh::n_threads(), parallel_reduce(), and libMesh::perflog.

{
  BoolAcquire b(in_threads);

#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
  const bool logging_was_enabled = libMesh::perflog.logging_enabled();

  if (libMesh::n_threads() > 1)
    libMesh::perflog.disable_logging();
#endif

  if (libMesh::n_threads() > 1)
    tbb::parallel_reduce (range, body, partitioner);

  else
    body(range);

#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
  if (libMesh::n_threads() > 1 && logging_was_enabled)
    libMesh::perflog.enable_logging();
#endif
}

When called by a thread this will return a unique number from 0 to num_pthreads-1 Very useful for creating long-lived thread local storage

Definition at line 35 of file threads.C.

References _pthread_unique_id_mutex, and _pthread_unique_ids.

{
#if LIBMESH_HAVE_OPENMP
  return omp_get_thread_num();
#else
  spin_mutex::scoped_lock lock(_pthread_unique_id_mutex);
  return _pthread_unique_ids[pthread_self()];
#endif
}
template<typename Range , typename Body >
void* libMesh::Threads::run_body ( void *  args)

Definition at line 458 of file threads.h.

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

{

  RangeBody<Range, Body> * range_body = (RangeBody<Range, Body>*)args;

  Body & body = *range_body->body;
  Range & range = *range_body->range;

  body(range);

  return NULL;
}

Variable Documentation

std::map< pthread_t, unsigned int > libMesh::Threads::_pthread_unique_ids

Definition at line 32 of file threads.C.

Referenced by pthread_unique_id().

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

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

A recursive mutex object which

Definition at line 49 of file threads.C.