-
Notifications
You must be signed in to change notification settings - Fork 480
/
Copy pathstatic_thread_pool_tests.cpp
290 lines (225 loc) · 6.45 KB
/
static_thread_pool_tests.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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/static_thread_pool.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/when_all.hpp>
#include <vector>
#include <thread>
#include <cassert>
#include <chrono>
#include <iostream>
#include <numeric>
#include "doctest/doctest.h"
TEST_SUITE_BEGIN("static_thread_pool");
TEST_CASE("construct/destruct")
{
cppcoro::static_thread_pool threadPool;
CHECK(threadPool.thread_count() == std::thread::hardware_concurrency());
}
TEST_CASE("construct/destruct to specific thread count")
{
cppcoro::static_thread_pool threadPool{ 5 };
CHECK(threadPool.thread_count() == 5);
}
TEST_CASE("run one task")
{
cppcoro::static_thread_pool threadPool{ 2 };
auto initiatingThreadId = std::this_thread::get_id();
cppcoro::sync_wait([&]() -> cppcoro::task<void>
{
co_await threadPool.schedule();
if (std::this_thread::get_id() == initiatingThreadId)
{
FAIL("schedule() did not switch threads");
}
}());
}
TEST_CASE("launch many tasks remotely")
{
cppcoro::static_thread_pool threadPool;
auto makeTask = [&]() -> cppcoro::task<>
{
co_await threadPool.schedule();
};
std::vector<cppcoro::task<>> tasks;
for (std::uint32_t i = 0; i < 100; ++i)
{
tasks.push_back(makeTask());
}
cppcoro::sync_wait(cppcoro::when_all(std::move(tasks)));
}
cppcoro::task<std::uint64_t> sum_of_squares(
std::uint32_t start,
std::uint32_t end,
cppcoro::static_thread_pool& tp)
{
co_await tp.schedule();
auto count = end - start;
if (count > 1000)
{
auto half = start + count / 2;
auto[a, b] = co_await cppcoro::when_all(
sum_of_squares(start, half, tp),
sum_of_squares(half, end, tp));
co_return a + b;
}
else
{
std::uint64_t sum = 0;
for (std::uint64_t x = start; x < end; ++x)
{
sum += x * x;
}
co_return sum;
}
}
TEST_CASE("launch sub-task with many sub-tasks")
{
using namespace std::chrono_literals;
constexpr std::uint64_t limit = 1'000'000'000;
cppcoro::static_thread_pool tp;
// Wait for the thread-pool thread to start up.
std::this_thread::sleep_for(1ms);
auto start = std::chrono::high_resolution_clock::now();
auto result = cppcoro::sync_wait(sum_of_squares(0, limit , tp));
auto end = std::chrono::high_resolution_clock::now();
std::uint64_t sum = 0;
for (std::uint64_t i = 0; i < limit; ++i)
{
sum += i * i;
}
auto end2 = std::chrono::high_resolution_clock::now();
auto toNs = [](auto time)
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(time).count();
};
std::cout
<< "multi-threaded version took " << toNs(end - start) << "ns\n"
<< "single-threaded version took " << toNs(end2 - end) << "ns" << std::endl;
CHECK(result == sum);
}
struct fork_join_operation
{
std::atomic<std::size_t> m_count;
std::experimental::coroutine_handle<> m_coro;
fork_join_operation() : m_count(1) {}
void begin_work() noexcept
{
m_count.fetch_add(1, std::memory_order_relaxed);
}
void end_work() noexcept
{
if (m_count.fetch_sub(1, std::memory_order_acq_rel) == 1)
{
m_coro.resume();
}
}
bool await_ready() noexcept { return m_count.load(std::memory_order_acquire) == 1; }
bool await_suspend(std::experimental::coroutine_handle<> coro) noexcept
{
m_coro = coro;
return m_count.fetch_sub(1, std::memory_order_acq_rel) != 1;
}
void await_resume() noexcept {};
};
template<typename FUNC, typename RANGE, typename SCHEDULER>
cppcoro::task<void> for_each_async(SCHEDULER& scheduler, RANGE& range, FUNC func)
{
using reference_type = decltype(*range.begin());
// TODO: Use awaiter_t here instead. This currently assumes that
// result of scheduler.schedule() doesn't have an operator co_await().
using schedule_operation = decltype(scheduler.schedule());
struct work_operation
{
fork_join_operation& m_forkJoin;
FUNC& m_func;
reference_type m_value;
schedule_operation m_scheduleOp;
work_operation(fork_join_operation& forkJoin, SCHEDULER& scheduler, FUNC& func, reference_type&& value)
: m_forkJoin(forkJoin)
, m_func(func)
, m_value(static_cast<reference_type&&>(value))
, m_scheduleOp(scheduler.schedule())
{
}
bool await_ready() noexcept { return false; }
CPPCORO_NOINLINE
void await_suspend(std::experimental::coroutine_handle<> coro) noexcept
{
fork_join_operation& forkJoin = m_forkJoin;
FUNC& func = m_func;
reference_type value = static_cast<reference_type&&>(m_value);
static_assert(std::is_same_v<decltype(m_scheduleOp.await_suspend(coro)), void>);
forkJoin.begin_work();
// Schedule the next iteration of the loop to run
m_scheduleOp.await_suspend(coro);
func(static_cast<reference_type&&>(value));
forkJoin.end_work();
}
void await_resume() noexcept {}
};
co_await scheduler.schedule();
fork_join_operation forkJoin;
for (auto&& x : range)
{
co_await work_operation{
forkJoin,
scheduler,
func,
static_cast<decltype(x)>(x)
};
}
co_await forkJoin;
}
std::uint64_t collatz_distance(std::uint64_t number)
{
std::uint64_t count = 0;
while (number > 1)
{
if (number % 2 == 0) number /= 2;
else number = number * 3 + 1;
++count;
}
return count;
}
TEST_CASE("for_each_async")
{
cppcoro::static_thread_pool tp;
{
std::vector<std::uint64_t> values(1'000'000);
std::iota(values.begin(), values.end(), 1);
cppcoro::sync_wait([&]() -> cppcoro::task<>
{
auto start = std::chrono::high_resolution_clock::now();
co_await for_each_async(tp, values, [](std::uint64_t& value)
{
value = collatz_distance(value);
});
auto end = std::chrono::high_resolution_clock::now();
std::cout << "for_each_async of " << values.size()
<< " took " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()
<< "us" << std::endl;
for (std::size_t i = 0; i < 1'000'000; ++i)
{
CHECK(values[i] == collatz_distance(i + 1));
}
}());
}
{
std::vector<std::uint64_t> values(1'000'000);
std::iota(values.begin(), values.end(), 1);
auto start = std::chrono::high_resolution_clock::now();
for (auto&& x : values)
{
x = collatz_distance(x);
}
auto end = std::chrono::high_resolution_clock::now();
std::cout << "single-threaded for loop of " << values.size()
<< " took " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()
<< "us" << std::endl;
}
}
TEST_SUITE_END();