10#ifndef EIGEN_THREADPOOL_FORKJOIN_H
11#define EIGEN_THREADPOOL_FORKJOIN_H
14#include "./InternalHeaderCheck.h"
59class ForkJoinScheduler {
64 template <
typename DoFnType,
typename DoneFnType,
typename ThreadPoolEnv>
65 static void ParallelForAsync(
Index start,
Index end,
Index granularity, DoFnType&& do_func, DoneFnType&& done,
66 ThreadPoolTempl<ThreadPoolEnv>* thread_pool) {
71 thread_pool->Schedule([start, end, granularity, thread_pool, do_func = std::forward<DoFnType>(do_func),
72 done = std::forward<DoneFnType>(done)]() {
73 RunParallelFor(start, end, granularity, do_func, thread_pool);
81 template <
typename DoFnType,
typename ThreadPoolEnv>
82 static void ParallelFor(
Index start,
Index end,
Index granularity, DoFnType&& do_func,
83 ThreadPoolTempl<ThreadPoolEnv>* thread_pool) {
84 if (start >= end)
return;
86 auto done = [&barrier]() { barrier.Notify(); };
87 ParallelForAsync(start, end, granularity, do_func, done, thread_pool);
93 template <
typename LeftType,
typename RightType,
typename ThreadPoolEnv>
94 static void ForkJoin(LeftType&& left_thunk, RightType&& right_thunk, ThreadPoolTempl<ThreadPoolEnv>* thread_pool) {
95 typedef typename ThreadPoolTempl<ThreadPoolEnv>::Task Task;
96 std::atomic<bool> right_done(
false);
97 auto execute_right = [&right_thunk, &right_done]() {
98 std::forward<RightType>(right_thunk)();
99 right_done.store(
true, std::memory_order_release);
101 thread_pool->Schedule(execute_right);
102 std::forward<LeftType>(left_thunk)();
104 while (!right_done.load(std::memory_order_acquire)) {
105 thread_pool->MaybeGetTask(&task);
106 if (task.f) task.f();
116 const Index size = end - start;
117 const Index offset = numext::round_down(9 * (size + 1) / 16, granularity);
118 return start + offset;
121 template <
typename DoFnType,
typename ThreadPoolEnv>
122 static void RunParallelFor(
Index start,
Index end,
Index granularity, DoFnType&& do_func,
123 ThreadPoolTempl<ThreadPoolEnv>* thread_pool) {
124 Index mid = ComputeMidpoint(start, end, granularity);
125 if ((end - start) < granularity || mid == start || mid == end) {
129 ForkJoin([start, mid, granularity, &do_func,
130 thread_pool]() { RunParallelFor(start, mid, granularity, do_func, thread_pool); },
131 [mid, end, granularity, &do_func, thread_pool]() {
132 RunParallelFor(mid, end, granularity, do_func, thread_pool);
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82