Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "CounterDirectory.hpp" |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 7 | #include "ProfilingUtils.hpp" |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 8 | |
| 9 | #include <armnn/Exceptions.hpp> |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 10 | #include <armnn/Conversion.hpp> |
| 11 | |
| 12 | #include <boost/format.hpp> |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 13 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | |
| 17 | namespace profiling |
| 18 | { |
| 19 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 20 | const Category* CounterDirectory::RegisterCategory(const std::string& categoryName, |
| 21 | const Optional<uint16_t>& deviceUid, |
| 22 | const Optional<uint16_t>& counterSetUid) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 23 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 24 | // Check that the given category name is valid |
| 25 | if (categoryName.empty() || |
| 26 | !IsValidSwTraceString<SwTraceNameCharPolicy>(categoryName)) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 27 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 28 | throw InvalidArgumentException("Trying to register a category with an invalid name"); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 29 | } |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 30 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 31 | // Check that the given category is not already registered |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 32 | if (IsCategoryRegistered(categoryName)) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 33 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 34 | throw InvalidArgumentException( |
| 35 | boost::str(boost::format("Trying to register a category already registered (\"%1%\")") |
| 36 | % categoryName)); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 37 | } |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 38 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 39 | // 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 42 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 43 | // Check that the (optional) device is already registered |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 44 | if (!IsDeviceRegistered(deviceUidValue)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 52 | } |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 53 | |
| 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 Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 59 | if (!IsCounterSetRegistered(counterSetUidValue)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 83 | const Device* CounterDirectory::RegisterDevice(const std::string& deviceName, |
| 84 | uint16_t cores, |
| 85 | const Optional<std::string>& parentCategoryName) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 86 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 87 | // 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 Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 95 | if (IsDeviceRegistered(deviceName)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 179 | const CounterSet* CounterDirectory::RegisterCounterSet(const std::string& counterSetName, |
| 180 | uint16_t count, |
| 181 | const Optional<std::string>& parentCategoryName) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 182 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 183 | // 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 Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 191 | if (IsCounterSetRegistered(counterSetName)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 275 | const 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 285 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 286 | // 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 Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 368 | if (!IsCounterSetRegistered(counterSetUidValue)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 417 | } |
| 418 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 419 | const Category* CounterDirectory::GetCategory(const std::string& categoryName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 420 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 421 | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 433 | const Device* CounterDirectory::GetDevice(uint16_t deviceUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 434 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 435 | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 448 | const CounterSet* CounterDirectory::GetCounterSet(uint16_t counterSetUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 449 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 450 | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 463 | const Counter* CounterDirectory::GetCounter(uint16_t counterUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 464 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 465 | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 479 | bool CounterDirectory::IsCategoryRegistered(const std::string& categoryName) const |
| 480 | { |
| 481 | auto it = FindCategory(categoryName); |
| 482 | |
| 483 | return it != m_Categories.end(); |
| 484 | } |
| 485 | |
| 486 | bool CounterDirectory::IsDeviceRegistered(uint16_t deviceUid) const |
| 487 | { |
| 488 | auto it = FindDevice(deviceUid); |
| 489 | |
| 490 | return it != m_Devices.end(); |
| 491 | } |
| 492 | |
| 493 | bool CounterDirectory::IsDeviceRegistered(const std::string& deviceName) const |
| 494 | { |
| 495 | auto it = FindDevice(deviceName); |
| 496 | |
| 497 | return it != m_Devices.end(); |
| 498 | } |
| 499 | |
| 500 | bool CounterDirectory::IsCounterSetRegistered(uint16_t counterSetUid) const |
| 501 | { |
| 502 | auto it = FindCounterSet(counterSetUid); |
| 503 | |
| 504 | return it != m_CounterSets.end(); |
| 505 | } |
| 506 | |
| 507 | bool CounterDirectory::IsCounterSetRegistered(const std::string& counterSetName) const |
| 508 | { |
| 509 | auto it = FindCounterSet(counterSetName); |
| 510 | |
| 511 | return it != m_CounterSets.end(); |
| 512 | } |
| 513 | |
| 514 | bool CounterDirectory::IsCounterRegistered(uint16_t counterUid) const |
| 515 | { |
| 516 | auto it = FindCounter(counterUid); |
| 517 | |
| 518 | return it != m_Counters.end(); |
| 519 | } |
| 520 | |
| 521 | bool CounterDirectory::IsCounterRegistered(const std::string& counterName) const |
| 522 | { |
| 523 | auto it = FindCounter(counterName); |
| 524 | |
| 525 | return it != m_Counters.end(); |
| 526 | } |
| 527 | |
| 528 | void 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 Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 537 | CategoriesIt CounterDirectory::FindCategory(const std::string& categoryName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 538 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 539 | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 547 | DevicesIt CounterDirectory::FindDevice(uint16_t deviceUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 548 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 549 | return m_Devices.find(deviceUid); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 550 | } |
| 551 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 552 | DevicesIt CounterDirectory::FindDevice(const std::string& deviceName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 553 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 554 | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 563 | CounterSetsIt CounterDirectory::FindCounterSet(uint16_t counterSetUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 564 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 565 | return m_CounterSets.find(counterSetUid); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 568 | CounterSetsIt CounterDirectory::FindCounterSet(const std::string& counterSetName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 569 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 570 | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 577 | } |
| 578 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 579 | CountersIt CounterDirectory::FindCounter(uint16_t counterUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 580 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 581 | return m_Counters.find(counterUid); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 584 | CountersIt CounterDirectory::FindCounter(const std::string& counterName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 585 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 586 | 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 Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 590 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 591 | return pair.second->m_Name == counterName; |
| 592 | }); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | uint16_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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | } // namespace profiling |
| 667 | |
| 668 | } // namespace armnn |