ATLAS Offline Software
Loading...
Searching...
No Matches
TransformStore.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "GeoModelKernel/throwExcept.h"
7
8#include <mutex>
9namespace{
10 std::mutex s_ticketMutex{};
11 static const Amg::Transform3D dummy{Amg::Transform3D::Identity()};
12}
13
14namespace ActsTrk::detail {
15 std::string TransformStore::toString(const Mode m) {
16 switch (m) {
17 using enum Mode;
18 case LazyFill: return "LazyFill";
19 case Block: return "Block";
20 }
21 return "";
22 }
23 std::ostream& TransformStore::print(std::ostream& ostr) const {
24 const auto f = filled();
25 const auto s = size();
26 ostr<<detectorType()<<" transform store in mode: "<<mode()
27 <<", size: "<<s<<", (un)allocated: ("<<(s- f)<<")"<<f;
28 return ostr;
29 }
30
32 const Mode mode) noexcept {
33 const unsigned size = TrfStoreTicketCounter::distributedTickets(dType);
34 switch (mode) {
35 using enum Mode;
36 case LazyFill: {
37 LazyStorage_t store(size);
38 return store;
39 } case Block: {
40 TrfVec_t trfStore(size, Amg::Transform3D::Identity());
41 CheckVec_t isSet(size, 0);
42 return std::make_pair(std::move(trfStore), std::move(isSet));
43 }
44 }
45 return Storage_t{};
46 }
48 const Mode m):
49 m_storage{initStorage(detType, m)},
50 m_detType{detType} {}
52 m_storage{initStorage(other.detectorType(), other.mode())},
53 m_detType{other.detectorType()} {
54 if (mode() == Mode::Block){
55 for (std::size_t t = 0; t < size(); ++t) {
56 if (const Amg::Transform3D* copyMe = other.getTransform(t); copyMe != nullptr) {
57 setTransform(t, Amg::Transform3D{*copyMe});
58 }
59 }
60 }
61 }
62
64 if (&other != this) {
65 m_storage = initStorage(other.detectorType(), other.mode());
66 m_detType = other.detectorType();
67 if (mode() == Mode::Block){
68 for (std::size_t t = 0; t < size(); ++t) {
69 if (const Amg::Transform3D* copyMe = other.getTransform(t);
70 copyMe != nullptr) {
71 setTransform(t, Amg::Transform3D{*copyMe});
72 }
73 }
74 }
75 }
76 return (*this);
77 }
79 const Amg::Transform3D& TransformStore::setTransform(const unsigned ticketNo, Amg::Transform3D && trf) const {
80 return std::visit([&](auto& store) -> const Amg::Transform3D& {
81 using Store_t = std::decay_t<decltype(store)>;
82 if constexpr(std::is_same_v<Store_t, LazyStorage_t>){
83 return (*store.at(ticketNo).set(std::make_unique<Amg::Transform3D>(std::move(trf))));
84 } else if constexpr( std::is_same_v<Store_t, BlockStorage_t>) {
85 THROW_EXCEPTION("The transform store "<<(*this)<<" has been initialized with"
86 <<" block storage caching. Cannot assign "<<ticketNo<<".");
87 return dummy;
88 } else {
89 THROW_EXCEPTION("Unsupported storage type "<<(*this));
90 return dummy;
91 }
92 }, m_storage);
93 }
94 std::size_t TransformStore::size() const {
95 return std::visit([](const auto& store){
96 using Store_t = std::decay_t<decltype(store)>;
97 if constexpr(std::is_same_v<Store_t, LazyStorage_t>) {
98 return store.size();
99 } else {
100 return store.first.size();
101 }
102 }, m_storage);
103 }
104
105 const Amg::Transform3D& TransformStore::setTransform(const unsigned ticketNo, Amg::Transform3D && trf) {
106 return std::visit([&](auto& store) -> const Amg::Transform3D& {
107 using Store_t = std::decay_t<decltype(store)>;
108 if constexpr(std::is_same_v<Store_t, LazyStorage_t>) {
109 return (*store.at(ticketNo).set(std::make_unique<Amg::Transform3D>(std::move(trf))));
110 } else if constexpr(std::is_same_v<Store_t, BlockStorage_t>) {
111 store.second.at(ticketNo) = true;
112 return (store.first.at(ticketNo) = std::move(trf));
113 } else {
114 THROW_EXCEPTION("Unsupported storage type "<<(*this));
115 return dummy;
116 }
117 }, m_storage);
118 }
119
121 return std::visit([&](const auto& store) {
122 using Store_t = std::decay_t<decltype(store)>;
123 if constexpr(std::is_same_v<Store_t, LazyStorage_t>) {
124 return Mode::LazyFill;
125 } else if constexpr(std::is_same_v<Store_t, BlockStorage_t>) {
126 return Mode::Block;
127 }
128 }, m_storage);
129 }
130
131 std::size_t TransformStore::filled() const {
132 return std::visit([](const auto& store) {
133 using Store_t = std::decay_t<decltype(store)>;
134 if constexpr(std::is_same_v<Store_t, LazyStorage_t>) {
135 return std::count_if(store.begin(), store.end(), [](const auto& trf){
136 return trf.get() != nullptr;
137 });
138 } else if constexpr(std::is_same_v<Store_t, BlockStorage_t>) {
139 return std::count_if(store.second.begin(), store.second.end(), [](const char filled){
140 return filled;
141 });
142 }
143 }, m_storage);
144 }
145
146
147
151 TicketCounterArr TrfStoreTicketCounter::s_clientCounter{};
152 ReturnedTicketArr TrfStoreTicketCounter::s_returnedTickets{};
153 ReturnedHintArr TrfStoreTicketCounter::s_returnedHints{};
154
156 std::unique_lock guard{s_ticketMutex};
157 const unsigned idx = static_cast<unsigned>(type);
158 std::vector<char>& returnedPool = s_returnedTickets[idx];
159 int& returnedHint = s_returnedHints[idx];
160 if (returnedPool.size() && returnedHint >= 0) {
161 for (size_t i = returnedHint; i < returnedPool.size(); ++i) {
162 if (returnedPool[i]) {
163 returnedPool[i] = false;
164
165 returnedHint = i+1;
166 if (static_cast<size_t>(returnedHint) >= returnedPool.size()) {
167 returnedHint = 0;
168 }
169 return i;
170 }
171 }
172
173 for (size_t i = 0; i < static_cast<size_t>(returnedHint); ++i) {
174 if (returnedPool[i]) {
175 returnedPool[i] = false;
176 returnedHint = i+1;
177 return i;
178 }
179 }
180 returnedHint = -1;
181 } else {
182 returnedHint = -1;
183 }
184 return s_clientCounter[idx]++;
185 }
187 std::unique_lock guard{s_ticketMutex};
188 return s_clientCounter[static_cast<unsigned>(type)];
189 }
191 unsigned ticketNo) {
192 std::unique_lock guard{s_ticketMutex};
193 const unsigned idx = static_cast<unsigned>(type);
194 std::vector<char>& returnedPool = s_returnedTickets[idx];
195 int& returnedHint = s_returnedHints[idx];
197 const unsigned distributed = s_clientCounter[idx];
198 if (ticketNo == distributed -1) {
199
200 if (ticketNo > 0 && ticketNo-1 < returnedPool.size()) {
201 for (; ticketNo > 0 && returnedPool[ticketNo-1]; --ticketNo){}
202 returnedPool.resize (ticketNo);
203 }
205 s_clientCounter[idx] = ticketNo;
206 if (returnedHint >= static_cast<int>(ticketNo)) {
207 returnedHint = 0;
208 }
209 } else {
210 if (returnedPool.size() <= ticketNo) {
211 returnedPool.resize (ticketNo+1);
212 }
213 returnedPool[ticketNo] = true;
214 if (returnedHint < 0 || static_cast<int>(ticketNo) < returnedHint) {
215 returnedHint = ticketNo;
216 }
217 }
218 }
219}
size_t size() const
Number of registered mappings.
const Amg::Transform3D & setTransform(const unsigned int ticketNo, Amg::Transform3D &&trf) const
Stores a transform at the index of the passed ticket number.
std::variant< LazyStorage_t, BlockStorage_t > Storage_t
Use the std::variant to dynamically switch between the two types.
Mode
Enum describing the operation mode of the cache.
@ Block
Transforms can be set later during the event processing.
std::ostream & print(std::ostream &ostr) const
std::size_t filled() const
Returns the number of filled transforms.
static std::string toString(const Mode m)
Define the to string operator.
static Storage_t initStorage(const DetectorType dType, const Mode mode) noexcept
Helper method to instantiate the storage for a given detector type.
std::vector< CxxUtils::CachedUniquePtr< Amg::Transform3D > > LazyStorage_t
Abrivation of the backend in the lazy filling mode.
TransformStore(const DetectorType detType, const Mode m)
Constructor.
std::vector< char > CheckVec_t
Abrivate the char vector.
std::vector< Amg::Transform3D > TrfVec_t
Abrivate the transform vector.
DetectorType m_detType
The associated detector type.
Mode mode() const
Returns the mode with which the store has been instantiated.
std::size_t size() const
Return the size (maximum capacity) of the current store.
TransformStore & operator=(TransformStore &&other) noexcept=default
Define the move assignment operator.
DetectorType detectorType() const
Returns the detector type.
static unsigned int distributedTickets(const DetectorType detType)
Returns the number of all distributed tickets.
std::array< int, s_techs > ReturnedHintArr
static unsigned int drawTicket(const DetectorType detType)
Returns a unique ID to the client under which the client can store its transfomrm inside the containe...
std::array< std::atomic< unsigned >, s_techs > TicketCounterArr
static void giveBackTicket(const DetectorType detType, unsigned int ticketNo)
Return back a ticket for the specified detector type such that its slot can be used by another instan...
std::array< std::vector< char >, s_techs > ReturnedTicketArr
StoreGateSvc * Store_t
Athena definition of the Eigen plugin.
TrfStoreTicketCounter::TicketCounterArr TicketCounterArr
TrfStoreTicketCounter::ReturnedHintArr ReturnedHintArr
TrfStoreTicketCounter::ReturnedTicketArr ReturnedTicketArr
DetectorType
Simple enum to Identify the Type of the ACTS sub detector.
Eigen::Affine3d Transform3D
#define THROW_EXCEPTION(MESSAGE)
Definition throwExcept.h:10