forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtbb.cpp
More file actions
31 lines (24 loc) · 913 Bytes
/
Copy pathtbb.cpp
File metadata and controls
31 lines (24 loc) · 913 Bytes
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
#include "async_task.hpp"
#include <tbb/task_group.h>
#include <tbb/global_control.h>
// async_task computation
void async_task_tbb(unsigned num_threads, size_t num_tasks) {
tbb::global_control c(
tbb::global_control::max_allowed_parallelism, num_threads
);
std::atomic<size_t> counter(0);
tbb::task_group tg;
for (size_t i = 0; i < num_tasks; i++) {
tg.run([&] { func(counter); });
}
tg.wait(); // Ensures all tasks are completed before returning
if(counter.load(std::memory_order_relaxed) != num_tasks) {
throw std::runtime_error("incorrect result");
}
}
std::chrono::microseconds measure_time_tbb(unsigned num_threads, size_t num_tasks) {
auto beg = std::chrono::high_resolution_clock::now();
async_task_tbb(num_threads, num_tasks);
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(end - beg);
}