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 | |
James Ward | 47fce87 | 2020-09-10 11:57:28 +0100 | [diff] [blame] | 18 | #include <fmt/format.h> |
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, |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 31 | arm_compute::CLGEMMHeuristicsHandle* heuristicsHandle, |
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) |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 34 | , m_HeuristicsHandle(heuristicsHandle) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 35 | , m_ProfilingEnabled(profilingEnabled) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 36 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 37 | // 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] | 38 | IgnoreUnused(m_ProfilingEnabled); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 39 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 40 | try |
| 41 | { |
| 42 | std::vector<cl::Platform> platforms; |
| 43 | cl::Platform::get(&platforms); |
| 44 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 45 | // Selects default platform for the first element. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 46 | cl::Platform::setDefault(platforms[0]); |
| 47 | |
| 48 | std::vector<cl::Device> devices; |
| 49 | platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices); |
| 50 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 51 | // Selects default device for the first element. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 52 | cl::Device::setDefault(devices[0]); |
| 53 | } |
| 54 | catch (const cl::Error& clError) |
| 55 | { |
James Ward | 47fce87 | 2020-09-10 11:57:28 +0100 | [diff] [blame] | 56 | throw ClRuntimeUnavailableException(fmt::format( |
| 57 | "Could not initialize the CL runtime. Error description: {0}. CL error code: {1}", |
| 58 | clError.what(), clError.err())); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 59 | } |
| 60 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 61 | // Removes the use of global CL context. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 62 | cl::Context::setDefault(cl::Context{}); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 63 | ARMNN_ASSERT(cl::Context::getDefault()() == NULL); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 64 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | // Removes the use of global CL command queue. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 66 | cl::CommandQueue::setDefault(cl::CommandQueue{}); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 67 | ARMNN_ASSERT(cl::CommandQueue::getDefault()() == NULL); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 68 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 69 | // Always load the OpenCL runtime. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 70 | LoadOpenClRuntime(); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | ClContextControl::~ClContextControl() |
| 74 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 75 | // 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] | 76 | try |
| 77 | { |
| 78 | UnloadOpenClRuntime(); |
| 79 | } |
| 80 | catch (const cl::Error& clError) |
| 81 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 82 | // This should not happen, it is ignored if it does. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 83 | |
| 84 | // Coverity fix: BOOST_LOG_TRIVIAL (previously used here to report the error) may throw an |
| 85 | // exception of type std::length_error. |
| 86 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 87 | std::cerr << "A CL error occurred unloading the runtime tuner parameters: " |
| 88 | << clError.what() << ". CL error code is: " << clError.err() << std::endl; |
| 89 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void ClContextControl::LoadOpenClRuntime() |
| 93 | { |
| 94 | DoLoadOpenClRuntime(true); |
| 95 | } |
| 96 | |
| 97 | void ClContextControl::UnloadOpenClRuntime() |
| 98 | { |
| 99 | DoLoadOpenClRuntime(false); |
| 100 | } |
| 101 | |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 102 | void ClContextControl::DoLoadOpenClRuntime(bool updateTunedParameters) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 103 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 104 | cl::Device device = cl::Device::getDefault(); |
| 105 | cl::Context context; |
| 106 | cl::CommandQueue commandQueue; |
| 107 | |
James Conroy | 663c184 | 2019-11-01 15:21:48 +0000 | [diff] [blame] | 108 | if (arm_compute::CLScheduler::get().is_initialised() && arm_compute::CLScheduler::get().context()() != NULL) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 109 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | // Wait for all queued CL requests to finish before reinitialising it. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 111 | arm_compute::CLScheduler::get().sync(); |
| 112 | } |
| 113 | |
| 114 | try |
| 115 | { |
| 116 | arm_compute::CLKernelLibrary::get().clear_programs_cache(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 117 | // 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] | 118 | // context references); it is initialised again, with a proper context, later. |
| 119 | arm_compute::CLScheduler::get().init(context, commandQueue, device); |
| 120 | arm_compute::CLKernelLibrary::get().init(".", context, device); |
| 121 | |
| 122 | { |
| 123 | // |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 124 | // Here we replace the context with a new one in which |
| 125 | // the memory leak checks show it as an extra allocation but |
| 126 | // because of the scope of the leak checks, it doesn't count |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 127 | // the disposal of the original object. On the other hand it |
| 128 | // does count the creation of this context which it flags |
| 129 | // as a memory leak. By adding the following line we prevent |
| 130 | // this to happen. |
| 131 | // |
| 132 | ARMNN_DISABLE_LEAK_CHECKING_IN_SCOPE(); |
| 133 | context = cl::Context(device); |
| 134 | } |
| 135 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 136 | // NOTE: In this specific case profiling has to be enabled on the command queue |
| 137 | // in order for the CLTuner to work. |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 138 | bool profilingNeededForClTuner = updateTunedParameters && m_Tuner && |
| 139 | m_Tuner->tune_new_kernels(); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 140 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 141 | if (m_ProfilingEnabled || profilingNeededForClTuner) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 142 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 143 | // Create a new queue with profiling enabled. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 144 | commandQueue = cl::CommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE); |
| 145 | } |
| 146 | else |
| 147 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 148 | // Use default queue. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 149 | commandQueue = cl::CommandQueue(context, device); |
| 150 | } |
| 151 | } |
| 152 | catch (const cl::Error& clError) |
| 153 | { |
James Ward | 47fce87 | 2020-09-10 11:57:28 +0100 | [diff] [blame] | 154 | throw ClRuntimeUnavailableException(fmt::format( |
| 155 | "Could not initialize the CL runtime. Error description: {0}. CL error code: {1}", |
| 156 | clError.what(), clError.err())); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Note the first argument (path to cl source code) will be ignored as they should be embedded in the armcompute. |
| 160 | arm_compute::CLKernelLibrary::get().init(".", context, device); |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 161 | arm_compute::CLScheduler::get().init(context, commandQueue, device, m_Tuner, m_HeuristicsHandle); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void ClContextControl::ClearClCache() |
| 165 | { |
| 166 | DoLoadOpenClRuntime(true); |
| 167 | } |
| 168 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 169 | armnn::IGpuAccTunedParameters* IGpuAccTunedParameters::CreateRaw(armnn::IGpuAccTunedParameters::Mode mode, |
| 170 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 171 | { |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 172 | return new ClTunedParameters(mode, tuningLevel); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 175 | armnn::IGpuAccTunedParametersPtr IGpuAccTunedParameters::Create(armnn::IGpuAccTunedParameters::Mode mode, |
| 176 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 177 | { |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 178 | return IGpuAccTunedParametersPtr(CreateRaw(mode, tuningLevel), &IGpuAccTunedParameters::Destroy); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 179 | } |
| 180 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 181 | void IGpuAccTunedParameters::Destroy(IGpuAccTunedParameters* params) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 182 | { |
| 183 | delete params; |
| 184 | } |
| 185 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 186 | ClTunedParameters::ClTunedParameters(armnn::IGpuAccTunedParameters::Mode mode, |
| 187 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 188 | : m_Mode(mode) |
Ruomei Yan | 8b194fb | 2019-05-03 12:10:19 +0100 | [diff] [blame] | 189 | , m_TuningLevel(tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 190 | , m_Tuner(mode == ClTunedParameters::Mode::UpdateTunedParameters) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 191 | { |
| 192 | } |
| 193 | |
| 194 | void ClTunedParameters::Load(const char* filename) |
| 195 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 196 | try |
| 197 | { |
| 198 | m_Tuner.load_from_file(filename); |
| 199 | } |
| 200 | catch (const std::exception& e) |
| 201 | { |
| 202 | throw armnn::Exception(std::string("Failed to load tuned parameters file '") + filename + "': " + |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 203 | e.what()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 204 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void ClTunedParameters::Save(const char* filename) const |
| 208 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 209 | try |
| 210 | { |
| 211 | m_Tuner.save_to_file(filename); |
| 212 | } |
| 213 | catch (const std::exception& e) |
| 214 | { |
| 215 | 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] | 216 | e.what()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 217 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | } // namespace armnn |