Project
Loading...
Searching...
No Matches
benchmark_EventMixing.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
16#include <benchmark/benchmark.h>
17#include <random>
18#include <vector>
19#include <list>
20
21using namespace o2::framework;
22using namespace arrow;
23using namespace o2::soa;
24
25// Validation of new event mixing: time complexity same as for naive loop
26
27#ifdef __APPLE__
28constexpr unsigned int maxPairsRange = 5;
29constexpr unsigned int maxFivesRange = 3;
30#else
31constexpr unsigned int maxPairsRange = 5;
32constexpr unsigned int maxFivesRange = 3;
33#endif
34constexpr int numEventsToMix = 5;
35constexpr int numTracksPerEvent = 10000;
36
37using namespace o2::framework;
38using namespace o2::soa;
39
40static void BM_EventMixingTraditional(benchmark::State& state)
41{
42 // Seed with a real random value, if available
43 std::default_random_engine e1(1234567891);
44 std::uniform_real_distribution<float> uniform_dist(0.f, 1.f);
45 std::uniform_real_distribution<float> uniform_dist_x(-0.065f, 0.073f);
46 std::uniform_real_distribution<float> uniform_dist_y(-0.320f, 0.360f);
47 std::uniform_int_distribution<int> uniform_dist_int(0, 5);
48
49 std::vector<double> xBins{VARIABLE_WIDTH, -0.064, -0.062, -0.060, 0.066, 0.068, 0.070, 0.072};
50 std::vector<double> yBins{VARIABLE_WIDTH, -0.320, -0.301, -0.300, 0.330, 0.340, 0.350, 0.360};
52 BinningType binningOnPositions{{xBins, yBins}, true}; // true is for 'ignore overflows' (true by default)
53
54 TableBuilder colBuilder, trackBuilder;
55 auto rowWriterCol = colBuilder.cursor<o2::aod::Collisions>();
56 for (auto i = 0; i < state.range(0); ++i) {
57 float x = uniform_dist_x(e1);
58 float y = uniform_dist_y(e1);
59 rowWriterCol(0, uniform_dist_int(e1),
60 x, y, uniform_dist(e1),
63 uniform_dist_int(e1), uniform_dist(e1),
64 uniform_dist_int(e1),
65 uniform_dist(e1), uniform_dist(e1));
66 }
67 auto tableCol = colBuilder.finalize();
68 o2::aod::Collisions collisions{tableCol};
69 std::uniform_int_distribution<int> uniform_dist_col_ind(0, collisions.size());
70
71 auto rowWriterTrack = trackBuilder.cursor<o2::aod::StoredTracks>();
72 for (auto i = 0; i < numTracksPerEvent * state.range(0); ++i) {
73 rowWriterTrack(0, uniform_dist_col_ind(e1), 0,
76 uniform_dist(e1));
77 }
78 auto tableTrack = trackBuilder.finalize();
79 o2::aod::StoredTracks tracks{tableTrack};
80
81 std::string key = "fIndex" + cutString(getLabelFromType<o2::aod::Collisions>());
82 ArrowTableSlicingCache atscache({{getLabelFromType<o2::aod::StoredTracks>(), getMatcherFromTypeForKey<o2::aod::StoredTracks>(key), key}});
83 auto s = atscache.updateCacheEntry(0, tableTrack);
84 SliceCache cache{&atscache};
85
86 int64_t count = 0;
87 int64_t colCount = 0;
88 int nBinsTot = (xBins.size() - 2) * (yBins.size() - 2);
89
90 for (auto _ : state) {
91 count = 0;
92 colCount = 0;
93 int n = state.range(0);
94 std::vector<std::list<o2::aod::Collisions::iterator>> mixingBufferVector;
95 for (int i = 0; i < nBinsTot; i++) {
96 mixingBufferVector.push_back(std::list<o2::aod::Collisions::iterator>());
97 }
98 for (auto& col1 : collisions) {
99 int bin = binningOnPositions.getBin({col1.posX(), col1.posY()});
100 if (bin == -1) {
101 continue;
102 }
103
104 auto& mixingBuffer = mixingBufferVector[bin];
105
106 if (mixingBuffer.size() > 0) {
107 auto tracks1 = tracks.sliceByCached(o2::aod::track::collisionId, col1.globalIndex(), cache);
108 for (auto& col2 : mixingBuffer) {
109 auto tracks2 = tracks.sliceByCached(o2::aod::track::collisionId, col2.globalIndex(), cache);
110 for (auto& [t1, t2] : combinations(CombinationsFullIndexPolicy(tracks1, tracks2))) {
111 count++;
112 }
113 colCount++;
114 }
115 if (mixingBuffer.size() >= numEventsToMix) {
116 mixingBuffer.pop_back();
117 }
118 }
119 mixingBuffer.push_front(col1);
120 }
121
122 benchmark::DoNotOptimize(count);
123 benchmark::DoNotOptimize(colCount);
124 }
125 state.counters["Mixed track pairs"] = count;
126 state.counters["Mixed collision pairs"] = colCount;
127 state.SetBytesProcessed(state.iterations() * sizeof(float) * count);
128}
129
130BENCHMARK(BM_EventMixingTraditional)->RangeMultiplier(2)->Range(4, 8 << maxPairsRange);
131
132static void BM_EventMixingCombinations(benchmark::State& state)
133{
134 // Seed with a real random value, if available
135 std::default_random_engine e1(1234567891);
136 std::uniform_real_distribution<float> uniform_dist(0.f, 1.f);
137 std::uniform_real_distribution<float> uniform_dist_x(-0.065f, 0.073f);
138 std::uniform_real_distribution<float> uniform_dist_y(-0.320f, 0.360f);
139 std::uniform_int_distribution<int> uniform_dist_int(0, 5);
140
141 std::vector<double> xBins{VARIABLE_WIDTH, -0.064, -0.062, -0.060, 0.066, 0.068, 0.070, 0.072};
142 std::vector<double> yBins{VARIABLE_WIDTH, -0.320, -0.301, -0.300, 0.330, 0.340, 0.350, 0.360};
144 BinningType binningOnPositions{{xBins, yBins}, true}; // true is for 'ignore overflows' (true by default)
145
146 TableBuilder colBuilder, trackBuilder;
147 auto rowWriterCol = colBuilder.cursor<o2::aod::Collisions>();
148 for (auto i = 0; i < state.range(0); ++i) {
149 float x = uniform_dist_x(e1);
150 float y = uniform_dist_y(e1);
151 rowWriterCol(0, uniform_dist_int(e1),
152 x, y, uniform_dist(e1),
155 uniform_dist_int(e1), uniform_dist(e1),
156 uniform_dist_int(e1),
157 uniform_dist(e1), uniform_dist(e1));
158 }
159 auto tableCol = colBuilder.finalize();
160 o2::aod::Collisions collisions{tableCol};
161 std::uniform_int_distribution<int> uniform_dist_col_ind(0, collisions.size());
162
163 auto rowWriterTrack = trackBuilder.cursor<o2::aod::StoredTracks>();
164 for (auto i = 0; i < numTracksPerEvent * state.range(0); ++i) {
165 rowWriterTrack(0, uniform_dist_col_ind(e1), 0,
168 uniform_dist(e1));
169 }
170 auto tableTrack = trackBuilder.finalize();
171 o2::aod::StoredTracks tracks{tableTrack};
172
173 int64_t count = 0;
174 int64_t colCount = 0;
175 std::string key = "fIndex" + getLabelFromType<o2::aod::Collisions>();
176 ArrowTableSlicingCache atscache{{{getLabelFromType<o2::aod::StoredTracks>(), getMatcherFromTypeForKey<o2::aod::StoredTracks>(key), key}}};
177 auto s = atscache.updateCacheEntry(0, tableTrack);
178 SliceCache cache{&atscache};
179
180 for (auto _ : state) {
181 count = 0;
182 colCount = 0;
183
184 auto tracksTuple = std::make_tuple(tracks);
185 SameKindPair<o2::aod::Collisions, o2::aod::StoredTracks, BinningType> pair{binningOnPositions, numEventsToMix - 1, -1, collisions, tracksTuple, &cache};
186 for (auto& [c1, tracks1, c2, tracks2] : pair) {
187 int bin = binningOnPositions.getBin({c1.posX(), c1.posY()});
188 for (auto& [t1, t2] : combinations(CombinationsFullIndexPolicy(tracks1, tracks2))) {
189 count++;
190 }
191 colCount++;
192 }
193 benchmark::DoNotOptimize(count);
194 benchmark::DoNotOptimize(colCount);
195 }
196 state.counters["Mixed track pairs"] = count;
197 state.counters["Mixed collision pairs"] = colCount;
198 state.SetBytesProcessed(state.iterations() * sizeof(float) * count);
199}
200
201BENCHMARK(BM_EventMixingCombinations)->RangeMultiplier(2)->Range(4, 8 << maxPairsRange);
202
benchmark::State & state
uniform_int_distribution< long long > uniform_dist(32, 126)
int32_t i
constexpr unsigned int maxPairsRange
BENCHMARK_MAIN()
BENCHMARK(BM_EventMixingTraditional) -> RangeMultiplier(2) ->Range(4, 8<< maxPairsRange)
constexpr int numTracksPerEvent
constexpr int numEventsToMix
constexpr unsigned int maxFivesRange
StringRef key
std::shared_ptr< arrow::Table > finalize()
GLdouble n
Definition glcorearb.h:1982
GLint GLenum GLint x
Definition glcorearb.h:403
GLint GLsizei count
Definition glcorearb.h:399
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition glcorearb.h:5034
Collisions_001 Collisions
Defining PrimaryVertex explicitly as messageable.
constexpr double VARIABLE_WIDTH
std::string cutString(std::string &&str)
Definition ASoA.cxx:266
auto combinations(const BP &binningPolicy, int categoryNeighbours, const T1 &outsider, const T2s &... tables)