Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2022 Arm Limited. |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/runtime/CL/CLTuner.h" |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 25 | #include "arm_compute/runtime/CL/tuners/CLTuningParametersList.h" |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 26 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 29 | #include "src/core/CL/ICLKernel.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 30 | #include "support/StringSupport.h" |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 31 | #if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |
| 32 | #include "src/gpu/cl/kernels/experimental/dynamic_fusion/ClCompositeKernel.h" |
| 33 | #endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 34 | |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 35 | #include <cerrno> |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 36 | #include <fstream> |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 37 | #include <limits> |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 38 | |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 41 | CLTuner::CLTuner(bool tune_new_kernels, CLTuningInfo tuning_info) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 42 | : real_clEnqueueNDRangeKernel(nullptr), _tuning_params_table(), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuning_info(tuning_info) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 46 | struct CLTuner::IKernelData |
| 47 | { |
| 48 | virtual ~IKernelData() = default; |
| 49 | virtual void do_run(ICLKernel &kernel, cl::CommandQueue &queue) = 0; |
| 50 | }; |
| 51 | struct DefaultKernelData : public CLTuner::IKernelData |
| 52 | { |
| 53 | DefaultKernelData(ITensorPack &tensors) |
| 54 | : _tensors{ tensors } |
| 55 | { |
| 56 | } |
| 57 | ~DefaultKernelData() override = default; |
| 58 | void do_run(ICLKernel &kernel, cl::CommandQueue &queue) override |
| 59 | { |
| 60 | const bool inject_memory = !_tensors.empty(); |
| 61 | inject_memory ? kernel.run_op(_tensors, kernel.window(), queue) : kernel.run(kernel.window(), queue); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | ITensorPack &_tensors; |
| 66 | }; |
| 67 | |
| 68 | #if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |
| 69 | struct CompositeKernelData : public CLTuner::IKernelData |
| 70 | { |
SiCong Li | b63b119 | 2022-01-28 18:24:39 +0000 | [diff] [blame] | 71 | CompositeKernelData(ITensorPack &tensors, const experimental::dynamic_fusion::ClExecutionDescriptor &exec_desc) |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 72 | : _tensors{ tensors }, _exec_desc{ exec_desc } |
| 73 | { |
| 74 | } |
| 75 | ~CompositeKernelData() override = default; |
| 76 | void do_run(ICLKernel &kernel, cl::CommandQueue &queue) override |
| 77 | { |
| 78 | // ClCompositeKernel is purely stateless, and thus always requires memory injection |
| 79 | kernel.run_composite_op(_tensors, kernel.window(), queue, _exec_desc); |
| 80 | } |
| 81 | |
| 82 | private: |
SiCong Li | b63b119 | 2022-01-28 18:24:39 +0000 | [diff] [blame] | 83 | ITensorPack &_tensors; |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 84 | const experimental::dynamic_fusion::ClExecutionDescriptor &_exec_desc; |
| 85 | }; |
| 86 | #endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |
| 87 | |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 88 | bool CLTuner::kernel_event_is_set() const |
| 89 | { |
| 90 | return _kernel_event() != nullptr; |
| 91 | } |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 92 | void CLTuner::set_cl_kernel_event(cl_event kernel_event) |
| 93 | { |
| 94 | _kernel_event = kernel_event; |
| 95 | } |
| 96 | |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 97 | void CLTuner::set_tune_new_kernels(bool tune_new_kernels) |
| 98 | { |
| 99 | _tune_new_kernels = tune_new_kernels; |
| 100 | } |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 101 | bool CLTuner::tune_new_kernels() const |
| 102 | { |
| 103 | return _tune_new_kernels; |
| 104 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 105 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 106 | void CLTuner::set_tuner_mode(CLTunerMode mode) |
| 107 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 108 | _tuning_info.tuner_mode = mode; |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 109 | } |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 110 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 111 | void CLTuner::tune_kernel_static(ICLKernel &kernel) |
| 112 | { |
| 113 | ARM_COMPUTE_UNUSED(kernel); |
| 114 | } |
| 115 | |
| 116 | void CLTuner::tune_kernel_dynamic(ICLKernel &kernel) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 117 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 118 | ITensorPack pack; |
| 119 | tune_kernel_dynamic(kernel, pack); |
Georgios Pinitas | 9c82e01 | 2020-07-17 12:47:56 +0100 | [diff] [blame] | 120 | } |
| 121 | |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 122 | void CLTuner::do_tune_kernel_dynamic(ICLKernel &kernel, IKernelData *data) |
Georgios Pinitas | 9c82e01 | 2020-07-17 12:47:56 +0100 | [diff] [blame] | 123 | { |
Giorgio Arena | 5d42b46 | 2019-07-26 15:54:20 +0100 | [diff] [blame] | 124 | // Get the configuration ID from the kernel and append GPU target name and number of available compute units |
| 125 | const std::string config_id = kernel.config_id() + "_" + string_from_target(kernel.get_target()) + "_MP" + support::cpp11::to_string(CLKernelLibrary::get().get_num_compute_units()); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 126 | |
Giorgio Arena | 5d42b46 | 2019-07-26 15:54:20 +0100 | [diff] [blame] | 127 | // Check if we need to find the Optimal LWS. If the kernel's config_id is equal to default_config_id, the kernel does not require to be tuned |
| 128 | if(kernel.config_id() != arm_compute::default_config_id) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 129 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 130 | auto p = _tuning_params_table.find(config_id); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 131 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 132 | if(p == _tuning_params_table.end()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 133 | { |
| 134 | if(_tune_new_kernels) |
| 135 | { |
| 136 | // Find the optimal LWS for the kernel |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 137 | CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, data); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 138 | |
| 139 | // Insert the optimal LWS in the table |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 140 | add_tuning_params(config_id, opt_tuning_params); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 141 | |
| 142 | // Set Local-Workgroup-Size |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 143 | kernel.set_lws_hint(opt_tuning_params.get_lws()); |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 144 | if(_tuning_info.tune_wbsm) |
| 145 | { |
| 146 | kernel.set_wbsm_hint(opt_tuning_params.get_wbsm()); |
| 147 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | // Set Local-Workgroup-Size |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 153 | kernel.set_lws_hint(p->second.get_lws()); |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 154 | if(_tuning_info.tune_wbsm) |
| 155 | { |
| 156 | kernel.set_wbsm_hint(p->second.get_wbsm()); |
| 157 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 161 | void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors) |
| 162 | { |
| 163 | DefaultKernelData data{ tensors }; |
| 164 | |
| 165 | do_tune_kernel_dynamic(kernel, &data); |
| 166 | } |
| 167 | |
| 168 | #if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |
SiCong Li | b63b119 | 2022-01-28 18:24:39 +0000 | [diff] [blame] | 169 | void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors, const experimental::dynamic_fusion::ClExecutionDescriptor &exec_desc) |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 170 | { |
| 171 | CompositeKernelData data{ tensors, exec_desc }; |
| 172 | |
| 173 | do_tune_kernel_dynamic(kernel, &data); |
| 174 | } |
| 175 | #endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 176 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 177 | void CLTuner::add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params) |
| 178 | { |
| 179 | _tuning_params_table.emplace(kernel_id, optimal_tuning_params); |
| 180 | } |
| 181 | |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 182 | CLTuningParams CLTuner::find_optimal_tuning_params(ICLKernel &kernel, IKernelData *data) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 183 | { |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 184 | // Profiling queue |
| 185 | cl::CommandQueue queue_profiler; |
| 186 | |
| 187 | // Extract real OpenCL function to intercept |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 188 | if(real_clEnqueueNDRangeKernel == nullptr) |
| 189 | { |
| 190 | real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 191 | } |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 192 | |
| 193 | // Get the default queue |
| 194 | cl::CommandQueue default_queue = CLScheduler::get().queue(); |
| 195 | |
| 196 | // Check if we can use the OpenCL timer with the default queue |
| 197 | cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>(); |
| 198 | |
| 199 | if((props & CL_QUEUE_PROFILING_ENABLE) == 0) |
| 200 | { |
| 201 | // Set the queue for profiling |
| 202 | queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | queue_profiler = default_queue; |
| 207 | } |
| 208 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 209 | // Start intercepting enqueues: |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 210 | auto interceptor = [this](cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim, const size_t *gwo, const size_t *gws, const size_t *lws, cl_uint num_events_in_wait_list, |
| 211 | const cl_event * event_wait_list, cl_event * event) |
| 212 | { |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 213 | if(this->kernel_event_is_set()) |
| 214 | { |
| 215 | // If the event is already set it means the kernel enqueue is sliced: given that we only time the first slice we can save time by skipping the other enqueues. |
| 216 | return CL_SUCCESS; |
| 217 | } |
| 218 | cl_event tmp; |
| 219 | cl_int retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp); |
| 220 | |
| 221 | // Set OpenCL event |
| 222 | this->set_cl_kernel_event(tmp); |
| 223 | |
Vidhya Sudhan Loganathan | ca65af3 | 2019-02-07 11:14:42 +0000 | [diff] [blame] | 224 | if(event != nullptr) |
| 225 | { |
| 226 | //return cl_event from the intercepted call |
| 227 | clRetainEvent(tmp); |
| 228 | *event = tmp; |
| 229 | } |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 230 | return retval; |
| 231 | }; |
| 232 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 233 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 234 | cl::NDRange gws = ICLKernel::gws_from_window(kernel.window()); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 235 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 236 | // Run the kernel with default lws to be used as baseline |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 237 | data->do_run(kernel, queue_profiler); |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 238 | |
| 239 | queue_profiler.finish(); |
| 240 | |
| 241 | const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>(); |
| 242 | const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>(); |
| 243 | cl_ulong min_exec_time = end - start; |
| 244 | _kernel_event = nullptr; |
| 245 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 246 | CLTuningParams opt_tuning_params(cl::NullRange, 0); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 247 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 248 | // Construct the list of tuning parameters values to be tested based on the tuner mode. |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 249 | auto tuning_list = cl_tuner::get_tuning_parameters_list(_tuning_info, gws); |
| 250 | for(size_t i = 0; i < tuning_list->size(); ++i) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 251 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 252 | CLTuningParams tuning_test = (*tuning_list)[i]; |
| 253 | // Setting the lws |
| 254 | cl::NDRange lws_test = tuning_test.get_lws(); |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 255 | auto x = lws_test[0]; |
| 256 | auto y = lws_test[1]; |
| 257 | auto z = lws_test[2]; |
Gian Marco Iodice | deaed2d | 2019-05-14 17:11:53 +0100 | [diff] [blame] | 258 | const bool invalid_lws = (x * y * z > kernel.get_max_workgroup_size()) || (x == 1 && y == 1 && z == 1); |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 259 | |
| 260 | if(invalid_lws) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 261 | { |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 262 | continue; |
| 263 | } |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 264 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 265 | kernel.set_lws_hint(lws_test); |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 266 | if(_tuning_info.tune_wbsm && CLKernelLibrary::get().is_wbsm_supported()) |
| 267 | { |
| 268 | cl_int wbsm_test = tuning_test.get_wbsm(); |
| 269 | kernel.set_wbsm_hint(wbsm_test); |
| 270 | } |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 271 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 272 | // Run the kernel |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 273 | data->do_run(kernel, queue_profiler); |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 274 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 275 | queue_profiler.finish(); |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 276 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 277 | const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>(); |
| 278 | const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>(); |
| 279 | const cl_ulong diff = end - start; |
| 280 | _kernel_event = nullptr; |
Gian Marco Iodice | deaed2d | 2019-05-14 17:11:53 +0100 | [diff] [blame] | 281 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 282 | // Check the execution time |
| 283 | if(diff < min_exec_time) |
| 284 | { |
| 285 | min_exec_time = diff; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 286 | opt_tuning_params.set_lws(tuning_test.get_lws()); |
| 287 | if(_tuning_info.tune_wbsm) |
| 288 | { |
| 289 | opt_tuning_params.set_wbsm(tuning_test.get_wbsm()); |
| 290 | } |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 294 | // Restore real function |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 295 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 296 | return opt_tuning_params; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 299 | const std::unordered_map<std::string, CLTuningParams> &CLTuner::tuning_params_table() const |
| 300 | { |
| 301 | return _tuning_params_table; |
| 302 | } |
| 303 | |
| 304 | void CLTuner::import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table) |
| 305 | { |
| 306 | _tuning_params_table.clear(); |
| 307 | _tuning_params_table = tuning_params_table; |
| 308 | } |
| 309 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 310 | void CLTuner::load_from_file(const std::string &filename) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 311 | { |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 312 | std::ifstream fs; |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 313 | fs.exceptions(std::ifstream::badbit); |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 314 | fs.open(filename, std::ios::in); |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 315 | if(!fs.is_open()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 316 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 317 | ARM_COMPUTE_ERROR_VAR("Failed to open '%s' (%s [%d])", filename.c_str(), strerror(errno), errno); |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 318 | } |
| 319 | std::string line; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 320 | bool header_line = true; |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 321 | while(!std::getline(fs, line).fail()) |
| 322 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 323 | if(header_line) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 324 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 325 | header_line = false; |
| 326 | size_t pos_lws = line.find("lws"); |
| 327 | size_t pos_wbsm = line.find("wbsm"); |
| 328 | _tuning_info.tune_wbsm = false; |
| 329 | if(pos_lws != std::string::npos || pos_wbsm != std::string::npos) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 330 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 331 | // The file has in the first line the parameters it has been tuned on |
| 332 | if(pos_wbsm != std::string::npos) |
| 333 | { |
| 334 | _tuning_info.tune_wbsm = true; |
| 335 | } |
| 336 | // Once the line with the tuning parameter is read we can |
| 337 | // read the next one to start collecting the values |
| 338 | if(std::getline(fs, line).fail()) |
| 339 | { |
| 340 | break; |
| 341 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 342 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 343 | } |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 344 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 345 | CLTuningParams tuning_params; |
| 346 | size_t pos = line.find(";"); |
| 347 | if(pos == std::string::npos) |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 348 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 349 | ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str()); |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 350 | } |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 351 | std::string kernel_id = line.substr(0, pos); |
| 352 | line.erase(0, pos + 1); |
| 353 | if(!tuning_params.from_string(_tuning_info, line)) |
| 354 | { |
| 355 | ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str()); |
| 356 | } |
| 357 | add_tuning_params(kernel_id, tuning_params); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 358 | } |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 359 | fs.close(); |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 362 | bool CLTuner::save_to_file(const std::string &filename) const |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 363 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 364 | if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty()) |
| 365 | { |
| 366 | return false; |
| 367 | } |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 368 | std::ofstream fs; |
| 369 | fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 370 | fs.open(filename, std::ios::out); |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 371 | std::string header_string = ""; |
| 372 | header_string += "lws"; |
| 373 | if(_tuning_info.tune_wbsm) |
| 374 | { |
| 375 | if(!header_string.empty()) |
| 376 | { |
| 377 | header_string += " "; |
| 378 | } |
| 379 | header_string += "wbsm"; |
| 380 | } |
| 381 | fs << header_string << std::endl; |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 382 | for(auto const &kernel_data : _tuning_params_table) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 383 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 384 | CLTuningParams tun_pams(kernel_data.second); |
| 385 | fs << kernel_data.first << tun_pams.to_string(_tuning_info) << std::endl; |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 386 | } |
| 387 | fs.close(); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 388 | return true; |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 389 | } |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 390 | } // namespace arm_compute |