-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfibonacci.cpp
More file actions
60 lines (35 loc) · 1.11 KB
/
fibonacci.cpp
File metadata and controls
60 lines (35 loc) · 1.11 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
// This example demonstrates how to use Taskflow's recursive tasking features,
// using the famous Fibonacci recursion as an example.
#include <taskflow/taskflow.hpp>
tf::Executor& get_executor() {
static tf::Executor executor;
return executor;
}
size_t spawn_async(size_t N) {
if (N < 2) {
return N;
}
size_t res1, res2;
// create a task group
tf::TaskGroup tg = get_executor().task_group();
tg.silent_async([N, &res1](){ res1 = spawn_async(N-1); });
// tail optimization
res2 = spawn_async(N-2);
tg.corun();
return res1 + res2;
}
int main(int argc, char* argv[]) {
if(argc != 2) {
std::cerr << "usage: ./fibonacci N\n";
std::exit(EXIT_FAILURE);
}
size_t N = std::atoi(argv[1]);
auto& executor = get_executor();
auto tbeg = std::chrono::steady_clock::now();
printf("fib[%zu] = %zu\n", N, executor.async([N](){ return spawn_async(N); }).get());
auto tend = std::chrono::steady_clock::now();
std::cout << "elapsed time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(tend-tbeg).count()
<< " ms\n";
return 0;
}