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) 51 #else // Maybe support gcc __thread eventually? 52 # define LIBMESH_TLS_TYPE(type) type 53 # define LIBMESH_TLS_REF(value) (value) 63 #ifdef LIBMESH_HAVE_CXX11_THREAD 67 typedef std::thread
Thread;
74 typedef NonConcurrentThread
Thread;
76 #endif // LIBMESH_HAVE_CXX11_THREAD 184 pthread_mutexattr_init(&
attr);
185 pthread_mutexattr_settype(&
attr, PTHREAD_MUTEX_RECURSIVE);
213 template <
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;
221 template <
typename Range,
typename Body>
229 template <
typename Range,
typename Body>
234 Body & body = *range_body->
body;
235 Range & range = *range_body->
range;
269 template <
typename Range,
typename Body>
271 void 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);
354 template <
typename Range,
typename Body,
typename Partitioner>
356 void parallel_for (
const Range & range,
const Body & body,
const Partitioner &,
366 template <
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]);
468 template <
typename Range,
typename Body,
typename Partitioner>
470 void parallel_reduce (
const Range & range, Body & body,
const Partitioner &,
481 template <
typename T>
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);
554 #endif // #ifdef LIBMESH_HAVE_PTHREAD 556 #endif // LIBMESH_SQUASH_HEADER_WARNING 558 #endif // LIBMESH_THREADS_PTHREAD_H
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.
scoped_lock(recursive_mutex &in_rmutex)
void acquire(recursive_mutex &in_rmutex)
void initialize(int=automatic)
void * run_body(void *args)
The libMesh namespace provides an interface to certain functionality in the library.
bool in_threads
A boolean which is true iff we are in a Threads:: function It may be useful to assert(!Threadsin_thre...
tbb::task_scheduler_init task_scheduler_init
Scheduler to manage the TBB thread pool.
unsigned int num_pthreads(const Range &range, unsigned int requested=libMesh::n_threads())
scoped_lock(spin_mutex &in_smutex)
void acquire(spin_mutex &in_smutex)
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.
tbb::spin_mutex spin_mutex
Spin mutex.
tbb::split split
Dummy "splitting object" used to distinguish splitting constructors from copy constructors.
Defines atomic operations which can only be executed on a single thread at a time.
atomic< T > & operator=(const atomic< T > &value)
int active_threads
An integer which is set to the number of active threads when we are in a Threads:: parallel operation...
RAIIAcquire< bool, true, true > BoolAcquire
static const int automatic
task_scheduler_init(int=automatic)
NonConcurrentThread Thread
Use the non-concurrent placeholder.