forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.cpp
More file actions
57 lines (33 loc) · 1004 Bytes
/
fibonacci.cpp
File metadata and controls
57 lines (33 loc) · 1004 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <taskflow/taskflow.hpp>
int spawn(int n, tf::Subflow& sbf) {
if (n < 2) return n;
int res1, res2;
// compute f(n-1)
sbf.emplace([&res1, n] (tf::Subflow& sbf_n_1) { res1 = spawn(n - 1, sbf_n_1); } )
.name(std::to_string(n-1));
// compute f(n-2)
sbf.emplace([&res2, n] (tf::Subflow& sbf_n_2) { res2 = spawn(n - 2, sbf_n_2); } )
.name(std::to_string(n-2));
sbf.join();
return res1 + res2;
}
int main(int argc, char* argv[]) {
if(argc != 2) {
std::cerr << "usage: ./fibonacci N\n";
std::exit(EXIT_FAILURE);
}
int N = std::atoi(argv[1]);
if(N < 0) {
throw std::runtime_error("N must be non-negative");
}
int res; // result
tf::Executor executor;
tf::Taskflow taskflow("fibonacci");
taskflow.emplace([&res, N] (tf::Subflow& sbf) {
res = spawn(N, sbf);
}).name(std::to_string(N));
executor.run(taskflow).wait();
//taskflow.dump(std::cout);
std::cout << "Fib[" << N << "]: " << res << std::endl;
return 0;
}