Project
Loading...
Searching...
No Matches
DictSizeEstimate.h
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
16#ifndef RANS_INTERNAL_METRICS_DICTSIZEESTIMATE_H_
17#define RANS_INTERNAL_METRICS_DICTSIZEESTIMATE_H_
18
19#include <cstddef>
20#include <cmath>
21
22#include <fairlogger/Logger.h>
23
26
27namespace o2::rans::internal
28{
30{
31 public:
32 DictSizeEstimate() = default;
33 inline DictSizeEstimate(size_t numSamples)
34 {
35 if (numSamples > 0) {
36 mScalingFactor = utils::pow2(DefaultScalingBits) / static_cast<double_t>(numSamples);
37 }
38 };
39
40 [[nodiscard]] inline size_t getIndexSize() const noexcept { return mIndexSizeBits; };
41 [[nodiscard]] inline size_t getFreqSize() const noexcept { return mFreqSizeBits; };
42 [[nodiscard]] inline size_t getIndexSizeB() const noexcept { return utils::toBytes(mIndexSizeBits); };
43 [[nodiscard]] inline size_t getFreqSizeB() const noexcept { return utils::toBytes(mFreqSizeBits); };
44
45 [[nodiscard]] inline size_t getSizeB(size_t nNonzero, size_t renormingBits) const
46 {
47 using namespace utils;
48
49 assert(isValidRenormingPrecision(renormingBits));
50 const float_t rescalingFactor = static_cast<float_t>(pow2(renormingBits)) / pow2(DefaultScalingBits);
51 const int64_t freqRescaled = getFreqSize() + static_cast<float_t>(nNonzero) * internal::fastlog2(rescalingFactor);
52 return toBytes(getIndexSize() + std::max(static_cast<int64_t>(nNonzero), freqRescaled));
53 };
54
55 inline void updateIndexSize(uint32_t delta)
56 {
57 mIndexSizeBits += computeEliasDeltaLength(delta);
58 };
59 inline void updateFreqSize(uint32_t frequency)
60 {
61 assert(frequency > 0);
62 const uint32_t scaledFrequency = std::max(1u, roundSymbolFrequency(frequency * mScalingFactor));
63 mFreqSizeBits += std::max(1u, computeEliasDeltaLength(scaledFrequency));
64 };
65
66 private:
67 [[nodiscard]] inline uint32_t computeEliasDeltaLength(uint32_t x) const noexcept
68 {
69 using namespace utils;
70 assert(x > 0);
71 return symbolLengthBits(x) + 2u * symbolLengthBits(symbolLengthBits(x) + 1u) + 1u;
72 };
73
74 static constexpr size_t DefaultScalingBits = defaults::MaxRenormPrecisionBits;
75
76 double_t mScalingFactor{1.0};
77 size_t mIndexSizeBits{};
78 size_t mFreqSizeBits{};
79};
80
82{
83 public:
84 inline DictSizeEstimateCounter(DictSizeEstimate* estimate) : mEstimate{estimate} {};
85
86 inline void update() noexcept { ++mDelta; };
87 inline void update(uint32_t frequency)
88 {
89 assert(frequency > 0);
90 assert(mDelta > 0);
91 mEstimate->updateIndexSize(mDelta);
92 mEstimate->updateFreqSize(frequency);
93 mDelta = 0u;
94 };
95
96 private:
97 uint32_t mDelta{};
98 DictSizeEstimate* mEstimate{};
99};
100} // namespace o2::rans::internal
101
102#endif /* RANS_INTERNAL_METRICS_DICTSIZEESTIMATE_H_ */
common helper classes and functions
DictSizeEstimateCounter(DictSizeEstimate *estimate)
size_t getSizeB(size_t nNonzero, size_t renormingBits) const
size_t getIndexSizeB() const noexcept
void updateFreqSize(uint32_t frequency)
GLint GLenum GLint x
Definition glcorearb.h:403
constexpr size_t MaxRenormPrecisionBits
Definition defaults.h:57
count_t roundSymbolFrequency(double_t rescaledFrequency)
Definition utils.h:109
constexpr float_t fastlog2(float_t x) noexcept
Definition utils.h:50
constexpr size_t pow2(size_t n) noexcept
Definition utils.h:165
constexpr size_t toBytes(size_t bits) noexcept
Definition utils.h:163
Common utility functions.