FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-taskflow/examples/fibonacci.cpp at master · feiyunwill/cpp-taskflow · GitHub
feiyunwill
/
cpp-taskflow
Public
forked from
taskflow/taskflow
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-taskflow
/
examples
/
fibonacci.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (35 loc) · 1.11 KB
Breadcrumbs
cpp-taskflow
/
examples
/
fibonacci.cpp
Copy path
File metadata and controls
60 lines (35 loc) · 1.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
;
}
Back
|
FazBrowse Home
|
New Git URL