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
39constexpr const char* const SlabSiteNames[SlabSite::NSlabSite]{"Tracklets", "Cells", "Neighbours", "RoadCandidates", "Roads", "TrackSeeds", "TracksExtended", "Tracks"};
40
42{
43 public:
44 struct Config {
45 float alpha{0.2f};
46 float marginInit{1.30f};
47 float marginMin{1.10f};
48 float marginMax{4.00f};
49 float marginUp{1.50f};
50 float marginOverflowSlack{1.05f};
51 float marginDown{0.98f};
52 float lowWatermark{0.60f};
53 uint32_t decayAfter{2};
54 size_t floorSlots{1024};
55 };
56
57 using KeyType = uint64_t;
58
59 struct Decoded {
63 int slot;
64 };
65
66 struct Statistics {
67 size_t requested{0};
68 size_t granted{0};
69 size_t emitted{0};
70 size_t spilled{0};
71 size_t maxEmitted{0};
72 uint32_t samples{0};
73 uint32_t overflowEvents{0};
74 uint32_t nLowStreak{0};
75 };
76
77 static constexpr KeyType makeKey(SlabSite site, int iteration, int variant, int slot) noexcept
78 {
79 return (static_cast<KeyType>(site) << 56) |
80 (static_cast<KeyType>(iteration & 0xFF) << 48) |
81 (static_cast<KeyType>(variant & 0xFFFF) << 32) |
82 static_cast<KeyType>(static_cast<uint32_t>(slot));
83 }
84
85 template <typename Identifier>
86 static constexpr KeyType makeKey(SlabSite site, int iteration, int variant, Identifier identifier) noexcept
87 {
88 return makeKey(site, iteration, variant, static_cast<int>(identifier.value()));
89 }
90
91 static constexpr Decoded decodeKey(KeyType key) noexcept
92 {
93 return {
94 .site = static_cast<SlabSite>((key >> 56) & 0xFF),
95 .iteration = static_cast<int>((key >> 48) & 0xFF),
96 .variant = static_cast<int>((key >> 32) & 0xFFFF),
97 .slot = static_cast<int>(static_cast<uint32_t>(key & 0xFFFFFFFF))};
98 }
99
100 static constexpr int makeVariant(int high, int low) noexcept
101 {
102 return ((high & 0xFF) << 8) | (low & 0xFF);
103 }
104
105 static constexpr int getVariantHigh(int variant) noexcept
106 {
107 return (variant >> 8) & 0xFF;
108 }
109
110 static constexpr int getVariantLow(int variant) noexcept
111 {
112 return variant & 0xFF;
113 }
114
116 explicit CapacityEstimator(Config cfg);
120
121 void reset();
122 void beginTransaction();
125 size_t capacity(uint64_t key, double scale) const;
126 size_t peakCapacity(uint64_t key) const;
127 double expected(uint64_t key, double scale) const;
128 Statistics statistics(uint64_t key) const;
129 void update(uint64_t key, double scale, size_t emitted, size_t capacityUsed, bool overflowed, bool memoryLimited);
130 void update(uint64_t key, double scale, size_t requested, size_t granted, size_t emitted,
131 size_t spilled, bool overflowed, bool memoryLimited);
132 void print() const;
133
134 private:
135 struct Impl;
136 std::unique_ptr<Impl> mImpl;
137};
138
139template <typename Emit>
140int runOnSlab(CapacityEstimator& estimator, const CapacityEstimator::KeyType key, const double scale, Emit&& emit, const size_t floorCapacity = 0)
141{
142 const auto toInt = [](const size_t v) { return static_cast<int>(std::min(v, static_cast<size_t>(std::numeric_limits<int>::max()))); };
143 const int initialCapacity = toInt(estimator.capacity(key, scale));
144 int capacity = std::max(initialCapacity, toInt(floorCapacity));
145 int emitted = 0;
146 bool overflowed = false;
147 bool needsRetry = false;
148 do {
149 const int attemptCapacity = capacity;
150 emitted = emit(attemptCapacity);
151 needsRetry = emitted > attemptCapacity;
152 overflowed |= needsRetry;
153 capacity = emitted;
154 } while (needsRetry);
155 estimator.update(key, scale, emitted, initialCapacity, overflowed, false);
156 return emitted;
157}
158
159} // namespace o2::itsmft::tracking
160
161#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