FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-taskflow/unittest/spmc_queue.cpp at master · cppwanderboy/cpp-taskflow · GitHub
cppwanderboy
/
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
/
unittest
/
spmc_queue.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
535 lines (439 loc) · 12.5 KB
Breadcrumbs
cpp-taskflow
/
unittest
/
spmc_queue.cpp
Copy path
File metadata and controls
535 lines (439 loc) · 12.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
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
//
2019/05/15 - modified by Tsung-Wei Huang
//
- temporarily disable executor test
//
//
2019/04/11 - modified by Tsung-Wei Huang
//
- renamed threadpool to executor
//
//
2019/02/15 - modified by Tsung-Wei Huang
//
- modified batch tests (reference instead of move)
//
//
2018/12/04 - modified by Tsung-Wei Huang
//
- replaced privatized executor with work stealing executor
//
//
2018/12/03 - modified by Tsung-Wei Huang
//
- added work stealing queue tests
//
//
2018/11/29 - modified by Chun-Xun Lin
//
- added batch tests
//
//
2018/10/04 - modified by Tsung-Wei Huang
//
- removed binary tree tests
//
- removed spawn/shutdown tests
//
- removed siltne_async and async tests
//
- added emplace test
//
- adopted the new thread pool implementation
//
//
2018/09/29 - modified by Tsung-Wei Huang
//
- added binary tree tests
//
- added worker queue tests
//
- added external thread tests
//
- refactored executor tests
//
//
2018/09/13 - modified by Tsung-Wei Huang & Chun-Xun
//
- added tests for ownership
//
- modified spawn-shutdown tests
//
//
2018/09/10 - modified by Tsung-Wei Huang
//
- added tests for SpeculativeExecutor
//
- added dynamic tasking tests
//
- added spawn and shutdown tests
//
//
2018/09/02 - created by Guannan
//
- test_silent_async
//
- test_async
//
- test_wait_for_all
#
define
DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#
include
<
doctest.h
>
#
include
<
taskflow/taskflow.hpp
>
#
include
<
chrono
>
//
#include <taskflow/executor/semaphore.hpp>
// ============================================================================
//
WorkStealingQueue tests
// ============================================================================
//
Procedure: wsq_test_owner
void
wsq_test_owner
() {
int64_t
cap =
2
;
tf::WorkStealingQueue<
int
>
queue
(cap);
std::deque<
int
> gold;
REQUIRE
(queue.
capacity
() ==
2
);
REQUIRE
(queue.
empty
());
for
(
int
i=
2
; i<=(
1
<<
16
); i <<=
1
) {
REQUIRE
(queue.
empty
());
for
(
int
j=
0
; j<i; ++j) {
queue.
push
(j);
}
for
(
int
j=
0
; j<i; ++j) {
auto
item = queue.
pop
();
REQUIRE
((item && *item == i-j-
1
));
}
REQUIRE
(!queue.
pop
());
REQUIRE
(queue.
empty
());
for
(
int
j=
0
; j<i; ++j) {
queue.
push
(j);
}
for
(
int
j=
0
; j<i; ++j) {
auto
item = queue.
steal
();
REQUIRE
((item && *item == j));
}
REQUIRE
(!queue.
pop
());
REQUIRE
(queue.
empty
());
for
(
int
j=
0
; j<i; ++j) {
//
enqueue
if
(
auto
dice = ::
rand
()%
3
; dice ==
0
) {
queue.
push
(j);
gold.
push_back
(j);
}
//
pop back
else
if
(dice ==
1
) {
auto
item = queue.
pop
();
if
(gold.
empty
()) {
REQUIRE
(!item);
}
else
{
REQUIRE
(*item == gold.
back
());
gold.
pop_back
();
}
}
//
pop front
else
{
auto
item = queue.
steal
();
if
(gold.
empty
()) {
REQUIRE
(!item);
}
else
{
REQUIRE
(*item == gold.
front
());
gold.
pop_front
();
}
}
REQUIRE
(queue.
size
() == (
int
)gold.
size
());
}
while
(!queue.
empty
()) {
auto
item = queue.
pop
();
REQUIRE
((item && *item == gold.
back
()));
gold.
pop_back
();
}
REQUIRE
(gold.
empty
());
REQUIRE
(queue.
capacity
() == i);
}
}
//
Procedure: wsq_test_n_thieves
void
wsq_test_n_thieves
(
int
N) {
int64_t
cap =
2
;
tf::WorkStealingQueue<
int
>
queue
(cap);
REQUIRE
(queue.
capacity
() ==
2
);
REQUIRE
(queue.
empty
());
for
(
int
i=
2
; i<=(
1
<<
16
); i <<=
1
) {
REQUIRE
(queue.
empty
());
int
p =
0
;
std::vector<std::deque<
int
>>
cdeqs
(N);
std::vector<std::thread> consumers;
std::deque<
int
> pdeq;
auto
num_stolen = [&] () {
int
total =
0
;
for
(
const
auto
& cdeq : cdeqs) {
total += cdeq.
size
();
}
return
total;
};
for
(
int
n=
0
; n<N; n++) {
consumers.
emplace_back
([&, n] () {
while
(
num_stolen
() + (
int
)pdeq.
size
() != i) {
if
(
auto
dice = ::
rand
() %
4
; dice ==
0
) {
if
(
auto
item = queue.
steal
(); item) {
cdeqs[n].
push_back
(*item);
}
}
}
});
}
std::thread
producer
([&] () {
while
(p < i) {
if
(
auto
dice = ::
rand
() %
4
; dice ==
0
) {
queue.
push
(p++);
}
else
if
(dice ==
1
) {
if
(
auto
item = queue.
pop
(); item) {
pdeq.
push_back
(*item);
}
}
}
});
producer.
join
();
for
(
auto
& c : consumers) {
c.
join
();
}
REQUIRE
(queue.
empty
());
REQUIRE
(queue.
capacity
() <= i);
std::set<
int
> set;
for
(
const
auto
& cdeq : cdeqs) {
for
(
auto
k : cdeq) {
set.
insert
(k);
}
}
for
(
auto
k : pdeq) {
set.
insert
(k);
}
for
(
int
j=
0
; j<i; ++j) {
REQUIRE
(set.
find
(j) != set.
end
());
}
REQUIRE
((
int
)set.
size
() == i);
}
}
//
----------------------------------------------------------------------------
//
Testcase: WSQTest.Owner
//
----------------------------------------------------------------------------
TEST_CASE
(
"
WSQ.Owner
"
*
doctest::timeout
(
300
)) {
wsq_test_owner
();
}
//
----------------------------------------------------------------------------
//
Testcase: WSQTest.1Thief
//
----------------------------------------------------------------------------
TEST_CASE
(
"
WSQ.1Thief
"
*
doctest::timeout
(
300
)) {
wsq_test_n_thieves
(
1
);
}
//
----------------------------------------------------------------------------
//
Testcase: WSQTest.2Thieves
//
----------------------------------------------------------------------------
TEST_CASE
(
"
WSQ.2Thieves
"
*
doctest::timeout
(
300
)) {
wsq_test_n_thieves
(
2
);
}
//
----------------------------------------------------------------------------
//
Testcase: WSQTest.3Thieves
//
----------------------------------------------------------------------------
TEST_CASE
(
"
WSQ.3Thieves
"
*
doctest::timeout
(
300
)) {
wsq_test_n_thieves
(
3
);
}
//
----------------------------------------------------------------------------
//
Testcase: WSQTest.4Thieves
//
----------------------------------------------------------------------------
TEST_CASE
(
"
WSQ.4Thieves
"
*
doctest::timeout
(
300
)) {
wsq_test_n_thieves
(
4
);
}
/*
// ============================================================================
// Executor tests
// ============================================================================
// Procedure: test_ownership
template <typename ExecutorType>
void test_ownership(ExecutorType& tp) {
REQUIRE(tp.is_owner());
tp.emplace([&](){
if(tp.num_workers() == 0) {
REQUIRE(tp.is_owner());
}
else {
REQUIRE(!tp.is_owner());
}
});
std::vector<std::thread> threads;
for(int i=0; i<10; ++i) {
threads.emplace_back([&] () {
REQUIRE(!tp.is_owner());
});
}
for(auto& t : threads) {
t.join();
}
}
// Procedure: test_emplace
template <typename ExecutorType>
void test_emplace(ExecutorType& tp) {
constexpr size_t num_tasks = 1024;
std::atomic<size_t> counter{0};
for(size_t i=0; i<num_tasks; i++){
tp.emplace([&counter](){
counter.fetch_add(1, std::memory_order_relaxed); }
);
}
while(counter != num_tasks);
}
// Procedure: test_dynamic_tasking
template <typename T>
void test_dynamic_tasking(T& executor) {
std::atomic<size_t> sum {0};
std::atomic<size_t> cnt {0};
std::function<void(int)> insert;
std::promise<int> promise;
auto future = promise.get_future();
insert = [&executor, &insert, &sum, &promise, &cnt] (int i) {
if(i > 0) {
++cnt;
executor.emplace([i=i-1, &insert] () {
insert(i);
});
}
else {
if(auto s = ++sum; s == executor.num_workers()) {
promise.set_value(1);
}
}
};
if(auto W = executor.num_workers(); W > 0) {
for(size_t i=0; i<executor.num_workers(); i++){
insert(100);
}
}
else {
promise.set_value(1);
}
// synchronize until all tasks finish
REQUIRE(future.get() == 1);
REQUIRE(cnt == 100 * executor.num_workers());
REQUIRE(sum == executor.num_workers());
}
// Procedure: test_external_threads
template <typename T>
void test_external_threads(T& executor) {
constexpr int num_tasks = 65536;
std::vector<std::thread> threads;
std::atomic<size_t> sum {0};
for(int i=0; i<10; ++i) {
threads.emplace_back([&] () {
std::this_thread::sleep_for(std::chrono::microseconds(100));
for(int j=0; j<num_tasks; ++j) {
executor.emplace([&] () {
sum.fetch_add(1, std::memory_order_relaxed);
});
}
});
}
// master thread to insert
for(int j=0; j<num_tasks; ++j) {
executor.emplace([&] () {
sum.fetch_add(1, std::memory_order_relaxed);
});
}
// worker thread to insert
for(int i=0; i<10; ++i) {
executor.emplace([&] () {
for(int j=0; j<num_tasks; ++j) {
executor.emplace([&] () {
sum.fetch_add(1, std::memory_order_relaxed);
});
}
});
}
for(auto& t : threads) {
t.join();
}
while(sum != num_tasks * 10 * 2 + num_tasks) {
std::this_thread::yield();
}
}
// Procedure: test_batch_insertion
template <typename T>
void test_batch_insertion(T& executor) {
constexpr int num_iterations = 50;
size_t total {0};
std::atomic<size_t> count {0};
for(size_t i=1; i<num_iterations; i++) {
std::vector<std::function<void()>> funs;
for(size_t j=0; j<i; j++) {
funs.emplace_back([&](){count++;});
}
executor.batch(funs);
total += i;
}
while(count != total) {
std::this_thread::yield();
}
}
// Procedure: test_executor
template <typename T>
void test_executor() {
SUBCASE("Ownership") {
for(unsigned i=0; i<=4; ++i) {
T tp(i);
test_ownership(tp);
}
}
SUBCASE("Emplace") {
for(unsigned i=0; i<=4; ++i) {
T tp(i);
test_emplace(tp);
}
}
SUBCASE("DynamicTasking") {
for(unsigned i=0; i<=4; ++i) {
T tp(i);
test_dynamic_tasking(tp);
}
}
SUBCASE("ExternalThreads") {
for(unsigned i=0; i<=4; ++i) {
T tp(i);
test_external_threads(tp);
}
}
SUBCASE("Batch") {
for(unsigned i=0; i<=4; ++i) {
T tp(i);
test_batch_insertion(tp);
}
}
}
// ----------------------------------------------------------------------------
// Testcase: SimpleExecutor
// ----------------------------------------------------------------------------
TEST_CASE("SimpleExecutor" * doctest::timeout(300)) {
test_executor<tf::SimpleExecutor<std::function<void()>>>();
}
// ----------------------------------------------------------------------------
// Testcase: ProactiveExecutor
// ----------------------------------------------------------------------------
TEST_CASE("ProactiveExecutor" * doctest::timeout(300)) {
test_executor<tf::ProactiveExecutor<std::function<void()>>>();
}
// ----------------------------------------------------------------------------
// Testcase: SpeculativeExecutor
// ----------------------------------------------------------------------------
TEST_CASE("SpeculativeExecutor" * doctest::timeout(300)) {
test_executor<tf::SpeculativeExecutor<std::function<void()>>>();
}
// ----------------------------------------------------------------------------
// Testcase: WorkStealingExecutor
// ----------------------------------------------------------------------------
TEST_CASE("WorkStealingExecutor" * doctest::timeout(300)) {
test_executor<tf::WorkStealingExecutor<std::function<void()>>>();
}
// ----------------------------------------------------------------------------
// Testcase: EigenWorkStealingExecutor
// ----------------------------------------------------------------------------
TEST_CASE("EigenWorkStealingExecutor" * doctest::timeout(300)) {
test_executor<tf::EigenWorkStealingExecutor<std::function<void()>>>();
}
// ----------------------------------------------------------------------------
// Testcase: BinarySemaphore
// ----------------------------------------------------------------------------
TEST_CASE("BinarySemaphore" * doctest::timeout(300)) {
tf::BinarySemaphore sema;
size_t count {0};
std::thread t1([&](){
using namespace std::chrono_literals;
for(int i=0; i<100; i++) {
std::this_thread::sleep_for(2ms);
sema.P();
++ count;
}
});
{
using namespace std::chrono_literals;
for(int i=0; i<100; i++) {
sema.V();
std::this_thread::sleep_for(10ms);
REQUIRE(count == i+1);
}
}
t1.join();
count = 0;
std::thread t2([&](){
using namespace std::chrono_literals;
for(int i=0; i<10; i++) {
std::this_thread::sleep_for(10ms);
sema.V();
count += 1;
}
});
t2.join();
REQUIRE(count == 10);
}
*/
Back
|
FazBrowse Home
|
New Git URL