From e898db9aaf07b4d0ea0242a1f3296f0192c42939 Mon Sep 17 00:00:00 2001 From: Aron Virginas-Tar Date: Thu, 22 Aug 2019 12:56:34 +0100 Subject: [PATCH] IVGCVSW-3427 Create PacketVersionResolver class * Create first version of PacketVersionResolver class * Add basic unit test * Move existing classes inside the armnn::profiling namespace * Add utility methods for Version Signed-off-by: Aron Virginas-Tar Change-Id: If0ea0e1b9dea7fbfcd8b808e97b1e2aa91964dfa --- CMakeLists.txt | 2 ++ src/profiling/CommandHandlerFunctor.cpp | 12 ++++++- src/profiling/CommandHandlerFunctor.hpp | 12 ++++++- src/profiling/CommandHandlerKey.cpp | 10 ++++++ src/profiling/CommandHandlerKey.hpp | 10 ++++++ src/profiling/CommandHandlerRegistry.cpp | 10 ++++++ src/profiling/CommandHandlerRegistry.hpp | 13 ++++++- src/profiling/EncodeVersion.hpp | 59 +++++++++++++++++++++--------- src/profiling/Packet.cpp | 12 ++++++- src/profiling/Packet.hpp | 13 +++++-- src/profiling/PacketVersionResolver.cpp | 22 ++++++++++++ src/profiling/PacketVersionResolver.hpp | 27 ++++++++++++++ src/profiling/test/ProfilingTests.cpp | 62 +++++++++++++++++++++++--------- 13 files changed, 226 insertions(+), 38 deletions(-) create mode 100644 src/profiling/PacketVersionResolver.cpp create mode 100644 src/profiling/PacketVersionResolver.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 420aabd..068768c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -420,6 +420,8 @@ list(APPEND armnn_sources src/profiling/EncodeVersion.hpp src/profiling/Packet.cpp src/profiling/Packet.hpp + src/profiling/PacketVersionResolver.cpp + src/profiling/PacketVersionResolver.hpp third-party/half/half.hpp ) diff --git a/src/profiling/CommandHandlerFunctor.cpp b/src/profiling/CommandHandlerFunctor.cpp index 7289221..894e2d4 100644 --- a/src/profiling/CommandHandlerFunctor.cpp +++ b/src/profiling/CommandHandlerFunctor.cpp @@ -5,6 +5,12 @@ #include "CommandHandlerFunctor.hpp" +namespace armnn +{ + +namespace profiling +{ + uint32_t CommandHandlerFunctor::GetPacketId() const { return m_PacketId; @@ -13,4 +19,8 @@ uint32_t CommandHandlerFunctor::GetPacketId() const uint32_t CommandHandlerFunctor::GetVersion() const { return m_Version; -} \ No newline at end of file +} + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/CommandHandlerFunctor.hpp b/src/profiling/CommandHandlerFunctor.hpp index fce6e3f..a9a59c1 100644 --- a/src/profiling/CommandHandlerFunctor.hpp +++ b/src/profiling/CommandHandlerFunctor.hpp @@ -7,6 +7,12 @@ #include +namespace armnn +{ + +namespace profiling +{ + #pragma once class CommandHandlerFunctor @@ -22,4 +28,8 @@ public: private: uint32_t m_PacketId; uint32_t m_Version; -}; \ No newline at end of file +}; + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/CommandHandlerKey.cpp b/src/profiling/CommandHandlerKey.cpp index 6ce7344..66b20c5 100644 --- a/src/profiling/CommandHandlerKey.cpp +++ b/src/profiling/CommandHandlerKey.cpp @@ -5,6 +5,12 @@ #include "CommandHandlerKey.hpp" +namespace armnn +{ + +namespace profiling +{ + uint32_t CommandHandlerKey::GetPacketId() const { return m_PacketId; @@ -55,3 +61,7 @@ bool CommandHandlerKey::operator!=(const CommandHandlerKey& rhs) const { return !(*this == rhs); } + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/CommandHandlerKey.hpp b/src/profiling/CommandHandlerKey.hpp index 12bafbe..1ec5f51 100644 --- a/src/profiling/CommandHandlerKey.hpp +++ b/src/profiling/CommandHandlerKey.hpp @@ -7,6 +7,12 @@ #include +namespace armnn +{ + +namespace profiling +{ + class CommandHandlerKey { public: @@ -26,3 +32,7 @@ private: uint32_t m_PacketId; uint32_t m_Version; }; + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/CommandHandlerRegistry.cpp b/src/profiling/CommandHandlerRegistry.cpp index d392db0..9731347 100644 --- a/src/profiling/CommandHandlerRegistry.cpp +++ b/src/profiling/CommandHandlerRegistry.cpp @@ -8,6 +8,12 @@ #include #include +namespace armnn +{ + +namespace profiling +{ + void CommandHandlerRegistry::RegisterFunctor(CommandHandlerFunctor* functor, uint32_t packetId, uint32_t version) { BOOST_ASSERT_MSG(functor, "Provided functor should not be a nullptr."); @@ -27,3 +33,7 @@ CommandHandlerFunctor* CommandHandlerRegistry::GetFunctor(uint32_t packetId, uin return registry.at(key); } + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/CommandHandlerRegistry.hpp b/src/profiling/CommandHandlerRegistry.hpp index ba81f17..61d45b0 100644 --- a/src/profiling/CommandHandlerRegistry.hpp +++ b/src/profiling/CommandHandlerRegistry.hpp @@ -9,8 +9,15 @@ #include "CommandHandlerKey.hpp" #include + #include +namespace armnn +{ + +namespace profiling +{ + struct CommandHandlerHash { std::size_t operator() (const CommandHandlerKey& commandHandlerKey) const @@ -33,4 +40,8 @@ public: private: std::unordered_map registry; -}; \ No newline at end of file +}; + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/EncodeVersion.hpp b/src/profiling/EncodeVersion.hpp index 2573933..f66f727 100644 --- a/src/profiling/EncodeVersion.hpp +++ b/src/profiling/EncodeVersion.hpp @@ -4,20 +4,21 @@ // #pragma once -#include +#include +#include +#include +#include -namespace mlutil +namespace armnn { -namespace Impl +namespace profiling { - constexpr uint32_t EncodeVersion(uint32_t major, uint32_t minor, uint32_t patch) - { - return (major << 22) | (minor << 12) | patch; - } - -} // namespace Impl +constexpr inline uint32_t EncodeVersion(uint32_t major, uint32_t minor, uint32_t patch) +{ + return (major << 22) | (minor << 12) | patch; +} // Encodes a semantic version https://semver.org/ into a 32 bit integer in the following fashion // @@ -35,17 +36,35 @@ public: m_Patch = encodedValue & 4095; } - Version(uint32_t major, uint32_t minor, uint32_t patch) - : m_Major(major), m_Minor(minor), m_Patch(patch) {} + Version(uint32_t major, uint32_t minor, uint32_t patch) : + m_Major(major), + m_Minor(minor), + m_Patch(patch) + {} uint32_t GetEncodedValue() { - return mlutil::Impl::EncodeVersion(m_Major, m_Minor, m_Patch); + return EncodeVersion(m_Major, m_Minor, m_Patch); } - uint32_t GetMajor() {return m_Major;} - uint32_t GetMinor() {return m_Minor;} - uint32_t GetPatch() {return m_Patch;} + uint32_t GetMajor() { return m_Major; } + uint32_t GetMinor() { return m_Minor; } + uint32_t GetPatch() { return m_Patch; } + + bool operator==(const Version& other) const + { + return m_Major == other.m_Major && m_Minor == other.m_Minor && m_Patch == other.m_Patch; + } + + std::string ToString() const + { + constexpr char separator = '.'; + + std::stringstream stringStream; + stringStream << m_Major << separator << m_Minor << separator << m_Patch; + + return stringStream.str(); + } private: uint32_t m_Major; @@ -53,4 +72,12 @@ private: uint32_t m_Patch; }; -} // namespace mlutil +inline std::ostream& operator<<(std::ostream& os, const Version& version) +{ + os << version.ToString(); + return os; +} + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/Packet.cpp b/src/profiling/Packet.cpp index 97cb89b..d0650a2 100644 --- a/src/profiling/Packet.cpp +++ b/src/profiling/Packet.cpp @@ -5,6 +5,12 @@ #include "Packet.hpp" +namespace armnn +{ + +namespace profiling +{ + std::uint32_t Packet::GetHeader() const { return m_Header; @@ -38,4 +44,8 @@ std::uint32_t Packet::GetPacketClass() const std::uint32_t Packet::GetPacketType() const { return (m_PacketId & 7); -} \ No newline at end of file +} + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/Packet.hpp b/src/profiling/Packet.hpp index 0d2ba42..ce37844 100644 --- a/src/profiling/Packet.hpp +++ b/src/profiling/Packet.hpp @@ -4,10 +4,15 @@ // #pragma once +#include #include -#include +namespace armnn +{ + +namespace profiling +{ class Packet { @@ -42,4 +47,8 @@ private: uint32_t m_PacketId; uint32_t m_Length; const char* m_Data; -}; \ No newline at end of file +}; + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/PacketVersionResolver.cpp b/src/profiling/PacketVersionResolver.cpp new file mode 100644 index 0000000..66e2b4d --- /dev/null +++ b/src/profiling/PacketVersionResolver.cpp @@ -0,0 +1,22 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "PacketVersionResolver.hpp" + +namespace armnn +{ + +namespace profiling +{ + +Version PacketVersionResolver::ResolvePacketVersion(uint32_t packetId) const +{ + // NOTE: For now every packet specification is at version 1.0.0 + return Version(1, 0, 0); +} + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/PacketVersionResolver.hpp b/src/profiling/PacketVersionResolver.hpp new file mode 100644 index 0000000..168a32c --- /dev/null +++ b/src/profiling/PacketVersionResolver.hpp @@ -0,0 +1,27 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "EncodeVersion.hpp" + +namespace armnn +{ + +namespace profiling +{ + +class PacketVersionResolver final +{ +public: + PacketVersionResolver() = default; + ~PacketVersionResolver() = default; + + Version ResolvePacketVersion(uint32_t packetId) const; +}; + +} // namespace profiling + +} // namespace armnn diff --git a/src/profiling/test/ProfilingTests.cpp b/src/profiling/test/ProfilingTests.cpp index 8a2f2bd..3fd8d79 100644 --- a/src/profiling/test/ProfilingTests.cpp +++ b/src/profiling/test/ProfilingTests.cpp @@ -8,14 +8,20 @@ #include "../CommandHandlerRegistry.hpp" #include "../EncodeVersion.hpp" #include "../Packet.hpp" +#include "../PacketVersionResolver.hpp" + +#include #include #include -#include +#include #include +#include BOOST_AUTO_TEST_SUITE(ExternalProfiling) +using namespace armnn::profiling; + BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons) { CommandHandlerKey testKey0(1, 1); @@ -45,51 +51,51 @@ BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons) BOOST_CHECK(testKey1.GetVersion()==1); std::vector vect = - { - CommandHandlerKey(0,1), CommandHandlerKey(2,0), CommandHandlerKey(1,0), - CommandHandlerKey(2,1), CommandHandlerKey(1,1), CommandHandlerKey(0,1), - CommandHandlerKey(2,0), CommandHandlerKey(0,0) - }; + { + CommandHandlerKey(0,1), CommandHandlerKey(2,0), CommandHandlerKey(1,0), + CommandHandlerKey(2,1), CommandHandlerKey(1,1), CommandHandlerKey(0,1), + CommandHandlerKey(2,0), CommandHandlerKey(0,0) + }; std::sort(vect.begin(), vect.end()); std::vector expectedVect = - { - CommandHandlerKey(0,0), CommandHandlerKey(0,1), CommandHandlerKey(0,1), - CommandHandlerKey(1,0), CommandHandlerKey(1,1), CommandHandlerKey(2,0), - CommandHandlerKey(2,0), CommandHandlerKey(2,1) - }; + { + CommandHandlerKey(0,0), CommandHandlerKey(0,1), CommandHandlerKey(0,1), + CommandHandlerKey(1,0), CommandHandlerKey(1,1), CommandHandlerKey(2,0), + CommandHandlerKey(2,0), CommandHandlerKey(2,1) + }; BOOST_CHECK(vect == expectedVect); } BOOST_AUTO_TEST_CASE(CheckEncodeVersion) { - mlutil::Version version1(12); + Version version1(12); BOOST_CHECK(version1.GetMajor() == 0); BOOST_CHECK(version1.GetMinor() == 0); BOOST_CHECK(version1.GetPatch() == 12); - mlutil::Version version2(4108); + Version version2(4108); BOOST_CHECK(version2.GetMajor() == 0); BOOST_CHECK(version2.GetMinor() == 1); BOOST_CHECK(version2.GetPatch() == 12); - mlutil::Version version3(4198412); + Version version3(4198412); BOOST_CHECK(version3.GetMajor() == 1); BOOST_CHECK(version3.GetMinor() == 1); BOOST_CHECK(version3.GetPatch() == 12); - mlutil::Version version4(0); + Version version4(0); BOOST_CHECK(version4.GetMajor() == 0); BOOST_CHECK(version4.GetMinor() == 0); BOOST_CHECK(version4.GetPatch() == 0); - mlutil::Version version5(1,0,0); + Version version5(1, 0, 0); BOOST_CHECK(version5.GetEncodedValue() == 4194304); } @@ -236,4 +242,28 @@ BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry) BOOST_CHECK_THROW(registry.GetFunctor(0, 0), armnn::Exception); } +BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver) +{ + // Set up random number generator for generating packetId values + std::random_device device; + std::mt19937 generator(device()); + std::uniform_int_distribution distribution(std::numeric_limits::min(), + std::numeric_limits::max()); + + // NOTE: Expected version is always 1.0.0, regardless of packetId + const Version expectedVersion(1, 0, 0); + + PacketVersionResolver packetVersionResolver; + + constexpr unsigned int numTests = 10u; + + for (unsigned int i = 0u; i < numTests; ++i) + { + const uint32_t packetId = distribution(generator); + Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(packetId); + + BOOST_TEST(resolvedVersion == expectedVersion); + } +} + BOOST_AUTO_TEST_SUITE_END() -- 2.7.4