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