-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfor_loop.cpp
More file actions
51 lines (40 loc) · 1.49 KB
/
for_loop.cpp
File metadata and controls
51 lines (40 loc) · 1.49 KB
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
#include <taskflow/taskflow.hpp>
void loop_body() { }
int main(int argc, const char* argv[]) {
if(argc != 2) {
std::cerr << "usage: ./for_loop num_iterations" << std::endl;
std::exit(EXIT_FAILURE);
}
const size_t N = std::atoi(argv[1]);
// expressing loop using Taskflow's control taskflow graph programming model
tf::Executor executor;
tf::Taskflow taskflow;
auto X = taskflow.emplace([](){});
auto Y = taskflow.emplace([&, i=size_t(0)]() mutable {
loop_body();
return (i++ < N) ? 0 : 1;
});
auto Z = taskflow.emplace([](){});
X.precede(Y);
Y.precede(Y, Z);
auto tbeg = std::chrono::steady_clock::now();
executor.run(taskflow).wait();
auto tend = std::chrono::steady_clock::now();
std::cout << "in-graph control flow time : " << std::chrono::duration_cast<std::chrono::microseconds>(tend-tbeg).count()
<< " us\n";
// expressing the loop without using cointrol taskflow graph
tf::Taskflow taskflow_x, taskflow_y, taskflow_z;
taskflow_x.emplace([](){});
taskflow_y.emplace(loop_body);
taskflow_z.emplace([](){});
tbeg = std::chrono::steady_clock::now();
executor.run(taskflow_x).wait();
for(size_t i=0; i<N; ++i) {
executor.run(taskflow_y).wait();
}
executor.run(taskflow_z).wait();
tend = std::chrono::steady_clock::now();
std::cout << "out-of-graph control flow time: " << std::chrono::duration_cast<std::chrono::microseconds>(tend-tbeg).count()
<< " us\n";
return 0;
}