17 struct TbbThreadContext {
21 static thread_local TbbThreadContext tbbThreadContext;
25 tbb::task_arena& arena;
26 tbb::task_group group;
27 std::atomic<int> taskCnt;
30 explicit TbbTask(tbb::task_arena& arena)
40 virtual void wait()
override {
41 SPH_ASSERT(!tbbThreadContext.task,
"waiting on child tasks is currently not implemented");
42 arena.execute([
this] { group.wait(); });
49 void submit(
const Function<
void()>& task) {
50 arena.execute([
this, task] {
51 group.run([
this, task] {
54 tbbThreadContext.task =
nullptr;
61 void submitChild(
const Function<
void()>& task) {
69 tbb::task_arena arena;
72 TbbData(
const Size numThreads,
const Size granularity)
73 : granularity(granularity) {
74 arena.initialize(numThreads == 0 ? tbb::task_scheduler_init::default_num_threads() : numThreads);
79 data = makeAuto<TbbData>(numThreads, granularity);
85 data->granularity = newGranularity;
89 data = makeAuto<TbbData>(numThreads, data->granularity);
94 if (tbbThreadContext.task) {
95 tbbThreadContext.task->submitChild(task);
98 return tbbThreadContext.task;
101 handle->submit(task);
107 const int index = tbb::this_task_arena::current_thread_index();
116 return tbb::this_task_arena::max_concurrency();
120 return data->granularity;
125 const Size granularity,
127 data->arena.execute([from, to, granularity, &functor] {
128 tbb::parallel_for(tbb::blocked_range<Size>(from, to, granularity),
129 [&functor](
const tbb::blocked_range<Size> range) { functor(range.begin(), range.end()); });
134 if (!globalInstance) {
135 globalInstance = makeShared<Tbb>();
137 return globalInstance;
#define SPH_ASSERT(x,...)
uint32_t Size
Integral type used to index arrays (by default).
Additional math routines (with more includes).
#define NAMESPACE_SPH_END
Wrapper of type value of which may or may not be present.
const NothingType NOTHING
Implements IScheduler interface using TBB.
Handle used to control tasks submitted into the scheduler.
virtual bool completed() const =0
Checks if the task already finished.
virtual void wait()=0
Waits till the task and all the child tasks are completed.
SharedPtr< T > sharedFromThis() const
virtual Optional< Size > getThreadIdx() const override
Returns the index of the calling thread.
void setGranularity(const Size newGranularity)
virtual void parallelFor(const Size from, const Size to, const Size granularity, const Function< void(Size n1, Size n2)> &functor) override
Processes the given range concurrently.
void setThreadCnt(const Size numThreads)
Tbb(const Size numThreads=0, const Size granularity=1000)
virtual Size getThreadCnt() const override
Returns the number of threads used by this scheduler.
virtual SharedPtr< ITask > submit(const Function< void()> &task) override
Submits a task to be potentially executed asynchronously.
static SharedPtr< Tbb > getGlobalInstance()
virtual Size getRecommendedGranularity() const override
Returns a value of granularity that is expected to perform well with the current thread count.