IVGCVSW-4391 Add Global/Backend Counter ID map
authorDavid Monahan <david.monahan@arm.com>
Thu, 30 Jan 2020 12:44:23 +0000 (12:44 +0000)
committerDavid Monahan <david.monahan@arm.com>
Thu, 30 Jan 2020 12:58:58 +0000 (12:58 +0000)
Signed-off-by: David Monahan <david.monahan@arm.com>
Change-Id: I943e02bc2026564e3a19d03e81ba75850d204497

CMakeLists.txt
src/profiling/CounterIdMap.cpp [new file with mode: 0644]
src/profiling/CounterIdMap.hpp [new file with mode: 0644]
src/profiling/test/ProfilingTests.cpp

index b1af90d..c811ee5 100644 (file)
@@ -459,6 +459,8 @@ list(APPEND armnn_sources
     src/profiling/ConnectionAcknowledgedCommandHandler.hpp
     src/profiling/CounterDirectory.cpp
     src/profiling/CounterDirectory.hpp
+    src/profiling/CounterIdMap.cpp
+    src/profiling/CounterIdMap.hpp
     src/profiling/DirectoryCaptureCommandHandler.cpp
     src/profiling/DirectoryCaptureCommandHandler.hpp
     src/profiling/EncodeVersion.hpp
diff --git a/src/profiling/CounterIdMap.cpp b/src/profiling/CounterIdMap.cpp
new file mode 100644 (file)
index 0000000..a925fb9
--- /dev/null
@@ -0,0 +1,50 @@
+//
+// Copyright © 2020 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+#include "CounterIdMap.hpp"
+#include "armnn/BackendId.hpp"
+#include <map>
+#include <armnn/Exceptions.hpp>
+
+namespace armnn
+{
+namespace profiling
+{
+
+void CounterIdMap::RegisterMapping(uint16_t globalCounterId,
+                                   uint16_t backendCounterId,
+                                   const armnn::BackendId& backendId)
+{
+    std::pair<uint16_t, armnn::BackendId> backendIdPair(backendCounterId, backendId);
+    m_GlobalCounterIdMap[globalCounterId] = backendIdPair;
+    m_BackendCounterIdMap[backendIdPair] = globalCounterId;
+}
+
+uint16_t CounterIdMap::GetGlobalId(uint16_t backendCounterId, const armnn::BackendId& backendId)
+{
+    std::pair<uint16_t, armnn::BackendId> backendIdPair(backendCounterId, backendId);
+    auto it = m_BackendCounterIdMap.find(backendIdPair);
+    if (it == m_BackendCounterIdMap.end())
+    {
+        std::stringstream ss;
+        ss << "No Backend Counter [" << backendIdPair.second << ":" << backendIdPair.first << "] registered";
+        throw armnn::Exception(ss.str());
+    }
+    return it->second;
+}
+
+const std::pair<uint16_t, armnn::BackendId>& CounterIdMap::GetBackendId(uint16_t globalCounterId)
+{
+    auto it = m_GlobalCounterIdMap.find(globalCounterId);
+    if (it == m_GlobalCounterIdMap.end())
+    {
+        std::stringstream ss;
+        ss << "No Global Counter ID [" << globalCounterId << "] registered";
+        throw armnn::Exception(ss.str());
+    }
+    return it->second;
+}
+
+}    // namespace profiling
+}    // namespace armnn
\ No newline at end of file
diff --git a/src/profiling/CounterIdMap.hpp b/src/profiling/CounterIdMap.hpp
new file mode 100644 (file)
index 0000000..e401491
--- /dev/null
@@ -0,0 +1,28 @@
+//
+// Copyright © 2020 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+#pragma once
+
+#include "armnn/BackendId.hpp"
+#include <map>
+
+namespace armnn
+{
+namespace profiling
+{
+
+class CounterIdMap
+{
+
+public:
+    void RegisterMapping(uint16_t globalCounterId, uint16_t backendCounterId, const armnn::BackendId& backendId);
+    uint16_t GetGlobalId(uint16_t backendCounterId, const armnn::BackendId& backendId);
+    const std::pair<uint16_t, armnn::BackendId>& GetBackendId(uint16_t globalCounterId);
+private:
+    std::map<uint16_t, std::pair<uint16_t, armnn::BackendId>> m_GlobalCounterIdMap;
+    std::map<std::pair<uint16_t, armnn::BackendId>, uint16_t> m_BackendCounterIdMap;
+};
+
+}    // namespace profiling
+}    // namespace armnn
\ No newline at end of file
index 13863b6..e127a18 100644 (file)
@@ -10,6 +10,7 @@
 #include <CommandHandlerRegistry.hpp>
 #include <ConnectionAcknowledgedCommandHandler.hpp>
 #include <CounterDirectory.hpp>
+#include <CounterIdMap.hpp>
 #include <EncodeVersion.hpp>
 #include <Holder.hpp>
 #include <ICounterValues.hpp>
@@ -3167,5 +3168,44 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket)
         BOOST_FAIL("Expected string not found.");
     }
 }
+BOOST_AUTO_TEST_CASE(CheckCounterIdMap)
+{
+    CounterIdMap counterIdMap;
+    BOOST_CHECK_THROW(counterIdMap.GetBackendId(0), armnn::Exception);
+    BOOST_CHECK_THROW(counterIdMap.GetGlobalId(0, armnn::profiling::BACKEND_ID), armnn::Exception);
+
+    uint16_t globalCounterIds = 0;
+
+    armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
+    armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
+
+    std::vector<uint16_t> cpuRefCounters = {0, 1, 2, 3};
+    std::vector<uint16_t> cpuAccCounters = {0, 1};
+
+    for (uint16_t backendCounterId : cpuRefCounters)
+    {
+        counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuRefId);
+        ++globalCounterIds;
+    }
+    for (uint16_t backendCounterId : cpuAccCounters)
+    {
+        counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuAccId);
+        ++globalCounterIds;
+    }
+
+    BOOST_CHECK(counterIdMap.GetBackendId(0) == (std::pair<uint16_t, armnn::BackendId>(0, cpuRefId)));
+    BOOST_CHECK(counterIdMap.GetBackendId(1) == (std::pair<uint16_t, armnn::BackendId>(1, cpuRefId)));
+    BOOST_CHECK(counterIdMap.GetBackendId(2) == (std::pair<uint16_t, armnn::BackendId>(2, cpuRefId)));
+    BOOST_CHECK(counterIdMap.GetBackendId(3) == (std::pair<uint16_t, armnn::BackendId>(3, cpuRefId)));
+    BOOST_CHECK(counterIdMap.GetBackendId(4) == (std::pair<uint16_t, armnn::BackendId>(0, cpuAccId)));
+    BOOST_CHECK(counterIdMap.GetBackendId(5) == (std::pair<uint16_t, armnn::BackendId>(1, cpuAccId)));
+
+    BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuRefId) == 0);
+    BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuRefId) == 1);
+    BOOST_CHECK(counterIdMap.GetGlobalId(2, cpuRefId) == 2);
+    BOOST_CHECK(counterIdMap.GetGlobalId(3, cpuRefId) == 3);
+    BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuAccId) == 4);
+    BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuAccId) == 5);
+}
 
 BOOST_AUTO_TEST_SUITE_END()