surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 1 | // |
| 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 | |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 12 | #include <armnn/utility/IgnoreUnused.hpp> |
| 13 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 14 | #include <arm_compute/core/CL/CLKernelLibrary.h> |
| 15 | #include <arm_compute/runtime/CL/CLScheduler.h> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 16 | |
| 17 | #include <boost/assert.hpp> |
| 18 | #include <boost/format.hpp> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 19 | #include <boost/polymorphic_cast.hpp> |
| 20 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 21 | namespace cl |
| 22 | { |
| 23 | class Context; |
| 24 | class CommandQueue; |
| 25 | class Device; |
| 26 | } |
| 27 | |
| 28 | namespace armnn |
| 29 | { |
| 30 | |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 31 | ClContextControl::ClContextControl(arm_compute::CLTuner *tuner, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | bool profilingEnabled) |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 33 | : m_Tuner(tuner) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 34 | , m_ProfilingEnabled(profilingEnabled) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 35 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | // 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] | 37 | IgnoreUnused(m_ProfilingEnabled); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 38 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 39 | try |
| 40 | { |
| 41 | std::vector<cl::Platform> platforms; |
| 42 | cl::Platform::get(&platforms); |
| 43 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 44 | // Selects default platform for the first element. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 45 | cl::Platform::setDefault(platforms[0]); |
| 46 | |
| 47 | std::vector<cl::Device> devices; |
| 48 | platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices); |
| 49 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 50 | // Selects default device for the first element. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | // Removes the use of global CL context. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 61 | cl::Context::setDefault(cl::Context{}); |
| 62 | BOOST_ASSERT(cl::Context::getDefault()() == NULL); |
| 63 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | // Removes the use of global CL command queue. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 65 | cl::CommandQueue::setDefault(cl::CommandQueue{}); |
| 66 | BOOST_ASSERT(cl::CommandQueue::getDefault()() == NULL); |
| 67 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 68 | // Always load the OpenCL runtime. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 69 | LoadOpenClRuntime(); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | ClContextControl::~ClContextControl() |
| 73 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 74 | // 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] | 75 | try |
| 76 | { |
| 77 | UnloadOpenClRuntime(); |
| 78 | } |
| 79 | catch (const cl::Error& clError) |
| 80 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | // This should not happen, it is ignored if it does. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void ClContextControl::LoadOpenClRuntime() |
| 92 | { |
| 93 | DoLoadOpenClRuntime(true); |
| 94 | } |
| 95 | |
| 96 | void ClContextControl::UnloadOpenClRuntime() |
| 97 | { |
| 98 | DoLoadOpenClRuntime(false); |
| 99 | } |
| 100 | |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 101 | void ClContextControl::DoLoadOpenClRuntime(bool updateTunedParameters) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 102 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 103 | cl::Device device = cl::Device::getDefault(); |
| 104 | cl::Context context; |
| 105 | cl::CommandQueue commandQueue; |
| 106 | |
James Conroy | 663c184 | 2019-11-01 15:21:48 +0000 | [diff] [blame] | 107 | if (arm_compute::CLScheduler::get().is_initialised() && arm_compute::CLScheduler::get().context()() != NULL) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 108 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 109 | // Wait for all queued CL requests to finish before reinitialising it. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 110 | arm_compute::CLScheduler::get().sync(); |
| 111 | } |
| 112 | |
| 113 | try |
| 114 | { |
| 115 | arm_compute::CLKernelLibrary::get().clear_programs_cache(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 116 | // 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] | 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 | // |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 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 |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 | // |
| 131 | ARMNN_DISABLE_LEAK_CHECKING_IN_SCOPE(); |
| 132 | context = cl::Context(device); |
| 133 | } |
| 134 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | // NOTE: In this specific case profiling has to be enabled on the command queue |
| 136 | // in order for the CLTuner to work. |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 137 | bool profilingNeededForClTuner = updateTunedParameters && m_Tuner && |
| 138 | m_Tuner->tune_new_kernels(); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 139 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 140 | if (m_ProfilingEnabled || profilingNeededForClTuner) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 141 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | // Create a new queue with profiling enabled. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 143 | commandQueue = cl::CommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE); |
| 144 | } |
| 145 | else |
| 146 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 147 | // Use default queue. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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); |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 160 | arm_compute::CLScheduler::get().init(context, commandQueue, device, m_Tuner); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void ClContextControl::ClearClCache() |
| 164 | { |
| 165 | DoLoadOpenClRuntime(true); |
| 166 | } |
| 167 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 168 | armnn::IGpuAccTunedParameters* IGpuAccTunedParameters::CreateRaw(armnn::IGpuAccTunedParameters::Mode mode, |
| 169 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 170 | { |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 171 | return new ClTunedParameters(mode, tuningLevel); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 174 | armnn::IGpuAccTunedParametersPtr IGpuAccTunedParameters::Create(armnn::IGpuAccTunedParameters::Mode mode, |
| 175 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 176 | { |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 177 | return IGpuAccTunedParametersPtr(CreateRaw(mode, tuningLevel), &IGpuAccTunedParameters::Destroy); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 178 | } |
| 179 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 180 | void IGpuAccTunedParameters::Destroy(IGpuAccTunedParameters* params) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 181 | { |
| 182 | delete params; |
| 183 | } |
| 184 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 185 | ClTunedParameters::ClTunedParameters(armnn::IGpuAccTunedParameters::Mode mode, |
| 186 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 187 | : m_Mode(mode) |
Ruomei Yan | 8b194fb | 2019-05-03 12:10:19 +0100 | [diff] [blame] | 188 | , m_TuningLevel(tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 189 | , m_Tuner(mode == ClTunedParameters::Mode::UpdateTunedParameters) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 190 | { |
| 191 | } |
| 192 | |
| 193 | void ClTunedParameters::Load(const char* filename) |
| 194 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 + "': " + |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 202 | e.what()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 203 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void ClTunedParameters::Save(const char* filename) const |
| 207 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 + "': " + |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 215 | e.what()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 216 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | } // namespace armnn |