From d4dfa684941a21314b70593d01b0fc2167eebad4 Mon Sep 17 00:00:00 2001 From: David Beck Date: Wed, 24 Oct 2018 17:09:46 +0100 Subject: IVGCVSW-2056 + IVGCVSW-2064 : move ClContextControl to the ClBackend Change-Id: Ice19d3f763298bc14585267df389e99df846320d --- src/backends/cl/CMakeLists.txt | 27 ++++----- src/backends/cl/ClBackend.cpp | 7 +-- src/backends/cl/ClBackend.hpp | 4 +- src/backends/cl/ClBackendContext.cpp | 111 +++++++++++++++++++++++++++++++++++ src/backends/cl/ClBackendContext.hpp | 23 ++++++++ src/backends/cl/ClLayerSupport.cpp | 2 +- src/backends/cl/backend.mk | 2 + 7 files changed, 153 insertions(+), 23 deletions(-) create mode 100644 src/backends/cl/ClBackendContext.cpp create mode 100644 src/backends/cl/ClBackendContext.hpp (limited to 'src/backends/cl') diff --git a/src/backends/cl/CMakeLists.txt b/src/backends/cl/CMakeLists.txt index 7182332956..a0f1e4d9cc 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(ARMCOMPUTECL) 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 d6a3a89391..da91de8e48 100644 --- a/src/backends/cl/ClBackend.cpp +++ b/src/backends/cl/ClBackend.cpp @@ -8,8 +8,7 @@ #include "ClWorkloadFactory.hpp" #include - -#include +#include namespace armnn { @@ -21,13 +20,13 @@ static StaticRegistryInitializer g_RegisterHelper { 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 4eae6c92ec..201702eee3 100644 --- a/src/backends/cl/ClBackend.hpp +++ b/src/backends/cl/ClBackend.hpp @@ -12,8 +12,8 @@ namespace armnn 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 0000000000..4d1be33fc6 --- /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 +#include + +#include + +#ifdef ARMCOMPUTECL_ENABLED +// Needed for the CL scheduler calls +#include +#include +#include +#endif + +namespace armnn +{ + +namespace +{ + +static StaticRegistryInitializer g_RegisterHelper +{ + BackendContextRegistryInstance(), + ClBackendId(), + [](const IRuntime::CreationOptions& options) + { + return IBackendContextUniquePtr(new ClBackendContext{options}); + } +}; + +static std::mutex g_ContextControlMutex; + +std::shared_ptr +GetContextControlWrapper(const IRuntime::CreationOptions& options) +{ + static std::weak_ptr contextControlWrapper; + + std::lock_guard lockGuard(g_ContextControlMutex); + std::shared_ptr result; + + if (contextControlWrapper.expired()) + { + result = std::make_shared(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 0000000000..3df403efc5 --- /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 + +namespace armnn +{ + +class ClBackendContext : public IBackendContext +{ +public: + ClBackendContext(const IRuntime::CreationOptions& options); + ~ClBackendContext() override; + + struct ContextControlWrapper; +private: + std::shared_ptr 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 6c5704d7ab..ebb90a5abf 100644 --- a/src/backends/cl/ClLayerSupport.cpp +++ b/src/backends/cl/ClLayerSupport.cpp @@ -53,7 +53,7 @@ ILayerSupportSharedPtr GetLayerSupportPointer() static StaticRegistryInitializer g_RegisterHelper{ LayerSupportRegistryInstance(), ClBackendId(), - []() + [](const EmptyInitializer&) { return GetLayerSupportPointer(); } diff --git a/src/backends/cl/backend.mk b/src/backends/cl/backend.mk index 97df8e4903..8433240ae9 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 @@ BACKEND_TEST_SOURCES := \ test/ClRuntimeTests.cpp \ test/Fp16SupportTest.cpp \ test/OpenClTimerTest.cpp + -- cgit v1.2.1