-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_for.cpp
42 lines (37 loc) · 1.7 KB
/
parallel_for.cpp
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
#include "oneapi/tbb/blocked_range.h"
#include "oneapi/tbb/parallel_for.h"
#include <oneapi/tbb/task_arena.h>
#include <oneapi/tbb/concurrent_unordered_map.h>
example:
using CodeSizeSparseVec = tbb::concurrent_unordered_map<int, uint64_t>;
std::vector<std::thread> allThreads;
std::vector<std::shared_ptr<pva::Program>> allPrograms(programs().size());
std::copy(programs().begin(),programs().end(),allPrograms.begin());
oneapi::tbb::task_arena arena(30);
arena.execute([&]{
oneapi::tbb::parallel_for(tbb::blocked_range<size_t>(0, allPrograms.size()),
[&](const tbb::blocked_range<size_t>& range) {
for (size_t i=range.begin();i<range.end();++i){
allPrograms[i]->accept(visitor);
}
});
});
example:
auto cpu_kernel = [=, &lowered_vars] (const oneapi::tbb::blocked_range<size_t>& range) {
for (size_t i=range.begin(); i != range.end(); ++i) {
addVar(lowered_vars[i], i);
}
};
oneapi::tbb::global_control threads_control(oneapi::tbb::global_control::max_allowed_parallelism, THREADS_INDICES_BLOCK);
tbb::parallel_for(tbb::blocked_range<size_t>(0, lowered_vars.size()), cpu_kernel);
base_container_number_ += lowered_vars.size();
}
example:
// Limit the number of threads to two for all oneTBB parallel interfaces
oneapi::tbb::global_control global_limit(oneapi::tbb::global_control::max_allowed_parallelism, 2);
// the default parallelism
oneapi::tbb::parallel_for( /* ... */ [] {
// No more than two threads is expected; however, tbb::this_task_arena::max_concurrency() can return a bigger value
int thread_limit = oneapi::tbb::global_control::active_value(oneapi::tbb::global_control::max_allowed_parallelism);
assert(thread_limit == 2);
});