blob: 48682b90dd2eaa9ddfbe4a8c19ed1e266e2e2974 [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"
7#include "ClContextControl.hpp"
8
David Beck1b61be52018-11-08 09:19:14 +00009#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-Tard46e6472018-11-16 10:26:23 +000012
13#include <boost/log/trivial.hpp>
David Beck1b61be52018-11-08 09:19:14 +000014
15namespace armnn
16{
17
18struct ClBackendContext::ClContextControlWrapper
19{
20 ClContextControlWrapper(IGpuAccTunedParameters* clTunedParameters,
21 bool profilingEnabled)
22 : m_ClContextControl(clTunedParameters, profilingEnabled)
23 {}
24
25 bool Sync()
26 {
David Beck1b61be52018-11-08 09:19:14 +000027 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-Tard46e6472018-11-16 10:26:23 +000038 "the queued CL requests to finish";
David Beck1b61be52018-11-08 09:19:14 +000039 return false;
40 }
41 }
Aron Virginas-Tard46e6472018-11-16 10:26:23 +000042
David Beck1b61be52018-11-08 09:19:14 +000043 return true;
44 }
45
46 void ClearClCache()
47 {
David Beck1b61be52018-11-08 09:19:14 +000048 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 Beck1b61be52018-11-08 09:19:14 +000053 }
54
David Beck1b61be52018-11-08 09:19:14 +000055 ClContextControl m_ClContextControl;
56};
57
58
59ClBackendContext::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
67bool ClBackendContext::BeforeLoadNetwork(NetworkId)
68{
69 return true;
70}
71
72bool 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
81bool ClBackendContext::BeforeUnloadNetwork(NetworkId)
82{
83 return m_ClContextControlWrapper->Sync();
84}
85
86bool 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
103ClBackendContext::~ClBackendContext()
104{
105}
106
107} // namespace armnn