aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatteo Martincigh <matteo.martincigh@arm.com>2019-07-24 09:54:26 +0100
committerMatteo Martincigh <matteo.martincigh@arm.com>2019-07-29 12:54:24 +0000
commit2e7f6ad7a3476c42dda541d6d9b6beb40c08161d (patch)
treef86ac1b7c841352e1ff140cc1d06ad1b4934e37a
parentb38ed40d34b6c176dc47884419e9d367452b7fe0 (diff)
downloadarmnn-2e7f6ad7a3476c42dda541d6d9b6beb40c08161d.tar.gz
IVGCVSW-3556 Create the DynamicBackend class
* Created the new DynamicBackend class with all the necessary getters/utility functions * Created function types to mirror the backend interface functions Signed-off-by: Matteo Martincigh <matteo.martincigh@arm.com> Change-Id: I80a29e6e8a8d0b0655caba63d12cf62e0d8fc832
-rw-r--r--src/backends/backendsCommon/CMakeLists.txt2
-rw-r--r--src/backends/backendsCommon/DynamicBackend.cpp105
-rw-r--r--src/backends/backendsCommon/DynamicBackend.hpp47
3 files changed, 154 insertions, 0 deletions
diff --git a/src/backends/backendsCommon/CMakeLists.txt b/src/backends/backendsCommon/CMakeLists.txt
index bb31ce3813..4048c58956 100644
--- a/src/backends/backendsCommon/CMakeLists.txt
+++ b/src/backends/backendsCommon/CMakeLists.txt
@@ -9,6 +9,8 @@ list(APPEND armnnBackendsCommon_sources
CpuTensorHandle.cpp
CpuTensorHandleFwd.hpp
CpuTensorHandle.hpp
+ DynamicBackend.cpp
+ DynamicBackend.hpp
DynamicBackendUtils.cpp
DynamicBackendUtils.hpp
IBackendInternal.hpp
diff --git a/src/backends/backendsCommon/DynamicBackend.cpp b/src/backends/backendsCommon/DynamicBackend.cpp
new file mode 100644
index 0000000000..410265eb38
--- /dev/null
+++ b/src/backends/backendsCommon/DynamicBackend.cpp
@@ -0,0 +1,105 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "DynamicBackend.hpp"
+#include "DynamicBackendUtils.hpp"
+
+namespace armnn
+{
+
+DynamicBackend::DynamicBackend(const void* sharedObjectHandle)
+ : m_BackendIdFunction(nullptr)
+ , m_BackendVersionFunction(nullptr)
+ , m_BackendFactoryFunction(nullptr)
+ , m_Handle(const_cast<void*>(sharedObjectHandle), &DynamicBackendUtils::CloseHandle)
+{
+ if (m_Handle == nullptr)
+ {
+ throw InvalidArgumentException("Cannot create a DynamicBackend object from an invalid shared object handle");
+ }
+
+ // These calls will throw in case of error
+ m_BackendIdFunction = SetFunctionPointer<IdFunctionType>("GetBackendId");
+ m_BackendVersionFunction = SetFunctionPointer<VersionFunctionType>("GetVersion");
+ m_BackendFactoryFunction = SetFunctionPointer<FactoryFunctionType>("BackendFactory");
+
+ // Check that the backend is compatible with the current Backend API
+ BackendId backendId = GetBackendId();
+ BackendVersion backendVersion = GetBackendVersion();
+ if (!DynamicBackendUtils::IsBackendCompatible(backendVersion))
+ {
+ throw RuntimeException(boost::str(boost::format("The dynamic backend %1% (version %2%) is not compatible"
+ "with the current Backend API (vesion %3%)")
+ % backendId
+ % backendVersion
+ % IBackendInternal::GetApiVersion()));
+ }
+}
+
+BackendId DynamicBackend::GetBackendId()
+{
+ if (m_BackendIdFunction == nullptr)
+ {
+ throw RuntimeException("GetBackendId error: invalid function pointer");
+ }
+
+ return BackendId(m_BackendIdFunction());
+}
+
+BackendVersion DynamicBackend::GetBackendVersion()
+{
+ if (m_BackendVersionFunction == nullptr)
+ {
+ throw RuntimeException("GetBackendVersion error: invalid function pointer");
+ }
+
+ uint32_t major = 0;
+ uint32_t minor = 0;
+ m_BackendVersionFunction(&major, &minor);
+
+ return BackendVersion{ major, minor };
+}
+
+IBackendInternalUniquePtr DynamicBackend::GetBackend()
+{
+ if (m_BackendFactoryFunction == nullptr)
+ {
+ throw RuntimeException("GetBackend error: invalid function pointer");
+ }
+
+ auto backendPointer = reinterpret_cast<IBackendInternal*>(m_BackendFactoryFunction());
+ if (backendPointer == nullptr)
+ {
+ throw RuntimeException("GetBackend error: backend instance must not be null");
+ }
+
+ return std::unique_ptr<IBackendInternal>(backendPointer);
+}
+
+template<typename BackendFunctionType>
+BackendFunctionType DynamicBackend::SetFunctionPointer(const std::string& backendFunctionName)
+{
+ if (m_Handle == nullptr)
+ {
+ throw RuntimeException("SetFunctionPointer error: invalid shared object handle");
+ }
+
+ if (backendFunctionName.empty())
+ {
+ throw RuntimeException("SetFunctionPointer error: backend function name must not be empty");
+ }
+
+ // This call will throw in case of error
+ auto functionPointer = DynamicBackendUtils::GetEntryPoint<BackendFunctionType>(m_Handle.get(),
+ backendFunctionName.c_str());
+ if (!functionPointer)
+ {
+ throw RuntimeException("SetFunctionPointer error: invalid backend function pointer returned");
+ }
+
+ return functionPointer;
+}
+
+} // namespace armnn
diff --git a/src/backends/backendsCommon/DynamicBackend.hpp b/src/backends/backendsCommon/DynamicBackend.hpp
new file mode 100644
index 0000000000..06655293e7
--- /dev/null
+++ b/src/backends/backendsCommon/DynamicBackend.hpp
@@ -0,0 +1,47 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#pragma once
+
+#include "IBackendInternal.hpp"
+
+#include <functional>
+#include <memory>
+
+namespace armnn
+{
+
+class DynamicBackend
+{
+public:
+ using HandleCloser = std::function<void(const void*)>;
+ using HandlePtr = std::unique_ptr<void, HandleCloser>;
+
+ explicit DynamicBackend(const void* sharedObjectHandle);
+
+ /// Public dynamic backend functions
+ BackendId GetBackendId();
+ BackendVersion GetBackendVersion();
+ IBackendInternalUniquePtr GetBackend();
+
+private:
+ template<typename BackendFunctionType>
+ BackendFunctionType SetFunctionPointer(const std::string& backendFunctionName);
+
+ /// Backend function pointer types
+ using IdFunctionType = const char*(*)();
+ using VersionFunctionType = void(*)(uint32_t*, uint32_t*);
+ using FactoryFunctionType = void*(*)();
+
+ /// Backend function pointers
+ IdFunctionType m_BackendIdFunction;
+ VersionFunctionType m_BackendVersionFunction;
+ FactoryFunctionType m_BackendFactoryFunction;
+
+ /// Shared object handle
+ HandlePtr m_Handle;
+};
+
+} // namespace armnn