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 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 12 | #include <arm_compute/core/CL/CLKernelLibrary.h> |
| 13 | #include <arm_compute/runtime/CL/CLScheduler.h> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 14 | |
| 15 | #include <boost/assert.hpp> |
| 16 | #include <boost/format.hpp> |
| 17 | #include <boost/log/trivial.hpp> |
| 18 | #include <boost/polymorphic_cast.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 19 | #include <boost/core/ignore_unused.hpp> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 31 | ClContextControl::ClContextControl(IGpuAccTunedParameters* clTunedParameters, |
| 32 | bool profilingEnabled) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 33 | : m_clTunedParameters(boost::polymorphic_downcast<ClTunedParameters*>(clTunedParameters)) |
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. |
| 37 | boost::ignore_unused(m_ProfilingEnabled); |
| 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 | |
| 101 | void ClContextControl::DoLoadOpenClRuntime(bool useTunedParameters) |
| 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 | |
| 107 | if (arm_compute::CLScheduler::get().context()() != NULL) |
| 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. |
| 137 | bool profilingNeededForClTuner = useTunedParameters && m_clTunedParameters && |
| 138 | m_clTunedParameters->m_Mode == IGpuAccTunedParameters::Mode::UpdateTunedParameters; |
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); |
| 160 | |
| 161 | arm_compute::ICLTuner* tuner = nullptr; |
| 162 | if (useTunedParameters && m_clTunedParameters) |
| 163 | { |
| 164 | tuner = &m_clTunedParameters->m_Tuner; |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 165 | auto clTuner = boost::polymorphic_downcast<arm_compute::CLTuner*>(tuner); |
| 166 | |
| 167 | auto ConvertTuningLevel = [](IGpuAccTunedParameters::TuningLevel level) |
| 168 | { |
| 169 | switch(level) |
| 170 | { |
| 171 | case IGpuAccTunedParameters::TuningLevel::Rapid: |
| 172 | return arm_compute::CLTunerMode::RAPID; |
| 173 | case IGpuAccTunedParameters::TuningLevel::Normal: |
| 174 | return arm_compute::CLTunerMode::NORMAL; |
| 175 | case IGpuAccTunedParameters::TuningLevel::Exhaustive: |
| 176 | return arm_compute::CLTunerMode::EXHAUSTIVE; |
| 177 | default: |
| 178 | { |
| 179 | BOOST_ASSERT_MSG(false, "Tuning level not recognised."); |
| 180 | return arm_compute::CLTunerMode::NORMAL; |
| 181 | } |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | clTuner->set_tuner_mode(ConvertTuningLevel(m_clTunedParameters->m_TuningLevel)); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 186 | } |
| 187 | arm_compute::CLScheduler::get().init(context, commandQueue, device, tuner); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void ClContextControl::ClearClCache() |
| 191 | { |
| 192 | DoLoadOpenClRuntime(true); |
| 193 | } |
| 194 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 195 | armnn::IGpuAccTunedParameters* IGpuAccTunedParameters::CreateRaw(armnn::IGpuAccTunedParameters::Mode mode, |
| 196 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 197 | { |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 198 | return new ClTunedParameters(mode, tuningLevel); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 201 | armnn::IGpuAccTunedParametersPtr IGpuAccTunedParameters::Create(armnn::IGpuAccTunedParameters::Mode mode, |
| 202 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 203 | { |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 204 | return IGpuAccTunedParametersPtr(CreateRaw(mode, tuningLevel), &IGpuAccTunedParameters::Destroy); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 205 | } |
| 206 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 207 | void IGpuAccTunedParameters::Destroy(IGpuAccTunedParameters* params) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 208 | { |
| 209 | delete params; |
| 210 | } |
| 211 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 212 | ClTunedParameters::ClTunedParameters(armnn::IGpuAccTunedParameters::Mode mode, |
| 213 | armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 214 | : m_Mode(mode) |
Ruomei Yan | 8b194fb | 2019-05-03 12:10:19 +0100 | [diff] [blame] | 215 | , m_TuningLevel(tuningLevel) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 216 | , m_Tuner(mode == ClTunedParameters::Mode::UpdateTunedParameters) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 217 | { |
| 218 | } |
| 219 | |
| 220 | void ClTunedParameters::Load(const char* filename) |
| 221 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 222 | try |
| 223 | { |
| 224 | m_Tuner.load_from_file(filename); |
| 225 | } |
| 226 | catch (const std::exception& e) |
| 227 | { |
| 228 | throw armnn::Exception(std::string("Failed to load tuned parameters file '") + filename + "': " + |
| 229 | e.what()); |
| 230 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void ClTunedParameters::Save(const char* filename) const |
| 234 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 235 | try |
| 236 | { |
| 237 | m_Tuner.save_to_file(filename); |
| 238 | } |
| 239 | catch (const std::exception& e) |
| 240 | { |
| 241 | throw armnn::Exception(std::string("Failed to save tuned parameters file to '") + filename + "': " + |
| 242 | e.what()); |
| 243 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | } // namespace armnn |