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