-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.cpp
328 lines (285 loc) · 10.7 KB
/
test.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
#include <iostream>
#include "quickpool.hpp"
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 = [&](size_t i) { x[i] = 2 * x[i]; };
parallel_for(0, 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, 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");
}
// 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, x.size(), [&](int i) {
parallel_for(0, x[i].size(), [&x, i](int j) { x[i][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, x.size(), [&](int i) {
pool.parallel_for(
0, x[i].size(), [&x, i](int j) { x[i][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::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);
}
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");
// 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"); });
std::this_thread::sleep_for(std::chrono::milliseconds(30));
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;
}
// 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");
}
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;
return 0;
}