Project
Loading...
Searching...
No Matches
bench_ransUnpack.cxx
Go to the documentation of this file.
1// Copyright 2019-2023 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
15
17
18#include <vector>
19#include <cstring>
20#include <random>
21#include <algorithm>
22#ifdef RANS_PARALLEL_STL
23#include <execution>
24#endif
25#include <iterator>
26
27#include <benchmark/benchmark.h>
28
30
31#ifdef ENABLE_VTUNE_PROFILER
32#include <ittnotify.h>
33#endif
34
35#include "helpers.h"
36
37using namespace o2::rans;
38
39inline constexpr size_t MessageSize = 1ull << 22;
40
41using source_type = uint32_t;
42
43template <typename source_T>
44std::vector<source_T> makeRandomUniformVector(size_t nelems, source_T min = std::numeric_limits<source_T>::max(), source_T max = std::numeric_limits<source_T>::max())
45{
46 std::vector<source_T> result(nelems, 0);
47 std::mt19937 mt(0); // same seed we want always the same distrubution of random numbers;
48 std::uniform_int_distribution<source_T> dist(min, max);
49
50#ifdef RANS_PARALLEL_STL
51 std::generate(std::execution::par_unseq, result.begin(), result.end(), [&dist, &mt]() { return dist(mt); });
52#else
53 std::generate(result.begin(), result.end(), [&dist, &mt]() { return dist(mt); });
54#endif // RANS_PARALLEL_STL
55 return result;
56};
57
58static void copyBenchmark(benchmark::State& state)
59{
60 std::vector<source_type> src = makeRandomUniformVector<source_type>(MessageSize);
61 std::vector<source_type> dst(MessageSize, 0);
62 for (auto _ : state) {
63 std::copy(src.begin(), src.end(), dst.begin());
64 };
65 state.SetItemsProcessed(src.size() * state.iterations());
66 state.SetBytesProcessed(src.size() * sizeof(source_type) * state.iterations());
67};
68
69static void unpackingBenchmark(benchmark::State& state)
70{
71 size_t packingBits = state.range(0);
72
73 std::vector<source_type> src = makeRandomUniformVector<source_type>(MessageSize, 0, utils::pow2(packingBits) - 1);
74 std::vector<uint32_t> dst(MessageSize, 0);
75
76 BitPtr iter{dst.data()};
77 for (auto i : src) {
78 iter = internal::pack(iter, i, packingBits);
79 }
80
81 std::vector<uint32_t> unpacked(MessageSize, 0);
82
83 for (auto _ : state) {
84 BitPtr iter{dst.data()};
85 for (size_t i = 0; i < src.size(); ++i) {
86 unpacked[i] = internal::unpack<uint32_t>(iter, packingBits);
87 iter += packingBits;
88 }
89 }
90
91 state.SetItemsProcessed(src.size() * state.iterations());
92 state.SetBytesProcessed(src.size() * sizeof(uint32_t) * state.iterations());
93
94 if (!std::equal(unpacked.begin(), unpacked.end(), src.begin())) {
95 state.SkipWithError("error in packing");
96 }
97};
98
99BENCHMARK(copyBenchmark);
100BENCHMARK(unpackingBenchmark)->DenseRange(1, 32, 1);
benchmark::State & state
int32_t i
uint32_t source_type
constexpr size_t MessageSize
BENCHMARK_MAIN()
std::vector< source_T > makeRandomUniformVector(size_t nelems, source_T min=std::numeric_limits< source_T >::max(), source_T max=std::numeric_limits< source_T >::max())
BENCHMARK(copyBenchmark)
preprocessor defines to enable features based on CPU architecture
GLenum src
Definition glcorearb.h:1767
GLuint64EXT * result
Definition glcorearb.h:5662
GLenum GLenum dst
Definition glcorearb.h:1767
common functionality for rANS benchmarks.
packs data into a buffer
BitPtr pack(BitPtr pos, uint64_t data, size_t packingWidth)
Definition pack.h:68
constexpr size_t min
constexpr size_t max