Project
Loading...
Searching...
No Matches
CapacityEstimator.h
Go to the documentation of this file.
1// Copyright 2019-2026 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.
15
16#ifndef ALICEO2_ITSMFT_TRACKING_CAPACITYESTIMATOR_H_
17#define ALICEO2_ITSMFT_TRACKING_CAPACITYESTIMATOR_H_
18
19#include <algorithm>
20#include <cstddef>
21#include <cstdint>
22#include <limits>
23#include <memory>
24
26{
27
40constexpr const char* const SlabSiteNames[SlabSite::NSlabSite]{"Tracklets", "CellCandidates", "Cells", "Neighbours", "RoadCandidates", "Roads", "TrackSeeds", "TracksExtended", "Tracks"};
41
43{
44 public:
45 struct Config {
46 float alpha{0.2f};
47 float marginInit{1.30f};
48 float marginMin{1.10f};
49 float marginMax{4.00f};
50 float marginUp{1.50f};
51 float marginOverflowSlack{1.05f};
52 float marginDown{0.98f};
53 float lowWatermark{0.60f};
54 uint32_t decayAfter{2};
55 size_t floorSlots{1024};
56 };
57
58 using KeyType = uint64_t;
59
60 struct Decoded {
64 int slot;
65 };
66
67 struct Statistics {
68 size_t requested{0};
69 size_t granted{0};
70 size_t emitted{0};
71 size_t spilled{0};
72 size_t maxEmitted{0};
73 uint32_t samples{0};
74 uint32_t overflowEvents{0};
75 uint32_t nLowStreak{0};
76 };
77
78 static constexpr KeyType makeKey(SlabSite site, int iteration, int variant, int slot) noexcept
79 {
80 return (static_cast<KeyType>(site) << 56) |
81 (static_cast<KeyType>(iteration & 0xFF) << 48) |
82 (static_cast<KeyType>(variant & 0xFFFF) << 32) |
83 static_cast<KeyType>(static_cast<uint32_t>(slot));
84 }
85
86 template <typename Identifier>
87 static constexpr KeyType makeKey(SlabSite site, int iteration, int variant, Identifier identifier) noexcept
88 {
89 return makeKey(site, iteration, variant, static_cast<int>(identifier.value()));
90 }
91
92 static constexpr Decoded decodeKey(KeyType key) noexcept
93 {
94 return {
95 .site = static_cast<SlabSite>((key >> 56) & 0xFF),
96 .iteration = static_cast<int>((key >> 48) & 0xFF),
97 .variant = static_cast<int>((key >> 32) & 0xFFFF),
98 .slot = static_cast<int>(static_cast<uint32_t>(key & 0xFFFFFFFF))};
99 }
100
101 static constexpr int makeVariant(int high, int low) noexcept
102 {
103 return ((high & 0xFF) << 8) | (low & 0xFF);
104 }
105
106 static constexpr int getVariantHigh(int variant) noexcept
107 {
108 return (variant >> 8) & 0xFF;
109 }
110
111 static constexpr int getVariantLow(int variant) noexcept
112 {
113 return variant & 0xFF;
114 }
115
117 explicit CapacityEstimator(Config cfg);
121
122 void reset();
123 void beginTransaction();
126 size_t capacity(uint64_t key, double scale) const;
127 size_t peakCapacity(uint64_t key) const;
128 double expected(uint64_t key, double scale) const;
129 Statistics statistics(uint64_t key) const;
130 void update(uint64_t key, double scale, size_t emitted, size_t capacityUsed, bool overflowed, bool memoryLimited);
131 void update(uint64_t key, double scale, size_t requested, size_t granted, size_t emitted,
132 size_t spilled, bool overflowed, bool memoryLimited);
133 void print() const;
134
135 private:
136 struct Impl;
137 std::unique_ptr<Impl> mImpl;
138};
139
140template <typename Emit>
141int runOnSlab(CapacityEstimator& estimator, const CapacityEstimator::KeyType key, const double scale, Emit&& emit, const size_t floorCapacity = 0)
142{
143 const auto toInt = [](const size_t v) { return static_cast<int>(std::min(v, static_cast<size_t>(std::numeric_limits<int>::max()))); };
144 const int initialCapacity = toInt(estimator.capacity(key, scale));
145 int capacity = std::max(initialCapacity, toInt(floorCapacity));
146 int emitted = 0;
147 bool overflowed = false;
148 bool needsRetry = false;
149 do {
150 const int attemptCapacity = capacity;
151 emitted = emit(attemptCapacity);
152 needsRetry = emitted > attemptCapacity;
153 overflowed |= needsRetry;
154 capacity = emitted;
155 } while (needsRetry);
156 estimator.update(key, scale, emitted, initialCapacity, overflowed, false);
157 return emitted;
158}
159
160} // namespace o2::itsmft::tracking
161
162#endif /* ALICEO2_ITSMFT_TRACKING_CAPACITYESTIMATOR_H_ */
StringRef key
static constexpr int makeVariant(int high, int low) noexcept
static constexpr int getVariantHigh(int variant) noexcept
static constexpr KeyType makeKey(SlabSite site, int iteration, int variant, int slot) noexcept
static constexpr KeyType makeKey(SlabSite site, int iteration, int variant, Identifier identifier) noexcept
Statistics statistics(uint64_t key) const
size_t capacity(uint64_t key, double scale) const
CapacityEstimator & operator=(const CapacityEstimator &)=delete
static constexpr int getVariantLow(int variant) noexcept
void update(uint64_t key, double scale, size_t emitted, size_t capacityUsed, bool overflowed, bool memoryLimited)
CapacityEstimator(const CapacityEstimator &)=delete
static constexpr Decoded decodeKey(KeyType key) noexcept
GLfloat GLfloat GLfloat alpha
Definition glcorearb.h:279
const GLdouble * v
Definition glcorearb.h:832
GLsizei samples
Definition glcorearb.h:1309
uint32_t trackClusterIndicesSize noexcept
constexpr const char *const SlabSiteNames[SlabSite::NSlabSite]
std::map< std::string, ID > expected