aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/local_executor/tbb_local_executor.cpp
blob: 65d66594438b850159ed0713b1ec8bfb73218233 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "tbb_local_executor.h"

template <bool RespectTls>
void NPar::TTbbLocalExecutor<RespectTls>::SubmitAsyncTasks(TLocallyExecutableFunction exec, int firstId, int lastId) {
    for (int i = firstId; i < lastId; ++i) {
        Group.run([=] { exec(i); });
    }
}

template <bool RespectTls>
int NPar::TTbbLocalExecutor<RespectTls>::GetThreadCount() const noexcept {
    return NumberOfTbbThreads - 1;
}

template <bool RespectTls>
int NPar::TTbbLocalExecutor<RespectTls>::GetWorkerThreadId() const noexcept {
    return TbbArena.execute([] {
        return tbb::this_task_arena::current_thread_index();
    });
}

template <bool RespectTls>
void NPar::TTbbLocalExecutor<RespectTls>::Exec(TIntrusivePtr<ILocallyExecutable> exec, int id, int flags) {
    if (flags & WAIT_COMPLETE) {
        exec->LocalExec(id);
    } else {
        TbbArena.execute([=] {
            SubmitAsyncTasks([=] (int id) { exec->LocalExec(id); }, id, id + 1);
        });
    }
}

template <bool RespectTls>
void NPar::TTbbLocalExecutor<RespectTls>::ExecRange(TIntrusivePtr<ILocallyExecutable> exec, int firstId, int lastId, int flags) {
    if (flags & WAIT_COMPLETE) {
        TbbArena.execute([=] {
            if (RespectTls) {
                tbb::this_task_arena::isolate([=]{
                    tbb::parallel_for(firstId, lastId, [=] (int id) { exec->LocalExec(id); });
                });
            } else {
                tbb::parallel_for(firstId, lastId, [=] (int id) { exec->LocalExec(id); });
            }
        });
    } else {
        TbbArena.execute([=] {
            SubmitAsyncTasks([=] (int id) { exec->LocalExec(id); }, firstId, lastId);
        });
    }
}

template class NPar::TTbbLocalExecutor<true>;
template class NPar::TTbbLocalExecutor<false>;