blob: e16d6808ed876568a90a6f6be08647e095ea42e0 [file] [log] [blame]
Gian Marcode691f02017-09-08 16:13:11 +01001/*
Manuel Bottinib56c1752020-11-18 17:56:30 +00002 * Copyright (c) 2017-2021 Arm Limited.
Gian Marcode691f02017-09-08 16:13:11 +01003 *
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 Bottinib56c1752020-11-18 17:56:30 +000025#include "arm_compute/runtime/CL/tuners/CLTuningParametersList.h"
Gian Marcode691f02017-09-08 16:13:11 +010026
Gian Marco85e6f512018-02-01 16:57:48 +000027#include "arm_compute/core/Error.h"
Gian Marcode691f02017-09-08 16:13:11 +010028#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010029#include "src/core/CL/ICLKernel.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000030#include "support/StringSupport.h"
Gian Marcode691f02017-09-08 16:13:11 +010031
Anthony Barbier317fa7f2018-03-01 10:11:22 +000032#include <cerrno>
Anthony Barbier8db83182018-02-27 13:08:00 +000033#include <fstream>
Gian Marcode691f02017-09-08 16:13:11 +010034#include <limits>
Gian Marcode691f02017-09-08 16:13:11 +010035
Gian Marco Iodicea74923c2019-01-31 17:06:54 +000036namespace arm_compute
37{
Manuel Bottinib56c1752020-11-18 17:56:30 +000038CLTuner::CLTuner(bool tune_new_kernels, CLTuningInfo tuning_info)
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000039 : real_clEnqueueNDRangeKernel(nullptr), _tuning_params_table(), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuning_info(tuning_info)
Gian Marcode691f02017-09-08 16:13:11 +010040{
41}
42
Anthony Barbierf5dcf792018-02-28 18:04:45 +000043bool CLTuner::kernel_event_is_set() const
44{
45 return _kernel_event() != nullptr;
46}
Gian Marco85e6f512018-02-01 16:57:48 +000047void CLTuner::set_cl_kernel_event(cl_event kernel_event)
48{
49 _kernel_event = kernel_event;
50}
51
Anthony Barbier8db83182018-02-27 13:08:00 +000052void CLTuner::set_tune_new_kernels(bool tune_new_kernels)
53{
54 _tune_new_kernels = tune_new_kernels;
55}
Anthony Barbier8b811952018-02-28 13:47:58 +000056bool CLTuner::tune_new_kernels() const
57{
58 return _tune_new_kernels;
59}
Anthony Barbier8db83182018-02-27 13:08:00 +000060
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010061void CLTuner::set_tuner_mode(CLTunerMode mode)
62{
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000063 _tuning_info.tuner_mode = mode;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010064}
Manuel Bottinib56c1752020-11-18 17:56:30 +000065
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010066CLTunerMode CLTuner::get_tuner_mode() const
67{
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000068 return _tuning_info.tuner_mode;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010069}
70
Georgios Pinitasc0d1c862018-03-23 15:13:15 +000071void CLTuner::tune_kernel_static(ICLKernel &kernel)
72{
73 ARM_COMPUTE_UNUSED(kernel);
74}
75
76void CLTuner::tune_kernel_dynamic(ICLKernel &kernel)
Gian Marcode691f02017-09-08 16:13:11 +010077{
Georgios Pinitas0499dff2020-07-31 22:21:38 +010078 ITensorPack pack;
79 tune_kernel_dynamic(kernel, pack);
Georgios Pinitas9c82e012020-07-17 12:47:56 +010080}
81
Georgios Pinitas0499dff2020-07-31 22:21:38 +010082void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors)
Georgios Pinitas9c82e012020-07-17 12:47:56 +010083{
Giorgio Arena5d42b462019-07-26 15:54:20 +010084 // Get the configuration ID from the kernel and append GPU target name and number of available compute units
85 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 Barbier8db83182018-02-27 13:08:00 +000086
Giorgio Arena5d42b462019-07-26 15:54:20 +010087 // 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
88 if(kernel.config_id() != arm_compute::default_config_id)
Gian Marco85e6f512018-02-01 16:57:48 +000089 {
Manuel Bottinib56c1752020-11-18 17:56:30 +000090 auto p = _tuning_params_table.find(config_id);
Anthony Barbier8db83182018-02-27 13:08:00 +000091
Manuel Bottinib56c1752020-11-18 17:56:30 +000092 if(p == _tuning_params_table.end())
Anthony Barbier8db83182018-02-27 13:08:00 +000093 {
94 if(_tune_new_kernels)
95 {
96 // Find the optimal LWS for the kernel
Manuel Bottinib56c1752020-11-18 17:56:30 +000097 CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, tensors);
Anthony Barbier8db83182018-02-27 13:08:00 +000098
99 // Insert the optimal LWS in the table
Manuel Bottinib56c1752020-11-18 17:56:30 +0000100 add_tuning_params(config_id, opt_tuning_params);
Anthony Barbier8db83182018-02-27 13:08:00 +0000101
102 // Set Local-Workgroup-Size
Manuel Bottinib56c1752020-11-18 17:56:30 +0000103 kernel.set_lws_hint(opt_tuning_params.get_lws());
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000104 if(_tuning_info.tune_wbsm)
105 {
106 kernel.set_wbsm_hint(opt_tuning_params.get_wbsm());
107 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000108 }
109 }
110 else
111 {
112 // Set Local-Workgroup-Size
Manuel Bottinib56c1752020-11-18 17:56:30 +0000113 kernel.set_lws_hint(p->second.get_lws());
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000114 if(_tuning_info.tune_wbsm)
115 {
116 kernel.set_wbsm_hint(p->second.get_wbsm());
117 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000118 }
119 }
120}
121
122void CLTuner::add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws)
123{
Manuel Bottinib56c1752020-11-18 17:56:30 +0000124 add_tuning_params(kernel_id, CLTuningParams(optimal_lws));
Anthony Barbier8db83182018-02-27 13:08:00 +0000125}
126
Manuel Bottinib56c1752020-11-18 17:56:30 +0000127void CLTuner::add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params)
128{
129 _tuning_params_table.emplace(kernel_id, optimal_tuning_params);
130}
131
132CLTuningParams CLTuner::find_optimal_tuning_params(ICLKernel &kernel, ITensorPack &tensors)
Anthony Barbier8db83182018-02-27 13:08:00 +0000133{
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000134 // Profiling queue
135 cl::CommandQueue queue_profiler;
136
137 // Extract real OpenCL function to intercept
Anthony Barbier8db83182018-02-27 13:08:00 +0000138 if(real_clEnqueueNDRangeKernel == nullptr)
139 {
140 real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
Gian Marco85e6f512018-02-01 16:57:48 +0000141 }
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000142
143 // Get the default queue
144 cl::CommandQueue default_queue = CLScheduler::get().queue();
145
146 // Check if we can use the OpenCL timer with the default queue
147 cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>();
148
149 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
150 {
151 // Set the queue for profiling
152 queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
153 }
154 else
155 {
156 queue_profiler = default_queue;
157 }
158
Gian Marco85e6f512018-02-01 16:57:48 +0000159 // Start intercepting enqueues:
Anthony Barbier48c19f12018-04-20 11:31:52 +0100160 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,
161 const cl_event * event_wait_list, cl_event * event)
162 {
Anthony Barbier48c19f12018-04-20 11:31:52 +0100163 if(this->kernel_event_is_set())
164 {
165 // 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.
166 return CL_SUCCESS;
167 }
168 cl_event tmp;
169 cl_int retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
170
171 // Set OpenCL event
172 this->set_cl_kernel_event(tmp);
173
Vidhya Sudhan Loganathanca65af32019-02-07 11:14:42 +0000174 if(event != nullptr)
175 {
176 //return cl_event from the intercepted call
177 clRetainEvent(tmp);
178 *event = tmp;
179 }
Anthony Barbier48c19f12018-04-20 11:31:52 +0100180 return retval;
181 };
182 CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor;
Gian Marcode691f02017-09-08 16:13:11 +0100183
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100184 cl::NDRange gws = ICLKernel::gws_from_window(kernel.window());
Gian Marcode691f02017-09-08 16:13:11 +0100185
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100186 // Run the kernel with default lws to be used as baseline
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100187 const bool inject_memory = !tensors.empty();
188 inject_memory ? kernel.run_op(tensors, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler);
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100189
190 queue_profiler.finish();
191
192 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
193 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
194 cl_ulong min_exec_time = end - start;
195 _kernel_event = nullptr;
196
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000197 CLTuningParams opt_tuning_params(cl::NullRange, 0);
Gian Marcode691f02017-09-08 16:13:11 +0100198
Manuel Bottinib56c1752020-11-18 17:56:30 +0000199 // Construct the list of tuning parameters values to be tested based on the tuner mode.
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000200 auto tuning_list = cl_tuner::get_tuning_parameters_list(_tuning_info, gws);
201 for(size_t i = 0; i < tuning_list->size(); ++i)
Gian Marco85e6f512018-02-01 16:57:48 +0000202 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000203 CLTuningParams tuning_test = (*tuning_list)[i];
204 // Setting the lws
205 cl::NDRange lws_test = tuning_test.get_lws();
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100206 auto x = lws_test[0];
207 auto y = lws_test[1];
208 auto z = lws_test[2];
Gian Marco Iodicedeaed2d2019-05-14 17:11:53 +0100209 const bool invalid_lws = (x * y * z > kernel.get_max_workgroup_size()) || (x == 1 && y == 1 && z == 1);
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100210
211 if(invalid_lws)
Gian Marcode691f02017-09-08 16:13:11 +0100212 {
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100213 continue;
214 }
Gian Marco85e6f512018-02-01 16:57:48 +0000215
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100216 kernel.set_lws_hint(lws_test);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000217 if(_tuning_info.tune_wbsm && CLKernelLibrary::get().is_wbsm_supported())
218 {
219 cl_int wbsm_test = tuning_test.get_wbsm();
220 kernel.set_wbsm_hint(wbsm_test);
221 }
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000222
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100223 // Run the kernel
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100224 inject_memory ? kernel.run_op(tensors, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler);
Gian Marco85e6f512018-02-01 16:57:48 +0000225
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100226 queue_profiler.finish();
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000227
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100228 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
229 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
230 const cl_ulong diff = end - start;
231 _kernel_event = nullptr;
Gian Marco Iodicedeaed2d2019-05-14 17:11:53 +0100232
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100233 // Check the execution time
234 if(diff < min_exec_time)
235 {
236 min_exec_time = diff;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000237 opt_tuning_params.set_lws(tuning_test.get_lws());
238 if(_tuning_info.tune_wbsm)
239 {
240 opt_tuning_params.set_wbsm(tuning_test.get_wbsm());
241 }
Gian Marcode691f02017-09-08 16:13:11 +0100242 }
243 }
244
Gian Marco85e6f512018-02-01 16:57:48 +0000245 // Restore real function
Anthony Barbier8db83182018-02-27 13:08:00 +0000246 CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000247 return opt_tuning_params;
Gian Marcode691f02017-09-08 16:13:11 +0100248}
249
250void CLTuner::import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table)
251{
Manuel Bottinib56c1752020-11-18 17:56:30 +0000252 _tuning_params_table.clear();
253 for(auto && params : lws_table)
254 {
255 add_tuning_params(params.first, CLTuningParams(params.second));
256 }
Gian Marcode691f02017-09-08 16:13:11 +0100257}
258
Manuel Bottinib56c1752020-11-18 17:56:30 +0000259const std::unordered_map<std::string, cl::NDRange> &CLTuner::lws_table()
Gian Marcode691f02017-09-08 16:13:11 +0100260{
Manuel Bottinib56c1752020-11-18 17:56:30 +0000261 _lws_table.clear();
262 for(auto && params : _tuning_params_table)
263 {
264 _lws_table.emplace(params.first, params.second.get_lws());
265 }
Gian Marcode691f02017-09-08 16:13:11 +0100266 return _lws_table;
Gian Marco85e6f512018-02-01 16:57:48 +0000267}
268
Manuel Bottinib56c1752020-11-18 17:56:30 +0000269const std::unordered_map<std::string, CLTuningParams> &CLTuner::tuning_params_table() const
270{
271 return _tuning_params_table;
272}
273
274void CLTuner::import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table)
275{
276 _tuning_params_table.clear();
277 _tuning_params_table = tuning_params_table;
278}
279
Anthony Barbier8b811952018-02-28 13:47:58 +0000280void CLTuner::load_from_file(const std::string &filename)
Gian Marco85e6f512018-02-01 16:57:48 +0000281{
Anthony Barbier8b811952018-02-28 13:47:58 +0000282 std::ifstream fs;
Anthony Barbierf5dcf792018-02-28 18:04:45 +0000283 fs.exceptions(std::ifstream::badbit);
Anthony Barbier8b811952018-02-28 13:47:58 +0000284 fs.open(filename, std::ios::in);
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000285 if(!fs.is_open())
Anthony Barbier8db83182018-02-27 13:08:00 +0000286 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100287 ARM_COMPUTE_ERROR_VAR("Failed to open '%s' (%s [%d])", filename.c_str(), strerror(errno), errno);
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000288 }
289 std::string line;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000290 bool header_line = true;
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000291 while(!std::getline(fs, line).fail())
292 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000293 if(header_line)
Anthony Barbier8db83182018-02-27 13:08:00 +0000294 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000295 header_line = false;
296 size_t pos_lws = line.find("lws");
297 size_t pos_wbsm = line.find("wbsm");
298 _tuning_info.tune_wbsm = false;
299 if(pos_lws != std::string::npos || pos_wbsm != std::string::npos)
Anthony Barbier8db83182018-02-27 13:08:00 +0000300 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000301 // The file has in the first line the parameters it has been tuned on
302 if(pos_wbsm != std::string::npos)
303 {
304 _tuning_info.tune_wbsm = true;
305 }
306 // Once the line with the tuning parameter is read we can
307 // read the next one to start collecting the values
308 if(std::getline(fs, line).fail())
309 {
310 break;
311 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000312 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000313 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000314
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000315 CLTuningParams tuning_params;
316 size_t pos = line.find(";");
317 if(pos == std::string::npos)
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000318 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000319 ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str());
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000320 }
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000321 std::string kernel_id = line.substr(0, pos);
322 line.erase(0, pos + 1);
323 if(!tuning_params.from_string(_tuning_info, line))
324 {
325 ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str());
326 }
327 add_tuning_params(kernel_id, tuning_params);
Anthony Barbier8db83182018-02-27 13:08:00 +0000328 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000329 fs.close();
Gian Marco85e6f512018-02-01 16:57:48 +0000330}
331
Manuel Bottinib56c1752020-11-18 17:56:30 +0000332bool CLTuner::save_to_file(const std::string &filename) const
Gian Marco85e6f512018-02-01 16:57:48 +0000333{
Manuel Bottinib56c1752020-11-18 17:56:30 +0000334 if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty())
335 {
336 return false;
337 }
Anthony Barbier8b811952018-02-28 13:47:58 +0000338 std::ofstream fs;
339 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
340 fs.open(filename, std::ios::out);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000341 std::string header_string = "";
342 header_string += "lws";
343 if(_tuning_info.tune_wbsm)
344 {
345 if(!header_string.empty())
346 {
347 header_string += " ";
348 }
349 header_string += "wbsm";
350 }
351 fs << header_string << std::endl;
Manuel Bottinib56c1752020-11-18 17:56:30 +0000352 for(auto const &kernel_data : _tuning_params_table)
Anthony Barbier8db83182018-02-27 13:08:00 +0000353 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000354 CLTuningParams tun_pams(kernel_data.second);
355 fs << kernel_data.first << tun_pams.to_string(_tuning_info) << std::endl;
Anthony Barbier8db83182018-02-27 13:08:00 +0000356 }
357 fs.close();
Manuel Bottinib56c1752020-11-18 17:56:30 +0000358 return true;
Anthony Barbier8db83182018-02-27 13:08:00 +0000359}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000360} // namespace arm_compute