-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy paththread_pool.cpp
More file actions
130 lines (110 loc) · 3.83 KB
/
thread_pool.cpp
File metadata and controls
130 lines (110 loc) · 3.83 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
#include <benchmark/benchmark.h>
#include <thread_pool/thread_pool.h>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/when_all.hpp>
#include "utilities.h"
static void BM_array_multiplication_thread_pool(benchmark::State& state) {
const auto array_size = state.range(0);
const std::size_t multiplications_to_perform = state.range(1);
// generate the data
const auto computations = generate_benchmark_data<int>(array_size, multiplications_to_perform);
// task that is run on a new thread
auto thread_task = [](multiplication_pair<int> pair) {
std::vector<int> result(pair.first.size());
multiply_array(pair.first, pair.second, result);
};
// create our thread pool using the default size
dp::thread_pool pool{};
for (auto _ : state) {
{
for (const auto& mult : computations) {
pool.enqueue_detach(thread_task, mult);
}
}
}
}
BENCHMARK(BM_array_multiplication_thread_pool)
#if defined(NDEBUG)
->Args({8, 25'000})
->Args({64, 5'000})
->Args({256, 250})
->Args({512, 75})
->Args({1024, 10})
#else
->Args({8, 50})
#endif
->Unit(benchmark::kMillisecond)
->ReportAggregatesOnly(true)
->MeasureProcessCPUTime()
->UseRealTime()
->Name("dp::thread_pool array mult");
static void BM_array_multiplication_batch_thread_pool(benchmark::State& state) {
const auto array_size = state.range(0);
const std::size_t multiplications_to_perform = state.range(1);
// generate the data
const auto computations = generate_benchmark_data<int>(array_size, multiplications_to_perform);
// create our tasks
std::vector<std::function<void()>> tasks{};
tasks.reserve(computations.size());
// task that is run on a new thread
auto thread_task = [](multiplication_pair<int> pair) {
std::vector<int> result(pair.first.size());
multiply_array(pair.first, pair.second, result);
};
for (const auto& computation : computations) {
auto task = [comp = computation, execution_task = thread_task]() { execution_task(comp); };
tasks.emplace_back(task);
}
// create our thread pool using the default size
dp::thread_pool pool{};
for (auto _ : state) {
pool.enqueue(tasks.begin(), tasks.end());
}
}
BENCHMARK(BM_array_multiplication_batch_thread_pool)
#if defined(NDEBUG)
->Args({8, 25'000})
->Args({64, 5'000})
->Args({256, 250})
->Args({512, 75})
->Args({1024, 10})
#else
->Args({8, 50})
#endif
->Unit(benchmark::kMillisecond)
->ReportAggregatesOnly(true)
->MeasureProcessCPUTime()
->UseRealTime()
->Name("dp::thread_pool batched array mult");
inline cppcoro::task<void> mult_task(multiplication_pair<int> pair, dp::thread_pool<>& pool) {
std::vector<int> result(pair.first.size());
co_await co_multiply_array(pair.first, pair.second, result, pool);
}
static void BM_array_multiplication_thread_pool_coroutine(benchmark::State& state) {
const auto array_size = state.range(0);
const std::size_t multiplications_to_perform = state.range(1);
const auto computations = generate_benchmark_data<int>(array_size, multiplications_to_perform);
dp::thread_pool pool{};
std::vector<cppcoro::task<>> tasks;
for (auto _ : state) {
for (auto mult_pair : computations) {
tasks.emplace_back(mult_task(mult_pair, pool));
}
}
cppcoro::sync_wait(cppcoro::when_all(std::move(tasks)));
}
BENCHMARK(BM_array_multiplication_thread_pool_coroutine)
#if defined(NDEBUG)
->Args({8, 25'000})
->Args({64, 5'000})
->Args({256, 250})
->Args({512, 75})
->Args({1024, 10})
#else
->Args({8, 50})
#endif
->Unit(benchmark::kMillisecond)
->ReportAggregatesOnly(true)
->MeasureProcessCPUTime()
->UseRealTime()
->Name("dp::thread_pool coroutine array mult");