blob: ac0b8c8572a04f3d07b7bf7506f5b90cd8d93c6c [file] [log] [blame]
surmeh013537c2c2018-05-18 16:31:43 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
surmeh013537c2c2018-05-18 16:31:43 +01004//
5
6#include "ClContextControl.hpp"
7
Aron Virginas-Tar74ba3dc2018-11-15 17:44:36 +00008#include <armnn/Exceptions.hpp>
surmeh013537c2c2018-05-18 16:31:43 +01009
Aron Virginas-Tar74ba3dc2018-11-15 17:44:36 +000010#include <LeakChecking.hpp>
11
surmeh013537c2c2018-05-18 16:31:43 +010012#include <arm_compute/core/CL/CLKernelLibrary.h>
13#include <arm_compute/runtime/CL/CLScheduler.h>
surmeh013537c2c2018-05-18 16:31:43 +010014
15#include <boost/assert.hpp>
16#include <boost/format.hpp>
17#include <boost/log/trivial.hpp>
18#include <boost/polymorphic_cast.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010019#include <boost/core/ignore_unused.hpp>
surmeh013537c2c2018-05-18 16:31:43 +010020
surmeh013537c2c2018-05-18 16:31:43 +010021namespace cl
22{
23class Context;
24class CommandQueue;
25class Device;
26}
27
28namespace armnn
29{
30
telsoa01c577f2c2018-08-31 09:22:23 +010031ClContextControl::ClContextControl(IGpuAccTunedParameters* clTunedParameters,
32 bool profilingEnabled)
surmeh013537c2c2018-05-18 16:31:43 +010033 : m_clTunedParameters(boost::polymorphic_downcast<ClTunedParameters*>(clTunedParameters))
telsoa01c577f2c2018-08-31 09:22:23 +010034 , m_ProfilingEnabled(profilingEnabled)
surmeh013537c2c2018-05-18 16:31:43 +010035{
telsoa01c577f2c2018-08-31 09:22:23 +010036 // Ignore m_ProfilingEnabled if unused to avoid compiling problems when ArmCompute is disabled.
37 boost::ignore_unused(m_ProfilingEnabled);
38
surmeh013537c2c2018-05-18 16:31:43 +010039 try
40 {
41 std::vector<cl::Platform> platforms;
42 cl::Platform::get(&platforms);
43
telsoa01c577f2c2018-08-31 09:22:23 +010044 // Selects default platform for the first element.
surmeh013537c2c2018-05-18 16:31:43 +010045 cl::Platform::setDefault(platforms[0]);
46
47 std::vector<cl::Device> devices;
48 platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);
49
telsoa01c577f2c2018-08-31 09:22:23 +010050 // Selects default device for the first element.
surmeh013537c2c2018-05-18 16:31:43 +010051 cl::Device::setDefault(devices[0]);
52 }
53 catch (const cl::Error& clError)
54 {
55 throw ClRuntimeUnavailableException(boost::str(boost::format(
56 "Could not initialize the CL runtime. Error description: %1%. CL error code: %2%"
57 ) % clError.what() % clError.err()));
58 }
59
telsoa01c577f2c2018-08-31 09:22:23 +010060 // Removes the use of global CL context.
surmeh013537c2c2018-05-18 16:31:43 +010061 cl::Context::setDefault(cl::Context{});
62 BOOST_ASSERT(cl::Context::getDefault()() == NULL);
63
telsoa01c577f2c2018-08-31 09:22:23 +010064 // Removes the use of global CL command queue.
surmeh013537c2c2018-05-18 16:31:43 +010065 cl::CommandQueue::setDefault(cl::CommandQueue{});
66 BOOST_ASSERT(cl::CommandQueue::getDefault()() == NULL);
67
telsoa01c577f2c2018-08-31 09:22:23 +010068 // Always load the OpenCL runtime.
surmeh013537c2c2018-05-18 16:31:43 +010069 LoadOpenClRuntime();
surmeh013537c2c2018-05-18 16:31:43 +010070}
71
72ClContextControl::~ClContextControl()
73{
telsoa01c577f2c2018-08-31 09:22:23 +010074 // Load the OpencCL runtime without the tuned parameters to free the memory for them.
surmeh013537c2c2018-05-18 16:31:43 +010075 try
76 {
77 UnloadOpenClRuntime();
78 }
79 catch (const cl::Error& clError)
80 {
telsoa01c577f2c2018-08-31 09:22:23 +010081 // This should not happen, it is ignored if it does.
surmeh013537c2c2018-05-18 16:31:43 +010082
83 // Coverity fix: BOOST_LOG_TRIVIAL (previously used here to report the error) may throw an
84 // exception of type std::length_error.
85 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
86 std::cerr << "A CL error occurred unloading the runtime tuner parameters: "
87 << clError.what() << ". CL error code is: " << clError.err() << std::endl;
88 }
surmeh013537c2c2018-05-18 16:31:43 +010089}
90
91void ClContextControl::LoadOpenClRuntime()
92{
93 DoLoadOpenClRuntime(true);
94}
95
96void ClContextControl::UnloadOpenClRuntime()
97{
98 DoLoadOpenClRuntime(false);
99}
100
101void ClContextControl::DoLoadOpenClRuntime(bool useTunedParameters)
102{
surmeh013537c2c2018-05-18 16:31:43 +0100103 cl::Device device = cl::Device::getDefault();
104 cl::Context context;
105 cl::CommandQueue commandQueue;
106
107 if (arm_compute::CLScheduler::get().context()() != NULL)
108 {
telsoa01c577f2c2018-08-31 09:22:23 +0100109 // Wait for all queued CL requests to finish before reinitialising it.
surmeh013537c2c2018-05-18 16:31:43 +0100110 arm_compute::CLScheduler::get().sync();
111 }
112
113 try
114 {
115 arm_compute::CLKernelLibrary::get().clear_programs_cache();
telsoa01c577f2c2018-08-31 09:22:23 +0100116 // Initialise the scheduler with a dummy context to release the LLVM data (which only happens when there are no
surmeh013537c2c2018-05-18 16:31:43 +0100117 // context references); it is initialised again, with a proper context, later.
118 arm_compute::CLScheduler::get().init(context, commandQueue, device);
119 arm_compute::CLKernelLibrary::get().init(".", context, device);
120
121 {
122 //
telsoa01c577f2c2018-08-31 09:22:23 +0100123 // Here we replace the context with a new one in which
124 // the memory leak checks show it as an extra allocation but
125 // because of the scope of the leak checks, it doesn't count
surmeh013537c2c2018-05-18 16:31:43 +0100126 // the disposal of the original object. On the other hand it
127 // does count the creation of this context which it flags
128 // as a memory leak. By adding the following line we prevent
129 // this to happen.
130 //
131 ARMNN_DISABLE_LEAK_CHECKING_IN_SCOPE();
132 context = cl::Context(device);
133 }
134
telsoa01c577f2c2018-08-31 09:22:23 +0100135 // NOTE: In this specific case profiling has to be enabled on the command queue
136 // in order for the CLTuner to work.
137 bool profilingNeededForClTuner = useTunedParameters && m_clTunedParameters &&
138 m_clTunedParameters->m_Mode == IGpuAccTunedParameters::Mode::UpdateTunedParameters;
surmeh013537c2c2018-05-18 16:31:43 +0100139
telsoa01c577f2c2018-08-31 09:22:23 +0100140 if (m_ProfilingEnabled || profilingNeededForClTuner)
surmeh013537c2c2018-05-18 16:31:43 +0100141 {
telsoa01c577f2c2018-08-31 09:22:23 +0100142 // Create a new queue with profiling enabled.
surmeh013537c2c2018-05-18 16:31:43 +0100143 commandQueue = cl::CommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE);
144 }
145 else
146 {
telsoa01c577f2c2018-08-31 09:22:23 +0100147 // Use default queue.
surmeh013537c2c2018-05-18 16:31:43 +0100148 commandQueue = cl::CommandQueue(context, device);
149 }
150 }
151 catch (const cl::Error& clError)
152 {
153 throw ClRuntimeUnavailableException(boost::str(boost::format(
154 "Could not initialize the CL runtime. Error description: %1%. CL error code: %2%"
155 ) % clError.what() % clError.err()));
156 }
157
158 // Note the first argument (path to cl source code) will be ignored as they should be embedded in the armcompute.
159 arm_compute::CLKernelLibrary::get().init(".", context, device);
160
161 arm_compute::ICLTuner* tuner = nullptr;
162 if (useTunedParameters && m_clTunedParameters)
163 {
164 tuner = &m_clTunedParameters->m_Tuner;
165 }
166 arm_compute::CLScheduler::get().init(context, commandQueue, device, tuner);
surmeh013537c2c2018-05-18 16:31:43 +0100167}
168
169void ClContextControl::ClearClCache()
170{
171 DoLoadOpenClRuntime(true);
172}
173
telsoa01c577f2c2018-08-31 09:22:23 +0100174armnn::IGpuAccTunedParameters* IGpuAccTunedParameters::CreateRaw(armnn::IGpuAccTunedParameters::Mode mode)
surmeh013537c2c2018-05-18 16:31:43 +0100175{
176 return new ClTunedParameters(mode);
177}
178
telsoa01c577f2c2018-08-31 09:22:23 +0100179armnn::IGpuAccTunedParametersPtr IGpuAccTunedParameters::Create(armnn::IGpuAccTunedParameters::Mode mode)
surmeh013537c2c2018-05-18 16:31:43 +0100180{
telsoa01c577f2c2018-08-31 09:22:23 +0100181 return IGpuAccTunedParametersPtr(CreateRaw(mode), &IGpuAccTunedParameters::Destroy);
surmeh013537c2c2018-05-18 16:31:43 +0100182}
183
telsoa01c577f2c2018-08-31 09:22:23 +0100184void IGpuAccTunedParameters::Destroy(IGpuAccTunedParameters* params)
surmeh013537c2c2018-05-18 16:31:43 +0100185{
186 delete params;
187}
188
telsoa01c577f2c2018-08-31 09:22:23 +0100189ClTunedParameters::ClTunedParameters(armnn::IGpuAccTunedParameters::Mode mode)
surmeh013537c2c2018-05-18 16:31:43 +0100190 : m_Mode(mode)
surmeh013537c2c2018-05-18 16:31:43 +0100191 , m_Tuner(mode == ClTunedParameters::Mode::UpdateTunedParameters)
surmeh013537c2c2018-05-18 16:31:43 +0100192{
193}
194
195void ClTunedParameters::Load(const char* filename)
196{
surmeh013537c2c2018-05-18 16:31:43 +0100197 try
198 {
199 m_Tuner.load_from_file(filename);
200 }
201 catch (const std::exception& e)
202 {
203 throw armnn::Exception(std::string("Failed to load tuned parameters file '") + filename + "': " +
204 e.what());
205 }
surmeh013537c2c2018-05-18 16:31:43 +0100206}
207
208void ClTunedParameters::Save(const char* filename) const
209{
surmeh013537c2c2018-05-18 16:31:43 +0100210 try
211 {
212 m_Tuner.save_to_file(filename);
213 }
214 catch (const std::exception& e)
215 {
216 throw armnn::Exception(std::string("Failed to save tuned parameters file to '") + filename + "': " +
217 e.what());
218 }
surmeh013537c2c2018-05-18 16:31:43 +0100219}
220
221} // namespace armnn