Project
Loading...
Searching...
No Matches
benchmark_MergingCollections.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#include <benchmark/benchmark.h>
12
13#include <TObjArray.h>
14#include <TH1.h>
15#include <TH2.h>
16#include <TH3.h>
17#include <TTree.h>
18#include <THnSparse.h>
19#include <TF1.h>
20#include <TF2.h>
21#include <TF3.h>
22#include <TRandom.h>
23#include <TRandomGen.h>
24
25#include <boost/histogram.hpp>
26#include <chrono>
27namespace bh = boost::histogram;
28
29#include <ctime>
30
31#define MAX_SIZE_COLLECTION (1 << 10)
32#define BENCHMARK_RANGE_COLLECTIONS Arg(1)->Arg(1 << 2)->Arg(1 << 4)->Arg(1 << 6)->Arg(1 << 8)->Arg(1 << 10)
33
34static void BM_mergingCollectionsTH1I(benchmark::State& state)
35{
36 const size_t collectionSize = state.range(0);
37 const size_t numberOfCollections = MAX_SIZE_COLLECTION / collectionSize;
38 const size_t bins = 62500; // makes 250kB
39
40 for (auto _ : state) {
41 std::vector<std::unique_ptr<TCollection>> collections;
42 for (size_t ci = 0; ci < numberOfCollections; ci++) {
43 std::unique_ptr<TCollection> collection = std::make_unique<TObjArray>();
44 collection->SetOwner(true);
45 TF1* uni = new TF1("uni", "1", 0, 1000000);
46 for (size_t i = 0; i < collectionSize; i++) {
47 TH1I* h = new TH1I(("test" + std::to_string(ci) + "-" + std::to_string(i)).c_str(), "test", bins, 0, 1000000);
48 h->FillRandom("uni", 50000);
49 collection->Add(h);
50 }
51 collections.push_back(std::move(collection));
52 delete uni;
53 }
54 auto m = std::make_unique<TH1I>("merged", "merged", bins, 0, 1000000);
55 // avoid memory overcommitment by doing something with data.
56 for (size_t i = 0; i < bins; i++) {
57 m->SetBinContent(i, 1);
58 }
59 auto start = std::chrono::high_resolution_clock::now();
60 for (const auto& collection : collections) {
61 m->Merge(collection.get(), "-NOCHECK");
62 }
63 auto end = std::chrono::high_resolution_clock::now();
64
65 auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
66 state.SetIterationTime(elapsed_seconds.count());
67 }
68}
69
70static void BM_mergingCollectionsTH2I(benchmark::State& state)
71{
72 const size_t collectionSize = state.range(0);
73 // We should always merge the same amount of objects (histograms) in one benchmark cycle.
74 const size_t numberOfCollections = MAX_SIZE_COLLECTION / collectionSize;
75 const size_t bins = 250; // 250 bins * 250 bins * 4B makes 250kB
76
77 TF2* uni = new TF2("uni", "1", 0, 1000000, 0, 1000000);
78
79 for (auto _ : state) {
80
81 std::vector<std::unique_ptr<TCollection>> collections;
82
83 for (size_t ci = 0; ci < numberOfCollections; ci++) {
84 std::unique_ptr<TCollection> collection = std::make_unique<TObjArray>();
85 collection->SetOwner(true);
86 for (size_t i = 0; i < collectionSize; i++) {
87 TH2I* h = new TH2I(("test" + std::to_string(ci) + "-" + std::to_string(i)).c_str(), "test",
88 bins, 0, 1000000,
89 bins, 0, 1000000);
90 h->FillRandom("uni", 50000);
91 collection->Add(h);
92 }
93 collections.push_back(std::move(collection));
94 }
95 auto m = std::make_unique<TH2I>("merged", "merged", bins, 0, 1000000, bins, 0, 1000000);
96 // avoid memory overcommitment by doing something with data.
97 for (size_t i = 0; i < bins; i++) {
98 m->SetBinContent(i, 1);
99 }
100
101 auto start = std::chrono::high_resolution_clock::now();
102 for (const auto& collection : collections) {
103 m->Merge(collection.get(), "-NOCHECK");
104 }
105 auto end = std::chrono::high_resolution_clock::now();
106
107 auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
108 state.SetIterationTime(elapsed_seconds.count());
109 }
110 delete uni;
111}
112
113static void BM_mergingCollectionsTH3I(benchmark::State& state)
114{
115 const size_t collectionSize = state.range(0);
116 // We should always merge the same amount of objects (histograms) in one benchmark cycle.
117 const size_t numberOfCollections = MAX_SIZE_COLLECTION / collectionSize;
118 const size_t bins = 40; // 40 bins * 40 bins * 40 bins * 4B makes 256kB
119
120 TF3* uni = new TF3("uni", "1", 0, 1000000, 0, 1000000, 0, 1000000);
121
122 for (auto _ : state) {
123
124 std::vector<std::unique_ptr<TCollection>> collections;
125
126 for (size_t ci = 0; ci < numberOfCollections; ci++) {
127 std::unique_ptr<TCollection> collection = std::make_unique<TObjArray>();
128 collection->SetOwner(true);
129 for (size_t i = 0; i < collectionSize; i++) {
130 TH3I* h = new TH3I(("test" + std::to_string(ci) + "-" + std::to_string(i)).c_str(), "test",
131 bins, 0, 1000000,
132 bins, 0, 1000000,
133 bins, 0, 1000000);
134 h->FillRandom("uni", 50000);
135 collection->Add(h);
136 }
137 collections.push_back(std::move(collection));
138 }
139 auto m = std::make_unique<TH3I>("merged", "merged", bins, 0, 1000000, bins, 0, 1000000, bins, 0, 1000000);
140 // avoid memory overcommitment by doing something with data.
141 for (size_t i = 0; i < bins; i++) {
142 m->SetBinContent(i, 1);
143 }
144
145 auto start = std::chrono::high_resolution_clock::now();
146 for (const auto& collection : collections) {
147 m->Merge(collection.get(), "-NOCHECK");
148 }
149 auto end = std::chrono::high_resolution_clock::now();
150
151 auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
152 state.SetIterationTime(elapsed_seconds.count());
153 }
154
155 delete uni;
156}
157
158static void BM_mergingCollectionsTHNSparse(benchmark::State& state)
159{
160 const size_t collectionSize = state.range(0);
161 // We should always merge the same amount of objects (histograms) in one benchmark cycle.
162 const size_t numberOfCollections = MAX_SIZE_COLLECTION / collectionSize;
163
164 const Double_t min = 0.0;
165 const Double_t max = 1000000.0;
166 const size_t dim = 10;
167 const Int_t bins = 250;
168 const Int_t binsDims[dim] = {bins, bins, bins, bins, bins, bins, bins, bins, bins, bins};
169 const Double_t mins[dim] = {min, min, min, min, min, min, min, min, min, min};
170 const Double_t maxs[dim] = {max, max, max, max, max, max, max, max, max, max};
171
172 TRandomMixMax gen;
173 gen.SetSeed(std::random_device()());
174 Double_t randomArray[dim];
175
176 for (auto _ : state) {
177
178 std::vector<std::unique_ptr<TCollection>> collections;
179
180 for (size_t ci = 0; ci < numberOfCollections; ci++) {
181 std::unique_ptr<TCollection> collection = std::make_unique<TObjArray>();
182 collection->SetOwner(true);
183 for (size_t i = 0; i < collectionSize; i++) {
184 auto* h = new THnSparseI(("test" + std::to_string(ci) + "-" + std::to_string(i)).c_str(), "test", dim, binsDims, mins, maxs);
185 for (size_t entry = 0; entry < 50000; entry++) {
186 gen.RndmArray(dim, randomArray);
187 for (double& r : randomArray) {
188 r *= max;
189 }
190 h->Fill(randomArray);
191 }
192 collection->Add(h);
193 }
194 collections.push_back(std::move(collection));
195 }
196 auto m = std::make_unique<THnSparseI>("merged", "merged", dim, binsDims, mins, maxs);
197
198 auto start = std::chrono::high_resolution_clock::now();
199 for (const auto& collection : collections) {
200 m->Merge(collection.get());
201 }
202 auto end = std::chrono::high_resolution_clock::now();
203
204 auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
205 state.SetIterationTime(elapsed_seconds.count());
206 }
207}
208
209static void BM_mergingCollectionsTTree(benchmark::State& state)
210{
211 const size_t collectionSize = state.range(0);
212 // We should always merge the same amount of objects (histograms) in one benchmark cycle.
213 const size_t numberOfCollections = MAX_SIZE_COLLECTION / collectionSize;
214
215 struct format1 {
216 Int_t a;
217 Long64_t b;
218 Float_t c;
219 Double_t d;
220 } branch1;
221 ULong64_t branch2;
222
223 auto createTree = [&](std::string name) -> TTree* {
224 TTree* tree = new TTree();
225 tree->SetName(name.c_str());
226 tree->Branch("b1", &branch1, "a/I:b/L:c/F:d/D");
227 tree->Branch("b2", &branch2);
228 return tree;
229 };
230
231 TRandomMixMax gen;
232 gen.SetSeed(std::random_device()());
233 Double_t randomArray[5];
234
235 for (auto _ : state) {
236 std::vector<std::unique_ptr<TCollection>> collections;
237
238 for (size_t ci = 0; ci < numberOfCollections; ci++) {
239 std::unique_ptr<TCollection> collection = std::make_unique<TObjArray>();
240 collection->SetOwner(true);
241 for (size_t i = 0; i < collectionSize; i++) {
242 TTree* t = createTree(std::to_string(ci) + "-" + std::to_string(i));
243 for (size_t entry = 0; entry < 6250; entry++) {
244 gen.RndmArray(5, randomArray);
245 branch1 = {static_cast<Int_t>(randomArray[0]), static_cast<Long64_t>(randomArray[1]), static_cast<Float_t>(randomArray[2]), randomArray[3]};
246 branch2 = randomArray[4];
247 t->Fill();
248 }
249 collection->Add(t);
250 }
251 collections.emplace_back(std::move(collection));
252 }
253 TTree* merged = createTree("merged");
254
255 auto start = std::chrono::high_resolution_clock::now();
256 for (const auto& collection : collections) {
257 merged->Merge(collection.get());
258 }
259 auto end = std::chrono::high_resolution_clock::now();
260
261 auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
262 state.SetIterationTime(elapsed_seconds.count());
263
264 delete merged;
265 }
266}
267
268static void BM_mergingPODCollections(benchmark::State& state)
269{
270 const size_t collectionSize = state.range(0);
271 // We should always merge the same amount of objects (histograms) in one benchmark cycle.
272 const size_t numberOfCollections = MAX_SIZE_COLLECTION / collectionSize;
273 const size_t bins = 62500; // makes 250kB
274
275 using PODHistoType = std::vector<float>;
276 using PODHistoTypePtr = std::unique_ptr<PODHistoType>;
277 TF1* uni = new TF1("uni", "1", 0, 1000000);
278
279 const size_t randoms = 50000;
280 TRandomMixMax gen;
281 gen.SetSeed(std::random_device()());
282 Double_t randomArray[randoms];
283
284 for (auto _ : state) {
285 std::vector<std::unique_ptr<std::vector<PODHistoTypePtr>>> collections;
286
287 for (size_t ci = 0; ci < numberOfCollections; ci++) {
288 auto collection = std::make_unique<std::vector<PODHistoTypePtr>>();
289 for (size_t i = 0; i < collectionSize; i++) {
290 auto h = PODHistoTypePtr(new PODHistoType(bins, 0));
291 gen.RndmArray(randoms, randomArray);
292 for (double r : randomArray) {
293 size_t idx = r * bins;
294 if (idx != bins) {
295 (*h)[idx] += 1;
296 }
297 }
298 collection->emplace_back(std::move(h));
299 }
300 collections.emplace_back(std::move(collection));
301 }
302 auto m = PODHistoTypePtr(new PODHistoType(bins, 0));
303 // avoid memory overcommitment by doing something with data.
304 for (size_t i = 0; i < bins; i++) {
305 (*m)[i] = 1;
306 }
307
308 auto merge = [&](const std::vector<PODHistoTypePtr>& collection, size_t i) {
309 auto h = collection[i].get();
310 for (size_t b = 0; b < bins; b++) {
311 (*m)[b] += (*h)[b];
312 }
313 };
314
315 auto start = std::chrono::high_resolution_clock::now();
316 for (const auto& collection : collections) {
317 for (size_t i = 0; i < collectionSize; i++) {
318 merge(*collection, i);
319 }
320 }
321 auto end = std::chrono::high_resolution_clock::now();
322
323 auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
324 state.SetIterationTime(elapsed_seconds.count());
325 }
326 delete uni;
327}
328
329static void BM_mergingBoostRegular1DCollections(benchmark::State& state)
330{
331 const double min = 0.0;
332 const double max = 1000000.0;
333 const size_t collectionSize = state.range(0);
334 // We should always merge the same amount of objects (histograms) in one benchmark cycle.
335 const size_t numberOfCollections = MAX_SIZE_COLLECTION / collectionSize;
336 const size_t bins = 62500; // makes 250kB
337
338 auto merged = bh::make_histogram(bh::axis::regular<>(bins, min, max, "x"));
339 merged += merged; // avoid memory overcommitment by doing something with data.
340 using HistoType = decltype(merged);
341 using CollectionHistoType = std::vector<HistoType>;
342
343 TF1* uni = new TF1("uni", "1", 0, 1000000);
344
345 const size_t randoms = 50000;
346 TRandomMixMax gen;
347 gen.SetSeed(std::random_device()());
348 Double_t randomArray[randoms];
349
350 for (auto _ : state) {
351
352 std::vector<CollectionHistoType> collections;
353
354 for (size_t ci = 0; ci < numberOfCollections; ci++) {
355 CollectionHistoType collection;
356 for (size_t i = 0; i < collectionSize; i++) {
357 collection.emplace_back(std::move(bh::make_histogram(bh::axis::regular<>(bins, min, max, "x"))));
358
359 auto& h = collection.back();
360 static_assert(std::is_reference<decltype(h)>::value);
361
362 gen.RndmArray(randoms, randomArray);
363 for (double r : randomArray) {
364 h(r * max);
365 }
366 }
367 collections.emplace_back(std::move(collection));
368 }
369
370 auto merge = [&](const CollectionHistoType& collection) {
371 for (size_t i = 0; i < collectionSize; i++) {
372 merged += collection[i];
373 }
374 };
375
376 auto start = std::chrono::high_resolution_clock::now();
377 for (size_t ci = 0; ci < numberOfCollections; ci++) {
378 merge(collections[ci]);
379 }
380 auto end = std::chrono::high_resolution_clock::now();
381
382 auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
383 state.SetIterationTime(elapsed_seconds.count());
384 }
385
386 delete uni;
387}
388
389static void BM_mergingBoostRegular2DCollections(benchmark::State& state)
390{
391 const double min = 0.0;
392 const double max = 1000000.0;
393 const size_t collectionSize = state.range(0);
394 // We should always merge the same amount of objects (histograms) in one benchmark cycle.
395 const size_t numberOfCollections = MAX_SIZE_COLLECTION / collectionSize;
396 const size_t bins = 250; // 250 bins * 250 bins * 4B makes 250kB
397
398 auto merged = bh::make_histogram(bh::axis::regular<>(bins, min, max, "x"), bh::axis::regular<>(bins, min, max, "y"));
399 merged += merged; // avoid memory overcommitment by doing something with data.
400 using HistoType = decltype(merged);
401 using CollectionHistoType = std::vector<HistoType>;
402
403 TF1* uni = new TF1("uni", "1", 0, 1000000);
404
405 const size_t randoms = 50000;
406 TRandomMixMax gen;
407 gen.SetSeed(std::random_device()());
408 Double_t randomArrayX[randoms];
409 Double_t randomArrayY[randoms];
410
411 for (auto _ : state) {
412
413 state.PauseTiming();
414 std::vector<CollectionHistoType> collections;
415
416 for (size_t ci = 0; ci < numberOfCollections; ci++) {
417 CollectionHistoType collection;
418 for (size_t i = 0; i < collectionSize; i++) {
419 collection.emplace_back(std::move(bh::make_histogram(bh::axis::regular<>(bins, min, max, "x"), bh::axis::regular<>(bins, min, max, "y"))));
420
421 auto& h = collection.back();
422 static_assert(std::is_reference<decltype(h)>::value);
423
424 gen.RndmArray(randoms, randomArrayX);
425 gen.RndmArray(randoms, randomArrayY);
426 for (size_t r = 0; r < randoms; r++) {
427 h(randomArrayX[r] * max, randomArrayY[r] * max);
428 }
429 }
430 collections.emplace_back(std::move(collection));
431 }
432
433 auto merge = [&](const CollectionHistoType& collection) {
434 for (size_t i = 0; i < collectionSize; i++) {
435 merged += collection[i];
436 }
437 };
438
439 auto start = std::chrono::high_resolution_clock::now();
440 for (size_t ci = 0; ci < numberOfCollections; ci++) {
441 merge(collections[ci]);
442 }
443 auto end = std::chrono::high_resolution_clock::now();
444
445 auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
446 state.SetIterationTime(elapsed_seconds.count());
447 }
448
449 delete uni;
450}
451
452// one by one comparison
453BENCHMARK(BM_mergingCollectionsTH1I)->Arg(1)->UseManualTime();
454BENCHMARK(BM_mergingCollectionsTH1I)->Arg(1)->UseManualTime();
455BENCHMARK(BM_mergingCollectionsTH2I)->Arg(1)->UseManualTime();
456BENCHMARK(BM_mergingCollectionsTH3I)->Arg(1)->UseManualTime();
457BENCHMARK(BM_mergingCollectionsTHNSparse)->Arg(1)->UseManualTime();
458BENCHMARK(BM_mergingPODCollections)->Arg(1)->UseManualTime();
459BENCHMARK(BM_mergingBoostRegular1DCollections)->Arg(1)->UseManualTime();
460BENCHMARK(BM_mergingBoostRegular2DCollections)->Arg(1)->UseManualTime();
461BENCHMARK(BM_mergingCollectionsTTree)->Arg(1)->UseManualTime();
462
463// collections
464
465BENCHMARK(BM_mergingCollectionsTH1I)->BENCHMARK_RANGE_COLLECTIONS->UseManualTime();
466BENCHMARK(BM_mergingCollectionsTH2I)->BENCHMARK_RANGE_COLLECTIONS->UseManualTime();
467BENCHMARK(BM_mergingCollectionsTH3I)->BENCHMARK_RANGE_COLLECTIONS->UseManualTime();
468BENCHMARK(BM_mergingCollectionsTHNSparse)->BENCHMARK_RANGE_COLLECTIONS->UseManualTime();
469BENCHMARK(BM_mergingPODCollections)->BENCHMARK_RANGE_COLLECTIONS->UseManualTime();
470BENCHMARK(BM_mergingBoostRegular1DCollections)->BENCHMARK_RANGE_COLLECTIONS->UseManualTime();
471BENCHMARK(BM_mergingBoostRegular2DCollections)->BENCHMARK_RANGE_COLLECTIONS->UseManualTime();
472BENCHMARK(BM_mergingCollectionsTTree)->BENCHMARK_RANGE_COLLECTIONS->UseManualTime();
473
default_random_engine gen(dev())
int32_t i
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events d
SurfaceTrackState state
bounded_vector< float > bins
uint32_t c
Definition RawData.h:2
void merge(Options const &options)
const size_t collectionSize
BENCHMARK_MAIN()
BENCHMARK(BM_mergingCollectionsTH1I) -> Arg(1) ->UseManualTime()
#define MAX_SIZE_COLLECTION
Class for time synchronization of RawReader instances.
const GLfloat * m
Definition glcorearb.h:4066
GLuint GLuint end
Definition glcorearb.h:469
GLuint const GLchar * name
Definition glcorearb.h:781
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLuint start
Definition glcorearb.h:469
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
constexpr size_t min
constexpr size_t max
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))