FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
taskflow/examples/reduce.cpp at master · 521hellogithub/taskflow · GitHub
521hellogithub
/
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
taskflow
/
examples
/
reduce.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
127 lines (105 loc) · 3.24 KB
Breadcrumbs
taskflow
/
examples
/
reduce.cpp
Copy path
File metadata and controls
127 lines (105 loc) · 3.24 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
This example demonstrates how to use 'reduce' method.
#
include
<
taskflow/taskflow.hpp
>
#
include
<
chrono
>
#
include
<
limits.h
>
#
define
MAX_DATA_SIZE
40000000
struct
Data
{
int
a {::
rand
()};
int
b {::
rand
()};
int
transform
()
const
{
return
a*a +
2
*a*b + b*b;
}
};
//
Procedure: reduce
//
This procedure demonstrates
void
reduce
() {
std::cout <<
"
Benchmark: reduce
"
<< std::endl;
std::vector<
int
> data;
data.
reserve
(
MAX_DATA_SIZE
);
for
(
int
i=
0
; i<
MAX_DATA_SIZE
; ++i) {
data.
push_back
(::
rand
());
}
//
sequential method
auto
sbeg =
std::chrono::steady_clock::now
();
auto
smin = std::numeric_limits<
int
>::
max
();
for
(
auto
& d : data) {
smin =
std::min
(smin, d);
}
auto
send =
std::chrono::steady_clock::now
();
std::cout <<
"
[sequential] reduce:
"
<< std::chrono::duration_cast<std::chrono::microseconds>(send - sbeg).
count
()
<<
"
us
\n
"
;
//
taskflow
auto
tbeg =
std::chrono::steady_clock::now
();
tf::Taskflow taskflow;
tf::Executor executor;
auto
tmin = std::numeric_limits<
int
>::
max
();
taskflow.
reduce
(
data.
begin
(),
data.
end
(),
tmin,
[] (
int
& l,
const
auto
& r) {
return
std::min
(l, r); }
);
executor.
run
(taskflow).
get
();
auto
tend =
std::chrono::steady_clock::now
();
std::cout <<
"
[taskflow] reduce:
"
<< std::chrono::duration_cast<std::chrono::microseconds>(tend - tbeg).
count
()
<<
"
us
\n
"
;
//
assertion
if
(tmin == smin) {
std::cout <<
"
result is correct
"
<< std::endl;
}
else
{
std::cout <<
"
result is incorrect:
"
<< smin <<
"
!=
"
<< tmin << std::endl;
}
taskflow.
dump
(std::cout);
}
//
Procedure: transform_reduce
void
transform_reduce
() {
std::cout <<
"
Benchmark: transform_reduce
"
<< std::endl;
std::vector<Data>
data
(
MAX_DATA_SIZE
);
//
sequential method
auto
sbeg =
std::chrono::steady_clock::now
();
auto
smin = std::numeric_limits<
int
>::
max
();
for
(
auto
& d : data) {
smin =
std::min
(smin, d.
transform
());
}
auto
send =
std::chrono::steady_clock::now
();
std::cout <<
"
[sequential] transform_reduce
"
<< std::chrono::duration_cast<std::chrono::microseconds>(send - sbeg).
count
()
<<
"
us
\n
"
;
//
taskflow
auto
tbeg =
std::chrono::steady_clock::now
();
tf::Taskflow tf;
auto
tmin = std::numeric_limits<
int
>::
max
();
tf.
transform_reduce
(data.
begin
(), data.
end
(), tmin,
[] (
int
l,
int
r) {
return
std::min
(l, r); },
[] (
const
Data& d) {
return
d.
transform
(); }
);
tf::Executor
().
run
(tf).
get
();
auto
tend =
std::chrono::steady_clock::now
();
std::cout <<
"
[taskflow] transform_reduce
"
<< std::chrono::duration_cast<std::chrono::microseconds>(tend - tbeg).
count
()
<<
"
us
\n
"
;
//
assertion
assert
(tmin == smin);
}
//
----------------------------------------------------------------------------
//
Function: main
int
main
(
int
argc,
char
* argv[]) {
if
(argc !=
2
) {
std::cerr <<
"
usage: ./reduce [reduce|transform_reduce]
"
<< std::endl;
std::exit
(
EXIT_FAILURE
);
}
if
(
std::strcmp
(argv[
1
],
"
reduce
"
) ==
0
) {
reduce
();
}
else
if
(
std::strcmp
(argv[
1
],
"
transform_reduce
"
) ==
0
) {
transform_reduce
();
}
else
{
std::cerr <<
"
invalid method
"
<< argv[
1
] << std::endl;
std::exit
(
EXIT_FAILURE
);
}
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL