Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | |
| 26 | #include "arm_compute/core/CL/ICLKernel.h" |
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" |
| 29 | |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 30 | #include <cerrno> |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 31 | #include <fstream> |
| 32 | #include <iostream> |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 33 | #include <limits> |
| 34 | #include <string> |
| 35 | |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace |
| 39 | { |
| 40 | /** Utility function used to initialize the LWS values to test. |
| 41 | * Only the LWS values which are power of 2 or satisfy the modulo conditions with GWS are taken into account by the CLTuner |
| 42 | * |
| 43 | * @param[in, out] lws Vector of LWS to test for a specific dimension |
| 44 | * @param[in] gws Size of the GWS |
| 45 | * @param[in] lws_max Max LKWS value allowed to be tested |
| 46 | * @param[in] mod_let_one True if the results of the modulo operation between gws and the lws can be less than one. |
| 47 | */ |
| 48 | void initialize_lws_values(std::vector<unsigned int> &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one) |
| 49 | { |
| 50 | lws.push_back(1); |
| 51 | |
| 52 | for(unsigned int i = 2; i <= lws_max; ++i) |
| 53 | { |
| 54 | // Power of two condition |
| 55 | const bool is_power_of_two = (i & (i - 1)) == 0; |
| 56 | |
| 57 | // Condition for the module accordingly with the mod_let_one flag |
| 58 | const bool mod_cond = mod_let_one ? (gws % i) <= 1 : (gws % i) == 0; |
| 59 | |
| 60 | if(mod_cond || is_power_of_two) |
| 61 | { |
| 62 | lws.push_back(i); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } // namespace |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 67 | |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 68 | CLTuner::CLTuner(bool tune_new_kernels) |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 69 | : real_clEnqueueNDRangeKernel(nullptr), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 70 | { |
| 71 | } |
| 72 | |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 73 | bool CLTuner::kernel_event_is_set() const |
| 74 | { |
| 75 | return _kernel_event() != nullptr; |
| 76 | } |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 77 | void CLTuner::set_cl_kernel_event(cl_event kernel_event) |
| 78 | { |
| 79 | _kernel_event = kernel_event; |
| 80 | } |
| 81 | |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 82 | void CLTuner::set_tune_new_kernels(bool tune_new_kernels) |
| 83 | { |
| 84 | _tune_new_kernels = tune_new_kernels; |
| 85 | } |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 86 | bool CLTuner::tune_new_kernels() const |
| 87 | { |
| 88 | return _tune_new_kernels; |
| 89 | } |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 90 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 91 | void CLTuner::tune_kernel_static(ICLKernel &kernel) |
| 92 | { |
| 93 | ARM_COMPUTE_UNUSED(kernel); |
| 94 | } |
| 95 | |
| 96 | void CLTuner::tune_kernel_dynamic(ICLKernel &kernel) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 97 | { |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 98 | // Get the configuration ID from the kernel |
| 99 | const std::string &config_id = kernel.config_id(); |
| 100 | |
| 101 | // Check if we need to find the Optimal LWS. If config_id is equal to default_config_id, the kernel does not require to be tuned |
| 102 | if(config_id != arm_compute::default_config_id) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 103 | { |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 104 | auto p = _lws_table.find(config_id); |
| 105 | |
| 106 | if(p == _lws_table.end()) |
| 107 | { |
| 108 | if(_tune_new_kernels) |
| 109 | { |
| 110 | // Find the optimal LWS for the kernel |
| 111 | cl::NDRange opt_lws = find_optimal_lws(kernel); |
| 112 | |
| 113 | // Insert the optimal LWS in the table |
| 114 | add_lws_to_table(config_id, opt_lws); |
| 115 | |
| 116 | // Set Local-Workgroup-Size |
| 117 | kernel.set_lws_hint(opt_lws); |
| 118 | } |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | // Set Local-Workgroup-Size |
| 123 | kernel.set_lws_hint(p->second); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void CLTuner::add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws) |
| 129 | { |
| 130 | _lws_table.emplace(kernel_id, optimal_lws); |
| 131 | } |
| 132 | |
| 133 | cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel) |
| 134 | { |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 135 | // Profiling queue |
| 136 | cl::CommandQueue queue_profiler; |
| 137 | |
| 138 | // Extract real OpenCL function to intercept |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 139 | if(real_clEnqueueNDRangeKernel == nullptr) |
| 140 | { |
| 141 | real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 142 | } |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 143 | |
| 144 | // Get the default queue |
| 145 | cl::CommandQueue default_queue = CLScheduler::get().queue(); |
| 146 | |
| 147 | // Check if we can use the OpenCL timer with the default queue |
| 148 | cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>(); |
| 149 | |
| 150 | if((props & CL_QUEUE_PROFILING_ENABLE) == 0) |
| 151 | { |
| 152 | // Set the queue for profiling |
| 153 | queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | queue_profiler = default_queue; |
| 158 | } |
| 159 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 160 | // Start intercepting enqueues: |
Anthony Barbier | 48c19f1 | 2018-04-20 11:31:52 +0100 | [diff] [blame] | 161 | 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, |
| 162 | const cl_event * event_wait_list, cl_event * event) |
| 163 | { |
| 164 | ARM_COMPUTE_ERROR_ON_MSG(event != nullptr, "Not supported"); |
| 165 | ARM_COMPUTE_UNUSED(event); |
| 166 | if(this->kernel_event_is_set()) |
| 167 | { |
| 168 | // 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. |
| 169 | return CL_SUCCESS; |
| 170 | } |
| 171 | cl_event tmp; |
| 172 | cl_int retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp); |
| 173 | |
| 174 | // Set OpenCL event |
| 175 | this->set_cl_kernel_event(tmp); |
| 176 | |
| 177 | return retval; |
| 178 | }; |
| 179 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 180 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 181 | cl_ulong min_exec_time = std::numeric_limits<cl_ulong>::max(); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 182 | |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 183 | cl::NDRange gws = ICLKernel::gws_from_window(kernel.window()); |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 184 | cl::NDRange opt_lws = cl::NullRange; |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 185 | |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 186 | const unsigned int lws_x_max = std::min(static_cast<unsigned int>(gws[0]), 64u); |
| 187 | const unsigned int lws_y_max = std::min(static_cast<unsigned int>(gws[1]), 32u); |
| 188 | const unsigned int lws_z_max = std::min(static_cast<unsigned int>(gws[2]), 32u); |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 189 | |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 190 | std::vector<unsigned int> lws_x; |
| 191 | std::vector<unsigned int> lws_y; |
| 192 | std::vector<unsigned int> lws_z; |
| 193 | |
| 194 | // Initialize the LWS values to test |
| 195 | initialize_lws_values(lws_x, gws[0], lws_x_max, gws[2] > 16); |
| 196 | initialize_lws_values(lws_y, gws[1], lws_y_max, gws[2] > 16); |
| 197 | initialize_lws_values(lws_z, gws[2], lws_z_max, false); |
| 198 | |
| 199 | for(const auto &z : lws_z) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 200 | { |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 201 | for(const auto &y : lws_y) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 202 | { |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 203 | for(const auto &x : lws_x) |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 204 | { |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 205 | cl::NDRange lws_test = cl::NDRange(x, y, z); |
| 206 | |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 207 | bool invalid_lws = (x * y * z > kernel.get_max_workgroup_size()) || (x == 1 && y == 1 && z == 1); |
| 208 | |
| 209 | invalid_lws = invalid_lws || (x > gws[0]) || (y > gws[1]) || (z > gws[2]); |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 210 | |
| 211 | if(invalid_lws) |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 212 | { |
| 213 | continue; |
| 214 | } |
| 215 | |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 216 | //Set the Local-Workgroup-Size |
| 217 | kernel.set_lws_hint(lws_test); |
| 218 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 219 | // Run the kernel |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 220 | kernel.run(kernel.window(), queue_profiler); |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 221 | |
Georgios Pinitas | 4632e5e | 2019-02-06 14:47:59 +0000 | [diff] [blame] | 222 | queue_profiler.finish(); |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 223 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 224 | const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>(); |
| 225 | const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>(); |
| 226 | const cl_ulong diff = end - start; |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 227 | _kernel_event = nullptr; |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 228 | |
| 229 | // Check the execution time |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 230 | if(diff < min_exec_time) |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 231 | { |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 232 | min_exec_time = diff; |
Gian Marco | c78d4bc | 2018-01-25 13:49:44 +0000 | [diff] [blame] | 233 | opt_lws = cl::NDRange(x, y, z); |
| 234 | } |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 239 | // Restore real function |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 240 | CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel; |
| 241 | |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 242 | return opt_lws; |
| 243 | } |
| 244 | |
| 245 | void CLTuner::import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table) |
| 246 | { |
| 247 | _lws_table.clear(); |
| 248 | _lws_table = lws_table; |
| 249 | } |
| 250 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 251 | const std::unordered_map<std::string, cl::NDRange> &CLTuner::lws_table() const |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 252 | { |
| 253 | return _lws_table; |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 256 | void CLTuner::load_from_file(const std::string &filename) |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 257 | { |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 258 | std::ifstream fs; |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 259 | fs.exceptions(std::ifstream::badbit); |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 260 | fs.open(filename, std::ios::in); |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 261 | if(!fs.is_open()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 262 | { |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 263 | ARM_COMPUTE_ERROR("Failed to open '%s' (%s [%d])", filename.c_str(), strerror(errno), errno); |
| 264 | } |
| 265 | std::string line; |
| 266 | while(!std::getline(fs, line).fail()) |
| 267 | { |
| 268 | std::istringstream ss(line); |
| 269 | std::string token; |
| 270 | if(std::getline(ss, token, ';').fail()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 271 | { |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 272 | ARM_COMPUTE_ERROR("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()); |
| 273 | } |
| 274 | std::string kernel_id = token; |
| 275 | cl::NDRange lws(1, 1, 1); |
| 276 | for(int i = 0; i < 3; i++) |
| 277 | { |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 278 | if(std::getline(ss, token, ';').fail()) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 279 | { |
Anthony Barbier | f5dcf79 | 2018-02-28 18:04:45 +0000 | [diff] [blame] | 280 | ARM_COMPUTE_ERROR("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] | 281 | } |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 282 | lws.get()[i] = support::cpp11::stoi(token); |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 283 | } |
Anthony Barbier | 317fa7f | 2018-03-01 10:11:22 +0000 | [diff] [blame] | 284 | |
| 285 | // If all dimensions are 0: reset to NullRange (i.e nullptr) |
| 286 | if(lws[0] == 0 && lws[1] == 0 && lws[2] == 0) |
| 287 | { |
| 288 | lws = cl::NullRange; |
| 289 | } |
| 290 | add_lws_to_table(kernel_id, lws); |
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 | fs.close(); |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 295 | void CLTuner::save_to_file(const std::string &filename) const |
Gian Marco | 85e6f51 | 2018-02-01 16:57:48 +0000 | [diff] [blame] | 296 | { |
Anthony Barbier | 8b81195 | 2018-02-28 13:47:58 +0000 | [diff] [blame] | 297 | std::ofstream fs; |
| 298 | fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 299 | fs.open(filename, std::ios::out); |
| 300 | for(auto kernel_data : _lws_table) |
Anthony Barbier | 8db8318 | 2018-02-27 13:08:00 +0000 | [diff] [blame] | 301 | { |
| 302 | fs << kernel_data.first << ";" << kernel_data.second[0] << ";" << kernel_data.second[1] << ";" << kernel_data.second[2] << std::endl; |
| 303 | } |
| 304 | fs.close(); |
| 305 | } |
Gian Marco Iodice | a74923c | 2019-01-31 17:06:54 +0000 | [diff] [blame] | 306 | } // namespace arm_compute |