diff options
author | David Beck <david.beck@arm.com> | 2018-11-08 09:19:14 +0000 |
---|---|---|
committer | David Beck <david.beck@arm.com> | 2018-11-08 12:48:30 +0000 |
commit | 1b61be517387a20cd869e30587de2140b6d2252d (patch) | |
tree | 06ca37c640ccf4cd1fc5722393caf16c15a16b14 /src/backends/cl/ClBackendContext.cpp | |
parent | 60578950322491e44b4203fe085c3230ead19c7a (diff) | |
download | armnn-1b61be517387a20cd869e30587de2140b6d2252d.tar.gz |
IVGCVSW-2056+IVGCVSW-2064 : move ClContextControl to the ClBackendexperimental/nhwc-preview
* add IBackendContext interface
* add ClBackendContext implementation
Change-Id: I13e4d12b73d4c7775069587675276f7cee7d630b
Diffstat (limited to 'src/backends/cl/ClBackendContext.cpp')
-rw-r--r-- | src/backends/cl/ClBackendContext.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/backends/cl/ClBackendContext.cpp b/src/backends/cl/ClBackendContext.cpp new file mode 100644 index 0000000000..a2c1b87359 --- /dev/null +++ b/src/backends/cl/ClBackendContext.cpp @@ -0,0 +1,113 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "ClBackendContext.hpp" +#include "ClContextControl.hpp" + +#include <boost/log/trivial.hpp> + +#ifdef ARMCOMPUTECL_ENABLED +#include <arm_compute/core/CL/OpenCL.h> +#include <arm_compute/core/CL/CLKernelLibrary.h> +#include <arm_compute/runtime/CL/CLScheduler.h> +#endif + +namespace armnn +{ + +struct ClBackendContext::ClContextControlWrapper +{ + ClContextControlWrapper(IGpuAccTunedParameters* clTunedParameters, + bool profilingEnabled) + : m_ClContextControl(clTunedParameters, profilingEnabled) + {} + + bool Sync() + { +#ifdef ARMCOMPUTECL_ENABLED + 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(); + } + catch (const cl::Error&) + { + BOOST_LOG_TRIVIAL(warning) << "WARNING: Runtime::UnloadNetwork(): an error occurred while waiting for " + "the queued CL requests to finish"; + return false; + } + } +#endif + return true; + } + + void ClearClCache() + { +#ifdef ARMCOMPUTECL_ENABLED + if (arm_compute::CLScheduler::get().context()() != NULL) + { + // There are no loaded networks left, so clear the CL cache to free up memory + m_ClContextControl.ClearClCache(); + } +#endif + } + + + ClContextControl m_ClContextControl; +}; + + +ClBackendContext::ClBackendContext(const IRuntime::CreationOptions& options) + : IBackendContext(options) + , m_ClContextControlWrapper( + std::make_unique<ClContextControlWrapper>(options.m_GpuAccTunedParameters.get(), + options.m_EnableGpuProfiling)) +{ +} + +bool ClBackendContext::BeforeLoadNetwork(NetworkId) +{ + return true; +} + +bool ClBackendContext::AfterLoadNetwork(NetworkId networkId) +{ + { + std::lock_guard<std::mutex> lockGuard(m_Mutex); + m_NetworkIds.insert(networkId); + } + return true; +} + +bool ClBackendContext::BeforeUnloadNetwork(NetworkId) +{ + return m_ClContextControlWrapper->Sync(); +} + +bool ClBackendContext::AfterUnloadNetwork(NetworkId networkId) +{ + bool clearCache = false; + { + std::lock_guard<std::mutex> lockGuard(m_Mutex); + m_NetworkIds.erase(networkId); + clearCache = m_NetworkIds.empty(); + } + + if (clearCache) + { + m_ClContextControlWrapper->ClearClCache(); + } + + return true; +} + +ClBackendContext::~ClBackendContext() +{ +} + +} // namespace armnn
\ No newline at end of file |