-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest.cpp
More file actions
603 lines (530 loc) · 19.3 KB
/
Copy pathtest.cpp
File metadata and controls
603 lines (530 loc) · 19.3 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include <algorithm>
#include <chrono>
#include <atomic>
#include <iostream>
#include <list>
#include <limits>
#include <stdexcept>
#include <thread>
#include <vector>
#include "quickpool.hpp"
int
checked_size_int(size_t size)
{
if (size > static_cast<size_t>(std::numeric_limits<int>::max())) {
throw std::length_error("test range is too large");
}
return static_cast<int>(size);
}
struct ThrowsOnCopy
{
ThrowsOnCopy() {}
ThrowsOnCopy(const ThrowsOnCopy&)
{
throw std::runtime_error("copy failed");
}
void operator()() const {}
};
void
stress_queue_growth_and_reuse()
{
using namespace quickpool;
const auto batches = 8;
const auto tasks = 1024;
ThreadPool pool(1);
for (auto batch = 0; batch < batches; ++batch) {
std::atomic_bool release{ false };
std::atomic_int done{ 0 };
for (auto i = 0; i < tasks; ++i) {
pool.push([&] {
while (!release.load()) {
std::this_thread::yield();
}
done++;
});
}
release = true;
pool.wait();
if (done != tasks) {
throw std::runtime_error("queue growth stress lost work");
}
}
}
void
stress_concurrent_push_and_reuse()
{
using namespace quickpool;
const auto hardware = std::max(std::thread::hardware_concurrency(), 2u);
const auto workers =
std::min<size_t>(static_cast<size_t>(hardware), static_cast<size_t>(8));
const auto producers = 4;
const auto batches = 20;
const auto tasks_per_producer = 512;
ThreadPool pool(workers);
for (auto batch = 0; batch < batches; ++batch) {
std::atomic_bool start{ false };
std::atomic_int ready{ 0 };
std::atomic_int done{ 0 };
std::vector<std::atomic_int> counts(static_cast<size_t>(producers));
for (auto& count : counts) {
count = 0;
}
std::vector<std::thread> producer_threads;
producer_threads.reserve(static_cast<size_t>(producers));
for (auto producer = 0; producer < producers; ++producer) {
producer_threads.emplace_back([&, producer] {
ready++;
while (!start.load()) {
std::this_thread::yield();
}
for (auto task = 0; task < tasks_per_producer; ++task) {
pool.push([&, producer, task] {
if ((task % 8) == 0) {
std::this_thread::yield();
}
counts[static_cast<size_t>(producer)]++;
done++;
});
if ((task % 16) == 0) {
std::this_thread::yield();
}
}
});
}
while (ready != producers) {
std::this_thread::yield();
}
start = true;
for (auto& producer_thread : producer_threads) {
producer_thread.join();
}
pool.wait();
const auto expected = producers * tasks_per_producer;
if (done != expected) {
throw std::runtime_error("concurrent push stress lost work");
}
for (auto producer = 0; producer < producers; ++producer) {
if (counts[static_cast<size_t>(producer)] != tasks_per_producer) {
throw std::runtime_error(
"concurrent push stress has wrong producer count");
}
}
}
}
int
main()
{
using namespace quickpool;
mem::aligned::atomic<loop::State> test{};
std::cout << "* [quickpool] lock free: "
<< (test.is_lock_free() ? "yes\n" : "no\n");
auto runs = 100;
for (auto run = 0; run < runs; run++) {
std::cout << "* [quickpool] unit tests: run " << run + 1 << "/" << runs
<< "\t\r" << std::flush;
// thread pool push
{
// std::cout << " * push: ";
std::vector<size_t> x(10000, 1);
for (size_t i = 0; i < x.size(); i++)
push([&](size_t i) -> void { x[i] = 2 * x[i]; }, i);
wait();
size_t count_wrong = 0;
for (size_t i = 0; i < x.size(); i++) {
if (count_wrong += (x[i] != 2))
std::cout << x[i];
}
if (count_wrong > 0) {
throw std::runtime_error("static push gives wrong result");
}
ThreadPool pool;
x = std::vector<size_t>(10000, 1);
for (size_t i = 0; i < x.size(); i++)
pool.push([&](size_t i) -> void { x[i] = 2 * x[i]; }, i);
pool.wait();
count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 2);
if (count_wrong > 0)
throw std::runtime_error("push gives wrong result");
// std::cout << "OK" << std::endl;
}
// async()
{
// std::cout << " * async: ";
std::vector<size_t> x(10000, 1);
auto dummy = [&](size_t i) { return 2 * x[i]; };
std::vector<std::future<size_t>> fut(x.size());
for (size_t i = 0; i < x.size(); i++)
fut[i] = async(dummy, i);
for (size_t i = 0; i < x.size(); i++)
x[i] = fut[i].get();
wait();
size_t count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 2);
if (count_wrong > 0)
throw std::runtime_error("static async gives wrong result");
ThreadPool pool;
x = std::vector<size_t>(10000, 1);
std::vector<std::future<size_t>> fut2(x.size());
for (size_t i = 0; i < x.size(); i++)
fut2[i] = pool.async(dummy, i);
for (size_t i = 0; i < x.size(); i++)
x[i] = fut2[i].get();
pool.wait();
count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 2);
if (count_wrong > 0)
throw std::runtime_error("async gives wrong result");
// std::cout << "OK" << std::endl;
}
// parallel_for()
{
// std::cout << " * parallel_for: ";
std::vector<size_t> x(10000, 1);
auto fun = [&](int i) {
auto idx = static_cast<size_t>(i);
x[idx] = 2 * x[idx];
};
parallel_for(0, checked_size_int(x.size()), fun);
size_t count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 2);
if (count_wrong > 0) {
for (auto xx : x)
std::cout << xx;
std::cout << std::endl;
throw std::runtime_error(
"static parallel_for gives wrong result");
}
ThreadPool pool;
pool.parallel_for(0, checked_size_int(x.size()), fun);
count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 4);
if (count_wrong > 0) {
for (auto xx : x)
std::cout << xx;
std::cout << std::endl;
throw std::runtime_error("parallel_for gives wrong result");
}
int empty_count = 0;
parallel_for(4, 4, [&](int) { empty_count++; });
pool.parallel_for(8, 2, [&](int) { empty_count++; });
if (empty_count != 0) {
throw std::runtime_error("empty parallel_for runs work");
}
// std::cout << "OK" << std::endl;
}
// nested parallel_for()
{
// std::cout << " * nested parallel_for: ";
std::vector<std::vector<double>> x(100);
for (auto& xx : x)
xx = std::vector<double>(100, 1.0);
parallel_for(0, checked_size_int(x.size()), [&](int i) {
auto row = static_cast<size_t>(i);
parallel_for(0,
checked_size_int(x[row].size()),
[&x, row](int j) {
x[row][static_cast<size_t>(j)] *= 2;
});
});
size_t count_wrong = 0;
for (auto xx : x) {
for (auto xxx : xx)
count_wrong += xxx != 2;
}
if (count_wrong > 0) {
throw std::runtime_error(
"static nested parallel_for gives wrong result");
}
ThreadPool pool;
pool.parallel_for(0, checked_size_int(x.size()), [&](int i) {
auto row = static_cast<size_t>(i);
pool.parallel_for(
0, checked_size_int(x[row].size()), [&x, row](int j) {
x[row][static_cast<size_t>(j)] *= 2;
});
});
count_wrong = 0;
for (auto xx : x) {
for (auto xxx : xx)
count_wrong += xxx != 4;
}
if (count_wrong > 0) {
throw std::runtime_error(
"nested parallel_for gives wrong result");
}
// std::cout << "OK" << std::endl;
}
// parallel_for_each()
{
// std::cout << " * parallel_for_each: ";
std::vector<size_t> x(10000, 1);
auto fun = [](size_t& xx) { xx = 2 * xx; };
parallel_for_each(x, fun);
size_t count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 2);
if (count_wrong > 0) {
for (auto xx : x)
std::cout << xx;
throw std::runtime_error(
"static parallel_for_each gives wrong result");
}
ThreadPool pool;
pool.parallel_for_each(x, fun);
count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 4);
if (count_wrong > 0)
throw std::runtime_error(
"parallel_for_each gives wrong result");
std::list<size_t> y(10000, 1);
parallel_for_each(y, fun);
count_wrong = 0;
for (auto yy : y)
count_wrong += (yy != 2);
if (count_wrong > 0)
throw std::runtime_error(
"static parallel_for_each list gives wrong result");
pool.parallel_for_each(y, fun);
count_wrong = 0;
for (auto yy : y)
count_wrong += (yy != 4);
if (count_wrong > 0)
throw std::runtime_error(
"parallel_for_each list gives wrong result");
// std::cout << "OK" << std::endl;
}
// nested parallel_for_each()
{
// std::cout << " * nested parallel_for_each: ";
std::vector<std::vector<double>> x(100);
for (auto& xx : x)
xx = std::vector<double>(100, 1.0);
parallel_for_each(x, [](std::vector<double>& xx) {
parallel_for_each(xx, [](double& xxx) { xxx *= 2; });
});
size_t count_wrong = 0;
for (auto xx : x) {
for (auto xxx : xx)
count_wrong += xxx != 2;
}
if (count_wrong > 0) {
throw std::runtime_error(
"static nested parallel_for_each gives wrong result");
}
ThreadPool pool;
pool.parallel_for_each(x, [&](std::vector<double>& xx) {
pool.parallel_for_each(xx, [](double& xxx) { xxx *= 2; });
});
count_wrong = 0;
for (auto xx : x) {
for (auto xxx : xx)
count_wrong += xxx != 4;
}
if (count_wrong > 0) {
throw std::runtime_error(
"nested parallel_for_each gives wrong result");
}
// std::cout << "OK" << std::endl;
}
// single threaded
{
// std::cout << " * single threaded: ";
ThreadPool pool(0);
std::vector<size_t> x(1000, 1);
auto dummy = [&](size_t i) -> void { x[i] = 2 * x[i]; };
for (size_t i = 0; i < x.size(); i++) {
pool.push(dummy, i);
}
std::atomic_int non_void_push_ran{ 0 };
pool.push([&] {
non_void_push_ran++;
return 1;
});
pool.wait();
size_t count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 2);
if (count_wrong > 0)
throw std::runtime_error("single threaded gives wrong result");
if (non_void_push_ran != 1)
throw std::runtime_error("single threaded non-void push failed");
pool.parallel_for(0, checked_size_int(x.size()), [&](int i) {
x[static_cast<size_t>(i)] += 1;
});
count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 3);
if (count_wrong > 0) {
throw std::runtime_error(
"single threaded parallel_for gives wrong result");
}
pool.parallel_for_each(x, [](size_t& xx) { xx += 1; });
count_wrong = 0;
for (size_t i = 0; i < x.size(); i++)
count_wrong += (x[i] != 4);
if (count_wrong > 0) {
throw std::runtime_error(
"single threaded parallel_for_each gives wrong result");
}
// std::cout << "OK" << std::endl;
}
// rethrows exceptions
{
// std::cout << " * exception handling: ";
ThreadPool pool;
// pool passes exceptions either via wait() or push()
std::exception_ptr eptr = nullptr;
try {
pool.push([] { throw std::runtime_error("test error"); });
while (!pool.done()) {
std::this_thread::yield();
}
for (size_t i = 0; i < 10; i++) {
pool.push([&] {});
}
} catch (...) {
eptr = std::current_exception();
}
if (!eptr) {
throw std::runtime_error("exception not rethrown by push");
} else {
eptr = nullptr;
}
// poool should be functional again
pool.push([] { throw std::runtime_error("test error"); });
try {
pool.wait();
} catch (...) {
eptr = std::current_exception();
}
if (!eptr) {
throw std::runtime_error("exception not rethrown by wait");
} else {
eptr = nullptr;
}
// std::cout << "OK" << std::endl;
}
// stop_and_reset()
{
ThreadPool pool(2);
std::atomic_bool started{ false };
std::atomic_bool release{ false };
std::atomic_int finished{ 0 };
pool.push([&] {
started = true;
while (!release.load()) {
std::this_thread::yield();
}
finished++;
});
while (!started.load()) {
std::this_thread::yield();
}
std::exception_ptr eptr = nullptr;
try {
try {
throw std::runtime_error("test error");
} catch (...) {
release = true;
pool.stop_and_reset();
}
} catch (...) {
eptr = std::current_exception();
}
if (!eptr) {
throw std::runtime_error(
"stop_and_reset does not rethrow pending exception");
}
if (finished != 1) {
throw std::runtime_error(
"stop_and_reset does not wait for running work");
}
std::atomic_int reused{ 0 };
pool.push([&] { reused++; });
pool.wait();
if (reused != 1) {
throw std::runtime_error(
"stop_and_reset does not restore pool");
}
}
// push exception safety
{
quickpool::sched::TaskManager manager(1);
try {
manager.push(ThrowsOnCopy{});
throw std::runtime_error("copy failure was not thrown");
} catch (const std::runtime_error&) {
}
if (!manager.done()) {
throw std::runtime_error("failed push leaves unfinished work");
}
}
// can be resized
{
// std::cout << " * resizing: ";
std::atomic_int dummy{ 0 };
ThreadPool pool(2);
pool.set_active_threads(1);
for (int i = 0; i < 100; i++)
pool.push([&] { dummy++; });
pool.wait();
if (dummy != 100) {
throw std::runtime_error("downsizing doesn't work");
}
pool.set_active_threads(2);
for (int i = 0; i < 100; i++)
pool.push([&] { dummy++; });
pool.wait();
pool.wait();
if (dummy != 200) {
throw std::runtime_error("restore size doesn't work");
}
pool.set_active_threads(3);
for (int i = 0; i < 100; i++)
pool.push([&] { dummy++; });
pool.wait();
pool.wait();
if (dummy != 300) {
throw std::runtime_error("upsizing doesn't work");
}
ThreadPool busy_pool(1);
std::atomic_int busy_resize_count{ 0 };
for (int i = 0; i < 10; i++) {
busy_pool.push([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
busy_resize_count++;
});
}
busy_pool.set_active_threads(2);
if (busy_resize_count != 10) {
throw std::runtime_error("busy upsizing drops work");
}
pool.set_active_threads(std::thread::hardware_concurrency() + 1);
for (int i = 0; i < 100; i++)
pool.push([&] { dummy++; });
pool.wait();
pool.wait();
if (dummy != 400) {
throw std::runtime_error("oversizing doesn't work");
}
// std::cout << "OK" << std::endl;
}
}
std::cout << "* [quickpool] unit tests: OK " << std::endl;
std::cout << "* [quickpool] stress tests: queue growth\t\r"
<< std::flush;
stress_queue_growth_and_reuse();
std::cout << "* [quickpool] stress tests: concurrent push\t\r"
<< std::flush;
stress_concurrent_push_and_reuse();
std::cout << "* [quickpool] stress tests: OK "
<< std::endl;
return 0;
}