-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsimple_for_benchmark.cpp
More file actions
237 lines (198 loc) · 6.69 KB
/
simple_for_benchmark.cpp
File metadata and controls
237 lines (198 loc) · 6.69 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <dispenso/parallel_for.h>
#if defined(_OPENMP)
#include <omp.h>
#endif
#include <unordered_map>
#if !defined(BENCHMARK_WITHOUT_TBB)
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include "tbb/task_scheduler_init.h"
#endif // !BENCHMARK_WITHOUT_TBB
#include <taskflow/taskflow.hpp>
#if TF_VERSION > 300000
#include <taskflow/algorithm/for_each.hpp>
#endif // TF_VERSION
#include "thread_benchmark_common.h"
static uint32_t kSeed(8);
static constexpr int kSmallSize = 1000;
static constexpr int kMediumSize = 1000000;
static constexpr int kLargeSize = 100000000;
static constexpr uint32_t kMinSizePerChunk = 10000;
const std::vector<int>& getInputs(int num_elements) {
static std::unordered_map<int, std::vector<int>> vecs;
auto it = vecs.find(num_elements);
if (it != vecs.end()) {
return it->second;
}
// No need to use a high-quality rng for this test.
srand(kSeed);
std::vector<int> values;
values.reserve(num_elements);
for (int i = 0; i < num_elements; ++i) {
values.push_back((rand() & 255) - 127);
}
auto res = vecs.emplace(num_elements, std::move(values));
assert(res.second);
return res.first->second;
}
template <int num_elements>
void BM_serial(benchmark::State& state) {
std::vector<int> output(num_elements, 0);
auto& input = getInputs(num_elements);
for (auto UNUSED_VAR : state) {
for (size_t i = 0; i < num_elements; ++i) {
output[i] = input[i] * input[i] - 3 * input[i];
}
}
}
void checkResults(const std::vector<int>& input, const std::vector<int>& output) {
for (size_t i = 0; i < input.size(); ++i) {
if (output[i] != input[i] * input[i] - 3 * input[i]) {
std::cerr << "FAIL! " << output[i] << " vs " << input[i] * input[i] - 3 * input[i]
<< std::endl;
abort();
}
}
}
void BM_dispenso(benchmark::State& state) {
const int num_threads = state.range(0) - 1;
const int num_elements = state.range(1);
std::vector<int> output(num_elements, 0);
dispenso::ThreadPool pool(num_threads);
dispenso::ParForOptions options;
options.minItemsPerChunk = kMinSizePerChunk;
auto& input = getInputs(num_elements);
for (auto UNUSED_VAR : state) {
dispenso::TaskSet tasks(pool);
dispenso::parallel_for(
tasks,
0,
num_elements,
[&input, &output](size_t i) { output[i] = input[i] * input[i] - 3 * input[i]; },
options);
}
checkResults(input, output);
}
void BM_taskflow(benchmark::State& state) {
const int num_threads = state.range(0);
const int num_elements = state.range(1);
std::vector<int> output(num_elements, 0);
auto& input = getInputs(num_elements);
tf::Executor executor(num_threads);
tf::Taskflow taskflow;
for (auto UNUSED_VAR : state) {
taskflow.for_each_index(0, num_elements, 1, [&input, &output](int i) {
output[i] = input[i] * input[i] - 3 * input[i];
});
executor.run(taskflow).wait();
}
checkResults(input, output);
}
void BM_dispenso_static_chunk(benchmark::State& state) {
const int num_threads = state.range(0) - 1;
const int num_elements = state.range(1);
std::vector<int> output(num_elements, 0);
dispenso::ThreadPool pool(num_threads);
dispenso::ParForOptions options;
options.minItemsPerChunk = kMinSizePerChunk;
auto& input = getInputs(num_elements);
for (auto UNUSED_VAR : state) {
dispenso::TaskSet tasks(pool);
auto range = dispenso::makeChunkedRange(0, num_elements, dispenso::ParForChunking::kStatic);
dispenso::parallel_for(
tasks,
range,
[&input, &output](size_t begin, size_t end) {
for (size_t i = begin; i < end; ++i) {
output[i] = input[i] * input[i] - 3 * input[i];
}
},
options);
}
checkResults(input, output);
}
void BM_dispenso_auto_chunk(benchmark::State& state) {
const int num_threads = state.range(0) - 1;
const int num_elements = state.range(1);
std::vector<int> output(num_elements, 0);
dispenso::ThreadPool pool(num_threads);
dispenso::ParForOptions options;
options.minItemsPerChunk = kMinSizePerChunk;
auto& input = getInputs(num_elements);
for (auto UNUSED_VAR : state) {
dispenso::TaskSet tasks(pool);
auto range = dispenso::makeChunkedRange(0, num_elements, dispenso::ParForChunking::kAuto);
dispenso::parallel_for(
tasks,
range,
[&input, &output](size_t begin, size_t end) {
for (size_t i = begin; i < end; ++i) {
output[i] = input[i] * input[i] - 3 * input[i];
}
},
options);
}
checkResults(input, output);
}
#if defined(_OPENMP)
void BM_omp(benchmark::State& state) {
const int num_threads = state.range(0);
const int num_elements = state.range(1);
std::vector<int> output(num_elements, 0);
omp_set_num_threads(num_threads);
auto& input = getInputs(num_elements);
for (auto UNUSED_VAR : state) {
#pragma omp parallel for
for (int i = 0; i < num_elements; ++i) {
output[i] = input[i] * input[i] - 3 * input[i];
}
}
checkResults(input, output);
}
#endif /*defined(_OPENMP)*/
#if !defined(BENCHMARK_WITHOUT_TBB)
void BM_tbb(benchmark::State& state) {
const int num_threads = state.range(0);
const int num_elements = state.range(1);
std::vector<int> output(num_elements, 0);
auto& input = getInputs(num_elements);
for (auto UNUSED_VAR : state) {
tbb::task_scheduler_init initsched(num_threads);
tbb::parallel_for(
tbb::blocked_range<size_t>(0, num_elements),
[&input, &output](const tbb::blocked_range<size_t>& r) {
for (size_t i = r.begin(); i < r.end(); ++i) {
output[i] = input[i] * input[i] - 3 * input[i];
}
});
}
checkResults(input, output);
}
#endif // !BENCHMARK_WITHOUT_TBB
static void CustomArguments(benchmark::internal::Benchmark* b) {
for (int j : {kSmallSize, kMediumSize, kLargeSize}) {
for (int i : pow2HalfStepThreads()) {
b->Args({i, j});
}
}
}
BENCHMARK_TEMPLATE(BM_serial, kSmallSize);
BENCHMARK_TEMPLATE(BM_serial, kMediumSize);
BENCHMARK_TEMPLATE(BM_serial, kLargeSize);
#if defined(_OPENMP)
BENCHMARK(BM_omp)->Apply(CustomArguments)->UseRealTime();
#endif // OPENMP
#if !defined(BENCHMARK_WITHOUT_TBB)
BENCHMARK(BM_tbb)->Apply(CustomArguments)->UseRealTime();
#endif // !BENCHMARK_WITHOUT_TBB
BENCHMARK(BM_taskflow)->Apply(CustomArguments)->UseRealTime();
BENCHMARK(BM_dispenso)->Apply(CustomArguments)->UseRealTime();
BENCHMARK(BM_dispenso_static_chunk)->Apply(CustomArguments)->UseRealTime();
BENCHMARK(BM_dispenso_auto_chunk)->Apply(CustomArguments)->UseRealTime();
BENCHMARK_MAIN();