blob: 415a66072f069294b838ef48c99819fb553b1129 [file] [log] [blame]
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// 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>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010010#include <armnn/Conversion.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010011#include <armnn/utility/Assert.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000012#include <armnn/utility/IgnoreUnused.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010013
14#include <boost/format.hpp>
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010015
16namespace armnn
17{
18
19namespace profiling
20{
21
Sadik Armagan4c998992020-02-25 12:44:44 +000022const Category* CounterDirectory::RegisterCategory(const std::string& categoryName)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010023{
Matteo Martincigh6db5f202019-09-05 12:02:04 +010024 // Check that the given category name is valid
25 if (categoryName.empty() ||
26 !IsValidSwTraceString<SwTraceNameCharPolicy>(categoryName))
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010027 {
Matteo Martincigh6db5f202019-09-05 12:02:04 +010028 throw InvalidArgumentException("Trying to register a category with an invalid name");
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010029 }
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010030
Matteo Martincigh6db5f202019-09-05 12:02:04 +010031 // Check that the given category is not already registered
Matteo Martincigha84edee2019-10-02 12:50:57 +010032 if (IsCategoryRegistered(categoryName))
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010033 {
Matteo Martincigh6db5f202019-09-05 12:02:04 +010034 throw InvalidArgumentException(
35 boost::str(boost::format("Trying to register a category already registered (\"%1%\")")
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,
55 const 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() ||
59 !IsValidSwTraceString<SwTraceCharPolicy>(deviceName))
60 {
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 {
67 throw InvalidArgumentException(
68 boost::str(boost::format("Trying to register a device already registered (\"%1%\")")
69 % deviceName));
70 }
71
Matteo Martincigh6db5f202019-09-05 12:02:04 +010072 // Check that a category with the given (optional) parent category name is already registered
Matteo Martincigh6db5f202019-09-05 12:02:04 +010073 if (parentCategoryName.has_value())
74 {
75 // Get the (optional) parent category name
76 const std::string& parentCategoryNameValue = parentCategoryName.value();
77 if (parentCategoryNameValue.empty())
78 {
79 throw InvalidArgumentException(
80 boost::str(boost::format("Trying to connect a device (name: \"%1%\") to an invalid "
81 "parent category (name: \"%2%\")")
82 % deviceName
83 % parentCategoryNameValue));
84 }
85
86 // Check that the given parent category is already registered
87 auto categoryIt = FindCategory(parentCategoryNameValue);
88 if (categoryIt == m_Categories.end())
89 {
90 throw InvalidArgumentException(
91 boost::str(boost::format("Trying to connect a device (name: \"%1%\") to a parent category that "
92 "is not registered (name: \"%2%\")")
93 % deviceName
94 % parentCategoryNameValue));
95 }
Matteo Martincigh6db5f202019-09-05 12:02:04 +010096 }
97
98 // Get the device UID
99 uint16_t deviceUid = GetNextUid();
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100100
101 // Create the device
102 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100103 ARMNN_ASSERT(device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100104
105 // Get the raw device pointer
106 const Device* devicePtr = device.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100107 ARMNN_ASSERT(devicePtr);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100108
109 // Register the device
110 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
111
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100112 return devicePtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100113}
114
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100115const CounterSet* CounterDirectory::RegisterCounterSet(const std::string& counterSetName,
116 uint16_t count,
117 const Optional<std::string>& parentCategoryName)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100118{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100119 // Check that the given counter set name is valid
120 if (counterSetName.empty() ||
121 !IsValidSwTraceString<SwTraceNameCharPolicy>(counterSetName))
122 {
123 throw InvalidArgumentException("Trying to register a counter set with an invalid name");
124 }
125
126 // Check that a counter set with the given name is not already registered
Matteo Martincigha84edee2019-10-02 12:50:57 +0100127 if (IsCounterSetRegistered(counterSetName))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100128 {
129 throw InvalidArgumentException(
130 boost::str(boost::format("Trying to register a counter set already registered (\"%1%\")")
131 % counterSetName));
132 }
133
134 // 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
135 // the registration fails. We'll get a proper one once we're sure that the counter set can be registered
136 uint16_t counterSetUidPeek = GetNextUid(true);
137
138 // Check that a category with the given (optional) parent category name is already registered
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100139 if (parentCategoryName.has_value())
140 {
141 // Get the (optional) parent category name
142 const std::string& parentCategoryNameValue = parentCategoryName.value();
143 if (parentCategoryNameValue.empty())
144 {
145 throw InvalidArgumentException(
146 boost::str(boost::format("Trying to connect a counter set (UID: %1%) to an invalid "
147 "parent category (name: \"%2%\")")
148 % counterSetUidPeek
149 % parentCategoryNameValue));
150 }
151
152 // Check that the given parent category is already registered
153 auto it = FindCategory(parentCategoryNameValue);
154 if (it == m_Categories.end())
155 {
156 throw InvalidArgumentException(
157 boost::str(boost::format("Trying to connect a counter set (UID: %1%) to a parent category "
158 "that is not registered (name: \"%2%\")")
159 % counterSetUidPeek
160 % parentCategoryNameValue));
161 }
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100162 }
163
164 // Get the counter set UID
165 uint16_t counterSetUid = GetNextUid();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100166 ARMNN_ASSERT(counterSetUid == counterSetUidPeek);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100167
168 // Create the counter set
169 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100170 ARMNN_ASSERT(counterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100171
172 // Get the raw counter set pointer
173 const CounterSet* counterSetPtr = counterSet.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100174 ARMNN_ASSERT(counterSetPtr);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100175
176 // Register the counter set
177 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
178
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100179 return counterSetPtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100180}
181
Keith Davise394bd92019-12-02 15:12:19 +0000182const Counter* CounterDirectory::RegisterCounter(const BackendId& backendId,
183 const uint16_t uid,
184 const std::string& parentCategoryName,
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100185 uint16_t counterClass,
186 uint16_t interpolation,
187 double multiplier,
188 const std::string& name,
189 const std::string& description,
190 const Optional<std::string>& units,
191 const Optional<uint16_t>& numberOfCores,
192 const Optional<uint16_t>& deviceUid,
193 const Optional<uint16_t>& counterSetUid)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100194{
Jan Eilers8eb25602020-03-09 12:13:48 +0000195 IgnoreUnused(backendId);
Derek Lambertif143fba2020-01-02 13:50:57 +0000196
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100197 // Check that the given parent category name is valid
198 if (parentCategoryName.empty() ||
199 !IsValidSwTraceString<SwTraceNameCharPolicy>(parentCategoryName))
200 {
201 throw InvalidArgumentException("Trying to register a counter with an invalid parent category name");
202 }
203
204 // Check that the given class is valid
205 if (counterClass != 0 && counterClass != 1)
206 {
207 throw InvalidArgumentException("Trying to register a counter with an invalid class");
208 }
209
210 // Check that the given interpolation is valid
211 if (interpolation != 0 && interpolation != 1)
212 {
213 throw InvalidArgumentException("Trying to register a counter with an invalid interpolation");
214 }
215
216 // Check that the given multiplier is valid
217 if (multiplier == .0f)
218 {
219 throw InvalidArgumentException("Trying to register a counter with an invalid multiplier");
220 }
221
222 // Check that the given name is valid
223 if (name.empty() ||
224 !IsValidSwTraceString<SwTraceCharPolicy>(name))
225 {
226 throw InvalidArgumentException("Trying to register a counter with an invalid name");
227 }
228
229 // Check that the given description is valid
230 if (description.empty() ||
231 !IsValidSwTraceString<SwTraceCharPolicy>(description))
232 {
233 throw InvalidArgumentException("Trying to register a counter with an invalid description");
234 }
235
236 // Check that the given units are valid
237 if (units.has_value()
238 && !IsValidSwTraceString<SwTraceNameCharPolicy>(units.value()))
239 {
240 throw InvalidArgumentException("Trying to register a counter with a invalid units");
241 }
242
243 // Check that the given parent category is registered
244 auto categoryIt = FindCategory(parentCategoryName);
245 if (categoryIt == m_Categories.end())
246 {
247 throw InvalidArgumentException(
248 boost::str(boost::format("Trying to connect a counter to a category "
249 "that is not registered (name: \"%1%\")")
250 % parentCategoryName));
251 }
252
253 // Get the parent category
254 const CategoryPtr& parentCategory = *categoryIt;
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100255 ARMNN_ASSERT(parentCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100256
257 // Check that a counter with the given name is not already registered within the parent category
258 const std::vector<uint16_t>& parentCategoryCounters = parentCategory->m_Counters;
259 for (uint16_t parentCategoryCounterUid : parentCategoryCounters)
260 {
261 const Counter* parentCategoryCounter = GetCounter(parentCategoryCounterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100262 ARMNN_ASSERT(parentCategoryCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100263
264 if (parentCategoryCounter->m_Name == name)
265 {
266 throw InvalidArgumentException(
267 boost::str(boost::format("Trying to register a counter to category \"%1%\" with a name that "
268 "is already used within that category (name: \"%2%\")")
269 % parentCategoryName
270 % name));
271 }
272 }
273
274 // Check that a counter set with the given (optional) UID is already registered
275 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
276 if (counterSetUidValue > 0)
277 {
278 // Check that the (optional) counter set is already registered
Matteo Martincigha84edee2019-10-02 12:50:57 +0100279 if (!IsCounterSetRegistered(counterSetUidValue))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100280 {
281 throw InvalidArgumentException(
282 boost::str(boost::format("Trying to connect a counter to a counter set that is "
283 "not registered (counter set UID: %1%)")
284 % counterSetUidValue));
285 }
286 }
287
288 // Get the number of cores (this call may throw)
289 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
Sadik Armagan4c998992020-02-25 12:44:44 +0000290 uint16_t deviceCores = GetNumberOfCores(numberOfCores, deviceUidValue);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100291
292 // Get the counter UIDs and calculate the max counter UID
Keith Davise394bd92019-12-02 15:12:19 +0000293 std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100294 ARMNN_ASSERT(!counterUids.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100295 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
296
297 // Get the counter units
298 const std::string unitsValue = units.has_value() ? units.value() : "";
299
300 // Create the counter
Keith Davise394bd92019-12-02 15:12:19 +0000301 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
302 counterUids.front(),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100303 maxCounterUid,
304 counterClass,
305 interpolation,
306 multiplier,
307 name,
308 description,
309 unitsValue,
310 deviceUidValue,
311 counterSetUidValue);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100312 ARMNN_ASSERT(counter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100313
314 // Get the raw counter pointer
315 const Counter* counterPtr = counter.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100316 ARMNN_ASSERT(counterPtr);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100317
318 // Process multiple counters if necessary
319 for (uint16_t counterUid : counterUids)
320 {
321 // Connect the counter to the parent category
322 parentCategory->m_Counters.push_back(counterUid);
323
324 // Register the counter
325 m_Counters.insert(std::make_pair(counterUid, counter));
326 }
327
328 return counterPtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100329}
330
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100331const Category* CounterDirectory::GetCategory(const std::string& categoryName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100332{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100333 auto it = FindCategory(categoryName);
334 if (it == m_Categories.end())
335 {
336 return nullptr;
337 }
338
339 const Category* category = it->get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100340 ARMNN_ASSERT(category);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100341
342 return category;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100343}
344
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100345const Device* CounterDirectory::GetDevice(uint16_t deviceUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100346{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100347 auto it = FindDevice(deviceUid);
348 if (it == m_Devices.end())
349 {
350 return nullptr;
351 }
352
353 const Device* device = it->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100354 ARMNN_ASSERT(device);
355 ARMNN_ASSERT(device->m_Uid == deviceUid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100356
357 return device;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100358}
359
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100360const CounterSet* CounterDirectory::GetCounterSet(uint16_t counterSetUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100361{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100362 auto it = FindCounterSet(counterSetUid);
363 if (it == m_CounterSets.end())
364 {
365 return nullptr;
366 }
367
368 const CounterSet* counterSet = it->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100369 ARMNN_ASSERT(counterSet);
370 ARMNN_ASSERT(counterSet->m_Uid == counterSetUid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100371
372 return counterSet;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100373}
374
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100375const Counter* CounterDirectory::GetCounter(uint16_t counterUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100376{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100377 auto it = FindCounter(counterUid);
378 if (it == m_Counters.end())
379 {
380 return nullptr;
381 }
382
383 const Counter* counter = it->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100384 ARMNN_ASSERT(counter);
385 ARMNN_ASSERT(counter->m_Uid <= counterUid);
386 ARMNN_ASSERT(counter->m_Uid <= counter->m_MaxCounterUid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100387
388 return counter;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100389}
390
Matteo Martincigha84edee2019-10-02 12:50:57 +0100391bool CounterDirectory::IsCategoryRegistered(const std::string& categoryName) const
392{
393 auto it = FindCategory(categoryName);
394
395 return it != m_Categories.end();
396}
397
398bool CounterDirectory::IsDeviceRegistered(uint16_t deviceUid) const
399{
400 auto it = FindDevice(deviceUid);
401
402 return it != m_Devices.end();
403}
404
405bool CounterDirectory::IsDeviceRegistered(const std::string& deviceName) const
406{
407 auto it = FindDevice(deviceName);
408
409 return it != m_Devices.end();
410}
411
412bool CounterDirectory::IsCounterSetRegistered(uint16_t counterSetUid) const
413{
414 auto it = FindCounterSet(counterSetUid);
415
416 return it != m_CounterSets.end();
417}
418
419bool CounterDirectory::IsCounterSetRegistered(const std::string& counterSetName) const
420{
421 auto it = FindCounterSet(counterSetName);
422
423 return it != m_CounterSets.end();
424}
425
426bool CounterDirectory::IsCounterRegistered(uint16_t counterUid) const
427{
428 auto it = FindCounter(counterUid);
429
430 return it != m_Counters.end();
431}
432
433bool CounterDirectory::IsCounterRegistered(const std::string& counterName) const
434{
435 auto it = FindCounter(counterName);
436
437 return it != m_Counters.end();
438}
439
440void CounterDirectory::Clear()
441{
442 // Clear all the counter directory contents
443 m_Categories.clear();
444 m_Devices.clear();
445 m_CounterSets.clear();
446 m_Counters.clear();
447}
448
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100449CategoriesIt CounterDirectory::FindCategory(const std::string& categoryName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100450{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100451 return std::find_if(m_Categories.begin(), m_Categories.end(), [&categoryName](const CategoryPtr& category)
452 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100453 ARMNN_ASSERT(category);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100454
455 return category->m_Name == categoryName;
456 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100457}
458
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100459DevicesIt CounterDirectory::FindDevice(uint16_t deviceUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100460{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100461 return m_Devices.find(deviceUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100462}
463
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100464DevicesIt CounterDirectory::FindDevice(const std::string& deviceName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100465{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100466 return std::find_if(m_Devices.begin(), m_Devices.end(), [&deviceName](const auto& pair)
467 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100468 ARMNN_ASSERT(pair.second);
469 ARMNN_ASSERT(pair.second->m_Uid == pair.first);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100470
471 return pair.second->m_Name == deviceName;
472 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100473}
474
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100475CounterSetsIt CounterDirectory::FindCounterSet(uint16_t counterSetUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100476{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100477 return m_CounterSets.find(counterSetUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100478}
479
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100480CounterSetsIt CounterDirectory::FindCounterSet(const std::string& counterSetName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100481{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100482 return std::find_if(m_CounterSets.begin(), m_CounterSets.end(), [&counterSetName](const auto& pair)
483 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100484 ARMNN_ASSERT(pair.second);
485 ARMNN_ASSERT(pair.second->m_Uid == pair.first);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100486
487 return pair.second->m_Name == counterSetName;
488 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100489}
490
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100491CountersIt CounterDirectory::FindCounter(uint16_t counterUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100492{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100493 return m_Counters.find(counterUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100494}
495
Matteo Martincigha84edee2019-10-02 12:50:57 +0100496CountersIt CounterDirectory::FindCounter(const std::string& counterName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100497{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100498 return std::find_if(m_Counters.begin(), m_Counters.end(), [&counterName](const auto& pair)
499 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100500 ARMNN_ASSERT(pair.second);
501 ARMNN_ASSERT(pair.second->m_Uid == pair.first);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100502
Matteo Martincigha84edee2019-10-02 12:50:57 +0100503 return pair.second->m_Name == counterName;
504 });
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100505}
506
507uint16_t CounterDirectory::GetNumberOfCores(const Optional<uint16_t>& numberOfCores,
Sadik Armagan4c998992020-02-25 12:44:44 +0000508 uint16_t deviceUid)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100509{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100510 // To get the number of cores, apply the following rules:
511 //
512 // 1. If numberOfCores is set then take it as the deviceCores value
513 // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device,
514 // if so then that devices number of cores is taken as the deviceCores value
Sadik Armagan4c998992020-02-25 12:44:44 +0000515 // 3. If none of the above holds then set deviceCores to zero
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100516
517 // 1. If numberOfCores is set then take it as the deviceCores value
518 if (numberOfCores.has_value())
519 {
520 // Get the number of cores
521 return numberOfCores.value();
522 }
523
524 // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device,
525 // if so then that devices number of cores is taken as the deviceCores value
526 if (deviceUid > 0)
527 {
528 // Check that the (optional) device is already registered
529 auto deviceIt = FindDevice(deviceUid);
530 if (deviceIt == m_Devices.end())
531 {
532 throw InvalidArgumentException(
533 boost::str(boost::format("Trying to connect a counter to a device that is "
534 "not registered (device UID %1%)")
535 % deviceUid));
536 }
537
538 // Get the associated device
539 const DevicePtr& device = deviceIt->second;
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100540 ARMNN_ASSERT(device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100541
542 // Get the number of cores of the associated device
543 return device->m_Cores;
544 }
545
Sadik Armagan4c998992020-02-25 12:44:44 +0000546 // 3. If none of the above holds then set deviceCores to zero
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100547 return 0;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100548}
549
550} // namespace profiling
551
552} // namespace armnn