ArmNN
 20.02
ClContextControl.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ClContextControl.hpp"
7 
8 #include <armnn/Exceptions.hpp>
9 
10 #include <LeakChecking.hpp>
11 
13 
14 #include <arm_compute/core/CL/CLKernelLibrary.h>
15 #include <arm_compute/runtime/CL/CLScheduler.h>
16 
17 #include <boost/assert.hpp>
18 #include <boost/format.hpp>
19 #include <boost/polymorphic_cast.hpp>
20 
21 namespace cl
22 {
23 class Context;
24 class CommandQueue;
25 class Device;
26 }
27 
28 namespace armnn
29 {
30 
31 ClContextControl::ClContextControl(arm_compute::CLTuner *tuner,
32  bool profilingEnabled)
33  : m_Tuner(tuner)
34  , m_ProfilingEnabled(profilingEnabled)
35 {
36  // Ignore m_ProfilingEnabled if unused to avoid compiling problems when ArmCompute is disabled.
37  IgnoreUnused(m_ProfilingEnabled);
38 
39  try
40  {
41  std::vector<cl::Platform> platforms;
42  cl::Platform::get(&platforms);
43 
44  // Selects default platform for the first element.
45  cl::Platform::setDefault(platforms[0]);
46 
47  std::vector<cl::Device> devices;
48  platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);
49 
50  // Selects default device for the first element.
51  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 
60  // Removes the use of global CL context.
61  cl::Context::setDefault(cl::Context{});
62  BOOST_ASSERT(cl::Context::getDefault()() == NULL);
63 
64  // Removes the use of global CL command queue.
65  cl::CommandQueue::setDefault(cl::CommandQueue{});
66  BOOST_ASSERT(cl::CommandQueue::getDefault()() == NULL);
67 
68  // Always load the OpenCL runtime.
70 }
71 
73 {
74  // Load the OpencCL runtime without the tuned parameters to free the memory for them.
75  try
76  {
78  }
79  catch (const cl::Error& clError)
80  {
81  // This should not happen, it is ignored if it does.
82 
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  }
89 }
90 
92 {
93  DoLoadOpenClRuntime(true);
94 }
95 
97 {
98  DoLoadOpenClRuntime(false);
99 }
100 
101 void ClContextControl::DoLoadOpenClRuntime(bool updateTunedParameters)
102 {
103  cl::Device device = cl::Device::getDefault();
104  cl::Context context;
105  cl::CommandQueue commandQueue;
106 
107  if (arm_compute::CLScheduler::get().is_initialised() && arm_compute::CLScheduler::get().context()() != NULL)
108  {
109  // Wait for all queued CL requests to finish before reinitialising it.
110  arm_compute::CLScheduler::get().sync();
111  }
112 
113  try
114  {
115  arm_compute::CLKernelLibrary::get().clear_programs_cache();
116  // Initialise the scheduler with a dummy context to release the LLVM data (which only happens when there are no
117  // 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  //
123  // 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
126  // 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  //
132  context = cl::Context(device);
133  }
134 
135  // 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 = updateTunedParameters && m_Tuner &&
138  m_Tuner->tune_new_kernels();
139 
140  if (m_ProfilingEnabled || profilingNeededForClTuner)
141  {
142  // Create a new queue with profiling enabled.
143  commandQueue = cl::CommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE);
144  }
145  else
146  {
147  // Use default queue.
148  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  arm_compute::CLScheduler::get().init(context, commandQueue, device, m_Tuner);
161 }
162 
164 {
165  DoLoadOpenClRuntime(true);
166 }
167 
170 {
171  return new ClTunedParameters(mode, tuningLevel);
172 }
173 
176 {
177  return IGpuAccTunedParametersPtr(CreateRaw(mode, tuningLevel), &IGpuAccTunedParameters::Destroy);
178 }
179 
181 {
182  delete params;
183 }
184 
187  : m_Mode(mode)
188  , m_TuningLevel(tuningLevel)
189  , m_Tuner(mode == ClTunedParameters::Mode::UpdateTunedParameters)
190 {
191 }
192 
193 void ClTunedParameters::Load(const char* filename)
194 {
195  try
196  {
197  m_Tuner.load_from_file(filename);
198  }
199  catch (const std::exception& e)
200  {
201  throw armnn::Exception(std::string("Failed to load tuned parameters file '") + filename + "': " +
202  e.what());
203  }
204 }
205 
206 void ClTunedParameters::Save(const char* filename) const
207 {
208  try
209  {
210  m_Tuner.save_to_file(filename);
211  }
212  catch (const std::exception& e)
213  {
214  throw armnn::Exception(std::string("Failed to save tuned parameters file to '") + filename + "': " +
215  e.what());
216  }
217 }
218 
219 } // namespace armnn
static IGpuAccTunedParameters * CreateRaw(Mode mode, TuningLevel tunerMode)
Creates an IClTunedParameters with the given mode.
#define ARMNN_DISABLE_LEAK_CHECKING_IN_SCOPE()
static void Destroy(IGpuAccTunedParameters *params)
static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode)
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
virtual void Load(const char *filename)
Loads an existing set of tuned parameters from the given file.
std::shared_ptr< IGpuAccTunedParameters > IGpuAccTunedParametersPtr
The following API is replaced by the backend options API.
Definition: IRuntime.hpp:170
arm_compute::CLTuner m_Tuner
Manages a set of GpuAcc parameters which have been tuned for maximum performance. ...
Definition: IRuntime.hpp:183
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
virtual void Save(const char *filename) const
Saves the current set of tuned parameters to the given file.
ClTunedParameters(armnn::IGpuAccTunedParameters::Mode mode, armnn::IGpuAccTunedParameters::TuningLevel tuningLevel)