blob: 979b8046be663cf28078fd48793df8317a7fdefc [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
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 // 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
Matteo Martincigha84edee2019-10-02 12:50:57 +010044 if (!IsDeviceRegistered(deviceUidValue))
Matteo Martincigh6db5f202019-09-05 12:02:04 +010045 {
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
Matteo Martincigha84edee2019-10-02 12:50:57 +010059 if (!IsCounterSetRegistered(counterSetUidValue))
Matteo Martincigh6db5f202019-09-05 12:02:04 +010060 {
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
Matteo Martincigha84edee2019-10-02 12:50:57 +010095 if (IsDeviceRegistered(deviceName))
Matteo Martincigh6db5f202019-09-05 12:02:04 +010096 {
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
Matteo Martincigha84edee2019-10-02 12:50:57 +0100191 if (IsCounterSetRegistered(counterSetName))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100192 {
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
Matteo Martincigha84edee2019-10-02 12:50:57 +0100368 if (!IsCounterSetRegistered(counterSetUidValue))
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100369 {
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 Martincigha84edee2019-10-02 12:50:57 +0100479bool CounterDirectory::IsCategoryRegistered(const std::string& categoryName) const
480{
481 auto it = FindCategory(categoryName);
482
483 return it != m_Categories.end();
484}
485
486bool CounterDirectory::IsDeviceRegistered(uint16_t deviceUid) const
487{
488 auto it = FindDevice(deviceUid);
489
490 return it != m_Devices.end();
491}
492
493bool CounterDirectory::IsDeviceRegistered(const std::string& deviceName) const
494{
495 auto it = FindDevice(deviceName);
496
497 return it != m_Devices.end();
498}
499
500bool CounterDirectory::IsCounterSetRegistered(uint16_t counterSetUid) const
501{
502 auto it = FindCounterSet(counterSetUid);
503
504 return it != m_CounterSets.end();
505}
506
507bool CounterDirectory::IsCounterSetRegistered(const std::string& counterSetName) const
508{
509 auto it = FindCounterSet(counterSetName);
510
511 return it != m_CounterSets.end();
512}
513
514bool CounterDirectory::IsCounterRegistered(uint16_t counterUid) const
515{
516 auto it = FindCounter(counterUid);
517
518 return it != m_Counters.end();
519}
520
521bool CounterDirectory::IsCounterRegistered(const std::string& counterName) const
522{
523 auto it = FindCounter(counterName);
524
525 return it != m_Counters.end();
526}
527
528void CounterDirectory::Clear()
529{
530 // Clear all the counter directory contents
531 m_Categories.clear();
532 m_Devices.clear();
533 m_CounterSets.clear();
534 m_Counters.clear();
535}
536
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100537CategoriesIt CounterDirectory::FindCategory(const std::string& categoryName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100538{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100539 return std::find_if(m_Categories.begin(), m_Categories.end(), [&categoryName](const CategoryPtr& category)
540 {
541 BOOST_ASSERT(category);
542
543 return category->m_Name == categoryName;
544 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100545}
546
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100547DevicesIt CounterDirectory::FindDevice(uint16_t deviceUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100548{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100549 return m_Devices.find(deviceUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100550}
551
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100552DevicesIt CounterDirectory::FindDevice(const std::string& deviceName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100553{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100554 return std::find_if(m_Devices.begin(), m_Devices.end(), [&deviceName](const auto& pair)
555 {
556 BOOST_ASSERT(pair.second);
557 BOOST_ASSERT(pair.second->m_Uid == pair.first);
558
559 return pair.second->m_Name == deviceName;
560 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100561}
562
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100563CounterSetsIt CounterDirectory::FindCounterSet(uint16_t counterSetUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100564{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100565 return m_CounterSets.find(counterSetUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100566}
567
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100568CounterSetsIt CounterDirectory::FindCounterSet(const std::string& counterSetName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100569{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100570 return std::find_if(m_CounterSets.begin(), m_CounterSets.end(), [&counterSetName](const auto& pair)
571 {
572 BOOST_ASSERT(pair.second);
573 BOOST_ASSERT(pair.second->m_Uid == pair.first);
574
575 return pair.second->m_Name == counterSetName;
576 });
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100577}
578
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100579CountersIt CounterDirectory::FindCounter(uint16_t counterUid) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100580{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100581 return m_Counters.find(counterUid);
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100582}
583
Matteo Martincigha84edee2019-10-02 12:50:57 +0100584CountersIt CounterDirectory::FindCounter(const std::string& counterName) const
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100585{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100586 return std::find_if(m_Counters.begin(), m_Counters.end(), [&counterName](const auto& pair)
587 {
588 BOOST_ASSERT(pair.second);
589 BOOST_ASSERT(pair.second->m_Uid == pair.first);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100590
Matteo Martincigha84edee2019-10-02 12:50:57 +0100591 return pair.second->m_Name == counterName;
592 });
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100593}
594
595uint16_t CounterDirectory::GetNumberOfCores(const Optional<uint16_t>& numberOfCores,
596 uint16_t deviceUid,
597 const CategoryPtr& parentCategory)
598{
599 BOOST_ASSERT(parentCategory);
600
601 // To get the number of cores, apply the following rules:
602 //
603 // 1. If numberOfCores is set then take it as the deviceCores value
604 // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device,
605 // if so then that devices number of cores is taken as the deviceCores value
606 // 3. If neither of the above is set then look at the category to see if it has a device associated with it,
607 // if it does then take that device's numberOfCores as the deviceCores value
608 // 4. If none of the above holds then set deviceCores to zero
609
610 // 1. If numberOfCores is set then take it as the deviceCores value
611 if (numberOfCores.has_value())
612 {
613 // Get the number of cores
614 return numberOfCores.value();
615 }
616
617 // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device,
618 // if so then that devices number of cores is taken as the deviceCores value
619 if (deviceUid > 0)
620 {
621 // Check that the (optional) device is already registered
622 auto deviceIt = FindDevice(deviceUid);
623 if (deviceIt == m_Devices.end())
624 {
625 throw InvalidArgumentException(
626 boost::str(boost::format("Trying to connect a counter to a device that is "
627 "not registered (device UID %1%)")
628 % deviceUid));
629 }
630
631 // Get the associated device
632 const DevicePtr& device = deviceIt->second;
633 BOOST_ASSERT(device);
634
635 // Get the number of cores of the associated device
636 return device->m_Cores;
637 }
638
639 // 3. If neither of the above is set then look at the category to see if it has a device associated with it,
640 // if it does then take that device's numberOfCores as the deviceCores value
641 uint16_t parentCategoryDeviceUid = parentCategory->m_DeviceUid;
642 if (parentCategoryDeviceUid > 0)
643 {
644 // Check that the device associated to the parent category is already registered
645 auto deviceIt = FindDevice(parentCategoryDeviceUid);
646 if (deviceIt == m_Devices.end())
647 {
648 throw InvalidArgumentException(
649 boost::str(boost::format("Trying to get the number of cores from a device that is "
650 "not registered (device UID %1%)")
651 % parentCategoryDeviceUid));
652 }
653
654 // Get the associated device
655 const DevicePtr& device = deviceIt->second;
656 BOOST_ASSERT(device);
657
658 // Get the number of cores of the device associated to the parent category
659 return device->m_Cores;
660 }
661
662 // 4. If none of the above holds then set deviceCores to zero
663 return 0;
Aron Virginas-Tar4e5fc1f2019-08-22 18:10:52 +0100664}
665
666} // namespace profiling
667
668} // namespace armnn