Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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" |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 31 | |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 32 | #include <cerrno> |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 33 | #include <fstream> |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 34 | #include <limits> |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 35 | #include <memory> |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 36 | #include <string> |
| 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) |
| 41 | : real_clEnqueueNDRangeKernel(nullptr), _tuning_params_table(), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuning_info(tuning_info), _tuner_mode(CLTunerMode::NORMAL) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 45 | bool CLTuner::kernel_event_is_set() const |
| 46 | { |
| 47 | return _kernel_event() != nullptr; |
| 48 | } |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 49 | void CLTuner::set_cl_kernel_event(cl_event kernel_event) |
| 50 | { |
| 51 | _kernel_event = kernel_event; |
| 52 | } |
| 53 | |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 54 | void CLTuner::set_tune_new_kernels(bool tune_new_kernels) |
| 55 | { |
| 56 | _tune_new_kernels = tune_new_kernels; |
| 57 | } |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 58 | bool CLTuner::tune_new_kernels() const |
| 59 | { |
| 60 | return _tune_new_kernels; |
| 61 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 62 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 63 | void CLTuner::set_tuner_mode(CLTunerMode mode) |
| 64 | { |
| 65 | _tuner_mode = mode; |
| 66 | } |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 67 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 68 | CLTunerMode CLTuner::get_tuner_mode() const |
| 69 | { |
| 70 | return _tuner_mode; |
| 71 | } |
| 72 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 73 | void CLTuner::tune_kernel_static(ICLKernel &kernel) |
| 74 | { |
| 75 | ARM_COMPUTE_UNUSED(kernel); |
| 76 | } |
| 77 | |
| 78 | void CLTuner::tune_kernel_dynamic(ICLKernel &kernel) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 79 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 80 | ITensorPack pack; |
| 81 | tune_kernel_dynamic(kernel, pack); |
Georgios Pinitas | 9c82e01 | 2020-07-17 12:47:56 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 84 | void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors) |
Georgios Pinitas | 9c82e01 | 2020-07-17 12:47:56 +0100 | [diff] [blame] | 85 | { |
Giorgio Arena | 5d42b46 | 2019-07-26 15:54:20 +0100 | [diff] [blame] | 86 | // Get the configuration ID from the kernel and append GPU target name and number of available compute units |
| 87 | 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] | 88 | |
Giorgio Arena | 5d42b46 | 2019-07-26 15:54:20 +0100 | [diff] [blame] | 89 | // 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 |
| 90 | if(kernel.config_id() != arm_compute::default_config_id) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 91 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 92 | auto p = _tuning_params_table.find(config_id); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 93 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 94 | if(p == _tuning_params_table.end()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 95 | { |
| 96 | if(_tune_new_kernels) |
| 97 | { |
| 98 | // Find the optimal LWS for the kernel |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 99 | CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, tensors); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 100 | |
| 101 | // Insert the optimal LWS in the table |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 102 | add_tuning_params(config_id, opt_tuning_params); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 103 | |
| 104 | // Set Local-Workgroup-Size |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 105 | kernel.set_lws_hint(opt_tuning_params.get_lws()); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | // Set Local-Workgroup-Size |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 111 | kernel.set_lws_hint(p->second.get_lws()); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void CLTuner::add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws) |
| 117 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 118 | add_tuning_params(kernel_id, CLTuningParams(optimal_lws)); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 121 | void CLTuner::add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params) |
| 122 | { |
| 123 | _tuning_params_table.emplace(kernel_id, optimal_tuning_params); |
| 124 | } |
| 125 | |
| 126 | CLTuningParams CLTuner::find_optimal_tuning_params(ICLKernel &kernel, ITensorPack &tensors) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 127 | { |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 128 | // Profiling queue |
| 129 | cl::CommandQueue queue_profiler; |
| 130 | |
| 131 | // Extract real OpenCL function to intercept |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 132 | if(real_clEnqueueNDRangeKernel == nullptr) |
| 133 | { |
| 134 | real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 135 | } |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 136 | |
| 137 | // Get the default queue |
| 138 | cl::CommandQueue default_queue = CLScheduler::get().queue(); |
| 139 | |
| 140 | // Check if we can use the OpenCL timer with the default queue |
| 141 | cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>(); |
| 142 | |
| 143 | if((props & CL_QUEUE_PROFILING_ENABLE) == 0) |
| 144 | { |
| 145 | // Set the queue for profiling |
| 146 | queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | queue_profiler = default_queue; |
| 151 | } |
| 152 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 153 | // Start intercepting enqueues: |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 154 | 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, |
| 155 | const cl_event * event_wait_list, cl_event * event) |
| 156 | { |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 157 | if(this->kernel_event_is_set()) |
| 158 | { |
| 159 | // 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. |
| 160 | return CL_SUCCESS; |
| 161 | } |
| 162 | cl_event tmp; |
| 163 | cl_int retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp); |
| 164 | |
| 165 | // Set OpenCL event |
| 166 | this->set_cl_kernel_event(tmp); |
| 167 | |
Vidhya Sudhan Loganathan | ca65af3 | 2019-02-07 11:14:42 +0000 | [diff] [blame] | 168 | if(event != nullptr) |
| 169 | { |
| 170 | //return cl_event from the intercepted call |
| 171 | clRetainEvent(tmp); |
| 172 | *event = tmp; |
| 173 | } |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 174 | return retval; |
| 175 | }; |
| 176 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 177 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 178 | cl::NDRange gws = ICLKernel::gws_from_window(kernel.window()); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 179 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 180 | // Run the kernel with default lws to be used as baseline |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 181 | const bool inject_memory = !tensors.empty(); |
| 182 | inject_memory ? kernel.run_op(tensors, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler); |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 183 | |
| 184 | queue_profiler.finish(); |
| 185 | |
| 186 | const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>(); |
| 187 | const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>(); |
| 188 | cl_ulong min_exec_time = end - start; |
| 189 | _kernel_event = nullptr; |
| 190 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 191 | cl::NDRange opt_lws = cl::NullRange; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 192 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 193 | // Construct the list of tuning parameters values to be tested based on the tuner mode. |
| 194 | auto lws_list = cl_tuner::get_tuning_parameters_list(_tuner_mode, gws); |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 195 | for(size_t i = 0; i < lws_list->size(); ++i) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 196 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 197 | cl::NDRange lws_test = (*lws_list)[i].get_lws(); |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 198 | auto x = lws_test[0]; |
| 199 | auto y = lws_test[1]; |
| 200 | auto z = lws_test[2]; |
Gian Marco Iodice | deaed2d | 2019-05-14 17:11:53 +0100 | [diff] [blame] | 201 | 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] | 202 | |
| 203 | if(invalid_lws) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 204 | { |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 205 | continue; |
| 206 | } |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 207 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 208 | //Set the Local-Workgroup-Size |
| 209 | kernel.set_lws_hint(lws_test); |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 210 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 211 | // Run the kernel |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 212 | inject_memory ? kernel.run_op(tensors, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler); |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 213 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 214 | queue_profiler.finish(); |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 215 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 216 | const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>(); |
| 217 | const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>(); |
| 218 | const cl_ulong diff = end - start; |
| 219 | _kernel_event = nullptr; |
Gian Marco Iodice | deaed2d | 2019-05-14 17:11:53 +0100 | [diff] [blame] | 220 | |
Vidhya Sudhan Loganathan | 050471e | 2019-04-25 09:27:24 +0100 | [diff] [blame] | 221 | // Check the execution time |
| 222 | if(diff < min_exec_time) |
| 223 | { |
| 224 | min_exec_time = diff; |
| 225 | opt_lws = cl::NDRange(x, y, z); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 229 | // Restore real function |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 230 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel; |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 231 | return CLTuningParams(opt_lws); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void CLTuner::import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table) |
| 235 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 236 | _tuning_params_table.clear(); |
| 237 | for(auto && params : lws_table) |
| 238 | { |
| 239 | add_tuning_params(params.first, CLTuningParams(params.second)); |
| 240 | } |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 243 | const std::unordered_map<std::string, cl::NDRange> &CLTuner::lws_table() |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 244 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 245 | _lws_table.clear(); |
| 246 | for(auto && params : _tuning_params_table) |
| 247 | { |
| 248 | _lws_table.emplace(params.first, params.second.get_lws()); |
| 249 | } |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 250 | return _lws_table; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 253 | const std::unordered_map<std::string, CLTuningParams> &CLTuner::tuning_params_table() const |
| 254 | { |
| 255 | return _tuning_params_table; |
| 256 | } |
| 257 | |
| 258 | void CLTuner::import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table) |
| 259 | { |
| 260 | _tuning_params_table.clear(); |
| 261 | _tuning_params_table = tuning_params_table; |
| 262 | } |
| 263 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 264 | void CLTuner::load_from_file(const std::string &filename) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 265 | { |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 266 | std::ifstream fs; |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 267 | fs.exceptions(std::ifstream::badbit); |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 268 | fs.open(filename, std::ios::in); |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 269 | if(!fs.is_open()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 270 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 271 | 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] | 272 | } |
| 273 | std::string line; |
| 274 | while(!std::getline(fs, line).fail()) |
| 275 | { |
| 276 | std::istringstream ss(line); |
| 277 | std::string token; |
| 278 | if(std::getline(ss, token, ';').fail()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 279 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 280 | ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s (Should be of the form 'kernel_id;lws[0];lws[1];lws[2]')", ss.str().c_str(), filename.c_str()); |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 281 | } |
| 282 | std::string kernel_id = token; |
| 283 | cl::NDRange lws(1, 1, 1); |
| 284 | for(int i = 0; i < 3; i++) |
| 285 | { |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 286 | if(std::getline(ss, token, ';').fail()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 287 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 288 | ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s (Should be of the form 'kernel_id;lws[0];lws[1];lws[2]')", ss.str().c_str(), filename.c_str()); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 289 | } |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 290 | lws.get()[i] = support::cpp11::stoi(token); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 291 | } |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 292 | |
| 293 | // If all dimensions are 0: reset to NullRange (i.e nullptr) |
| 294 | if(lws[0] == 0 && lws[1] == 0 && lws[2] == 0) |
| 295 | { |
| 296 | lws = cl::NullRange; |
| 297 | } |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 298 | add_tuning_params(kernel_id, lws); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 299 | } |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 300 | fs.close(); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 301 | _tuning_info.tune_lws = true; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 304 | bool CLTuner::save_to_file(const std::string &filename) const |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 305 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 306 | if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty()) |
| 307 | { |
| 308 | return false; |
| 309 | } |
| 310 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 311 | std::ofstream fs; |
| 312 | fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 313 | fs.open(filename, std::ios::out); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 314 | for(auto const &kernel_data : _tuning_params_table) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 315 | { |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 316 | const cl::NDRange lws = CLTuningParams(kernel_data.second).get_lws(); |
| 317 | fs << kernel_data.first << ";" << lws[0] << ";" << lws[1] << ";" << lws[2] << std::endl; |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 318 | } |
| 319 | fs.close(); |
Manuel Bottini | b56c175 | 2020-11-18 17:56:30 +0000 | [diff] [blame] | 320 | return true; |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 321 | } |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 322 | } // namespace arm_compute |