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> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 11 | #include <armnn/utility/Assert.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 12 | #include <armnn/utility/IgnoreUnused.hpp> |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 13 | |
| 14 | #include <boost/format.hpp> |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 15 | |
| 16 | namespace armnn |
| 17 | { |
| 18 | |
| 19 | namespace profiling |
| 20 | { |
| 21 | |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 22 | const Category* CounterDirectory::RegisterCategory(const std::string& categoryName) |
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 | // Create the category |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 40 | CategoryPtr category = std::make_unique<Category>(categoryName); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 41 | ARMNN_ASSERT(category); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 42 | |
| 43 | // Get the raw category pointer |
| 44 | const Category* categoryPtr = category.get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 45 | ARMNN_ASSERT(categoryPtr); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 46 | |
| 47 | // Register the category |
| 48 | m_Categories.insert(std::move(category)); |
| 49 | |
| 50 | return categoryPtr; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 53 | const Device* CounterDirectory::RegisterDevice(const std::string& deviceName, |
| 54 | uint16_t cores, |
| 55 | const Optional<std::string>& parentCategoryName) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 56 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 57 | // 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 Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 65 | if (IsDeviceRegistered(deviceName)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 66 | { |
| 67 | throw InvalidArgumentException( |
| 68 | boost::str(boost::format("Trying to register a device already registered (\"%1%\")") |
| 69 | % deviceName)); |
| 70 | } |
| 71 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 72 | // Check that a category with the given (optional) parent category name is already registered |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 73 | 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 Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Get the device UID |
| 99 | uint16_t deviceUid = GetNextUid(); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 100 | |
| 101 | // Create the device |
| 102 | DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 103 | ARMNN_ASSERT(device); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 104 | |
| 105 | // Get the raw device pointer |
| 106 | const Device* devicePtr = device.get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 107 | ARMNN_ASSERT(devicePtr); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 108 | |
| 109 | // Register the device |
| 110 | m_Devices.insert(std::make_pair(deviceUid, std::move(device))); |
| 111 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 112 | return devicePtr; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 115 | const CounterSet* CounterDirectory::RegisterCounterSet(const std::string& counterSetName, |
| 116 | uint16_t count, |
| 117 | const Optional<std::string>& parentCategoryName) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 118 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 119 | // 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 Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 127 | if (IsCounterSetRegistered(counterSetName)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 128 | { |
| 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 Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 139 | 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 Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Get the counter set UID |
| 165 | uint16_t counterSetUid = GetNextUid(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 166 | ARMNN_ASSERT(counterSetUid == counterSetUidPeek); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 167 | |
| 168 | // Create the counter set |
| 169 | CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 170 | ARMNN_ASSERT(counterSet); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 171 | |
| 172 | // Get the raw counter set pointer |
| 173 | const CounterSet* counterSetPtr = counterSet.get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 174 | ARMNN_ASSERT(counterSetPtr); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 175 | |
| 176 | // Register the counter set |
| 177 | m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet))); |
| 178 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 179 | return counterSetPtr; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 182 | const Counter* CounterDirectory::RegisterCounter(const BackendId& backendId, |
| 183 | const uint16_t uid, |
| 184 | const std::string& parentCategoryName, |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 185 | 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 194 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 195 | IgnoreUnused(backendId); |
Derek Lamberti | f143fba | 2020-01-02 13:50:57 +0000 | [diff] [blame] | 196 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 197 | // 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 Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 255 | ARMNN_ASSERT(parentCategory); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 256 | |
| 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 Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 262 | ARMNN_ASSERT(parentCategoryCounter); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 263 | |
| 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 Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 279 | if (!IsCounterSetRegistered(counterSetUidValue)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 280 | { |
| 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 Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 290 | uint16_t deviceCores = GetNumberOfCores(numberOfCores, deviceUidValue); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 291 | |
| 292 | // Get the counter UIDs and calculate the max counter UID |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 293 | std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 294 | ARMNN_ASSERT(!counterUids.empty()); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 295 | 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 Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 301 | CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID, |
| 302 | counterUids.front(), |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 303 | maxCounterUid, |
| 304 | counterClass, |
| 305 | interpolation, |
| 306 | multiplier, |
| 307 | name, |
| 308 | description, |
| 309 | unitsValue, |
| 310 | deviceUidValue, |
| 311 | counterSetUidValue); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 312 | ARMNN_ASSERT(counter); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 313 | |
| 314 | // Get the raw counter pointer |
| 315 | const Counter* counterPtr = counter.get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 316 | ARMNN_ASSERT(counterPtr); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 317 | |
| 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-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 331 | const Category* CounterDirectory::GetCategory(const std::string& categoryName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 332 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 333 | auto it = FindCategory(categoryName); |
| 334 | if (it == m_Categories.end()) |
| 335 | { |
| 336 | return nullptr; |
| 337 | } |
| 338 | |
| 339 | const Category* category = it->get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 340 | ARMNN_ASSERT(category); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 341 | |
| 342 | return category; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 345 | const Device* CounterDirectory::GetDevice(uint16_t deviceUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 346 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 347 | auto it = FindDevice(deviceUid); |
| 348 | if (it == m_Devices.end()) |
| 349 | { |
| 350 | return nullptr; |
| 351 | } |
| 352 | |
| 353 | const Device* device = it->second.get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 354 | ARMNN_ASSERT(device); |
| 355 | ARMNN_ASSERT(device->m_Uid == deviceUid); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 356 | |
| 357 | return device; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 360 | const CounterSet* CounterDirectory::GetCounterSet(uint16_t counterSetUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 361 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 362 | auto it = FindCounterSet(counterSetUid); |
| 363 | if (it == m_CounterSets.end()) |
| 364 | { |
| 365 | return nullptr; |
| 366 | } |
| 367 | |
| 368 | const CounterSet* counterSet = it->second.get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 369 | ARMNN_ASSERT(counterSet); |
| 370 | ARMNN_ASSERT(counterSet->m_Uid == counterSetUid); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 371 | |
| 372 | return counterSet; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 375 | const Counter* CounterDirectory::GetCounter(uint16_t counterUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 376 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 377 | auto it = FindCounter(counterUid); |
| 378 | if (it == m_Counters.end()) |
| 379 | { |
| 380 | return nullptr; |
| 381 | } |
| 382 | |
| 383 | const Counter* counter = it->second.get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 384 | ARMNN_ASSERT(counter); |
| 385 | ARMNN_ASSERT(counter->m_Uid <= counterUid); |
| 386 | ARMNN_ASSERT(counter->m_Uid <= counter->m_MaxCounterUid); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 387 | |
| 388 | return counter; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 391 | bool CounterDirectory::IsCategoryRegistered(const std::string& categoryName) const |
| 392 | { |
| 393 | auto it = FindCategory(categoryName); |
| 394 | |
| 395 | return it != m_Categories.end(); |
| 396 | } |
| 397 | |
| 398 | bool CounterDirectory::IsDeviceRegistered(uint16_t deviceUid) const |
| 399 | { |
| 400 | auto it = FindDevice(deviceUid); |
| 401 | |
| 402 | return it != m_Devices.end(); |
| 403 | } |
| 404 | |
| 405 | bool CounterDirectory::IsDeviceRegistered(const std::string& deviceName) const |
| 406 | { |
| 407 | auto it = FindDevice(deviceName); |
| 408 | |
| 409 | return it != m_Devices.end(); |
| 410 | } |
| 411 | |
| 412 | bool CounterDirectory::IsCounterSetRegistered(uint16_t counterSetUid) const |
| 413 | { |
| 414 | auto it = FindCounterSet(counterSetUid); |
| 415 | |
| 416 | return it != m_CounterSets.end(); |
| 417 | } |
| 418 | |
| 419 | bool CounterDirectory::IsCounterSetRegistered(const std::string& counterSetName) const |
| 420 | { |
| 421 | auto it = FindCounterSet(counterSetName); |
| 422 | |
| 423 | return it != m_CounterSets.end(); |
| 424 | } |
| 425 | |
| 426 | bool CounterDirectory::IsCounterRegistered(uint16_t counterUid) const |
| 427 | { |
| 428 | auto it = FindCounter(counterUid); |
| 429 | |
| 430 | return it != m_Counters.end(); |
| 431 | } |
| 432 | |
| 433 | bool CounterDirectory::IsCounterRegistered(const std::string& counterName) const |
| 434 | { |
| 435 | auto it = FindCounter(counterName); |
| 436 | |
| 437 | return it != m_Counters.end(); |
| 438 | } |
| 439 | |
| 440 | void 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 Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 449 | CategoriesIt CounterDirectory::FindCategory(const std::string& categoryName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 450 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 451 | return std::find_if(m_Categories.begin(), m_Categories.end(), [&categoryName](const CategoryPtr& category) |
| 452 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 453 | ARMNN_ASSERT(category); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 454 | |
| 455 | return category->m_Name == categoryName; |
| 456 | }); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 459 | DevicesIt CounterDirectory::FindDevice(uint16_t deviceUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 460 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 461 | return m_Devices.find(deviceUid); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 464 | DevicesIt CounterDirectory::FindDevice(const std::string& deviceName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 465 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 466 | return std::find_if(m_Devices.begin(), m_Devices.end(), [&deviceName](const auto& pair) |
| 467 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 468 | ARMNN_ASSERT(pair.second); |
| 469 | ARMNN_ASSERT(pair.second->m_Uid == pair.first); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 470 | |
| 471 | return pair.second->m_Name == deviceName; |
| 472 | }); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 473 | } |
| 474 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 475 | CounterSetsIt CounterDirectory::FindCounterSet(uint16_t counterSetUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 476 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 477 | return m_CounterSets.find(counterSetUid); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 478 | } |
| 479 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 480 | CounterSetsIt CounterDirectory::FindCounterSet(const std::string& counterSetName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 481 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 482 | return std::find_if(m_CounterSets.begin(), m_CounterSets.end(), [&counterSetName](const auto& pair) |
| 483 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 484 | ARMNN_ASSERT(pair.second); |
| 485 | ARMNN_ASSERT(pair.second->m_Uid == pair.first); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 486 | |
| 487 | return pair.second->m_Name == counterSetName; |
| 488 | }); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 491 | CountersIt CounterDirectory::FindCounter(uint16_t counterUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 492 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 493 | return m_Counters.find(counterUid); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 496 | CountersIt CounterDirectory::FindCounter(const std::string& counterName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 497 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 498 | return std::find_if(m_Counters.begin(), m_Counters.end(), [&counterName](const auto& pair) |
| 499 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 500 | ARMNN_ASSERT(pair.second); |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 501 | ARMNN_ASSERT(pair.first >= pair.second->m_Uid && pair.first <= pair.second->m_MaxCounterUid); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 502 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 503 | return pair.second->m_Name == counterName; |
| 504 | }); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | uint16_t CounterDirectory::GetNumberOfCores(const Optional<uint16_t>& numberOfCores, |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 508 | uint16_t deviceUid) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 509 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 510 | // 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 Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 515 | // 3. If none of the above holds then set deviceCores to zero |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 516 | |
| 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 Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 540 | ARMNN_ASSERT(device); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 541 | |
| 542 | // Get the number of cores of the associated device |
| 543 | return device->m_Cores; |
| 544 | } |
| 545 | |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 546 | // 3. If none of the above holds then set deviceCores to zero |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 547 | return 0; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | } // namespace profiling |
| 551 | |
| 552 | } // namespace armnn |