18#ifndef LIBMESH_THREADS_PTHREAD_H
19#define LIBMESH_THREADS_PTHREAD_H
23#ifndef LIBMESH_SQUASH_HEADER_WARNING
24# warning "This file is designed to be included through libmesh/threads.h"
27#ifdef LIBMESH_HAVE_PTHREAD
30#ifdef LIBMESH_HAVE_CXX11_THREAD
43# include <libkern/OSAtomic.h>
48#ifdef LIBMESH_HAVE_CXX11_THREAD
49# define LIBMESH_TLS_TYPE(type) thread_local type
50# define LIBMESH_TLS_REF(value) (value)
52# define LIBMESH_TLS_TYPE(type) type
53# define LIBMESH_TLS_REF(value) (value)
63#ifdef LIBMESH_HAVE_CXX11_THREAD
74typedef NonConcurrentThread
Thread;
184 pthread_mutexattr_init(&
attr);
185 pthread_mutexattr_settype(&
attr, PTHREAD_MUTEX_RECURSIVE);
213template <
typename Range>
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;
221template <
typename Range,
typename Body>
229template <
typename Range,
typename Body>
234 Body & body = *range_body->
body;
235 Range & range = *range_body->
range;
245class task_scheduler_init
269template <
typename Range,
typename Body>
271void parallel_for (
const Range & range,
const Body & body,
275 "Requested n_threads (" <<
n_threads <<
") exceeds the "
282 if (actual_threads == 1)
290 DisablePerfLogInScope disable_perf;
292 std::vector<std::unique_ptr<Range>> ranges(actual_threads);
293 std::vector<RangeBody<const Range, const Body>> range_bodies(actual_threads);
294 std::vector<pthread_t> threads(actual_threads);
297 std::size_t range_size = range.size() / actual_threads;
299 typename Range::const_iterator current_beginning = range.begin();
301 for (
unsigned int i=0; i<actual_threads; i++)
303 std::size_t this_range_size = range_size;
305 if (i+1 == actual_threads)
306 this_range_size += range.size() % actual_threads;
308 ranges[i] = std::make_unique<Range>(range, current_beginning, current_beginning + this_range_size);
310 current_beginning = current_beginning + this_range_size;
314 for (
unsigned int i=0; i<actual_threads; i++)
316 range_bodies[i].range = ranges[i].get();
317 range_bodies[i].body = &body;
324#ifdef LIBMESH_HAVE_OPENMP
325#pragma omp parallel for schedule (static)
327 for (
int i=0; i<static_cast<int>(actual_threads); i++)
329#if !LIBMESH_HAVE_OPENMP
330 pthread_create(&threads[i],
nullptr, &run_body<Range, Body>, (
void *)&range_bodies[i]);
332 run_body<Range, Body>((
void *)&range_bodies[i]);
336#if !LIBMESH_HAVE_OPENMP
345 for (
int i=0; i<static_cast<int>(actual_threads); i++)
346 pthread_join(threads[i],
nullptr);
354template <
typename Range,
typename Body,
typename Partitioner>
356void parallel_for (
const Range & range,
const Body & body,
const Partitioner &,
366template <
typename Range,
typename Body>
372 "Requested n_threads (" <<
n_threads <<
") exceeds the "
379 if (actual_threads == 1)
387 DisablePerfLogInScope disable_perf;
389 std::vector<std::unique_ptr<Range>> ranges(actual_threads);
390 std::vector<std::unique_ptr<Body>> managed_bodies(actual_threads);
391 std::vector<Body *> bodies(actual_threads);
392 std::vector<RangeBody<Range, Body>> range_bodies(actual_threads);
396 for (
unsigned int i=1; i<actual_threads; i++)
397 managed_bodies[i] = std::make_unique<Body>(body, Threads::split());
402 for (
unsigned int i=1; i<actual_threads; i++)
403 bodies[i] = managed_bodies[i].
get();
406 std::size_t range_size = range.size() / actual_threads;
408 typename Range::const_iterator current_beginning = range.begin();
410 for (
unsigned int i=0; i<actual_threads; i++)
412 std::size_t this_range_size = range_size;
414 if (i+1 == actual_threads)
415 this_range_size += range.size() % actual_threads;
417 ranges[i] = std::make_unique<Range>(range, current_beginning, current_beginning + this_range_size);
419 current_beginning = current_beginning + this_range_size;
423 for (
unsigned int i=0; i<actual_threads; i++)
425 range_bodies[i].range = ranges[i].get();
426 range_bodies[i].body = bodies[i];
430 std::vector<pthread_t> threads(actual_threads);
435#ifdef LIBMESH_HAVE_OPENMP
436#pragma omp parallel for schedule (static)
444 for (
int i=0; i<static_cast<int>(actual_threads); i++)
446#if !LIBMESH_HAVE_OPENMP
447 pthread_create(&threads[i],
nullptr, &run_body<Range, Body>, (
void *)&range_bodies[i]);
449 run_body<Range, Body>((
void *)&range_bodies[i]);
453#if !LIBMESH_HAVE_OPENMP
455 for (
unsigned int i=0; i<actual_threads; i++)
456 pthread_join(threads[i],
nullptr);
460 for (
unsigned int i=actual_threads-1; i != 0; i--)
461 bodies[i-1]->join(*bodies[i]);
468template <
typename Range,
typename Body,
typename Partitioner>
470void parallel_reduce (
const Range & range, Body & body,
const Partitioner &,
486 operator T () {
return val; }
490 spin_mutex::scoped_lock lock(
smutex);
497 spin_mutex::scoped_lock lock(
smutex);
505 spin_mutex::scoped_lock lock(
smutex);
512 spin_mutex::scoped_lock lock(
smutex);
519 spin_mutex::scoped_lock lock(
smutex);
526 spin_mutex::scoped_lock lock(
smutex);
533 spin_mutex::scoped_lock lock(
smutex);
540 spin_mutex::scoped_lock lock(
smutex);
Defines atomic operations which can only be executed on a single thread at a time.
atomic< T > & operator=(const atomic< T > &value)
void acquire(recursive_mutex &in_rmutex)
scoped_lock(recursive_mutex &in_rmutex)
scoped_lock(spin_mutex &in_smutex)
void acquire(spin_mutex &in_smutex)
void initialize(int=automatic)
static const int automatic
task_scheduler_init(int=automatic)
const Elem & get(const ElemType type_in)
bool in_threads
A boolean which is true iff we are in a Threads:: function It may be useful to assert(!...
int active_threads
An integer which is set to the number of active threads when we are in a Threads:: parallel operation...
void * run_body(void *args)
tbb::spin_mutex spin_mutex
Spin mutex.
NonConcurrentThread Thread
Use the non-concurrent placeholder.
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.
RAIIAcquire< bool, true, true > BoolAcquire
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.
unsigned int num_pthreads(const Range &range, unsigned int requested=libMesh::n_threads())
The libMesh namespace provides an interface to certain functionality in the library.