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 static constexpr Decoded decodeKey(KeyType key) noexcept
86 {
87 return {
88 .site = static_cast<SlabSite>((key >> 56) & 0xFF),
89 .iteration = static_cast<int>((key >> 48) & 0xFF),
90 .variant = static_cast<int>((key >> 32) & 0xFFFF),
91 .slot = static_cast<int>(static_cast<uint32_t>(key & 0xFFFFFFFF))};
92 }
93
94 static constexpr int makeVariant(int high, int low) noexcept
95 {
96 return ((high & 0xFF) << 8) | (low & 0xFF);
97 }
98
99 static constexpr int getVariantHigh(int variant) noexcept
100 {
101 return (variant >> 8) & 0xFF;
102 }
103
104 static constexpr int getVariantLow(int variant) noexcept
105 {
106 return variant & 0xFF;
107 }
108
110 explicit CapacityEstimator(Config cfg);
114
115 void reset();
116 void beginTransaction();
117 void commitTransaction() noexcept;
118 void rollbackTransaction() noexcept;
119 size_t capacity(uint64_t key, double scale) const;
120 size_t peakCapacity(uint64_t key) const;
121 double expected(uint64_t key, double scale) const;
122 Statistics statistics(uint64_t key) const;
123 void update(uint64_t key, double scale, size_t emitted, size_t capacityUsed, bool overflowed, bool memoryLimited);
124 void update(uint64_t key, double scale, size_t requested, size_t granted, size_t emitted,
125 size_t spilled, bool overflowed, bool memoryLimited);
126 void print() const;
127
128 private:
129 struct Impl;
130 std::unique_ptr<Impl> mImpl;
131};
132
133template <typename Emit>
134int runOnSlab(CapacityEstimator& estimator, const CapacityEstimator::KeyType key, const double scale, Emit&& emit, const size_t floorCapacity = 0)
135{
136 const auto toInt = [](const size_t v) { return static_cast<int>(std::min(v, static_cast<size_t>(std::numeric_limits<int>::max()))); };
137 const int initialCapacity = toInt(estimator.capacity(key, scale));
138 int capacity = std::max(initialCapacity, toInt(floorCapacity));
139 int emitted = 0;
140 bool overflowed = false;
141 bool needsRetry = false;
142 do {
143 const int attemptCapacity = capacity;
144 emitted = emit(attemptCapacity);
145 needsRetry = emitted > attemptCapacity;
146 overflowed |= needsRetry;
147 capacity = emitted;
148 } while (needsRetry);
149 estimator.update(key, scale, emitted, initialCapacity, overflowed, false);
150 return emitted;
151}
152
153} // namespace o2::itsmft::tracking
154
155#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
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
constexpr const char *const SlabSiteNames[SlabSite::NSlabSite]
std::map< std::string, ID > expected