ArmNN
 20.11
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 
12 #include <armnn/utility/Assert.hpp>
14 
15 #include <arm_compute/core/CL/CLKernelLibrary.h>
16 #include <arm_compute/runtime/CL/CLScheduler.h>
17 
18 #include <fmt/format.h>
19 
20 namespace cl
21 {
22 class Context;
23 class CommandQueue;
24 class Device;
25 }
26 
27 namespace armnn
28 {
29 
30 ClContextControl::ClContextControl(arm_compute::CLTuner *tuner,
31  bool profilingEnabled)
32  : m_Tuner(tuner)
33  , m_ProfilingEnabled(profilingEnabled)
34 {
35  // Ignore m_ProfilingEnabled if unused to avoid compiling problems when ArmCompute is disabled.
36  IgnoreUnused(m_ProfilingEnabled);
37 
38  try
39  {
40  std::vector<cl::Platform> platforms;
41  cl::Platform::get(&platforms);
42 
43  // Selects default platform for the first element.
44  cl::Platform::setDefault(platforms[0]);
45 
46  std::vector<cl::Device> devices;
47  platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);
48 
49  // Selects default device for the first element.
50  cl::Device::setDefault(devices[0]);
51  }
52  catch (const cl::Error& clError)
53  {
54  throw ClRuntimeUnavailableException(fmt::format(
55  "Could not initialize the CL runtime. Error description: {0}. CL error code: {1}",
56  clError.what(), clError.err()));
57  }
58 
59  // Removes the use of global CL context.
60  cl::Context::setDefault(cl::Context{});
61  ARMNN_ASSERT(cl::Context::getDefault()() == NULL);
62 
63  // Removes the use of global CL command queue.
64  cl::CommandQueue::setDefault(cl::CommandQueue{});
65  ARMNN_ASSERT(cl::CommandQueue::getDefault()() == NULL);
66 
67  // Always load the OpenCL runtime.
69 }
70 
72 {
73  // Load the OpencCL runtime without the tuned parameters to free the memory for them.
74  try
75  {
77  }
78  catch (const cl::Error& clError)
79  {
80  // This should not happen, it is ignored if it does.
81 
82  // Coverity fix: BOOST_LOG_TRIVIAL (previously used here to report the error) may throw an
83  // exception of type std::length_error.
84  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
85  std::cerr << "A CL error occurred unloading the runtime tuner parameters: "
86  << clError.what() << ". CL error code is: " << clError.err() << std::endl;
87  }
88 }
89 
91 {
92  DoLoadOpenClRuntime(true);
93 }
94 
96 {
97  DoLoadOpenClRuntime(false);
98 }
99 
100 void ClContextControl::DoLoadOpenClRuntime(bool updateTunedParameters)
101 {
102  cl::Device device = cl::Device::getDefault();
103  cl::Context context;
104  cl::CommandQueue commandQueue;
105 
106  if (arm_compute::CLScheduler::get().is_initialised() && arm_compute::CLScheduler::get().context()() != NULL)
107  {
108  // Wait for all queued CL requests to finish before reinitialising it.
109  arm_compute::CLScheduler::get().sync();
110  }
111 
112  try
113  {
114  arm_compute::CLKernelLibrary::get().clear_programs_cache();
115  // Initialise the scheduler with a dummy context to release the LLVM data (which only happens when there are no
116  // context references); it is initialised again, with a proper context, later.
117  arm_compute::CLScheduler::get().init(context, commandQueue, device);
118  arm_compute::CLKernelLibrary::get().init(".", context, device);
119 
120  {
121  //
122  // Here we replace the context with a new one in which
123  // the memory leak checks show it as an extra allocation but
124  // because of the scope of the leak checks, it doesn't count
125  // the disposal of the original object. On the other hand it
126  // does count the creation of this context which it flags
127  // as a memory leak. By adding the following line we prevent
128  // this to happen.
129  //
131  context = cl::Context(device);
132  }
133 
134  // NOTE: In this specific case profiling has to be enabled on the command queue
135  // in order for the CLTuner to work.
136  bool profilingNeededForClTuner = updateTunedParameters && m_Tuner &&
137  m_Tuner->tune_new_kernels();
138 
139  if (m_ProfilingEnabled || profilingNeededForClTuner)
140  {
141  // Create a new queue with profiling enabled.
142  commandQueue = cl::CommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE);
143  }
144  else
145  {
146  // Use default queue.
147  commandQueue = cl::CommandQueue(context, device);
148  }
149  }
150  catch (const cl::Error& clError)
151  {
152  throw ClRuntimeUnavailableException(fmt::format(
153  "Could not initialize the CL runtime. Error description: {0}. CL error code: {1}",
154  clError.what(), clError.err()));
155  }
156 
157  // Note the first argument (path to cl source code) will be ignored as they should be embedded in the armcompute.
158  arm_compute::CLKernelLibrary::get().init(".", context, device);
159  arm_compute::CLScheduler::get().init(context, commandQueue, device, m_Tuner);
160 }
161 
163 {
164  DoLoadOpenClRuntime(true);
165 }
166 
169 {
170  return new ClTunedParameters(mode, tuningLevel);
171 }
172 
175 {
176  return IGpuAccTunedParametersPtr(CreateRaw(mode, tuningLevel), &IGpuAccTunedParameters::Destroy);
177 }
178 
180 {
181  delete params;
182 }
183 
186  : m_Mode(mode)
187  , m_TuningLevel(tuningLevel)
188  , m_Tuner(mode == ClTunedParameters::Mode::UpdateTunedParameters)
189 {
190 }
191 
192 void ClTunedParameters::Load(const char* filename)
193 {
194  try
195  {
196  m_Tuner.load_from_file(filename);
197  }
198  catch (const std::exception& e)
199  {
200  throw armnn::Exception(std::string("Failed to load tuned parameters file '") + filename + "': " +
201  e.what());
202  }
203 }
204 
205 void ClTunedParameters::Save(const char* filename) const
206 {
207  try
208  {
209  m_Tuner.save_to_file(filename);
210  }
211  catch (const std::exception& e)
212  {
213  throw armnn::Exception(std::string("Failed to save tuned parameters file to '") + filename + "': " +
214  e.what());
215  }
216 }
217 
218 } // 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.
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::shared_ptr< IGpuAccTunedParameters > IGpuAccTunedParametersPtr
The following API is replaced by the backend options API.
Definition: IRuntime.hpp:177
arm_compute::CLTuner m_Tuner
Manages a set of GpuAcc parameters which have been tuned for maximum performance. ...
Definition: IRuntime.hpp:190
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)