Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 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" |
| 25 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 28 | #include "arm_compute/runtime/CL/tuners/CLTuningParametersList.h" |
| 29 | |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 30 | #include "src/common/utils/Log.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 31 | #include "src/core/CL/ICLKernel.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 32 | #include "support/StringSupport.h" |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 33 | |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 34 | #include <cerrno> |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 35 | #include <fstream> |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 36 | #include <limits> |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 37 | |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 40 | CLTuner::CLTuner(bool tune_new_kernels, CLTuningInfo tuning_info) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 41 | : real_clEnqueueNDRangeKernel(nullptr), |
| 42 | _tuning_params_table(), |
| 43 | _lws_table(), |
| 44 | _kernel_event(), |
| 45 | _tune_new_kernels(tune_new_kernels), |
| 46 | _tuning_info(tuning_info) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 50 | struct CLTuner::IKernelData |
| 51 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 52 | virtual ~IKernelData() = default; |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 53 | virtual void do_run(ICLKernel &kernel, cl::CommandQueue &queue) = 0; |
| 54 | }; |
| 55 | struct DefaultKernelData : public CLTuner::IKernelData |
| 56 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 57 | DefaultKernelData(ITensorPack &tensors) : _tensors{tensors} |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 58 | { |
| 59 | } |
| 60 | ~DefaultKernelData() override = default; |
| 61 | void do_run(ICLKernel &kernel, cl::CommandQueue &queue) override |
| 62 | { |
| 63 | const bool inject_memory = !_tensors.empty(); |
| 64 | inject_memory ? kernel.run_op(_tensors, kernel.window(), queue) : kernel.run(kernel.window(), queue); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | ITensorPack &_tensors; |
| 69 | }; |
| 70 | |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 71 | bool CLTuner::kernel_event_is_set() const |
| 72 | { |
| 73 | return _kernel_event() != nullptr; |
| 74 | } |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 75 | void CLTuner::set_cl_kernel_event(cl_event kernel_event) |
| 76 | { |
| 77 | _kernel_event = kernel_event; |
| 78 | } |
| 79 | |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 80 | void CLTuner::set_tune_new_kernels(bool tune_new_kernels) |
| 81 | { |
| 82 | _tune_new_kernels = tune_new_kernels; |
| 83 | } |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 84 | bool CLTuner::tune_new_kernels() const |
| 85 | { |
| 86 | return _tune_new_kernels; |
| 87 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 88 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 89 | void CLTuner::set_tuner_mode(CLTunerMode mode) |
| 90 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 91 | _tuning_info.tuner_mode = mode; |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 92 | } |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 93 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 94 | void CLTuner::tune_kernel_static(ICLKernel &kernel) |
| 95 | { |
| 96 | ARM_COMPUTE_UNUSED(kernel); |
| 97 | } |
| 98 | |
| 99 | void CLTuner::tune_kernel_dynamic(ICLKernel &kernel) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 100 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 101 | ITensorPack pack; |
| 102 | tune_kernel_dynamic(kernel, pack); |
Georgios Pinitas | 9c82e01 | 2020-07-17 12:47:56 +0100 | [diff] [blame] | 103 | } |
| 104 | |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 105 | void CLTuner::do_tune_kernel_dynamic(ICLKernel &kernel, IKernelData *data) |
Georgios Pinitas | 9c82e01 | 2020-07-17 12:47:56 +0100 | [diff] [blame] | 106 | { |
Giorgio Arena | 5d42b46 | 2019-07-26 15:54:20 +0100 | [diff] [blame] | 107 | // Get the configuration ID from the kernel and append GPU target name and number of available compute units |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 108 | const std::string config_id = kernel.config_id() + "_" + string_from_target(kernel.get_target()) + "_MP" + |
| 109 | support::cpp11::to_string(CLKernelLibrary::get().get_num_compute_units()); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 110 | |
Giorgio Arena | 5d42b46 | 2019-07-26 15:54:20 +0100 | [diff] [blame] | 111 | // 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 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 112 | if (kernel.config_id() != arm_compute::default_config_id) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 113 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 114 | auto p = _tuning_params_table.find(config_id); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 115 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 116 | if (p == _tuning_params_table.end()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 117 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 118 | if (_tune_new_kernels) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 119 | { |
| 120 | // Find the optimal LWS for the kernel |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 121 | CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, data); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 122 | |
| 123 | // Insert the optimal LWS in the table |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 124 | add_tuning_params(config_id, opt_tuning_params); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 125 | |
| 126 | // Set Local-Workgroup-Size |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 127 | kernel.set_lws_hint(opt_tuning_params.get_lws()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 128 | if (_tuning_info.tune_wbsm) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 129 | { |
| 130 | kernel.set_wbsm_hint(opt_tuning_params.get_wbsm()); |
| 131 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | // Set Local-Workgroup-Size |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 137 | kernel.set_lws_hint(p->second.get_lws()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 138 | if (_tuning_info.tune_wbsm) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 139 | { |
| 140 | kernel.set_wbsm_hint(p->second.get_wbsm()); |
| 141 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 145 | void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors) |
| 146 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 147 | DefaultKernelData data{tensors}; |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 148 | |
| 149 | do_tune_kernel_dynamic(kernel, &data); |
| 150 | } |
| 151 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 152 | void CLTuner::add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params) |
| 153 | { |
| 154 | _tuning_params_table.emplace(kernel_id, optimal_tuning_params); |
| 155 | } |
| 156 | |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 157 | CLTuningParams CLTuner::find_optimal_tuning_params(ICLKernel &kernel, IKernelData *data) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 158 | { |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 159 | // Profiling queue |
| 160 | cl::CommandQueue queue_profiler; |
| 161 | |
| 162 | // Extract real OpenCL function to intercept |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 163 | if (real_clEnqueueNDRangeKernel == nullptr) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 164 | { |
| 165 | real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 166 | } |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 167 | |
| 168 | // Get the default queue |
| 169 | cl::CommandQueue default_queue = CLScheduler::get().queue(); |
| 170 | |
| 171 | // Check if we can use the OpenCL timer with the default queue |
| 172 | cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>(); |
| 173 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 174 | if ((props & CL_QUEUE_PROFILING_ENABLE) == 0) |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 175 | { |
| 176 | // Set the queue for profiling |
| 177 | queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | queue_profiler = default_queue; |
| 182 | } |
| 183 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 184 | // Start intercepting enqueues: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 185 | auto interceptor = [this](cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim, const size_t *gwo, |
| 186 | const size_t *gws, const size_t *lws, cl_uint num_events_in_wait_list, |
| 187 | const cl_event *event_wait_list, cl_event *event) |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 188 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 189 | if (this->kernel_event_is_set()) |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 190 | { |
| 191 | // 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. |
| 192 | return CL_SUCCESS; |
| 193 | } |
| 194 | cl_event tmp; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 195 | cl_int retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, |
| 196 | num_events_in_wait_list, event_wait_list, &tmp); |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 197 | |
| 198 | // Set OpenCL event |
| 199 | this->set_cl_kernel_event(tmp); |
| 200 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 201 | if (event != nullptr) |
Vidhya Sudhan Loganathan | ca65af3 | 2019-02-07 11:14:42 +0000 | [diff] [blame] | 202 | { |
| 203 | //return cl_event from the intercepted call |
| 204 | clRetainEvent(tmp); |
| 205 | *event = tmp; |
| 206 | } |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 207 | return retval; |
| 208 | }; |
| 209 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 210 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 211 | // Run the kernel with default lws to be used as baseline |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 212 | data->do_run(kernel, queue_profiler); |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 213 | |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 214 | /// Get the cached gws used by the kernel |
| 215 | /// NOTE: The window configured inside configure() is usually changed in run(). Thus we should not calculate gws |
| 216 | /// from this static window. Instead we get the real gws used (and cached) by run() in the previous step. |
| 217 | /// This is only a temporary workaround. An ideal solution involves decoupling the execution window from run() / run_op() |
| 218 | /// Please see COMPMID-5934 |
| 219 | cl::NDRange gws = kernel.get_cached_gws(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 220 | ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL( |
| 221 | arm_compute::logging::LogLevel::INFO, |
| 222 | "[CLTuner] Kernel with config_id '%s' uses %s as the upper-bound for lws search", kernel.config_id().c_str(), |
| 223 | to_string(gws).c_str()); |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 224 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 225 | queue_profiler.finish(); |
| 226 | |
| 227 | const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>(); |
| 228 | const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>(); |
| 229 | cl_ulong min_exec_time = end - start; |
| 230 | _kernel_event = nullptr; |
| 231 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 232 | CLTuningParams opt_tuning_params(cl::NullRange, 0); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 233 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 234 | // 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] | 235 | auto tuning_list = cl_tuner::get_tuning_parameters_list(_tuning_info, gws); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 236 | for (size_t i = 0; i < tuning_list->size(); ++i) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 237 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 238 | CLTuningParams tuning_test = (*tuning_list)[i]; |
| 239 | // Setting the lws |
| 240 | cl::NDRange lws_test = tuning_test.get_lws(); |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 241 | auto x = lws_test[0]; |
| 242 | auto y = lws_test[1]; |
| 243 | auto z = lws_test[2]; |
Gian Marco Iodice | deaed2d | 2019-05-14 17:11:53 +0100 | [diff] [blame] | 244 | 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] | 245 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 246 | if (invalid_lws) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 247 | { |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 248 | continue; |
| 249 | } |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 250 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 251 | kernel.set_lws_hint(lws_test); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 252 | if (_tuning_info.tune_wbsm && CLKernelLibrary::get().is_wbsm_supported()) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 253 | { |
| 254 | cl_int wbsm_test = tuning_test.get_wbsm(); |
| 255 | kernel.set_wbsm_hint(wbsm_test); |
| 256 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 257 | ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(arm_compute::logging::LogLevel::INFO, "[CLTuner] Trying LWS: %s, WBSM: %d", |
SiCong Li | 47f177e | 2023-02-22 17:24:09 +0000 | [diff] [blame] | 258 | to_string(kernel.lws_hint()).c_str(), kernel.wbsm_hint()); |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 259 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 260 | // Run the kernel |
SiCong Li | 0a486cf | 2022-04-07 17:41:51 +0100 | [diff] [blame] | 261 | data->do_run(kernel, queue_profiler); |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 262 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 263 | queue_profiler.finish(); |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 264 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 265 | const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>(); |
| 266 | const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>(); |
| 267 | const cl_ulong diff = end - start; |
| 268 | _kernel_event = nullptr; |
Gian Marco Iodice | deaed2d | 2019-05-14 17:11:53 +0100 | [diff] [blame] | 269 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 270 | // Check the execution time |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 271 | if (diff < min_exec_time) |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 272 | { |
| 273 | min_exec_time = diff; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 274 | opt_tuning_params.set_lws(tuning_test.get_lws()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 275 | if (_tuning_info.tune_wbsm) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 276 | { |
| 277 | opt_tuning_params.set_wbsm(tuning_test.get_wbsm()); |
| 278 | } |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 282 | // Restore real function |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 283 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 284 | return opt_tuning_params; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 287 | const std::unordered_map<std::string, CLTuningParams> &CLTuner::tuning_params_table() const |
| 288 | { |
| 289 | return _tuning_params_table; |
| 290 | } |
| 291 | |
| 292 | void CLTuner::import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table) |
| 293 | { |
| 294 | _tuning_params_table.clear(); |
| 295 | _tuning_params_table = tuning_params_table; |
| 296 | } |
| 297 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 298 | void CLTuner::load_from_file(const std::string &filename) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 299 | { |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 300 | std::ifstream fs; |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 301 | fs.exceptions(std::ifstream::badbit); |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 302 | fs.open(filename, std::ios::in); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 303 | if (!fs.is_open()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 304 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 305 | 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] | 306 | } |
| 307 | std::string line; |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 308 | bool header_line = true; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 309 | while (!std::getline(fs, line).fail()) |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 310 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 311 | if (header_line) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 312 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 313 | header_line = false; |
| 314 | size_t pos_lws = line.find("lws"); |
| 315 | size_t pos_wbsm = line.find("wbsm"); |
| 316 | _tuning_info.tune_wbsm = false; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 317 | if (pos_lws != std::string::npos || pos_wbsm != std::string::npos) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 318 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 319 | // The file has in the first line the parameters it has been tuned on |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 320 | if (pos_wbsm != std::string::npos) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 321 | { |
| 322 | _tuning_info.tune_wbsm = true; |
| 323 | } |
| 324 | // Once the line with the tuning parameter is read we can |
| 325 | // read the next one to start collecting the values |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 326 | if (std::getline(fs, line).fail()) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 327 | { |
| 328 | break; |
| 329 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 330 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 331 | } |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 332 | |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 333 | CLTuningParams tuning_params; |
| 334 | size_t pos = line.find(";"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 335 | if (pos == std::string::npos) |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 336 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 337 | 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] | 338 | } |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 339 | std::string kernel_id = line.substr(0, pos); |
| 340 | line.erase(0, pos + 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 341 | if (!tuning_params.from_string(_tuning_info, line)) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 342 | { |
| 343 | ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str()); |
| 344 | } |
| 345 | add_tuning_params(kernel_id, tuning_params); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 346 | } |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 347 | fs.close(); |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 350 | bool CLTuner::save_to_file(const std::string &filename) const |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 351 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 352 | if (!_tune_new_kernels || _tuning_params_table.empty() || filename.empty()) |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 353 | { |
| 354 | return false; |
| 355 | } |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 356 | std::ofstream fs; |
| 357 | fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 358 | fs.open(filename, std::ios::out); |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 359 | std::string header_string = ""; |
| 360 | header_string += "lws"; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 361 | if (_tuning_info.tune_wbsm) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 362 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 363 | if (!header_string.empty()) |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 364 | { |
| 365 | header_string += " "; |
| 366 | } |
| 367 | header_string += "wbsm"; |
| 368 | } |
| 369 | fs << header_string << std::endl; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 370 | for (auto const &kernel_data : _tuning_params_table) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 371 | { |
Manuel Bottini | be9f9f9 | 2021-01-25 15:07:17 +0000 | [diff] [blame] | 372 | CLTuningParams tun_pams(kernel_data.second); |
| 373 | fs << kernel_data.first << tun_pams.to_string(_tuning_info) << std::endl; |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 374 | } |
| 375 | fs.close(); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 376 | return true; |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 377 | } |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 378 | } // namespace arm_compute |