blob: 1cc20f0c1e448bdace8f18e3c2656eab75fcdb47 [file] [log] [blame]
Gian Marcode691f02017-09-08 16:13:11 +01001/*
SiCong Li0a486cf2022-04-07 17:41:51 +01002 * Copyright (c) 2017-2022 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
SiCong Li0a486cf2022-04-07 17:41:51 +010043struct CLTuner::IKernelData
44{
45 virtual ~IKernelData() = default;
46 virtual void do_run(ICLKernel &kernel, cl::CommandQueue &queue) = 0;
47};
48struct DefaultKernelData : public CLTuner::IKernelData
49{
50 DefaultKernelData(ITensorPack &tensors)
51 : _tensors{ tensors }
52 {
53 }
54 ~DefaultKernelData() override = default;
55 void do_run(ICLKernel &kernel, cl::CommandQueue &queue) override
56 {
57 const bool inject_memory = !_tensors.empty();
58 inject_memory ? kernel.run_op(_tensors, kernel.window(), queue) : kernel.run(kernel.window(), queue);
59 }
60
61private:
62 ITensorPack &_tensors;
63};
64
Anthony Barbierf5dcf792018-02-28 18:04:45 +000065bool CLTuner::kernel_event_is_set() const
66{
67 return _kernel_event() != nullptr;
68}
Gian Marco85e6f512018-02-01 16:57:48 +000069void CLTuner::set_cl_kernel_event(cl_event kernel_event)
70{
71 _kernel_event = kernel_event;
72}
73
Anthony Barbier8db83182018-02-27 13:08:00 +000074void CLTuner::set_tune_new_kernels(bool tune_new_kernels)
75{
76 _tune_new_kernels = tune_new_kernels;
77}
Anthony Barbier8b811952018-02-28 13:47:58 +000078bool CLTuner::tune_new_kernels() const
79{
80 return _tune_new_kernels;
81}
Anthony Barbier8db83182018-02-27 13:08:00 +000082
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010083void CLTuner::set_tuner_mode(CLTunerMode mode)
84{
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000085 _tuning_info.tuner_mode = mode;
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010086}
Manuel Bottinib56c1752020-11-18 17:56:30 +000087
Georgios Pinitasc0d1c862018-03-23 15:13:15 +000088void CLTuner::tune_kernel_static(ICLKernel &kernel)
89{
90 ARM_COMPUTE_UNUSED(kernel);
91}
92
93void CLTuner::tune_kernel_dynamic(ICLKernel &kernel)
Gian Marcode691f02017-09-08 16:13:11 +010094{
Georgios Pinitas0499dff2020-07-31 22:21:38 +010095 ITensorPack pack;
96 tune_kernel_dynamic(kernel, pack);
Georgios Pinitas9c82e012020-07-17 12:47:56 +010097}
98
SiCong Li0a486cf2022-04-07 17:41:51 +010099void CLTuner::do_tune_kernel_dynamic(ICLKernel &kernel, IKernelData *data)
Georgios Pinitas9c82e012020-07-17 12:47:56 +0100100{
Giorgio Arena5d42b462019-07-26 15:54:20 +0100101 // Get the configuration ID from the kernel and append GPU target name and number of available compute units
102 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 +0000103
Giorgio Arena5d42b462019-07-26 15:54:20 +0100104 // 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
105 if(kernel.config_id() != arm_compute::default_config_id)
Gian Marco85e6f512018-02-01 16:57:48 +0000106 {
Manuel Bottinib56c1752020-11-18 17:56:30 +0000107 auto p = _tuning_params_table.find(config_id);
Anthony Barbier8db83182018-02-27 13:08:00 +0000108
Manuel Bottinib56c1752020-11-18 17:56:30 +0000109 if(p == _tuning_params_table.end())
Anthony Barbier8db83182018-02-27 13:08:00 +0000110 {
111 if(_tune_new_kernels)
112 {
113 // Find the optimal LWS for the kernel
SiCong Li0a486cf2022-04-07 17:41:51 +0100114 CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, data);
Anthony Barbier8db83182018-02-27 13:08:00 +0000115
116 // Insert the optimal LWS in the table
Manuel Bottinib56c1752020-11-18 17:56:30 +0000117 add_tuning_params(config_id, opt_tuning_params);
Anthony Barbier8db83182018-02-27 13:08:00 +0000118
119 // Set Local-Workgroup-Size
Manuel Bottinib56c1752020-11-18 17:56:30 +0000120 kernel.set_lws_hint(opt_tuning_params.get_lws());
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000121 if(_tuning_info.tune_wbsm)
122 {
123 kernel.set_wbsm_hint(opt_tuning_params.get_wbsm());
124 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000125 }
126 }
127 else
128 {
129 // Set Local-Workgroup-Size
Manuel Bottinib56c1752020-11-18 17:56:30 +0000130 kernel.set_lws_hint(p->second.get_lws());
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000131 if(_tuning_info.tune_wbsm)
132 {
133 kernel.set_wbsm_hint(p->second.get_wbsm());
134 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000135 }
136 }
137}
SiCong Li0a486cf2022-04-07 17:41:51 +0100138void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors)
139{
140 DefaultKernelData data{ tensors };
141
142 do_tune_kernel_dynamic(kernel, &data);
143}
144
Manuel Bottinib56c1752020-11-18 17:56:30 +0000145void CLTuner::add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params)
146{
147 _tuning_params_table.emplace(kernel_id, optimal_tuning_params);
148}
149
SiCong Li0a486cf2022-04-07 17:41:51 +0100150CLTuningParams CLTuner::find_optimal_tuning_params(ICLKernel &kernel, IKernelData *data)
Anthony Barbier8db83182018-02-27 13:08:00 +0000151{
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000152 // Profiling queue
153 cl::CommandQueue queue_profiler;
154
155 // Extract real OpenCL function to intercept
Anthony Barbier8db83182018-02-27 13:08:00 +0000156 if(real_clEnqueueNDRangeKernel == nullptr)
157 {
158 real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
Gian Marco85e6f512018-02-01 16:57:48 +0000159 }
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000160
161 // Get the default queue
162 cl::CommandQueue default_queue = CLScheduler::get().queue();
163
164 // Check if we can use the OpenCL timer with the default queue
165 cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>();
166
167 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
168 {
169 // Set the queue for profiling
170 queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
171 }
172 else
173 {
174 queue_profiler = default_queue;
175 }
176
Gian Marco85e6f512018-02-01 16:57:48 +0000177 // Start intercepting enqueues:
Anthony Barbier48c19f12018-04-20 11:31:52 +0100178 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,
179 const cl_event * event_wait_list, cl_event * event)
180 {
Anthony Barbier48c19f12018-04-20 11:31:52 +0100181 if(this->kernel_event_is_set())
182 {
183 // 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.
184 return CL_SUCCESS;
185 }
186 cl_event tmp;
187 cl_int retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
188
189 // Set OpenCL event
190 this->set_cl_kernel_event(tmp);
191
Vidhya Sudhan Loganathanca65af32019-02-07 11:14:42 +0000192 if(event != nullptr)
193 {
194 //return cl_event from the intercepted call
195 clRetainEvent(tmp);
196 *event = tmp;
197 }
Anthony Barbier48c19f12018-04-20 11:31:52 +0100198 return retval;
199 };
200 CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor;
Gian Marcode691f02017-09-08 16:13:11 +0100201
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100202 cl::NDRange gws = ICLKernel::gws_from_window(kernel.window());
Gian Marcode691f02017-09-08 16:13:11 +0100203
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100204 // Run the kernel with default lws to be used as baseline
SiCong Li0a486cf2022-04-07 17:41:51 +0100205 data->do_run(kernel, queue_profiler);
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100206
207 queue_profiler.finish();
208
209 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
210 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
211 cl_ulong min_exec_time = end - start;
212 _kernel_event = nullptr;
213
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000214 CLTuningParams opt_tuning_params(cl::NullRange, 0);
Gian Marcode691f02017-09-08 16:13:11 +0100215
Manuel Bottinib56c1752020-11-18 17:56:30 +0000216 // Construct the list of tuning parameters values to be tested based on the tuner mode.
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000217 auto tuning_list = cl_tuner::get_tuning_parameters_list(_tuning_info, gws);
218 for(size_t i = 0; i < tuning_list->size(); ++i)
Gian Marco85e6f512018-02-01 16:57:48 +0000219 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000220 CLTuningParams tuning_test = (*tuning_list)[i];
221 // Setting the lws
222 cl::NDRange lws_test = tuning_test.get_lws();
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100223 auto x = lws_test[0];
224 auto y = lws_test[1];
225 auto z = lws_test[2];
Gian Marco Iodicedeaed2d2019-05-14 17:11:53 +0100226 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 +0100227
228 if(invalid_lws)
Gian Marcode691f02017-09-08 16:13:11 +0100229 {
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100230 continue;
231 }
Gian Marco85e6f512018-02-01 16:57:48 +0000232
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100233 kernel.set_lws_hint(lws_test);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000234 if(_tuning_info.tune_wbsm && CLKernelLibrary::get().is_wbsm_supported())
235 {
236 cl_int wbsm_test = tuning_test.get_wbsm();
237 kernel.set_wbsm_hint(wbsm_test);
238 }
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000239
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100240 // Run the kernel
SiCong Li0a486cf2022-04-07 17:41:51 +0100241 data->do_run(kernel, queue_profiler);
Gian Marco85e6f512018-02-01 16:57:48 +0000242
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100243 queue_profiler.finish();
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000244
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100245 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
246 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
247 const cl_ulong diff = end - start;
248 _kernel_event = nullptr;
Gian Marco Iodicedeaed2d2019-05-14 17:11:53 +0100249
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100250 // Check the execution time
251 if(diff < min_exec_time)
252 {
253 min_exec_time = diff;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000254 opt_tuning_params.set_lws(tuning_test.get_lws());
255 if(_tuning_info.tune_wbsm)
256 {
257 opt_tuning_params.set_wbsm(tuning_test.get_wbsm());
258 }
Gian Marcode691f02017-09-08 16:13:11 +0100259 }
260 }
261
Gian Marco85e6f512018-02-01 16:57:48 +0000262 // Restore real function
Anthony Barbier8db83182018-02-27 13:08:00 +0000263 CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000264 return opt_tuning_params;
Gian Marcode691f02017-09-08 16:13:11 +0100265}
266
Manuel Bottinib56c1752020-11-18 17:56:30 +0000267const std::unordered_map<std::string, CLTuningParams> &CLTuner::tuning_params_table() const
268{
269 return _tuning_params_table;
270}
271
272void CLTuner::import_tuning_params(const std::unordered_map<std::string, CLTuningParams> &tuning_params_table)
273{
274 _tuning_params_table.clear();
275 _tuning_params_table = tuning_params_table;
276}
277
Anthony Barbier8b811952018-02-28 13:47:58 +0000278void CLTuner::load_from_file(const std::string &filename)
Gian Marco85e6f512018-02-01 16:57:48 +0000279{
Anthony Barbier8b811952018-02-28 13:47:58 +0000280 std::ifstream fs;
Anthony Barbierf5dcf792018-02-28 18:04:45 +0000281 fs.exceptions(std::ifstream::badbit);
Anthony Barbier8b811952018-02-28 13:47:58 +0000282 fs.open(filename, std::ios::in);
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000283 if(!fs.is_open())
Anthony Barbier8db83182018-02-27 13:08:00 +0000284 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100285 ARM_COMPUTE_ERROR_VAR("Failed to open '%s' (%s [%d])", filename.c_str(), strerror(errno), errno);
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000286 }
287 std::string line;
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000288 bool header_line = true;
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000289 while(!std::getline(fs, line).fail())
290 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000291 if(header_line)
Anthony Barbier8db83182018-02-27 13:08:00 +0000292 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000293 header_line = false;
294 size_t pos_lws = line.find("lws");
295 size_t pos_wbsm = line.find("wbsm");
296 _tuning_info.tune_wbsm = false;
297 if(pos_lws != std::string::npos || pos_wbsm != std::string::npos)
Anthony Barbier8db83182018-02-27 13:08:00 +0000298 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000299 // The file has in the first line the parameters it has been tuned on
300 if(pos_wbsm != std::string::npos)
301 {
302 _tuning_info.tune_wbsm = true;
303 }
304 // Once the line with the tuning parameter is read we can
305 // read the next one to start collecting the values
306 if(std::getline(fs, line).fail())
307 {
308 break;
309 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000310 }
Anthony Barbier8db83182018-02-27 13:08:00 +0000311 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000312
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000313 CLTuningParams tuning_params;
314 size_t pos = line.find(";");
315 if(pos == std::string::npos)
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000316 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000317 ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str());
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000318 }
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000319 std::string kernel_id = line.substr(0, pos);
320 line.erase(0, pos + 1);
321 if(!tuning_params.from_string(_tuning_info, line))
322 {
323 ARM_COMPUTE_ERROR_VAR("Malformed row '%s' in %s", line.c_str(), filename.c_str());
324 }
325 add_tuning_params(kernel_id, tuning_params);
Anthony Barbier8db83182018-02-27 13:08:00 +0000326 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000327 fs.close();
Gian Marco85e6f512018-02-01 16:57:48 +0000328}
329
Manuel Bottinib56c1752020-11-18 17:56:30 +0000330bool CLTuner::save_to_file(const std::string &filename) const
Gian Marco85e6f512018-02-01 16:57:48 +0000331{
Manuel Bottinib56c1752020-11-18 17:56:30 +0000332 if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty())
333 {
334 return false;
335 }
Anthony Barbier8b811952018-02-28 13:47:58 +0000336 std::ofstream fs;
337 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
338 fs.open(filename, std::ios::out);
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000339 std::string header_string = "";
340 header_string += "lws";
341 if(_tuning_info.tune_wbsm)
342 {
343 if(!header_string.empty())
344 {
345 header_string += " ";
346 }
347 header_string += "wbsm";
348 }
349 fs << header_string << std::endl;
Manuel Bottinib56c1752020-11-18 17:56:30 +0000350 for(auto const &kernel_data : _tuning_params_table)
Anthony Barbier8db83182018-02-27 13:08:00 +0000351 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000352 CLTuningParams tun_pams(kernel_data.second);
353 fs << kernel_data.first << tun_pams.to_string(_tuning_info) << std::endl;
Anthony Barbier8db83182018-02-27 13:08:00 +0000354 }
355 fs.close();
Manuel Bottinib56c1752020-11-18 17:56:30 +0000356 return true;
Anthony Barbier8db83182018-02-27 13:08:00 +0000357}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000358} // namespace arm_compute