blob: 5a7d8c6165eae7f341b00dc1e885a93c7424480e [file] [log] [blame]
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "CounterDirectory.hpp"
Matteo Martincigh6db5f202019-09-05 12:02:04 +01007#include "ProfilingUtils.hpp"
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +01008
9#include <armnn/Exceptions.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010010#include <armnn/utility/Assert.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000011#include <armnn/utility/IgnoreUnused.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012
Jim Flynnbbfe6032020-07-20 16:57:44 +010013#include <common/include/SwTrace.hpp>
14
Jan Eilers156113c2020-09-09 19:11:16 +010015#include <fmt/format.h>
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010016
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000017namespace arm
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010018{
19
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000020namespace pipe
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010021{
22
Sadik Armagan4c998992020-02-25 12:44:44 +000023const Category* CounterDirectory::RegisterCategory(const std::string& categoryName)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010024{
Matteo Martincigh6db5f202019-09-05 12:02:04 +010025 // Check that the given category name is valid
26 if (categoryName.empty() ||
Jim Flynnbbfe6032020-07-20 16:57:44 +010027 !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(categoryName))
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010028 {
Matteo Martincigh6db5f202019-09-05 12:02:04 +010029 throw InvalidArgumentException("Trying to register a category with an invalid name");
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010030 }
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010031
Matteo Martincigh6db5f202019-09-05 12:02:04 +010032 // Check that the given category is not already registered
Matteo Martincigha84edee2019-10-02 12:50:57 +010033 if (IsCategoryRegistered(categoryName))
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010034 {
Jan Eilers156113c2020-09-09 19:11:16 +010035 throw InvalidArgumentException(fmt::format("Trying to register a category already registered (\"{}\")",
36 categoryName));
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010037 }
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010038
Matteo Martincigh6db5f202019-09-05 12:02:04 +010039 // Create the category
Sadik Armagan4c998992020-02-25 12:44:44 +000040 CategoryPtr category = std::make_unique<Category>(categoryName);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010041 ARMNN_ASSERT(category);
Matteo Martincigh6db5f202019-09-05 12:02:04 +010042
43 // Get the raw category pointer
44 const Category* categoryPtr = category.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010045 ARMNN_ASSERT(categoryPtr);
Matteo Martincigh6db5f202019-09-05 12:02:04 +010046
47 // Register the category
48 m_Categories.insert(std::move(category));
49
50 return categoryPtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010051}
52
Matteo Martincigh6db5f202019-09-05 12:02:04 +010053const Device* CounterDirectory::RegisterDevice(const std::string& deviceName,
54 uint16_t cores,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000055 const armnn::Optional<std::string>& parentCategoryName)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010056{
Matteo Martincigh6db5f202019-09-05 12:02:04 +010057 // Check that the given device name is valid
58 if (deviceName.empty() ||
Jim Flynnbbfe6032020-07-20 16:57:44 +010059 !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(deviceName))
Matteo Martincigh6db5f202019-09-05 12:02:04 +010060 {
61 throw InvalidArgumentException("Trying to register a device with an invalid name");
62 }
63
64 // Check that a device with the given name is not already registered
Matteo Martincigha84edee2019-10-02 12:50:57 +010065 if (IsDeviceRegistered(deviceName))
Matteo Martincigh6db5f202019-09-05 12:02:04 +010066 {
Jan Eilers156113c2020-09-09 19:11:16 +010067 throw InvalidArgumentException(fmt::format("Trying to register a device already registered (\"{}\")",
68 deviceName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +010069 }
70
Matteo Martincigh6db5f202019-09-05 12:02:04 +010071 // Check that a category with the given (optional) parent category name is already registered
Matteo Martincigh6db5f202019-09-05 12:02:04 +010072 if (parentCategoryName.has_value())
73 {
74 // Get the (optional) parent category name
75 const std::string& parentCategoryNameValue = parentCategoryName.value();
76 if (parentCategoryNameValue.empty())
77 {
78 throw InvalidArgumentException(
Jan Eilers156113c2020-09-09 19:11:16 +010079 fmt::format("Trying to connect a device (name: \"{}\") to an invalid "
80 "parent category (name: \"{}\")",
81 deviceName,
82 parentCategoryNameValue));
Matteo Martincigh6db5f202019-09-05 12:02:04 +010083 }
84
85 // Check that the given parent category is already registered
86 auto categoryIt = FindCategory(parentCategoryNameValue);
87 if (categoryIt == m_Categories.end())
88 {
89 throw InvalidArgumentException(
Jan Eilers156113c2020-09-09 19:11:16 +010090 fmt::format("Trying to connect a device (name: \"{}\") to a parent category that "
91 "is not registered (name: \"{}\")",
92 deviceName,
93 parentCategoryNameValue));
Matteo Martincigh6db5f202019-09-05 12:02:04 +010094 }
Matteo Martincigh6db5f202019-09-05 12:02:04 +010095 }
96
97 // Get the device UID
98 uint16_t deviceUid = GetNextUid();
Matteo Martincigh6db5f202019-09-05 12:02:04 +010099
100 // Create the device
101 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100102 ARMNN_ASSERT(device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100103
104 // Get the raw device pointer
105 const Device* devicePtr = device.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100106 ARMNN_ASSERT(devicePtr);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100107
108 // Register the device
109 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
110
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100111 return devicePtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100112}
113
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100114const CounterSet* CounterDirectory::RegisterCounterSet(const std::string& counterSetName,
115 uint16_t count,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000116 const armnn::Optional<std::string>& parentCategoryName)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100117{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100118 // Check that the given counter set name is valid
119 if (counterSetName.empty() ||
Jim Flynnbbfe6032020-07-20 16:57:44 +0100120 !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(counterSetName))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100121 {
122 throw InvalidArgumentException("Trying to register a counter set with an invalid name");
123 }
124
125 // Check that a counter set with the given name is not already registered
Matteo Martincigha84edee2019-10-02 12:50:57 +0100126 if (IsCounterSetRegistered(counterSetName))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100127 {
128 throw InvalidArgumentException(
Jan Eilers156113c2020-09-09 19:11:16 +0100129 fmt::format("Trying to register a counter set already registered (\"{}\")",
130 counterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100131 }
132
133 // Peek the next UID, do not get an actual valid UID just now as we don't want to waste a good UID in case
134 // the registration fails. We'll get a proper one once we're sure that the counter set can be registered
135 uint16_t counterSetUidPeek = GetNextUid(true);
136
137 // Check that a category with the given (optional) parent category name is already registered
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100138 if (parentCategoryName.has_value())
139 {
140 // Get the (optional) parent category name
141 const std::string& parentCategoryNameValue = parentCategoryName.value();
142 if (parentCategoryNameValue.empty())
143 {
144 throw InvalidArgumentException(
Jan Eilers156113c2020-09-09 19:11:16 +0100145 fmt::format("Trying to connect a counter set (UID: {}) to an invalid "
146 "parent category (name: \"{}\")",
147 counterSetUidPeek,
148 parentCategoryNameValue));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100149 }
150
151 // Check that the given parent category is already registered
152 auto it = FindCategory(parentCategoryNameValue);
153 if (it == m_Categories.end())
154 {
155 throw InvalidArgumentException(
Jan Eilers156113c2020-09-09 19:11:16 +0100156 fmt::format("Trying to connect a counter set (UID: {}) to a parent category "
157 "that is not registered (name: \"{}\")",
158 counterSetUidPeek,
159 parentCategoryNameValue));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100160 }
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100161 }
162
163 // Get the counter set UID
164 uint16_t counterSetUid = GetNextUid();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100165 ARMNN_ASSERT(counterSetUid == counterSetUidPeek);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100166
167 // Create the counter set
168 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100169 ARMNN_ASSERT(counterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100170
171 // Get the raw counter set pointer
172 const CounterSet* counterSetPtr = counterSet.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100173 ARMNN_ASSERT(counterSetPtr);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100174
175 // Register the counter set
176 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
177
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100178 return counterSetPtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100179}
180
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000181const Counter* CounterDirectory::RegisterCounter(const armnn::BackendId& backendId,
Keith Davise394bd92019-12-02 15:12:19 +0000182 const uint16_t uid,
183 const std::string& parentCategoryName,
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100184 uint16_t counterClass,
185 uint16_t interpolation,
186 double multiplier,
187 const std::string& name,
188 const std::string& description,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000189 const armnn::Optional<std::string>& units,
190 const armnn::Optional<uint16_t>& numberOfCores,
191 const armnn::Optional<uint16_t>& deviceUid,
192 const armnn::Optional<uint16_t>& counterSetUid)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100193{
Jan Eilers8eb25602020-03-09 12:13:48 +0000194 IgnoreUnused(backendId);
Derek Lambertif143fba2020-01-02 13:50:57 +0000195
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100196 // Check that the given parent category name is valid
197 if (parentCategoryName.empty() ||
Jim Flynnbbfe6032020-07-20 16:57:44 +0100198 !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(parentCategoryName))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100199 {
200 throw InvalidArgumentException("Trying to register a counter with an invalid parent category name");
201 }
202
203 // Check that the given class is valid
204 if (counterClass != 0 && counterClass != 1)
205 {
206 throw InvalidArgumentException("Trying to register a counter with an invalid class");
207 }
208
209 // Check that the given interpolation is valid
210 if (interpolation != 0 && interpolation != 1)
211 {
212 throw InvalidArgumentException("Trying to register a counter with an invalid interpolation");
213 }
214
215 // Check that the given multiplier is valid
216 if (multiplier == .0f)
217 {
218 throw InvalidArgumentException("Trying to register a counter with an invalid multiplier");
219 }
220
221 // Check that the given name is valid
222 if (name.empty() ||
Jim Flynnbbfe6032020-07-20 16:57:44 +0100223 !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(name))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100224 {
225 throw InvalidArgumentException("Trying to register a counter with an invalid name");
226 }
227
228 // Check that the given description is valid
229 if (description.empty() ||
Jim Flynnbbfe6032020-07-20 16:57:44 +0100230 !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(description))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100231 {
232 throw InvalidArgumentException("Trying to register a counter with an invalid description");
233 }
234
235 // Check that the given units are valid
236 if (units.has_value()
Jim Flynnbbfe6032020-07-20 16:57:44 +0100237 && !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(units.value()))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100238 {
239 throw InvalidArgumentException("Trying to register a counter with a invalid units");
240 }
241
242 // Check that the given parent category is registered
243 auto categoryIt = FindCategory(parentCategoryName);
244 if (categoryIt == m_Categories.end())
245 {
246 throw InvalidArgumentException(
Jan Eilers156113c2020-09-09 19:11:16 +0100247 fmt::format("Trying to connect a counter to a category that is not registered (name: \"{}\")",
248 parentCategoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100249 }
250
251 // Get the parent category
252 const CategoryPtr& parentCategory = *categoryIt;
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100253 ARMNN_ASSERT(parentCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100254
255 // Check that a counter with the given name is not already registered within the parent category
256 const std::vector<uint16_t>& parentCategoryCounters = parentCategory->m_Counters;
257 for (uint16_t parentCategoryCounterUid : parentCategoryCounters)
258 {
259 const Counter* parentCategoryCounter = GetCounter(parentCategoryCounterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100260 ARMNN_ASSERT(parentCategoryCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100261
262 if (parentCategoryCounter->m_Name == name)
263 {
264 throw InvalidArgumentException(
Jan Eilers156113c2020-09-09 19:11:16 +0100265 fmt::format("Trying to register a counter to category \"{}\" with a name that "
266 "is already used within that category (name: \"{}\")",
267 parentCategoryName,
268 name));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100269 }
270 }
271
272 // Check that a counter set with the given (optional) UID is already registered
273 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
274 if (counterSetUidValue > 0)
275 {
276 // Check that the (optional) counter set is already registered
Matteo Martincigha84edee2019-10-02 12:50:57 +0100277 if (!IsCounterSetRegistered(counterSetUidValue))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100278 {
279 throw InvalidArgumentException(
Jan Eilers156113c2020-09-09 19:11:16 +0100280 fmt::format("Trying to connect a counter to a counter set that is "
281 "not registered (counter set UID: {})",
282 counterSetUidValue));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100283 }
284 }
285
286 // Get the number of cores (this call may throw)
287 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
Sadik Armagan4c998992020-02-25 12:44:44 +0000288 uint16_t deviceCores = GetNumberOfCores(numberOfCores, deviceUidValue);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100289
290 // Get the counter UIDs and calculate the max counter UID
Keith Davise394bd92019-12-02 15:12:19 +0000291 std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100292 ARMNN_ASSERT(!counterUids.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100293 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
294
295 // Get the counter units
296 const std::string unitsValue = units.has_value() ? units.value() : "";
297
298 // Create the counter
Keith Davise394bd92019-12-02 15:12:19 +0000299 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
300 counterUids.front(),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100301 maxCounterUid,
302 counterClass,
303 interpolation,
304 multiplier,
305 name,
306 description,
307 unitsValue,
308 deviceUidValue,
309 counterSetUidValue);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100310 ARMNN_ASSERT(counter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100311
312 // Get the raw counter pointer
313 const Counter* counterPtr = counter.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100314 ARMNN_ASSERT(counterPtr);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100315
316 // Process multiple counters if necessary
317 for (uint16_t counterUid : counterUids)
318 {
319 // Connect the counter to the parent category
320 parentCategory->m_Counters.push_back(counterUid);
321
322 // Register the counter
323 m_Counters.insert(std::make_pair(counterUid, counter));
324 }
325
326 return counterPtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100327}
328
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100329const Category* CounterDirectory::GetCategory(const std::string& categoryName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100330{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100331 auto it = FindCategory(categoryName);
332 if (it == m_Categories.end())
333 {
334 return nullptr;
335 }
336
337 const Category* category = it->get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100338 ARMNN_ASSERT(category);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100339
340 return category;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100341}
342
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100343const Device* CounterDirectory::GetDevice(uint16_t deviceUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100344{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100345 auto it = FindDevice(deviceUid);
346 if (it == m_Devices.end())
347 {
348 return nullptr;
349 }
350
351 const Device* device = it->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100352 ARMNN_ASSERT(device);
353 ARMNN_ASSERT(device->m_Uid == deviceUid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100354
355 return device;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100356}
357
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100358const CounterSet* CounterDirectory::GetCounterSet(uint16_t counterSetUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100359{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100360 auto it = FindCounterSet(counterSetUid);
361 if (it == m_CounterSets.end())
362 {
363 return nullptr;
364 }
365
366 const CounterSet* counterSet = it->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100367 ARMNN_ASSERT(counterSet);
368 ARMNN_ASSERT(counterSet->m_Uid == counterSetUid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100369
370 return counterSet;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100371}
372
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100373const Counter* CounterDirectory::GetCounter(uint16_t counterUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100374{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100375 auto it = FindCounter(counterUid);
376 if (it == m_Counters.end())
377 {
378 return nullptr;
379 }
380
381 const Counter* counter = it->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100382 ARMNN_ASSERT(counter);
383 ARMNN_ASSERT(counter->m_Uid <= counterUid);
384 ARMNN_ASSERT(counter->m_Uid <= counter->m_MaxCounterUid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100385
386 return counter;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100387}
388
Matteo Martincigha84edee2019-10-02 12:50:57 +0100389bool CounterDirectory::IsCategoryRegistered(const std::string& categoryName) const
390{
391 auto it = FindCategory(categoryName);
392
393 return it != m_Categories.end();
394}
395
396bool CounterDirectory::IsDeviceRegistered(uint16_t deviceUid) const
397{
398 auto it = FindDevice(deviceUid);
399
400 return it != m_Devices.end();
401}
402
403bool CounterDirectory::IsDeviceRegistered(const std::string& deviceName) const
404{
405 auto it = FindDevice(deviceName);
406
407 return it != m_Devices.end();
408}
409
410bool CounterDirectory::IsCounterSetRegistered(uint16_t counterSetUid) const
411{
412 auto it = FindCounterSet(counterSetUid);
413
414 return it != m_CounterSets.end();
415}
416
417bool CounterDirectory::IsCounterSetRegistered(const std::string& counterSetName) const
418{
419 auto it = FindCounterSet(counterSetName);
420
421 return it != m_CounterSets.end();
422}
423
424bool CounterDirectory::IsCounterRegistered(uint16_t counterUid) const
425{
426 auto it = FindCounter(counterUid);
427
428 return it != m_Counters.end();
429}
430
431bool CounterDirectory::IsCounterRegistered(const std::string& counterName) const
432{
433 auto it = FindCounter(counterName);
434
435 return it != m_Counters.end();
436}
437
438void CounterDirectory::Clear()
439{
440 // Clear all the counter directory contents
441 m_Categories.clear();
442 m_Devices.clear();
443 m_CounterSets.clear();
444 m_Counters.clear();
445}
446
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100447CategoriesIt CounterDirectory::FindCategory(const std::string& categoryName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100448{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100449 return std::find_if(m_Categories.begin(), m_Categories.end(), [&categoryName](const CategoryPtr& category)
450 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100451 ARMNN_ASSERT(category);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100452
453 return category->m_Name == categoryName;
454 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100455}
456
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100457DevicesIt CounterDirectory::FindDevice(uint16_t deviceUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100458{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100459 return m_Devices.find(deviceUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100460}
461
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100462DevicesIt CounterDirectory::FindDevice(const std::string& deviceName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100463{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100464 return std::find_if(m_Devices.begin(), m_Devices.end(), [&deviceName](const auto& pair)
465 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100466 ARMNN_ASSERT(pair.second);
467 ARMNN_ASSERT(pair.second->m_Uid == pair.first);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100468
469 return pair.second->m_Name == deviceName;
470 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100471}
472
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100473CounterSetsIt CounterDirectory::FindCounterSet(uint16_t counterSetUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100474{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100475 return m_CounterSets.find(counterSetUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100476}
477
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100478CounterSetsIt CounterDirectory::FindCounterSet(const std::string& counterSetName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100479{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100480 return std::find_if(m_CounterSets.begin(), m_CounterSets.end(), [&counterSetName](const auto& pair)
481 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100482 ARMNN_ASSERT(pair.second);
483 ARMNN_ASSERT(pair.second->m_Uid == pair.first);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100484
485 return pair.second->m_Name == counterSetName;
486 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100487}
488
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100489CountersIt CounterDirectory::FindCounter(uint16_t counterUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100490{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100491 return m_Counters.find(counterUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100492}
493
Matteo Martincigha84edee2019-10-02 12:50:57 +0100494CountersIt CounterDirectory::FindCounter(const std::string& counterName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100495{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100496 return std::find_if(m_Counters.begin(), m_Counters.end(), [&counterName](const auto& pair)
497 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100498 ARMNN_ASSERT(pair.second);
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100499 ARMNN_ASSERT(pair.first >= pair.second->m_Uid && pair.first <= pair.second->m_MaxCounterUid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100500
Matteo Martincigha84edee2019-10-02 12:50:57 +0100501 return pair.second->m_Name == counterName;
502 });
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100503}
504
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000505uint16_t CounterDirectory::GetNumberOfCores(const armnn::Optional<uint16_t>& numberOfCores,
Sadik Armagan4c998992020-02-25 12:44:44 +0000506 uint16_t deviceUid)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100507{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100508 // To get the number of cores, apply the following rules:
509 //
510 // 1. If numberOfCores is set then take it as the deviceCores value
511 // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device,
512 // if so then that devices number of cores is taken as the deviceCores value
Sadik Armagan4c998992020-02-25 12:44:44 +0000513 // 3. If none of the above holds then set deviceCores to zero
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100514
515 // 1. If numberOfCores is set then take it as the deviceCores value
516 if (numberOfCores.has_value())
517 {
518 // Get the number of cores
519 return numberOfCores.value();
520 }
521
522 // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device,
523 // if so then that devices number of cores is taken as the deviceCores value
524 if (deviceUid > 0)
525 {
526 // Check that the (optional) device is already registered
527 auto deviceIt = FindDevice(deviceUid);
528 if (deviceIt == m_Devices.end())
529 {
530 throw InvalidArgumentException(
Jan Eilers156113c2020-09-09 19:11:16 +0100531 fmt::format("Trying to connect a counter to a device that is not registered (device UID {})",
532 deviceUid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100533 }
534
535 // Get the associated device
536 const DevicePtr& device = deviceIt->second;
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100537 ARMNN_ASSERT(device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100538
539 // Get the number of cores of the associated device
540 return device->m_Cores;
541 }
542
Sadik Armagan4c998992020-02-25 12:44:44 +0000543 // 3. If none of the above holds then set deviceCores to zero
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100544 return 0;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100545}
546
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000547} // namespace pipe
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100548
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000549} // namespace arm