forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsq.cpp
More file actions
221 lines (189 loc) · 6.22 KB
/
Copy pathtsq.cpp
File metadata and controls
221 lines (189 loc) · 6.22 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
// 2020/02/24 - modified by Tsung-Wei Huang
// - isolaged wsq to a standalone project
// - here we focus on tsq where T must be a pointer type
//
// 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>
// ============================================================================
// WorkStealingQueue tests
// ============================================================================
// Procedure: tsq_test_owner
void tsq_test_owner() {
for(size_t N=1; N<=777777; N=N*2+1) {
tf::TaskQueue<void*> queue;
std::vector<void*> gold(N);
REQUIRE(queue.empty());
// push and pop
for(size_t i=0; i<N; ++i) {
gold[i] = &i;
queue.push(gold[i]);
}
for(size_t i=0; i<N; ++i) {
auto ptr = queue.pop();
REQUIRE(ptr != nullptr);
REQUIRE(gold[N-i-1] == ptr);
}
REQUIRE(queue.pop() == nullptr);
// push and steal
for(size_t i=0; i<N; ++i) {
queue.push(gold[i]);
}
for(size_t i=0; i<N; ++i) {
auto ptr = queue.steal();
REQUIRE(ptr != nullptr);
REQUIRE(gold[i] == ptr);
}
}
}
// Procedure: tsq_test_n_thieves
void tsq_test_n_thieves(size_t M) {
for(size_t N=1; N<=777777; N=N*2+1) {
tf::TaskQueue<void*> queue;
std::vector<void*> gold(N);
std::atomic<size_t> consumed {0};
for(size_t i=0; i<N; ++i) {
gold[i] = &i;
}
// thieves
std::vector<std::thread> threads;
std::vector<std::vector<void*>> stolens(M);
for(size_t i=0; i<M; ++i) {
threads.emplace_back([&, i](){
while(consumed != N) {
auto ptr = queue.steal();
if(ptr != nullptr) {
stolens[i].push_back(ptr);
consumed.fetch_add(1, std::memory_order_relaxed);
}
}
REQUIRE(queue.steal() == nullptr);
});
}
// master thread
for(size_t i=0; i<N; ++i) {
queue.push(gold[i]);
}
std::vector<void*> items;
while(consumed != N) {
auto ptr = queue.pop();
if(ptr != nullptr) {
items.push_back(ptr);
consumed.fetch_add(1, std::memory_order_relaxed);
}
}
REQUIRE(queue.steal() == nullptr);
REQUIRE(queue.pop() == nullptr);
REQUIRE(queue.empty());
// merge items
for(size_t i=0; i<M; ++i) {
for(auto s : stolens[i]) {
items.push_back(s);
}
}
// join thieves
for(auto& thread : threads) thread.join();
std::sort(items.begin(), items.end());
std::sort(gold.begin(), gold.end());
REQUIRE(items.size() == N);
REQUIRE(items == gold);
}
}
// ----------------------------------------------------------------------------
// Testcase: TSQTest.Owner
// ----------------------------------------------------------------------------
TEST_CASE("TSQ.Owner" * doctest::timeout(300)) {
tsq_test_owner();
}
// ----------------------------------------------------------------------------
// Testcase: TSQTest.1Thief
// ----------------------------------------------------------------------------
TEST_CASE("TSQ.1Thief" * doctest::timeout(300)) {
tsq_test_n_thieves(1);
}
// ----------------------------------------------------------------------------
// Testcase: TSQTest.2Thieves
// ----------------------------------------------------------------------------
TEST_CASE("TSQ.2Thieves" * doctest::timeout(300)) {
tsq_test_n_thieves(2);
}
// ----------------------------------------------------------------------------
// Testcase: TSQTest.3Thieves
// ----------------------------------------------------------------------------
TEST_CASE("TSQ.3Thieves" * doctest::timeout(300)) {
tsq_test_n_thieves(3);
}
// ----------------------------------------------------------------------------
// Testcase: TSQTest.4Thieves
// ----------------------------------------------------------------------------
TEST_CASE("TSQ.4Thieves" * doctest::timeout(300)) {
tsq_test_n_thieves(4);
}
// ----------------------------------------------------------------------------
// Testcase: TSQTest.5Thieves
// ----------------------------------------------------------------------------
TEST_CASE("TSQ.5Thieves" * doctest::timeout(300)) {
tsq_test_n_thieves(5);
}
// ----------------------------------------------------------------------------
// Testcase: TSQTest.6Thieves
// ----------------------------------------------------------------------------
TEST_CASE("TSQ.6Thieves" * doctest::timeout(300)) {
tsq_test_n_thieves(6);
}
// ----------------------------------------------------------------------------
// Testcase: TSQTest.7Thieves
// ----------------------------------------------------------------------------
TEST_CASE("TSQ.7Thieves" * doctest::timeout(300)) {
tsq_test_n_thieves(7);
}
// ----------------------------------------------------------------------------
// Testcase: TSQTest.8Thieves
// ----------------------------------------------------------------------------
TEST_CASE("TSQ.8Thieves" * doctest::timeout(300)) {
tsq_test_n_thieves(8);
}