IVGCVSW-2056 + IVGCVSW-2064 : move ClContextControl to the ClBackend
Change-Id: Ice19d3f763298bc14585267df389e99df846320d
diff --git a/src/backends/BackendContextRegistry.cpp b/src/backends/BackendContextRegistry.cpp
new file mode 100644
index 0000000..0168a12
--- /dev/null
+++ b/src/backends/BackendContextRegistry.cpp
@@ -0,0 +1,17 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "BackendContextRegistry.hpp"
+
+namespace armnn
+{
+
+BackendContextRegistry& BackendContextRegistryInstance()
+{
+ static BackendContextRegistry instance;
+ return instance;
+}
+
+} // namespace armnn
diff --git a/src/backends/BackendContextRegistry.hpp b/src/backends/BackendContextRegistry.hpp
new file mode 100644
index 0000000..c48d5b2
--- /dev/null
+++ b/src/backends/BackendContextRegistry.hpp
@@ -0,0 +1,27 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+#pragma once
+
+#include <armnn/Types.hpp>
+#include <armnn/IRuntime.hpp>
+#include "RegistryCommon.hpp"
+#include "IBackendContext.hpp"
+
+namespace armnn
+{
+
+using BackendContextRegistry = RegistryCommon<IBackendContext,
+ IBackendContextUniquePtr,
+ IRuntime::CreationOptions>;
+
+BackendContextRegistry& BackendContextRegistryInstance();
+
+template <>
+struct RegisteredTypeName<IBackendContext>
+{
+ static const char * Name() { return "IBackendContext"; }
+};
+
+} // namespace armnn
diff --git a/src/backends/BackendRegistry.hpp b/src/backends/BackendRegistry.hpp
index 4465e95..8f8ad37 100644
--- a/src/backends/BackendRegistry.hpp
+++ b/src/backends/BackendRegistry.hpp
@@ -11,7 +11,9 @@
namespace armnn
{
-using BackendRegistry = RegistryCommon<IBackendInternal, IBackendInternalUniquePtr>;
+using BackendRegistry = RegistryCommon<IBackendInternal,
+ IBackendInternalUniquePtr,
+ EmptyInitializer>;
BackendRegistry& BackendRegistryInstance();
diff --git a/src/backends/CMakeLists.txt b/src/backends/CMakeLists.txt
index 045bd2a..843e567 100644
--- a/src/backends/CMakeLists.txt
+++ b/src/backends/CMakeLists.txt
@@ -4,11 +4,14 @@
#
list(APPEND armnnBackendsCommon_sources
+ BackendContextRegistry.cpp
+ BackendContextRegistry.hpp
BackendRegistry.cpp
BackendRegistry.hpp
CpuTensorHandle.cpp
CpuTensorHandleFwd.hpp
CpuTensorHandle.hpp
+ IBackendContext.hpp
IBackendInternal.hpp
ILayerSupport.cpp
ITensorHandle.hpp
diff --git a/src/backends/IBackendContext.hpp b/src/backends/IBackendContext.hpp
new file mode 100644
index 0000000..d073d12
--- /dev/null
+++ b/src/backends/IBackendContext.hpp
@@ -0,0 +1,27 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+#pragma once
+
+#include <armnn/IRuntime.hpp>
+#include <memory>
+
+namespace armnn
+{
+
+class IBackendContext
+{
+public:
+ virtual ~IBackendContext() {}
+
+protected:
+ IBackendContext(const IRuntime::CreationOptions& options) {}
+
+private:
+ IBackendContext() = delete;
+};
+
+using IBackendContextUniquePtr = std::unique_ptr<IBackendContext>;
+
+} // namespace armnn
diff --git a/src/backends/IBackendInternal.hpp b/src/backends/IBackendInternal.hpp
index 7e44dbd..a24b600 100644
--- a/src/backends/IBackendInternal.hpp
+++ b/src/backends/IBackendInternal.hpp
@@ -4,7 +4,8 @@
//
#pragma once
-#include <armnn/Types.hpp>
+#include <armnn/IBackend.hpp>
+#include <memory>
namespace armnn
{
@@ -13,8 +14,6 @@
class IBackendInternal : public IBackend
{
protected:
- // Creation must be done through a specific
- // backend interface.
IBackendInternal() = default;
public:
diff --git a/src/backends/LayerSupportRegistry.hpp b/src/backends/LayerSupportRegistry.hpp
index a5efad0..6124685 100644
--- a/src/backends/LayerSupportRegistry.hpp
+++ b/src/backends/LayerSupportRegistry.hpp
@@ -6,11 +6,13 @@
#include "RegistryCommon.hpp"
#include <armnn/ILayerSupport.hpp>
+#include <armnn/Types.hpp>
namespace armnn
{
-
-using LayerSupportRegistry = RegistryCommon<ILayerSupport, ILayerSupportSharedPtr>;
+using LayerSupportRegistry = RegistryCommon<ILayerSupport,
+ ILayerSupportSharedPtr,
+ EmptyInitializer>;
LayerSupportRegistry& LayerSupportRegistryInstance();
diff --git a/src/backends/RegistryCommon.hpp b/src/backends/RegistryCommon.hpp
index 616a63b..044a9e4 100644
--- a/src/backends/RegistryCommon.hpp
+++ b/src/backends/RegistryCommon.hpp
@@ -21,11 +21,11 @@
static const char * Name() { return "UNKNOWN"; }
};
-template <typename RegisteredType, typename PointerType>
+template <typename RegisteredType, typename PointerType, typename ParamType>
class RegistryCommon
{
public:
- using FactoryFunction = std::function<PointerType()>;
+ using FactoryFunction = std::function<PointerType(const ParamType&)>;
void Register(const BackendId& id, FactoryFunction factory)
{
@@ -52,6 +52,20 @@
return it->second;
}
+ FactoryFunction GetFactory(const BackendId& id,
+ FactoryFunction defaultFactory) const
+ {
+ auto it = m_Factories.find(id);
+ if (it == m_Factories.end())
+ {
+ return defaultFactory;
+ }
+ else
+ {
+ return it->second;
+ }
+ }
+
size_t Size() const
{
return m_Factories.size();
@@ -116,4 +130,4 @@
}
};
-} // namespace armnn
+} // namespace armnn
\ No newline at end of file
diff --git a/src/backends/WorkloadFactory.cpp b/src/backends/WorkloadFactory.cpp
index fea383f..2e7a24f 100644
--- a/src/backends/WorkloadFactory.cpp
+++ b/src/backends/WorkloadFactory.cpp
@@ -64,7 +64,7 @@
auto const& layerSupportRegistry = LayerSupportRegistryInstance();
auto layerSupportFactory = layerSupportRegistry.GetFactory(backendId);
- auto layerSupportObject = layerSupportFactory();
+ auto layerSupportObject = layerSupportFactory(EmptyInitializer());
switch(layer.GetType())
{
diff --git a/src/backends/cl/CMakeLists.txt b/src/backends/cl/CMakeLists.txt
index 7182332..a0f1e4d 100644
--- a/src/backends/cl/CMakeLists.txt
+++ b/src/backends/cl/CMakeLists.txt
@@ -3,18 +3,23 @@
# SPDX-License-Identifier: MIT
#
+list(APPEND armnnClBackend_sources
+ ClBackendId.hpp
+ ClLayerSupport.cpp
+ ClLayerSupport.hpp
+ ClWorkloadFactory.cpp
+ ClWorkloadFactory.hpp
+)
+
if(ARMCOMPUTECL)
list(APPEND armnnClBackend_sources
+ ClBackendContext.cpp
+ ClBackendContext.hpp
ClBackend.cpp
ClBackend.hpp
- ClBackendId.hpp
ClContextControl.cpp
ClContextControl.hpp
- ClLayerSupport.cpp
- ClLayerSupport.hpp
ClTensorHandle.hpp
- ClWorkloadFactory.cpp
- ClWorkloadFactory.hpp
OpenClTimer.cpp
OpenClTimer.hpp
)
@@ -24,20 +29,10 @@
if(BUILD_UNIT_TESTS)
add_subdirectory(test)
endif()
-
-else()
- list(APPEND armnnClBackend_sources
- ClBackendId.hpp
- ClContextControl.cpp
- ClContextControl.hpp
- ClLayerSupport.cpp
- ClLayerSupport.hpp
- ClWorkloadFactory.cpp
- ClWorkloadFactory.hpp
- )
endif()
add_library(armnnClBackend OBJECT ${armnnClBackend_sources})
target_include_directories(armnnClBackend PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_include_directories(armnnClBackend PRIVATE ${PROJECT_SOURCE_DIR}/src/armnn)
target_include_directories(armnnClBackend PRIVATE ${PROJECT_SOURCE_DIR}/src/armnnUtils)
+
diff --git a/src/backends/cl/ClBackend.cpp b/src/backends/cl/ClBackend.cpp
index d6a3a89..da91de8 100644
--- a/src/backends/cl/ClBackend.cpp
+++ b/src/backends/cl/ClBackend.cpp
@@ -8,8 +8,7 @@
#include "ClWorkloadFactory.hpp"
#include <backends/BackendRegistry.hpp>
-
-#include <boost/cast.hpp>
+#include <boost/log/trivial.hpp>
namespace armnn
{
@@ -21,13 +20,13 @@
{
BackendRegistryInstance(),
ClBackend::GetIdStatic(),
- []()
+ [](const EmptyInitializer&)
{
return IBackendInternalUniquePtr(new ClBackend);
}
};
-}
+} // anonymous namespace
const BackendId& ClBackend::GetIdStatic()
{
diff --git a/src/backends/cl/ClBackend.hpp b/src/backends/cl/ClBackend.hpp
index 4eae6c9..201702e 100644
--- a/src/backends/cl/ClBackend.hpp
+++ b/src/backends/cl/ClBackend.hpp
@@ -12,8 +12,8 @@
class ClBackend : public IBackendInternal
{
public:
- ClBackend() = default;
- ~ClBackend() = default;
+ ClBackend() = default;
+ ~ClBackend() override = default;
static const BackendId& GetIdStatic();
const BackendId& GetId() const override { return GetIdStatic(); }
diff --git a/src/backends/cl/ClBackendContext.cpp b/src/backends/cl/ClBackendContext.cpp
new file mode 100644
index 0000000..4d1be33
--- /dev/null
+++ b/src/backends/cl/ClBackendContext.cpp
@@ -0,0 +1,111 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#include "ClBackendContext.hpp"
+#include "ClBackendId.hpp"
+#include "ClContextControl.hpp"
+
+#include <backends/BackendContextRegistry.hpp>
+#include <boost/log/trivial.hpp>
+
+#include <mutex>
+
+#ifdef ARMCOMPUTECL_ENABLED
+// Needed for the CL scheduler calls
+#include <arm_compute/core/CL/OpenCL.h>
+#include <arm_compute/core/CL/CLKernelLibrary.h>
+#include <arm_compute/runtime/CL/CLScheduler.h>
+#endif
+
+namespace armnn
+{
+
+namespace
+{
+
+static StaticRegistryInitializer<BackendContextRegistry> g_RegisterHelper
+{
+ BackendContextRegistryInstance(),
+ ClBackendId(),
+ [](const IRuntime::CreationOptions& options)
+ {
+ return IBackendContextUniquePtr(new ClBackendContext{options});
+ }
+};
+
+static std::mutex g_ContextControlMutex;
+
+std::shared_ptr<ClBackendContext::ContextControlWrapper>
+GetContextControlWrapper(const IRuntime::CreationOptions& options)
+{
+ static std::weak_ptr<ClBackendContext::ContextControlWrapper> contextControlWrapper;
+
+ std::lock_guard<std::mutex> lockGuard(g_ContextControlMutex);
+ std::shared_ptr<ClBackendContext::ContextControlWrapper> result;
+
+ if (contextControlWrapper.expired())
+ {
+ result = std::make_shared<ClBackendContext::ContextControlWrapper>(options);
+ contextControlWrapper = result;
+ }
+ else
+ {
+ result = contextControlWrapper.lock();
+ }
+
+ return result;
+}
+
+} // anonymous namespace
+
+
+#ifdef ARMCOMPUTECL_ENABLED
+struct ClBackendContext::ContextControlWrapper
+{
+ ContextControlWrapper(const IRuntime::CreationOptions& options)
+ : m_ClContextControl{options.m_GpuAccTunedParameters.get(),
+ options.m_EnableGpuProfiling}
+ {
+ }
+
+ ~ContextControlWrapper()
+ {
+ if (arm_compute::CLScheduler::get().context()() != NULL)
+ {
+ // Waits for all queued CL requests to finish before unloading the network they may be using.
+ try
+ {
+ // Coverity fix: arm_compute::CLScheduler::sync() may throw an exception of type cl::Error.
+ arm_compute::CLScheduler::get().sync();
+ m_ClContextControl.ClearClCache();
+ }
+ catch (const cl::Error&)
+ {
+ BOOST_LOG_TRIVIAL(warning) << "WARNING: Runtime::UnloadNetwork(): an error occurred while waiting for "
+ "the queued CL requests to finish";
+ }
+ }
+ }
+
+ ClContextControl m_ClContextControl;
+};
+#else //ARMCOMPUTECL_ENABLED
+struct ClBackendContext::ContextControlWrapper
+{
+ ContextControlWrapper(const IRuntime::CreationOptions&){}
+};
+#endif //ARMCOMPUTECL_ENABLED
+
+ClBackendContext::ClBackendContext(const IRuntime::CreationOptions& options)
+: IBackendContext{options}
+, m_ContextControl{GetContextControlWrapper(options)}
+{
+}
+
+ClBackendContext::~ClBackendContext()
+{
+}
+
+} // namespace armnn
\ No newline at end of file
diff --git a/src/backends/cl/ClBackendContext.hpp b/src/backends/cl/ClBackendContext.hpp
new file mode 100644
index 0000000..3df403e
--- /dev/null
+++ b/src/backends/cl/ClBackendContext.hpp
@@ -0,0 +1,23 @@
+//
+// Copyright © 2017 Arm Ltd. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+#pragma once
+
+#include <backends/IBackendContext.hpp>
+
+namespace armnn
+{
+
+class ClBackendContext : public IBackendContext
+{
+public:
+ ClBackendContext(const IRuntime::CreationOptions& options);
+ ~ClBackendContext() override;
+
+ struct ContextControlWrapper;
+private:
+ std::shared_ptr<ContextControlWrapper> m_ContextControl;
+};
+
+} // namespace armnn
\ No newline at end of file
diff --git a/src/backends/cl/ClLayerSupport.cpp b/src/backends/cl/ClLayerSupport.cpp
index 6c5704d..ebb90a5 100644
--- a/src/backends/cl/ClLayerSupport.cpp
+++ b/src/backends/cl/ClLayerSupport.cpp
@@ -53,7 +53,7 @@
static StaticRegistryInitializer<LayerSupportRegistry> g_RegisterHelper{
LayerSupportRegistryInstance(),
ClBackendId(),
- []()
+ [](const EmptyInitializer&)
{
return GetLayerSupportPointer();
}
diff --git a/src/backends/cl/backend.mk b/src/backends/cl/backend.mk
index 97df8e4..8433240 100644
--- a/src/backends/cl/backend.mk
+++ b/src/backends/cl/backend.mk
@@ -8,6 +8,7 @@
# file in the root of ArmNN
BACKEND_SOURCES := \
+ ClBackendContext.cpp \
ClBackend.cpp \
ClContextControl.cpp \
ClLayerSupport.cpp \
@@ -54,3 +55,4 @@
test/ClRuntimeTests.cpp \
test/Fp16SupportTest.cpp \
test/OpenClTimerTest.cpp
+
diff --git a/src/backends/common.mk b/src/backends/common.mk
index b1583b9..152ada3 100644
--- a/src/backends/common.mk
+++ b/src/backends/common.mk
@@ -8,6 +8,7 @@
# file in the root of ArmNN
COMMON_SOURCES := \
+ BackendContextRegistry.cpp \
BackendRegistry.cpp \
CpuTensorHandle.cpp \
ILayerSupport.cpp \
@@ -26,3 +27,4 @@
test/WorkloadDataValidation.cpp \
test/TensorCopyUtils.cpp \
test/LayerTests.cpp
+
diff --git a/src/backends/neon/CMakeLists.txt b/src/backends/neon/CMakeLists.txt
index 058553d..b5e6ff3 100644
--- a/src/backends/neon/CMakeLists.txt
+++ b/src/backends/neon/CMakeLists.txt
@@ -3,17 +3,20 @@
# SPDX-License-Identifier: MIT
#
+list(APPEND armnnNeonBackend_sources
+ NeonBackendId.hpp
+ NeonLayerSupport.cpp
+ NeonLayerSupport.hpp
+ NeonWorkloadFactory.cpp
+ NeonWorkloadFactory.hpp
+)
+
if(ARMCOMPUTENEON)
list(APPEND armnnNeonBackend_sources
NeonBackend.cpp
NeonBackend.hpp
- NeonBackendId.hpp
NeonInterceptorScheduler.hpp
NeonInterceptorScheduler.cpp
- NeonLayerSupport.cpp
- NeonLayerSupport.hpp
- NeonWorkloadFactory.cpp
- NeonWorkloadFactory.hpp
NeonTensorHandle.hpp
NeonTimer.hpp
NeonTimer.cpp
@@ -24,18 +27,10 @@
if(BUILD_UNIT_TESTS)
add_subdirectory(test)
endif()
-
-else()
- list(APPEND armnnNeonBackend_sources
- NeonBackendId.hpp
- NeonLayerSupport.cpp
- NeonLayerSupport.hpp
- NeonWorkloadFactory.cpp
- NeonWorkloadFactory.hpp
- )
endif()
add_library(armnnNeonBackend OBJECT ${armnnNeonBackend_sources})
target_include_directories(armnnNeonBackend PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_include_directories(armnnNeonBackend PRIVATE ${PROJECT_SOURCE_DIR}/src/armnn)
target_include_directories(armnnNeonBackend PRIVATE ${PROJECT_SOURCE_DIR}/src/armnnUtils)
+
diff --git a/src/backends/neon/NeonBackend.cpp b/src/backends/neon/NeonBackend.cpp
index e475f02..f8d7bb0 100644
--- a/src/backends/neon/NeonBackend.cpp
+++ b/src/backends/neon/NeonBackend.cpp
@@ -9,8 +9,6 @@
#include <backends/BackendRegistry.hpp>
-#include <boost/cast.hpp>
-
namespace armnn
{
@@ -21,7 +19,7 @@
{
BackendRegistryInstance(),
NeonBackend::GetIdStatic(),
- []()
+ [](const EmptyInitializer&)
{
return IBackendInternalUniquePtr(new NeonBackend);
}
diff --git a/src/backends/neon/NeonBackend.hpp b/src/backends/neon/NeonBackend.hpp
index e1287c7..d74962b 100644
--- a/src/backends/neon/NeonBackend.hpp
+++ b/src/backends/neon/NeonBackend.hpp
@@ -12,8 +12,8 @@
class NeonBackend : public IBackendInternal
{
public:
- NeonBackend() = default;
- ~NeonBackend() = default;
+ NeonBackend() = default;
+ ~NeonBackend() override = default;
static const BackendId& GetIdStatic();
const BackendId& GetId() const override { return GetIdStatic(); }
diff --git a/src/backends/neon/NeonLayerSupport.cpp b/src/backends/neon/NeonLayerSupport.cpp
index 3638212..71a5be3 100644
--- a/src/backends/neon/NeonLayerSupport.cpp
+++ b/src/backends/neon/NeonLayerSupport.cpp
@@ -49,7 +49,7 @@
static StaticRegistryInitializer<LayerSupportRegistry> g_RegisterHelper{
LayerSupportRegistryInstance(),
NeonBackendId(),
- []()
+ [](const EmptyInitializer&)
{
return GetLayerSupportPointer();
}
diff --git a/src/backends/neon/backend.mk b/src/backends/neon/backend.mk
index 8f7e72b..29b3527 100644
--- a/src/backends/neon/backend.mk
+++ b/src/backends/neon/backend.mk
@@ -49,3 +49,4 @@
test/NeonOptimizedNetworkTests.cpp \
test/NeonRuntimeTests.cpp \
test/NeonTimerTest.cpp
+
diff --git a/src/backends/reference/CMakeLists.txt b/src/backends/reference/CMakeLists.txt
index 5f698f4..e2656d5 100644
--- a/src/backends/reference/CMakeLists.txt
+++ b/src/backends/reference/CMakeLists.txt
@@ -23,3 +23,4 @@
if(BUILD_UNIT_TESTS)
add_subdirectory(test)
endif()
+
diff --git a/src/backends/reference/RefBackend.cpp b/src/backends/reference/RefBackend.cpp
index 34348fa..3aa4f03 100644
--- a/src/backends/reference/RefBackend.cpp
+++ b/src/backends/reference/RefBackend.cpp
@@ -9,8 +9,6 @@
#include <backends/BackendRegistry.hpp>
-#include <boost/cast.hpp>
-
namespace armnn
{
@@ -21,7 +19,7 @@
{
BackendRegistryInstance(),
RefBackend::GetIdStatic(),
- []()
+ [](const EmptyInitializer&)
{
return IBackendInternalUniquePtr(new RefBackend);
}
diff --git a/src/backends/reference/RefBackend.hpp b/src/backends/reference/RefBackend.hpp
index 7162c9b..1949e77 100644
--- a/src/backends/reference/RefBackend.hpp
+++ b/src/backends/reference/RefBackend.hpp
@@ -12,8 +12,8 @@
class RefBackend : public IBackendInternal
{
public:
- RefBackend() = default;
- ~RefBackend() = default;
+ RefBackend() = default;
+ ~RefBackend() override = default;
static const BackendId& GetIdStatic();
const BackendId& GetId() const override { return GetIdStatic(); }
diff --git a/src/backends/reference/RefLayerSupport.cpp b/src/backends/reference/RefLayerSupport.cpp
index 253d9c2..79d4ab0 100644
--- a/src/backends/reference/RefLayerSupport.cpp
+++ b/src/backends/reference/RefLayerSupport.cpp
@@ -31,7 +31,7 @@
static StaticRegistryInitializer<LayerSupportRegistry> g_RegisterHelper{
LayerSupportRegistryInstance(),
RefBackendId(),
- []()
+ [](const EmptyInitializer&)
{
return GetLayerSupportPointer();
}
diff --git a/src/backends/reference/backend.mk b/src/backends/reference/backend.mk
index 007efce..8a8a78a 100644
--- a/src/backends/reference/backend.mk
+++ b/src/backends/reference/backend.mk
@@ -71,3 +71,4 @@
test/RefLayerTests.cpp \
test/RefOptimizedNetworkTests.cpp \
test/RefRuntimeTests.cpp
+
diff --git a/src/backends/test/BackendRegistryTests.cpp b/src/backends/test/BackendRegistryTests.cpp
index 34a2706..bfeefda 100644
--- a/src/backends/test/BackendRegistryTests.cpp
+++ b/src/backends/test/BackendRegistryTests.cpp
@@ -52,7 +52,7 @@
StaticRegistryInitializer<BackendRegistry> factoryHelper(
BackendRegistryInstance(),
"HelloWorld",
- [&called]()
+ [&called](const EmptyInitializer&)
{
called = true;
return armnn::IBackendInternalUniquePtr(nullptr);
@@ -67,7 +67,7 @@
// sanity check: the factory still not called
BOOST_TEST(called == false);
- factoryFunction();
+ factoryFunction(EmptyInitializer());
BOOST_TEST(called == true);
}
@@ -79,7 +79,7 @@
bool called = false;
BackendRegistryInstance().Register(
"HelloWorld",
- [&called]()
+ [&called](const EmptyInitializer&)
{
called = true;
return armnn::IBackendInternalUniquePtr(nullptr);
@@ -94,7 +94,7 @@
// sanity check: the factory still not called
BOOST_TEST(called == false);
- factoryFunction();
+ factoryFunction(EmptyInitializer());
BOOST_TEST(called == true);
}