Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include "ClContextControl.hpp" |
| 7 | |
Aron Virginas-Tar | 74ba3dc | 2018-11-15 17:44:36 +0000 | [diff] [blame] | 8 | #include <armnn/Exceptions.hpp> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 9 | |
Aron Virginas-Tar | 74ba3dc | 2018-11-15 17:44:36 +0000 | [diff] [blame] | 10 | #include <LeakChecking.hpp> |
| 11 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 12 | #include <armnn/utility/Assert.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 13 | #include <armnn/utility/IgnoreUnused.hpp> |
| 14 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 15 | #include <arm_compute/core/CL/CLKernelLibrary.h> |
| 16 | #include <arm_compute/runtime/CL/CLScheduler.h> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 17 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 18 | #include <boost/format.hpp> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 19 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 20 | namespace cl |
| 21 | { |
| 22 | class Context; |
| 23 | class CommandQueue; |
| 24 | class Device; |
| 25 | } |
| 26 | |
| 27 | namespace armnn |
| 28 | { |
| 29 | |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 30 | ClContextControl::ClContextControl(arm_compute::CLTuner *tuner, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 31 | bool profilingEnabled) |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 32 | : m_Tuner(tuner) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 33 | , m_ProfilingEnabled(profilingEnabled) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 34 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 35 | // Ignore m_ProfilingEnabled if unused to avoid compiling problems when ArmCompute is disabled. |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 36 | IgnoreUnused(m_ProfilingEnabled); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 37 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 38 | try |
| 39 | { |
| 40 | std::vector<cl::Platform> platforms; |
| 41 | cl::Platform::get(&platforms); |
| 42 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 43 | // Selects default platform for the first element. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 44 | cl::Platform::setDefault(platforms[0]); |
| 45 | |
| 46 | std::vector<cl::Device> devices; |
| 47 | platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices); |
| 48 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | // Selects default device for the first element. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 50 | cl::Device::setDefault(devices[0]); |
| 51 | } |
| 52 | catch (const cl::Error& clError) |
| 53 | { |
| 54 | throw ClRuntimeUnavailableException(boost::str(boost::format( |
| 55 | "Could not initialize the CL runtime. Error description: %1%. CL error code: %2%" |
| 56 | ) % clError.what() % clError.err())); |
| 57 | } |
| 58 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | // Removes the use of global CL context. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 60 | cl::Context::setDefault(cl::Context{}); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 61 | ARMNN_ASSERT(cl::Context::getDefault()() == NULL); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 62 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | // Removes the use of global CL command queue. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 64 | cl::CommandQueue::setDefault(cl::CommandQueue{}); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 65 | ARMNN_ASSERT(cl::CommandQueue::getDefault()() == NULL); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 66 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 67 | // Always load the OpenCL runtime. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 68 | LoadOpenClRuntime(); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | ClContextControl::~ClContextControl() |
| 72 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | // Load the OpencCL runtime without the tuned parameters to free the memory for them. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 74 | try |
| 75 | { |
| 76 | UnloadOpenClRuntime(); |
| 77 | } |
| 78 | catch (const cl::Error& clError) |
| 79 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | // This should not happen, it is ignored if it does. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void ClContextControl::LoadOpenClRuntime() |
| 91 | { |
| 92 | DoLoadOpenClRuntime(true); |
| 93 | } |
| 94 | |
| 95 | void ClContextControl::UnloadOpenClRuntime() |
| 96 | { |
| 97 | DoLoadOpenClRuntime(false); |
| 98 | } |
| 99 | |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 100 | void ClContextControl::DoLoadOpenClRuntime(bool updateTunedParameters) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 101 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 102 | cl::Device device = cl::Device::getDefault(); |
| 103 | cl::Context context; |
| 104 | cl::CommandQueue commandQueue; |
| 105 | |
James Conroy | 663c184 | 2019-11-01 15:21:48 +0000 | [diff] [blame] | 106 | if (arm_compute::CLScheduler::get().is_initialised() && arm_compute::CLScheduler::get().context()() != NULL) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 107 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | // Wait for all queued CL requests to finish before reinitialising it. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 109 | arm_compute::CLScheduler::get().sync(); |
| 110 | } |
| 111 | |
| 112 | try |
| 113 | { |
| 114 | arm_compute::CLKernelLibrary::get().clear_programs_cache(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 115 | // Initialise the scheduler with a dummy context to release the LLVM data (which only happens when there are no |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 | // |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 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 |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 | // |
| 130 | ARMNN_DISABLE_LEAK_CHECKING_IN_SCOPE(); |
| 131 | context = cl::Context(device); |
| 132 | } |
| 133 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 134 | // NOTE: In this specific case profiling has to be enabled on the command queue |
| 135 | // in order for the CLTuner to work. |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 136 | bool profilingNeededForClTuner = updateTunedParameters && m_Tuner && |
| 137 | m_Tuner->tune_new_kernels(); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 138 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 139 | if (m_ProfilingEnabled || profilingNeededForClTuner) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 140 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 141 | // Create a new queue with profiling enabled. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 142 | commandQueue = cl::CommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE); |
| 143 | } |
| 144 | else |
| 145 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 146 | // Use default queue. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 147 | commandQueue = cl::CommandQueue(context, device); |
| 148 | } |
| 149 | } |
| 150 | catch (const cl::Error& clError) |
| 151 | { |
| 152 | throw ClRuntimeUnavailableException(boost::str(boost::format( |
| 153 | "Could not initialize the CL runtime. Error description: %1%. CL error code: %2%" |
| 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); |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 159 | arm_compute::CLScheduler::get().init(context, commandQueue, device, m_Tuner); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void ClContextControl::ClearClCache() |
| 163 | { |
| 164 | DoLoadOpenClRuntime(true); |
| 165 | } |
| 166 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 167 | armnn::IGpuAccTunedParameters* IGpuAccTunedParameters::CreateRaw(armnn::IGpuAccTunedParameters::Mode mode, |
| 168 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 169 | { |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 170 | return new ClTunedParameters(mode, tuningLevel); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 173 | armnn::IGpuAccTunedParametersPtr IGpuAccTunedParameters::Create(armnn::IGpuAccTunedParameters::Mode mode, |
| 174 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 175 | { |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 176 | return IGpuAccTunedParametersPtr(CreateRaw(mode, tuningLevel), &IGpuAccTunedParameters::Destroy); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 177 | } |
| 178 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 179 | void IGpuAccTunedParameters::Destroy(IGpuAccTunedParameters* params) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 180 | { |
| 181 | delete params; |
| 182 | } |
| 183 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 184 | ClTunedParameters::ClTunedParameters(armnn::IGpuAccTunedParameters::Mode mode, |
| 185 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 186 | : m_Mode(mode) |
Ruomei Yan | 8b194fb | 2019-05-03 12:10:19 +0100 | [diff] [blame] | 187 | , m_TuningLevel(tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 188 | , m_Tuner(mode == ClTunedParameters::Mode::UpdateTunedParameters) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 189 | { |
| 190 | } |
| 191 | |
| 192 | void ClTunedParameters::Load(const char* filename) |
| 193 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 + "': " + |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 201 | e.what()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 202 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void ClTunedParameters::Save(const char* filename) const |
| 206 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 + "': " + |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 214 | e.what()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 215 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // namespace armnn |