|
| 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 | + |
| 12 | +#include "Framework/ASoAHelpers.h" |
| 13 | +#include "Framework/GroupedCombinations.h" |
| 14 | +#include "Framework/TableBuilder.h" |
| 15 | +#include "Framework/AnalysisDataModel.h" |
| 16 | +#include <benchmark/benchmark.h> |
| 17 | +#include <random> |
| 18 | +#include <vector> |
| 19 | +#include <list> |
| 20 | + |
| 21 | +using namespace o2::framework; |
| 22 | +using namespace arrow; |
| 23 | +using namespace o2::soa; |
| 24 | + |
| 25 | +// Validation of new event mixing in detail |
| 26 | + |
| 27 | +#ifdef __APPLE__ |
| 28 | +constexpr unsigned int maxColPairsRange = 20; |
| 29 | +#else |
| 30 | +constexpr unsigned int maxColPairsRange = 20; |
| 31 | +#endif |
| 32 | +constexpr int numEventsToMix = 5; |
| 33 | + |
| 34 | +using namespace o2::framework; |
| 35 | +using namespace o2::soa; |
| 36 | + |
| 37 | +static void BM_EventMixingTableCreation(benchmark::State& state) |
| 38 | +{ |
| 39 | + for (auto _ : state) { |
| 40 | + // Seed with a real random value, if available |
| 41 | + std::default_random_engine e1(1234567891); |
| 42 | + std::uniform_real_distribution<float> uniform_dist(0.f, 1.f); |
| 43 | + std::uniform_real_distribution<float> uniform_dist_x(-0.065f, 0.073f); |
| 44 | + std::uniform_real_distribution<float> uniform_dist_y(-0.320f, 0.360f); |
| 45 | + std::uniform_int_distribution<int> uniform_dist_int(0, 5); |
| 46 | + |
| 47 | + TableBuilder colBuilder; |
| 48 | + auto rowWriterCol = colBuilder.cursor<o2::aod::Collisions>(); |
| 49 | + for (auto i = 0; i < state.range(0); ++i) { |
| 50 | + float x = uniform_dist_x(e1); |
| 51 | + float y = uniform_dist_y(e1); |
| 52 | + rowWriterCol(0, uniform_dist_int(e1), |
| 53 | + x, y, uniform_dist(e1), |
| 54 | + uniform_dist(e1), uniform_dist(e1), uniform_dist(e1), |
| 55 | + uniform_dist(e1), uniform_dist(e1), uniform_dist(e1), |
| 56 | + uniform_dist_int(e1), uniform_dist(e1), |
| 57 | + uniform_dist_int(e1), |
| 58 | + uniform_dist(e1), uniform_dist(e1)); |
| 59 | + } |
| 60 | + auto tableCol = colBuilder.finalize(); |
| 61 | + o2::aod::Collisions collisions{tableCol}; |
| 62 | + } |
| 63 | + state.SetBytesProcessed(state.iterations() * (12 * sizeof(float) + sizeof(int64_t) + 3 * sizeof(int)) * state.range(0)); |
| 64 | +} |
| 65 | + |
| 66 | +BENCHMARK(BM_EventMixingTableCreation)->RangeMultiplier(2)->Range(4UL, 2UL << maxColPairsRange); |
| 67 | + |
| 68 | +static void BM_EventMixingBinningCreation(benchmark::State& state) |
| 69 | +{ |
| 70 | + std::vector<double> xBins{VARIABLE_WIDTH, -0.064, -0.062, -0.060, 0.066, 0.068, 0.070, 0.072}; |
| 71 | + std::vector<double> yBins{VARIABLE_WIDTH, -0.320, -0.301, -0.300, 0.330, 0.340, 0.350, 0.360}; |
| 72 | + |
| 73 | + for (auto _ : state) { |
| 74 | + using BinningType = ColumnBinningPolicy<o2::aod::collision::PosX, o2::aod::collision::PosY>; |
| 75 | + BinningType binningOnPositions{{xBins, yBins}, true}; // true is for 'ignore overflows' (true by default) |
| 76 | + } |
| 77 | + state.SetBytesProcessed(state.iterations() * sizeof(float)); |
| 78 | +} |
| 79 | + |
| 80 | +BENCHMARK(BM_EventMixingBinningCreation)->RangeMultiplier(2)->Range(4UL, 2UL << maxColPairsRange); |
| 81 | + |
| 82 | +static void BM_EventMixingPolicyCreation(benchmark::State& state) |
| 83 | +{ |
| 84 | + // Seed with a real random value, if available |
| 85 | + std::default_random_engine e1(1234567891); |
| 86 | + std::uniform_real_distribution<float> uniform_dist(0.f, 1.f); |
| 87 | + std::uniform_real_distribution<float> uniform_dist_x(-0.065f, 0.073f); |
| 88 | + std::uniform_real_distribution<float> uniform_dist_y(-0.320f, 0.360f); |
| 89 | + std::uniform_int_distribution<int> uniform_dist_int(0, 5); |
| 90 | + |
| 91 | + TableBuilder colBuilder; |
| 92 | + auto rowWriterCol = colBuilder.cursor<o2::aod::Collisions>(); |
| 93 | + for (auto i = 0; i < state.range(0); ++i) { |
| 94 | + float x = uniform_dist_x(e1); |
| 95 | + float y = uniform_dist_y(e1); |
| 96 | + rowWriterCol(0, uniform_dist_int(e1), |
| 97 | + x, y, uniform_dist(e1), |
| 98 | + uniform_dist(e1), uniform_dist(e1), uniform_dist(e1), |
| 99 | + uniform_dist(e1), uniform_dist(e1), uniform_dist(e1), |
| 100 | + uniform_dist_int(e1), uniform_dist(e1), |
| 101 | + uniform_dist_int(e1), |
| 102 | + uniform_dist(e1), uniform_dist(e1)); |
| 103 | + } |
| 104 | + auto tableCol = colBuilder.finalize(); |
| 105 | + o2::aod::Collisions collisions{tableCol}; |
| 106 | + |
| 107 | + std::vector<double> xBins{VARIABLE_WIDTH, -0.064, -0.062, -0.060, 0.066, 0.068, 0.070, 0.072}; |
| 108 | + std::vector<double> yBins{VARIABLE_WIDTH, -0.320, -0.301, -0.300, 0.330, 0.340, 0.350, 0.360}; |
| 109 | + using BinningType = ColumnBinningPolicy<o2::aod::collision::PosX, o2::aod::collision::PosY>; |
| 110 | + BinningType binningOnPositions{{xBins, yBins}, true}; // true is for 'ignore overflows' (true by default) |
| 111 | + |
| 112 | + for (auto _ : state) { |
| 113 | + auto combPolicy = CombinationsBlockUpperSameIndexPolicy(binningOnPositions, numEventsToMix - 1, -1, collisions, collisions); |
| 114 | + } |
| 115 | + state.SetBytesProcessed(state.iterations() * sizeof(float) * state.range(0)); |
| 116 | +} |
| 117 | + |
| 118 | +BENCHMARK(BM_EventMixingPolicyCreation)->RangeMultiplier(2)->Range(4UL, 2UL << maxColPairsRange); |
| 119 | + |
| 120 | +static void BM_EventMixingCombinationsCreation(benchmark::State& state) |
| 121 | +{ |
| 122 | + // Seed with a real random value, if available |
| 123 | + std::default_random_engine e1(1234567891); |
| 124 | + std::uniform_real_distribution<float> uniform_dist(0.f, 1.f); |
| 125 | + std::uniform_real_distribution<float> uniform_dist_x(-0.065f, 0.073f); |
| 126 | + std::uniform_real_distribution<float> uniform_dist_y(-0.320f, 0.360f); |
| 127 | + std::uniform_int_distribution<int> uniform_dist_int(0, 5); |
| 128 | + |
| 129 | + TableBuilder colBuilder; |
| 130 | + auto rowWriterCol = colBuilder.cursor<o2::aod::Collisions>(); |
| 131 | + for (auto i = 0; i < state.range(0); ++i) { |
| 132 | + float x = uniform_dist_x(e1); |
| 133 | + float y = uniform_dist_y(e1); |
| 134 | + rowWriterCol(0, uniform_dist_int(e1), |
| 135 | + x, y, uniform_dist(e1), |
| 136 | + uniform_dist(e1), uniform_dist(e1), uniform_dist(e1), |
| 137 | + uniform_dist(e1), uniform_dist(e1), uniform_dist(e1), |
| 138 | + uniform_dist_int(e1), uniform_dist(e1), |
| 139 | + uniform_dist_int(e1), |
| 140 | + uniform_dist(e1), uniform_dist(e1)); |
| 141 | + } |
| 142 | + auto tableCol = colBuilder.finalize(); |
| 143 | + o2::aod::Collisions collisions{tableCol}; |
| 144 | + |
| 145 | + std::vector<double> xBins{VARIABLE_WIDTH, -0.064, -0.062, -0.060, 0.066, 0.068, 0.070, 0.072}; |
| 146 | + std::vector<double> yBins{VARIABLE_WIDTH, -0.320, -0.301, -0.300, 0.330, 0.340, 0.350, 0.360}; |
| 147 | + using BinningType = ColumnBinningPolicy<o2::aod::collision::PosX, o2::aod::collision::PosY>; |
| 148 | + BinningType binningOnPositions{{xBins, yBins}, true}; // true is for 'ignore overflows' (true by default) |
| 149 | + |
| 150 | + auto combPolicy = CombinationsBlockUpperSameIndexPolicy(binningOnPositions, numEventsToMix - 1, -1, collisions, collisions); |
| 151 | + |
| 152 | + for (auto _ : state) { |
| 153 | + auto comb = combinations(combPolicy); |
| 154 | + } |
| 155 | + state.SetBytesProcessed(state.iterations() * sizeof(float)); |
| 156 | +} |
| 157 | + |
| 158 | +BENCHMARK(BM_EventMixingCombinationsCreation)->RangeMultiplier(2)->Range(4UL, 2UL << maxColPairsRange); |
| 159 | + |
| 160 | +static void BM_EventMixingCombinations(benchmark::State& state) |
| 161 | +{ |
| 162 | + // Seed with a real random value, if available |
| 163 | + std::default_random_engine e1(1234567891); |
| 164 | + std::uniform_real_distribution<float> uniform_dist(0.f, 1.f); |
| 165 | + std::uniform_real_distribution<float> uniform_dist_x(-0.065f, 0.073f); |
| 166 | + std::uniform_real_distribution<float> uniform_dist_y(-0.320f, 0.360f); |
| 167 | + std::uniform_int_distribution<int> uniform_dist_int(0, 5); |
| 168 | + |
| 169 | + TableBuilder colBuilder; |
| 170 | + auto rowWriterCol = colBuilder.cursor<o2::aod::Collisions>(); |
| 171 | + for (auto i = 0; i < state.range(0); ++i) { |
| 172 | + float x = uniform_dist_x(e1); |
| 173 | + float y = uniform_dist_y(e1); |
| 174 | + rowWriterCol(0, uniform_dist_int(e1), |
| 175 | + x, y, uniform_dist(e1), |
| 176 | + uniform_dist(e1), uniform_dist(e1), uniform_dist(e1), |
| 177 | + uniform_dist(e1), uniform_dist(e1), uniform_dist(e1), |
| 178 | + uniform_dist_int(e1), uniform_dist(e1), |
| 179 | + uniform_dist_int(e1), |
| 180 | + uniform_dist(e1), uniform_dist(e1)); |
| 181 | + } |
| 182 | + auto tableCol = colBuilder.finalize(); |
| 183 | + o2::aod::Collisions collisions{tableCol}; |
| 184 | + |
| 185 | + std::vector<double> xBins{VARIABLE_WIDTH, -0.064, -0.062, -0.060, 0.066, 0.068, 0.070, 0.072}; |
| 186 | + std::vector<double> yBins{VARIABLE_WIDTH, -0.320, -0.301, -0.300, 0.330, 0.340, 0.350, 0.360}; |
| 187 | + using BinningType = ColumnBinningPolicy<o2::aod::collision::PosX, o2::aod::collision::PosY>; |
| 188 | + BinningType binningOnPositions{{xBins, yBins}, true}; // true is for 'ignore overflows' (true by default) |
| 189 | + |
| 190 | + auto combPolicy = CombinationsBlockUpperSameIndexPolicy(binningOnPositions, numEventsToMix - 1, -1, collisions, collisions); |
| 191 | + |
| 192 | + auto comb = combinations(combPolicy); |
| 193 | + |
| 194 | + int64_t colCount = 0; |
| 195 | + |
| 196 | + for (auto _ : state) { |
| 197 | + colCount = 0; |
| 198 | + for (auto& combT : comb) { |
| 199 | + colCount++; |
| 200 | + } |
| 201 | + benchmark::DoNotOptimize(colCount); |
| 202 | + } |
| 203 | + state.counters["Mixed collision pairs"] = colCount; |
| 204 | + state.SetBytesProcessed(state.iterations() * sizeof(float) * colCount); |
| 205 | +} |
| 206 | + |
| 207 | +BENCHMARK(BM_EventMixingCombinations)->RangeMultiplier(2)->Range(4UL, 2UL << maxColPairsRange); |
| 208 | + |
| 209 | +BENCHMARK_MAIN(); |
0 commit comments