forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadpool.cpp
361 lines (291 loc) · 9.53 KB
/
threadpool.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// 2019/02/15 modified by Tsung-Wei Huang
// - modified batch_insertion
//
// 2018/12/08 modified by Tsung-Wei Huang
// - refactored the output format
//
// 2018/12/07 modified by Tsung-Wei Huang
// - refactored the output format
//
// 2018/12/06 modified by Tsung-Wei Huang
// - added nested insertions test
//
// 2018/12/04 modified by Tsung-Wei Huang
// - replace privatized threadpool with work stealing threadpool
//
// 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 <cmath>
#include <random>
#include <numeric>
#include <climits>
#include <iomanip>
constexpr int WIDTH = 12;
using Closure = std::function<void()>;
// Procedure: benchmark
#define BENCHMARK(TITLE, F) \
std::cout \
<< std::setw(WIDTH) << TITLE << std::flush \
<< std::setw(WIDTH) << F<tf::SimpleThreadpool<Closure>>() << std::flush \
<< std::setw(WIDTH) << F<tf::ProactiveThreadpool<Closure>>() << std::flush \
<< std::setw(WIDTH) << F<tf::SpeculativeThreadpool<Closure>>() << std::flush \
<< std::setw(WIDTH) << F<tf::WorkStealingThreadpool<Closure>>() << std::flush \
<< std::endl;
// ============================================================================
// 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();
}
// ============================================================================
// skewed insertions
// ============================================================================
// Function: nested_insertions
template <typename T>
auto nested_insertions() {
const int num_threads = std::thread::hardware_concurrency();
const int num_tasks = 32;
auto beg = std::chrono::high_resolution_clock::now();
std::atomic<int64_t> counter(0);
std::promise<void> promise;
auto future = promise.get_future();
auto increment = [&] () {
int64_t sum = 0;
for(int i=0; i<5; ++i) {
sum = (sum + 1)*num_tasks;
}
if(++counter == sum) {
promise.set_value();
}
};
T threadpool(num_threads);
threadpool.emplace([&] () {
std::this_thread::sleep_for(std::chrono::microseconds(100));
for(int i=0; i<num_tasks; ++i) {
increment();
threadpool.emplace([&] () {
for(int i=0; i<num_tasks; ++i) {
increment();
threadpool.emplace([&] () {
for(int i=0; i<num_tasks; ++i) {
increment();
threadpool.emplace([&] () {
for(int i=0; i<num_tasks; ++i) {
increment();
threadpool.emplace([&] () {
for(int i=0; i<num_tasks; ++i) {
increment();
}
});
}
});
}
});
}
});
}
});
future.get();
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(end - beg).count();
}
// ============================================================================
// batch insertion
// ============================================================================
// Function: batch_insertions
template <typename T>
auto batch_insertions() {
const int num_threads = std::thread::hardware_concurrency();
const int num_batches = 512;
const int num_tasks = 512;
auto beg = std::chrono::high_resolution_clock::now();
std::atomic<int> counter(0);
std::vector<std::function<void()>> tasks (num_tasks);
std::promise<void> promise;
auto future = promise.get_future();
for(auto & task : tasks) {
task = [&] () {
if(++counter == 2*num_tasks*num_batches) {
promise.set_value();
}
};
}
T threadpool(num_threads);
// master to insert a batch
for(size_t i=0; i<num_batches; ++i) {
auto copy = tasks;
threadpool.batch(copy);
}
for(size_t i=0; i<num_batches; i++){
threadpool.emplace([&](){
auto copy = tasks;
threadpool.batch(copy);
});
}
future.get();
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[]) {
std::cout << std::setw(WIDTH) << "workload"
<< std::setw(WIDTH) << "simple"
<< std::setw(WIDTH) << "pro"
<< std::setw(WIDTH) << "spec"
<< std::setw(WIDTH) << "steal"
<< std::endl;
BENCHMARK("Atomic", atomic_add);
BENCHMARK("Linear", linear_insertions);
BENCHMARK("D&C", subsum);
BENCHMARK("Batch", batch_insertions);
BENCHMARK("Nested", nested_insertions);
return 0;
}