-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathpipeline_benchmark.cpp
More file actions
381 lines (316 loc) · 10.5 KB
/
pipeline_benchmark.cpp
File metadata and controls
381 lines (316 loc) · 10.5 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* 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 <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <random>
#include <dispenso/pipeline.h>
#include "benchmark_common.h"
#if !defined(BENCHMARK_WITHOUT_TBB)
#include "tbb/pipeline.h"
#endif // !BENCHMARK_WITHOUT_TBB
#include <taskflow/taskflow.hpp>
#if TF_VERSION > 300000
#include <taskflow/algorithm/pipeline.hpp>
#endif // TF_VERSION
// (1) Generate images
// (2) Calculate geometric mean
// (3) Tonemap based on geometric mean
constexpr size_t kWidth = 256;
constexpr size_t kHeight = 256;
constexpr size_t kNumImages = 500;
constexpr size_t kSeed = 55;
struct Work {
Work(size_t idx) : index(idx) {}
Work(Work&& w)
: index(w.index), geometricMean(w.geometricMean), inputImage(std::move(w.inputImage)) {}
Work& operator=(Work&& w) {
index = w.index;
geometricMean = w.geometricMean;
inputImage = std::move(w.inputImage);
return *this;
}
size_t index;
double geometricMean = 0;
std::unique_ptr<uint16_t[]> inputImage;
};
std::vector<std::unique_ptr<uint8_t[]>> g_serialResults;
Work fillImage(Work work) {
std::mt19937 rng(kSeed + work.index);
work.inputImage = std::make_unique<uint16_t[]>(kWidth * kHeight);
std::uniform_int_distribution<uint16_t> dist;
for (size_t i = 0; i < kWidth * kHeight; ++i) {
work.inputImage[i] = dist(rng);
}
return work;
}
Work computeGeometricMean(Work work) {
double sum = 0;
for (size_t i = 0; i < kWidth * kHeight; ++i) {
sum += std::log(1.0e-10 + work.inputImage[i]);
}
work.geometricMean = std::exp(sum / (kWidth * kHeight));
return work;
}
std::unique_ptr<uint8_t[]> tonemap(Work work) {
auto out = std::make_unique<uint8_t[]>(kWidth * kHeight);
for (size_t i = 0; i < kWidth * kHeight; ++i) {
double adjLum = work.inputImage[i] / work.geometricMean;
out[i] = 255 * adjLum / (1.0 + adjLum);
}
return out;
}
void runSerial() {
g_serialResults.resize(kNumImages);
size_t index = 0;
for (auto& out : g_serialResults) {
out = tonemap(computeGeometricMean(fillImage(Work(index++))));
}
}
void checkResults(const std::vector<std::unique_ptr<uint8_t[]>>& results) {
if (g_serialResults.empty()) {
runSerial();
}
if (g_serialResults.size() != results.size()) {
std::cerr << "Number of results don't match" << std::endl;
std::abort();
}
for (size_t i = 0; i < results.size(); ++i) {
if (std::memcmp(
g_serialResults[i].get(), results[i].get(), kWidth * kHeight * sizeof(uint8_t))) {
std::cerr << "Mismatch in results" << std::endl;
for (size_t j = 0; j < 10; ++j) {
std::cerr << (int)g_serialResults[i][j] << " vs " << (int)results[i][j] << std::endl;
}
std::abort();
}
}
}
void BM_serial(benchmark::State& state) {
for (auto UNUSED_VAR : state) {
runSerial();
}
}
void runDispenso(std::vector<std::unique_ptr<uint8_t[]>>& results) {
results.resize(kNumImages);
size_t counter = 0;
dispenso::pipeline(
[&counter]() -> dispenso::OpResult<Work> {
if (counter < kNumImages) {
return fillImage(Work(counter++));
}
return {};
},
computeGeometricMean,
[&results](Work work) {
size_t index = work.index;
results[index] = tonemap(std::move(work));
});
}
void BM_dispenso(benchmark::State& state) {
std::vector<std::unique_ptr<uint8_t[]>> results;
(void)dispenso::globalThreadPool();
for (auto UNUSED_VAR : state) {
runDispenso(results);
}
checkResults(results);
}
void runDispensoPar(std::vector<std::unique_ptr<uint8_t[]>>& results) {
results.resize(kNumImages);
std::atomic<size_t> counter(0);
dispenso::pipeline(
dispenso::stage(
[&counter]() -> dispenso::OpResult<Work> {
size_t curIndex = counter.fetch_add(1, std::memory_order_acquire);
if (curIndex < kNumImages) {
return fillImage(Work(curIndex));
}
return {};
},
dispenso::kStageNoLimit),
dispenso::stage(computeGeometricMean, dispenso::kStageNoLimit),
dispenso::stage(
[&results](Work work) {
size_t index = work.index;
results[index] = tonemap(std::move(work));
},
dispenso::kStageNoLimit));
}
void BM_dispenso_par(benchmark::State& state) {
std::vector<std::unique_ptr<uint8_t[]>> results;
(void)dispenso::globalThreadPool();
for (auto UNUSED_VAR : state) {
runDispensoPar(results);
}
checkResults(results);
}
#if !defined(BENCHMARK_WITHOUT_TBB)
void runTBB(std::vector<std::unique_ptr<uint8_t[]>>& results) {
results.resize(kNumImages);
size_t counter = 0;
tbb::parallel_pipeline(
/*max_number_of_live_token=*/std::thread::hardware_concurrency(),
tbb::make_filter<void, Work*>(
tbb::filter::serial,
[&counter](tbb::flow_control& fc) -> Work* {
if (counter < kNumImages) {
return new Work(fillImage(Work(counter++)));
}
fc.stop();
return nullptr;
}) &
tbb::make_filter<Work*, Work*>(
tbb::filter::serial,
[](Work* workIn) {
Work& work = *workIn;
work = computeGeometricMean(std::move(work));
return workIn;
}) &
tbb::make_filter<Work*, void>(tbb::filter::serial, [&results](Work* workIn) {
size_t index = workIn->index;
results[index] = tonemap(std::move(*workIn));
delete workIn;
}));
}
void BM_tbb(benchmark::State& state) {
std::vector<std::unique_ptr<uint8_t[]>> results;
for (auto UNUSED_VAR : state) {
runTBB(results);
}
checkResults(results);
}
void runTBBPar(std::vector<std::unique_ptr<uint8_t[]>>& results) {
results.resize(kNumImages);
std::atomic<size_t> counter(0);
tbb::parallel_pipeline(
/*max_number_of_live_token=*/std::thread::hardware_concurrency(),
tbb::make_filter<void, Work*>(
tbb::filter::parallel,
[&counter](tbb::flow_control& fc) -> Work* {
size_t curIndex = counter.fetch_add(1, std::memory_order_acquire);
if (curIndex < kNumImages) {
return new Work(fillImage(Work(curIndex)));
}
fc.stop();
return nullptr;
}) &
tbb::make_filter<Work*, Work*>(
tbb::filter::parallel,
[](Work* work) {
*work = computeGeometricMean(std::move(*work));
return work;
}) &
tbb::make_filter<Work*, void>(tbb::filter::parallel, [&results](Work* work) {
size_t index = work->index;
results[index] = tonemap(std::move(*work));
delete work;
}));
}
void BM_tbb_par(benchmark::State& state) {
std::vector<std::unique_ptr<uint8_t[]>> results;
for (auto UNUSED_VAR : state) {
runTBBPar(results);
}
checkResults(results);
}
#endif // !BENCHMARK_WITHOUT_TBB
void runTaskflow(std::vector<std::unique_ptr<uint8_t[]>>& results, tf::Executor& exec) {
results.resize(kNumImages);
std::vector<std::unique_ptr<Work>> work;
// Ensure we don't resize underlying buffer causing data races
work.reserve(kNumImages);
tf::Taskflow taskflow;
size_t counter2 = 0;
size_t counter3 = 0;
tf::Pipeline pl(
std::thread::hardware_concurrency(),
tf::Pipe{
tf::PipeType::SERIAL,
[&work](auto& pf) mutable {
if (work.size() < kNumImages) {
work.push_back(std::make_unique<Work>(fillImage(Work(work.size()))));
} else {
pf.stop();
}
}},
tf::Pipe{
tf::PipeType::SERIAL,
[&counter2, &work](auto& pf) mutable {
Work& w = *work[counter2++];
w = computeGeometricMean(std::move(w));
}},
tf::Pipe{tf::PipeType::SERIAL, [&counter3, &work, &results](auto& pf) mutable {
Work& w = *work[counter3];
results[counter3++] = tonemap(std::move(w));
}});
taskflow.composed_of(pl);
exec.run(taskflow).wait();
}
void BM_taskflow(benchmark::State& state) {
std::vector<std::unique_ptr<uint8_t[]>> results;
tf::Executor executor(std::thread::hardware_concurrency());
for (auto UNUSED_VAR : state) {
runTaskflow(results, executor);
}
checkResults(results);
}
// TODO(bbudge): Debug this. Unclear exactly why this crashes and/or hangs (TSAN)
void runTaskflowPar(std::vector<std::unique_ptr<uint8_t[]>>& results, tf::Executor& exec) {
results.resize(kNumImages);
std::vector<std::unique_ptr<Work>> work;
// Ensure we don't resize underlying buffer causing data races
work.reserve(kNumImages);
tf::Taskflow taskflow;
std::atomic<size_t> counter2 = 0;
std::atomic<size_t> counter3 = 0;
tf::Pipeline pl(
std::thread::hardware_concurrency(),
tf::Pipe{
tf::PipeType::SERIAL,
[&work](auto& pf) mutable {
if (work.size() < kNumImages) {
work.push_back(std::make_unique<Work>(fillImage(Work(work.size()))));
} else {
pf.stop();
}
}},
tf::Pipe{
tf::PipeType::PARALLEL,
[&counter2, &work](auto& pf) mutable {
Work& w = *work[counter2.fetch_add(1, std::memory_order_relaxed)];
w = computeGeometricMean(std::move(w));
}},
tf::Pipe{tf::PipeType::PARALLEL, [&counter3, &work, &results](auto& pf) mutable {
size_t index = counter3.fetch_add(1, std::memory_order_relaxed);
Work& w = *work[index];
results[index] = tonemap(std::move(w));
}});
taskflow.composed_of(pl);
exec.run(taskflow).wait();
}
void BM_taskflow_par(benchmark::State& state) {
std::vector<std::unique_ptr<uint8_t[]>> results;
tf::Executor executor(std::thread::hardware_concurrency());
for (auto UNUSED_VAR : state) {
runTaskflowPar(results, executor);
}
checkResults(results);
}
BENCHMARK(BM_serial)->UseRealTime();
BENCHMARK(BM_dispenso)->UseRealTime();
#if !defined(BENCHMARK_WITHOUT_TBB)
BENCHMARK(BM_tbb)->UseRealTime();
#endif // !BENCHMARK_WITHOUT_TBB
BENCHMARK(BM_taskflow)->UseRealTime();
BENCHMARK(BM_dispenso_par)->UseRealTime();
#if !defined(BENCHMARK_WITHOUT_TBB)
BENCHMARK(BM_tbb_par)->UseRealTime();
#endif // !BENCHMARK_WITHOUT_TBB
// TODO(bbudge): Re-enable once this is fixed.
// BENCHMARK(BM_taskflow_par)->UseRealTime();
BENCHMARK_MAIN();