blob: cef3d6a76d30eb6377bbf4d12630355f3ef7d196 [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>
11
12#include <boost/format.hpp>
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010013
14namespace armnn
15{
16
17namespace profiling
18{
19
Matteo Martincigh6db5f202019-09-05 12:02:04 +010020const Category* CounterDirectory::RegisterCategory(const std::string& categoryName,
21 const Optional<uint16_t>& deviceUid,
22 const Optional<uint16_t>& counterSetUid)
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
32 if (CheckIfCategoryIsRegistered(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 // Check that a device with the given (optional) UID is already registered
40 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
41 if (deviceUidValue > 0)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010042 {
Matteo Martincigh6db5f202019-09-05 12:02:04 +010043 // Check that the (optional) device is already registered
44 if (!CheckIfDeviceIsRegistered(deviceUidValue))
45 {
46 throw InvalidArgumentException(
47 boost::str(boost::format("Trying to connect a category (\"%1%\") to a device that is "
48 "not registered (UID %2%)")
49 % categoryName
50 % deviceUidValue));
51 }
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010052 }
Matteo Martincigh6db5f202019-09-05 12:02:04 +010053
54 // Check that a counter set with the given (optional) UID is already registered
55 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
56 if (counterSetUidValue > 0)
57 {
58 // Check that the (optional) counter set is already registered
59 if (!CheckIfCounterSetIsRegistered(counterSetUidValue))
60 {
61 throw InvalidArgumentException(
62 boost::str(boost::format("Trying to connect a category (name: \"%1%\") to a counter set "
63 "that is not registered (UID: %2%)")
64 % categoryName
65 % counterSetUidValue));
66 }
67 }
68
69 // Create the category
70 CategoryPtr category = std::make_unique<Category>(categoryName, deviceUidValue, counterSetUidValue);
71 BOOST_ASSERT(category);
72
73 // Get the raw category pointer
74 const Category* categoryPtr = category.get();
75 BOOST_ASSERT(categoryPtr);
76
77 // Register the category
78 m_Categories.insert(std::move(category));
79
80 return categoryPtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010081}
82
Matteo Martincigh6db5f202019-09-05 12:02:04 +010083const Device* CounterDirectory::RegisterDevice(const std::string& deviceName,
84 uint16_t cores,
85 const Optional<std::string>& parentCategoryName)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +010086{
Matteo Martincigh6db5f202019-09-05 12:02:04 +010087 // Check that the given device name is valid
88 if (deviceName.empty() ||
89 !IsValidSwTraceString<SwTraceCharPolicy>(deviceName))
90 {
91 throw InvalidArgumentException("Trying to register a device with an invalid name");
92 }
93
94 // Check that a device with the given name is not already registered
95 if (CheckIfDeviceIsRegistered(deviceName))
96 {
97 throw InvalidArgumentException(
98 boost::str(boost::format("Trying to register a device already registered (\"%1%\")")
99 % deviceName));
100 }
101
102 // 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
103 // the registration fails. We'll get a proper one once we're sure that the device can be registered
104 uint16_t deviceUidPeek = GetNextUid(true);
105
106 // Check that a category with the given (optional) parent category name is already registered
107 Category* parentCategoryPtr = nullptr;
108 if (parentCategoryName.has_value())
109 {
110 // Get the (optional) parent category name
111 const std::string& parentCategoryNameValue = parentCategoryName.value();
112 if (parentCategoryNameValue.empty())
113 {
114 throw InvalidArgumentException(
115 boost::str(boost::format("Trying to connect a device (name: \"%1%\") to an invalid "
116 "parent category (name: \"%2%\")")
117 % deviceName
118 % parentCategoryNameValue));
119 }
120
121 // Check that the given parent category is already registered
122 auto categoryIt = FindCategory(parentCategoryNameValue);
123 if (categoryIt == m_Categories.end())
124 {
125 throw InvalidArgumentException(
126 boost::str(boost::format("Trying to connect a device (name: \"%1%\") to a parent category that "
127 "is not registered (name: \"%2%\")")
128 % deviceName
129 % parentCategoryNameValue));
130 }
131
132 // Get the parent category
133 const CategoryPtr& parentCategory = *categoryIt;
134 BOOST_ASSERT(parentCategory);
135
136 // Check that the given parent category is not already connected to another device
137 if (parentCategory->m_DeviceUid != 0 && parentCategory->m_DeviceUid != deviceUidPeek)
138 {
139 throw InvalidArgumentException(
140 boost::str(boost::format("Trying to connect a device (UID: %1%) to a parent category that is "
141 "already connected to a different device "
142 "(category \"%2%\" connected to device %3%)")
143 % deviceUidPeek
144 % parentCategoryNameValue
145 % parentCategory->m_DeviceUid));
146 }
147
148 // The parent category can be associated to the device that is about to be registered.
149 // Get the raw pointer to the parent category (to be used later when the device is actually been
150 // registered, to make sure that the category is associated to an existing device)
151 parentCategoryPtr = parentCategory.get();
152 }
153
154 // Get the device UID
155 uint16_t deviceUid = GetNextUid();
156 BOOST_ASSERT(deviceUid == deviceUidPeek);
157
158 // Create the device
159 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
160 BOOST_ASSERT(device);
161
162 // Get the raw device pointer
163 const Device* devicePtr = device.get();
164 BOOST_ASSERT(devicePtr);
165
166 // Register the device
167 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
168
169 // Connect the device to the parent category, if required
170 if (parentCategoryPtr)
171 {
172 // Set the device UID in the parent category
173 parentCategoryPtr->m_DeviceUid = deviceUid;
174 }
175
176 return devicePtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100177}
178
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100179const CounterSet* CounterDirectory::RegisterCounterSet(const std::string& counterSetName,
180 uint16_t count,
181 const Optional<std::string>& parentCategoryName)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100182{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100183 // Check that the given counter set name is valid
184 if (counterSetName.empty() ||
185 !IsValidSwTraceString<SwTraceNameCharPolicy>(counterSetName))
186 {
187 throw InvalidArgumentException("Trying to register a counter set with an invalid name");
188 }
189
190 // Check that a counter set with the given name is not already registered
191 if (CheckIfCounterSetIsRegistered(counterSetName))
192 {
193 throw InvalidArgumentException(
194 boost::str(boost::format("Trying to register a counter set already registered (\"%1%\")")
195 % counterSetName));
196 }
197
198 // 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
199 // the registration fails. We'll get a proper one once we're sure that the counter set can be registered
200 uint16_t counterSetUidPeek = GetNextUid(true);
201
202 // Check that a category with the given (optional) parent category name is already registered
203 Category* parentCategoryPtr = nullptr;
204 if (parentCategoryName.has_value())
205 {
206 // Get the (optional) parent category name
207 const std::string& parentCategoryNameValue = parentCategoryName.value();
208 if (parentCategoryNameValue.empty())
209 {
210 throw InvalidArgumentException(
211 boost::str(boost::format("Trying to connect a counter set (UID: %1%) to an invalid "
212 "parent category (name: \"%2%\")")
213 % counterSetUidPeek
214 % parentCategoryNameValue));
215 }
216
217 // Check that the given parent category is already registered
218 auto it = FindCategory(parentCategoryNameValue);
219 if (it == m_Categories.end())
220 {
221 throw InvalidArgumentException(
222 boost::str(boost::format("Trying to connect a counter set (UID: %1%) to a parent category "
223 "that is not registered (name: \"%2%\")")
224 % counterSetUidPeek
225 % parentCategoryNameValue));
226 }
227
228 // Get the parent category
229 const CategoryPtr& parentCategory = *it;
230 BOOST_ASSERT(parentCategory);
231
232 // Check that the given parent category is not already connected to another counter set
233 if (parentCategory->m_CounterSetUid != 0 && parentCategory->m_CounterSetUid != counterSetUidPeek)
234 {
235 throw InvalidArgumentException(
236 boost::str(boost::format("Trying to connect a counter set (UID: %1%) to a parent category "
237 "that is already connected to a different counter set "
238 "(category \"%2%\" connected to counter set %3%)")
239 % counterSetUidPeek
240 % parentCategoryNameValue
241 % parentCategory->m_CounterSetUid));
242 }
243
244 // The parent category can be associated to the counter set that is about to be registered.
245 // Get the raw pointer to the parent category (to be used later when the counter set is actually been
246 // registered, to make sure that the category is associated to an existing counter set)
247 parentCategoryPtr = parentCategory.get();
248 }
249
250 // Get the counter set UID
251 uint16_t counterSetUid = GetNextUid();
252 BOOST_ASSERT(counterSetUid == counterSetUidPeek);
253
254 // Create the counter set
255 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
256 BOOST_ASSERT(counterSet);
257
258 // Get the raw counter set pointer
259 const CounterSet* counterSetPtr = counterSet.get();
260 BOOST_ASSERT(counterSetPtr);
261
262 // Register the counter set
263 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
264
265 // Connect the counter set to the parent category, if required
266 if (parentCategoryPtr)
267 {
268 // Set the counter set UID in the parent category
269 parentCategoryPtr->m_CounterSetUid = counterSetUid;
270 }
271
272 return counterSetPtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100273}
274
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100275const Counter* CounterDirectory::RegisterCounter(const std::string& parentCategoryName,
276 uint16_t counterClass,
277 uint16_t interpolation,
278 double multiplier,
279 const std::string& name,
280 const std::string& description,
281 const Optional<std::string>& units,
282 const Optional<uint16_t>& numberOfCores,
283 const Optional<uint16_t>& deviceUid,
284 const Optional<uint16_t>& counterSetUid)
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100285{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100286 // Check that the given parent category name is valid
287 if (parentCategoryName.empty() ||
288 !IsValidSwTraceString<SwTraceNameCharPolicy>(parentCategoryName))
289 {
290 throw InvalidArgumentException("Trying to register a counter with an invalid parent category name");
291 }
292
293 // Check that the given class is valid
294 if (counterClass != 0 && counterClass != 1)
295 {
296 throw InvalidArgumentException("Trying to register a counter with an invalid class");
297 }
298
299 // Check that the given interpolation is valid
300 if (interpolation != 0 && interpolation != 1)
301 {
302 throw InvalidArgumentException("Trying to register a counter with an invalid interpolation");
303 }
304
305 // Check that the given multiplier is valid
306 if (multiplier == .0f)
307 {
308 throw InvalidArgumentException("Trying to register a counter with an invalid multiplier");
309 }
310
311 // Check that the given name is valid
312 if (name.empty() ||
313 !IsValidSwTraceString<SwTraceCharPolicy>(name))
314 {
315 throw InvalidArgumentException("Trying to register a counter with an invalid name");
316 }
317
318 // Check that the given description is valid
319 if (description.empty() ||
320 !IsValidSwTraceString<SwTraceCharPolicy>(description))
321 {
322 throw InvalidArgumentException("Trying to register a counter with an invalid description");
323 }
324
325 // Check that the given units are valid
326 if (units.has_value()
327 && !IsValidSwTraceString<SwTraceNameCharPolicy>(units.value()))
328 {
329 throw InvalidArgumentException("Trying to register a counter with a invalid units");
330 }
331
332 // Check that the given parent category is registered
333 auto categoryIt = FindCategory(parentCategoryName);
334 if (categoryIt == m_Categories.end())
335 {
336 throw InvalidArgumentException(
337 boost::str(boost::format("Trying to connect a counter to a category "
338 "that is not registered (name: \"%1%\")")
339 % parentCategoryName));
340 }
341
342 // Get the parent category
343 const CategoryPtr& parentCategory = *categoryIt;
344 BOOST_ASSERT(parentCategory);
345
346 // Check that a counter with the given name is not already registered within the parent category
347 const std::vector<uint16_t>& parentCategoryCounters = parentCategory->m_Counters;
348 for (uint16_t parentCategoryCounterUid : parentCategoryCounters)
349 {
350 const Counter* parentCategoryCounter = GetCounter(parentCategoryCounterUid);
351 BOOST_ASSERT(parentCategoryCounter);
352
353 if (parentCategoryCounter->m_Name == name)
354 {
355 throw InvalidArgumentException(
356 boost::str(boost::format("Trying to register a counter to category \"%1%\" with a name that "
357 "is already used within that category (name: \"%2%\")")
358 % parentCategoryName
359 % name));
360 }
361 }
362
363 // Check that a counter set with the given (optional) UID is already registered
364 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
365 if (counterSetUidValue > 0)
366 {
367 // Check that the (optional) counter set is already registered
368 if (!CheckIfCounterSetIsRegistered(counterSetUidValue))
369 {
370 throw InvalidArgumentException(
371 boost::str(boost::format("Trying to connect a counter to a counter set that is "
372 "not registered (counter set UID: %1%)")
373 % counterSetUidValue));
374 }
375 }
376
377 // Get the number of cores (this call may throw)
378 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
379 uint16_t deviceCores = GetNumberOfCores(numberOfCores, deviceUidValue, parentCategory);
380
381 // Get the counter UIDs and calculate the max counter UID
382 std::vector<uint16_t> counterUids = GetNextCounterUids(deviceCores);
383 BOOST_ASSERT(!counterUids.empty());
384 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
385
386 // Get the counter units
387 const std::string unitsValue = units.has_value() ? units.value() : "";
388
389 // Create the counter
390 CounterPtr counter = std::make_shared<Counter>(counterUids.front(),
391 maxCounterUid,
392 counterClass,
393 interpolation,
394 multiplier,
395 name,
396 description,
397 unitsValue,
398 deviceUidValue,
399 counterSetUidValue);
400 BOOST_ASSERT(counter);
401
402 // Get the raw counter pointer
403 const Counter* counterPtr = counter.get();
404 BOOST_ASSERT(counterPtr);
405
406 // Process multiple counters if necessary
407 for (uint16_t counterUid : counterUids)
408 {
409 // Connect the counter to the parent category
410 parentCategory->m_Counters.push_back(counterUid);
411
412 // Register the counter
413 m_Counters.insert(std::make_pair(counterUid, counter));
414 }
415
416 return counterPtr;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100417}
418
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100419const Category* CounterDirectory::GetCategory(const std::string& categoryName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100420{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100421 auto it = FindCategory(categoryName);
422 if (it == m_Categories.end())
423 {
424 return nullptr;
425 }
426
427 const Category* category = it->get();
428 BOOST_ASSERT(category);
429
430 return category;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100431}
432
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100433const Device* CounterDirectory::GetDevice(uint16_t deviceUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100434{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100435 auto it = FindDevice(deviceUid);
436 if (it == m_Devices.end())
437 {
438 return nullptr;
439 }
440
441 const Device* device = it->second.get();
442 BOOST_ASSERT(device);
443 BOOST_ASSERT(device->m_Uid == deviceUid);
444
445 return device;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100446}
447
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100448const CounterSet* CounterDirectory::GetCounterSet(uint16_t counterSetUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100449{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100450 auto it = FindCounterSet(counterSetUid);
451 if (it == m_CounterSets.end())
452 {
453 return nullptr;
454 }
455
456 const CounterSet* counterSet = it->second.get();
457 BOOST_ASSERT(counterSet);
458 BOOST_ASSERT(counterSet->m_Uid == counterSetUid);
459
460 return counterSet;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100461}
462
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100463const Counter* CounterDirectory::GetCounter(uint16_t counterUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100464{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100465 auto it = FindCounter(counterUid);
466 if (it == m_Counters.end())
467 {
468 return nullptr;
469 }
470
471 const Counter* counter = it->second.get();
472 BOOST_ASSERT(counter);
473 BOOST_ASSERT(counter->m_Uid <= counterUid);
474 BOOST_ASSERT(counter->m_Uid <= counter->m_MaxCounterUid);
475
476 return counter;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100477}
478
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100479CategoriesIt CounterDirectory::FindCategory(const std::string& categoryName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100480{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100481 return std::find_if(m_Categories.begin(), m_Categories.end(), [&categoryName](const CategoryPtr& category)
482 {
483 BOOST_ASSERT(category);
484
485 return category->m_Name == categoryName;
486 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100487}
488
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100489DevicesIt CounterDirectory::FindDevice(uint16_t deviceUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100490{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100491 return m_Devices.find(deviceUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100492}
493
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100494DevicesIt CounterDirectory::FindDevice(const std::string& deviceName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100495{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100496 return std::find_if(m_Devices.begin(), m_Devices.end(), [&deviceName](const auto& pair)
497 {
498 BOOST_ASSERT(pair.second);
499 BOOST_ASSERT(pair.second->m_Uid == pair.first);
500
501 return pair.second->m_Name == deviceName;
502 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100503}
504
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100505CounterSetsIt CounterDirectory::FindCounterSet(uint16_t counterSetUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100506{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100507 return m_CounterSets.find(counterSetUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100508}
509
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100510CounterSetsIt CounterDirectory::FindCounterSet(const std::string& counterSetName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100511{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100512 return std::find_if(m_CounterSets.begin(), m_CounterSets.end(), [&counterSetName](const auto& pair)
513 {
514 BOOST_ASSERT(pair.second);
515 BOOST_ASSERT(pair.second->m_Uid == pair.first);
516
517 return pair.second->m_Name == counterSetName;
518 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100519}
520
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100521CountersIt CounterDirectory::FindCounter(uint16_t counterUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100522{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100523 return m_Counters.find(counterUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100524}
525
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100526bool CounterDirectory::CheckIfCategoryIsRegistered(const std::string& categoryName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100527{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100528 auto it = FindCategory(categoryName);
529
530 return it != m_Categories.end();
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100531}
532
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100533bool CounterDirectory::CheckIfDeviceIsRegistered(uint16_t deviceUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100534{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100535 auto it = FindDevice(deviceUid);
536
537 return it != m_Devices.end();
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100538}
539
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100540bool CounterDirectory::CheckIfDeviceIsRegistered(const std::string& deviceName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100541{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100542 auto it = FindDevice(deviceName);
543
544 return it != m_Devices.end();
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100545}
546
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100547bool CounterDirectory::CheckIfCounterSetIsRegistered(uint16_t counterSetUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100548{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100549 auto it = FindCounterSet(counterSetUid);
550
551 return it != m_CounterSets.end();
552}
553
554bool CounterDirectory::CheckIfCounterSetIsRegistered(const std::string& counterSetName) const
555{
556 auto it = FindCounterSet(counterSetName);
557
558 return it != m_CounterSets.end();
559}
560
561uint16_t CounterDirectory::GetNumberOfCores(const Optional<uint16_t>& numberOfCores,
562 uint16_t deviceUid,
563 const CategoryPtr& parentCategory)
564{
565 BOOST_ASSERT(parentCategory);
566
567 // To get the number of cores, apply the following rules:
568 //
569 // 1. If numberOfCores is set then take it as the deviceCores value
570 // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device,
571 // if so then that devices number of cores is taken as the deviceCores value
572 // 3. If neither of the above is set then look at the category to see if it has a device associated with it,
573 // if it does then take that device's numberOfCores as the deviceCores value
574 // 4. If none of the above holds then set deviceCores to zero
575
576 // 1. If numberOfCores is set then take it as the deviceCores value
577 if (numberOfCores.has_value())
578 {
579 // Get the number of cores
580 return numberOfCores.value();
581 }
582
583 // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device,
584 // if so then that devices number of cores is taken as the deviceCores value
585 if (deviceUid > 0)
586 {
587 // Check that the (optional) device is already registered
588 auto deviceIt = FindDevice(deviceUid);
589 if (deviceIt == m_Devices.end())
590 {
591 throw InvalidArgumentException(
592 boost::str(boost::format("Trying to connect a counter to a device that is "
593 "not registered (device UID %1%)")
594 % deviceUid));
595 }
596
597 // Get the associated device
598 const DevicePtr& device = deviceIt->second;
599 BOOST_ASSERT(device);
600
601 // Get the number of cores of the associated device
602 return device->m_Cores;
603 }
604
605 // 3. If neither of the above is set then look at the category to see if it has a device associated with it,
606 // if it does then take that device's numberOfCores as the deviceCores value
607 uint16_t parentCategoryDeviceUid = parentCategory->m_DeviceUid;
608 if (parentCategoryDeviceUid > 0)
609 {
610 // Check that the device associated to the parent category is already registered
611 auto deviceIt = FindDevice(parentCategoryDeviceUid);
612 if (deviceIt == m_Devices.end())
613 {
614 throw InvalidArgumentException(
615 boost::str(boost::format("Trying to get the number of cores from a device that is "
616 "not registered (device UID %1%)")
617 % parentCategoryDeviceUid));
618 }
619
620 // Get the associated device
621 const DevicePtr& device = deviceIt->second;
622 BOOST_ASSERT(device);
623
624 // Get the number of cores of the device associated to the parent category
625 return device->m_Cores;
626 }
627
628 // 4. If none of the above holds then set deviceCores to zero
629 return 0;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100630}
631
632} // namespace profiling
633
634} // namespace armnn