FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-taskflow/example/threadpool.cpp at master · loveleon/cpp-taskflow · GitHub
loveleon
/
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
/
example
/
threadpool.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
225 lines (181 loc) · 6.5 KB
Breadcrumbs
cpp-taskflow
/
example
/
threadpool.cpp
Copy path
File metadata and controls
225 lines (181 loc) · 6.5 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
2018/10/04 modified by Tsung-Wei Huang
//
- removed binary_tree
//
- removed modulo_insertions
//
- adopted to the new threadpool implementation
//
//
2018/09/19 modified by Tsung-Wei Huang
//
- added binary_tree benchmark
//
- added modulo_insertions benchmark
//
- refactored benchmark calls
//
//
2018/08/31 contributed by Guannan
//
//
Examples to test different threadpool implementations:
//
- SimpleThreadpool
//
- ProactiveThreadpool
//
- SpeculativeThreadpool
//
- PrivatizedThreadpool
#
include
<
taskflow/threadpool/threadpool.hpp
>
#
include
<
chrono
>
#
include
<
random
>
#
include
<
numeric
>
#
include
<
climits
>
//
Procedure: benchmark
#
define
BENCHMARK
(
TITLE, F
) \
std::cout <<
"
==========
"
<<
TITLE
<<
"
==========
\n
"
; \
\
std::cout <<
"
Threadpool [simple ] elapsed time:
"
\
<< F<tf::SimpleThreadpool<std::function<
void
()>>>() <<
"
ms
\n
"
; \
\
std::cout <<
"
Threadpool [proactive ] elapsed time:
"
\
<< F<tf::ProactiveThreadpool<std::function<
void
()>>>() <<
"
ms
\n
"
; \
\
std::cout <<
"
Threadpool [speculative] elapsed time:
"
\
<< F<tf::SpeculativeThreadpool<std::function<
void
()>>>() <<
"
ms
\n
"
; \
\
std::cout <<
"
Threadpool [privatized ] elapsed time:
"
\
<< F<tf::PrivatizedThreadpool<std::function<
void
()>>>() <<
"
ms
\n
"
; \
// ============================================================================
//
Divide and conquer to solve max subarray sum problem
//
https://www.geeksforgeeks.org/divide-and-conquer-maximum-sum-subarray/
// ============================================================================
constexpr
auto
tree_height =
20u
;
constexpr
auto
total_nodes =
1u
<< tree_height;
void
update_max
(std::atomic<
int
>& max_val,
const
int
value) {
int
old = max_val;
while
(old < value && !max_val.
compare_exchange_weak
(old, value));
}
int
max_cross_sum
(
const
std::vector<
int
>& vec,
int
l,
int
m,
int
r){
//
Include elements on left of mid.
auto
sum =
0
;
auto
left_sum =
INT_MIN
;
for
(
auto
i = m; i >= l; i--){
sum = sum + vec[i];
if
(sum > left_sum)
left_sum = sum;
}
//
Include elements on right of mid
sum =
0
;
auto
right_sum =
INT_MIN
;
for
(
auto
i = m+
1
; i <= r; i++)
{
sum = sum + vec[i];
if
(sum > right_sum)
right_sum = sum;
}
//
Return sum of elements on left and right of mid
return
left_sum + right_sum;
}
template
<
typename
T>
void
max_subsum
(
const
std::vector<
int
>& vec,
int
l,
int
r,
std::atomic<
int
>& max_num,
T& tp,
std::atomic<
size_t
>& counter,
std::promise<
void
>& promise
) {
//
Base Case: Only one element
if
(l == r) {
update_max
(max_num, vec[l]);
if
(++counter == total_nodes*
2
-
1
){
promise.
set_value
();
}
return
;
}
//
Find middle point
int
m = (l + r)/
2
;
tp.
emplace
([&, l=l, m=m] () {
max_subsum
(vec, l, m, max_num, tp, counter, promise);
});
tp.
emplace
([&, m=m, r=r] () {
max_subsum
(vec, m+
1
, r, max_num, tp, counter, promise);
});
update_max
(max_num,
max_cross_sum
(vec, l, m, r));
if
(++counter == total_nodes*
2
-
1
){
promise.
set_value
();
}
}
template
<
typename
T>
auto
subsum
(){
std::vector<
int
>
vec
(total_nodes);
std::iota
(vec.
begin
(), vec.
end
(), -
50
);
std::atomic<
int
> result {
INT_MIN
};
std::atomic<
size_t
> counter{
0
};
std::promise<
void
> promise;
auto
future = promise.
get_future
();
auto
start =
std::chrono::high_resolution_clock::now
();
T
tp
(
std::thread::hardware_concurrency
());
max_subsum
(vec,
0
, total_nodes-
1
, result, tp, counter, promise);
future.
get
();
auto
end =
std::chrono::high_resolution_clock::now
();
auto
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
return
elapsed.
count
();
}
// ============================================================================
//
Dynamic tasking through linear insertions
// ============================================================================
//
Procedure: linear_insertions
template
<
typename
T>
auto
linear_insertions
() {
const
int
num_threads =
std::thread::hardware_concurrency
();
const
int
num_tasks =
2000000
;
auto
beg =
std::chrono::high_resolution_clock::now
();
T
threadpool
(num_threads);
std::atomic<
int
> sum {
0
};
std::function<
void
(
int
)> insert;
std::promise<
int
> promise;
auto
future = promise.
get_future
();
insert = [&threadpool, &insert, &sum, &promise] (
int
i) {
if
(i >
0
) {
threadpool.
emplace
([i=i-
1
, &insert] () {
insert
(i);
});
}
else
{
if
(
size_t
s = ++sum; s == threadpool.
num_workers
()) {
promise.
set_value
(
1
);
}
}
};
for
(
int
i=
0
; i<num_threads; i++){
insert
(num_tasks / num_threads);
}
//
synchronize until all tasks finish
assert
(future.
get
() ==
1
);
assert
(sum == num_threads);
auto
end =
std::chrono::high_resolution_clock::now
();
return
std::chrono::duration_cast<std::chrono::milliseconds>(end - beg).
count
();
}
// ============================================================================
//
Insertions with atomic summation
// ============================================================================
//
Function: atomic_add
template
<
typename
T>
auto
atomic_add
() {
const
int
num_threads =
std::thread::hardware_concurrency
();
const
int
num_tasks =
1000000
;
std::atomic<
int
>
counter
(
0
);
auto
beg =
std::chrono::high_resolution_clock::now
();
std::promise<
void
> promise;
auto
future = promise.
get_future
();
T
threadpool
(num_threads);
for
(
size_t
i=
0
; i<num_tasks; i++){
threadpool.
emplace
([&](){
if
(counter.
fetch_add
(
1
, std::memory_order_relaxed) +
1
== num_tasks) {
promise.
set_value
();
}
});
}
future.
get
();
assert
(counter == num_tasks);
auto
end =
std::chrono::high_resolution_clock::now
();
return
std::chrono::duration_cast<std::chrono::milliseconds>(end - beg).
count
();
}
//
Function: main
int
main
(
int
argc,
char
* argv[]) {
BENCHMARK
(
"
Atomic Add
"
, atomic_add);
BENCHMARK
(
"
Linear Insertions
"
, linear_insertions);
BENCHMARK
(
"
Divide and Conquer
"
, subsum);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL