David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ClBackendContext.hpp" |
| 7 | #include "ClContextControl.hpp" |
| 8 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 9 | #include <arm_compute/core/CL/OpenCL.h> |
| 10 | #include <arm_compute/core/CL/CLKernelLibrary.h> |
| 11 | #include <arm_compute/runtime/CL/CLScheduler.h> |
Aron Virginas-Tar | d46e647 | 2018-11-16 10:26:23 +0000 | [diff] [blame] | 12 | |
| 13 | #include <boost/log/trivial.hpp> |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 14 | |
| 15 | namespace armnn |
| 16 | { |
| 17 | |
| 18 | struct ClBackendContext::ClContextControlWrapper |
| 19 | { |
| 20 | ClContextControlWrapper(IGpuAccTunedParameters* clTunedParameters, |
| 21 | bool profilingEnabled) |
| 22 | : m_ClContextControl(clTunedParameters, profilingEnabled) |
| 23 | {} |
| 24 | |
| 25 | bool Sync() |
| 26 | { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 27 | if (arm_compute::CLScheduler::get().context()() != NULL) |
| 28 | { |
| 29 | // Waits for all queued CL requests to finish before unloading the network they may be using. |
| 30 | try |
| 31 | { |
| 32 | // Coverity fix: arm_compute::CLScheduler::sync() may throw an exception of type cl::Error. |
| 33 | arm_compute::CLScheduler::get().sync(); |
| 34 | } |
| 35 | catch (const cl::Error&) |
| 36 | { |
| 37 | BOOST_LOG_TRIVIAL(warning) << "WARNING: Runtime::UnloadNetwork(): an error occurred while waiting for " |
Aron Virginas-Tar | d46e647 | 2018-11-16 10:26:23 +0000 | [diff] [blame] | 38 | "the queued CL requests to finish"; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | } |
Aron Virginas-Tar | d46e647 | 2018-11-16 10:26:23 +0000 | [diff] [blame] | 42 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 43 | return true; |
| 44 | } |
| 45 | |
| 46 | void ClearClCache() |
| 47 | { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 48 | if (arm_compute::CLScheduler::get().context()() != NULL) |
| 49 | { |
| 50 | // There are no loaded networks left, so clear the CL cache to free up memory |
| 51 | m_ClContextControl.ClearClCache(); |
| 52 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 53 | } |
| 54 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 55 | ClContextControl m_ClContextControl; |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | ClBackendContext::ClBackendContext(const IRuntime::CreationOptions& options) |
| 60 | : IBackendContext(options) |
| 61 | , m_ClContextControlWrapper( |
| 62 | std::make_unique<ClContextControlWrapper>(options.m_GpuAccTunedParameters.get(), |
| 63 | options.m_EnableGpuProfiling)) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | bool ClBackendContext::BeforeLoadNetwork(NetworkId) |
| 68 | { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool ClBackendContext::AfterLoadNetwork(NetworkId networkId) |
| 73 | { |
| 74 | { |
| 75 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 76 | m_NetworkIds.insert(networkId); |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool ClBackendContext::BeforeUnloadNetwork(NetworkId) |
| 82 | { |
| 83 | return m_ClContextControlWrapper->Sync(); |
| 84 | } |
| 85 | |
| 86 | bool ClBackendContext::AfterUnloadNetwork(NetworkId networkId) |
| 87 | { |
| 88 | bool clearCache = false; |
| 89 | { |
| 90 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 91 | m_NetworkIds.erase(networkId); |
| 92 | clearCache = m_NetworkIds.empty(); |
| 93 | } |
| 94 | |
| 95 | if (clearCache) |
| 96 | { |
| 97 | m_ClContextControlWrapper->ClearClCache(); |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | ClBackendContext::~ClBackendContext() |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | } // namespace armnn |