blob: b2e3476e20f49215f6cb34faed087232139bd4c4 [file] [log] [blame]
Gian Marcode691f02017-09-08 16:13:11 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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"
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010025#include "arm_compute/runtime/CL/tuners/CLLWSList.h"
Gian Marcode691f02017-09-08 16:13:11 +010026
27#include "arm_compute/core/CL/ICLKernel.h"
Gian Marco85e6f512018-02-01 16:57:48 +000028#include "arm_compute/core/Error.h"
Gian Marcode691f02017-09-08 16:13:11 +010029#include "arm_compute/runtime/CL/CLScheduler.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>
34#include <iostream>
Gian Marcode691f02017-09-08 16:13:11 +010035#include <limits>
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010036#include <memory>
Gian Marcode691f02017-09-08 16:13:11 +010037#include <string>
38
Gian Marco Iodicea74923c2019-01-31 17:06:54 +000039namespace arm_compute
40{
Anthony Barbier8db83182018-02-27 13:08:00 +000041CLTuner::CLTuner(bool tune_new_kernels)
Michalis Spyrou083a0692019-05-13 15:35:50 +010042 : real_clEnqueueNDRangeKernel(nullptr), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuner_mode(CLTunerMode::NORMAL)
Gian Marcode691f02017-09-08 16:13:11 +010043{
44}
45
Anthony Barbierf5dcf792018-02-28 18:04:45 +000046bool CLTuner::kernel_event_is_set() const
47{
48 return _kernel_event() != nullptr;
49}
Gian Marco85e6f512018-02-01 16:57:48 +000050void CLTuner::set_cl_kernel_event(cl_event kernel_event)
51{
52 _kernel_event = kernel_event;
53}
54
Anthony Barbier8db83182018-02-27 13:08:00 +000055void CLTuner::set_tune_new_kernels(bool tune_new_kernels)
56{
57 _tune_new_kernels = tune_new_kernels;
58}
Anthony Barbier8b811952018-02-28 13:47:58 +000059bool CLTuner::tune_new_kernels() const
60{
61 return _tune_new_kernels;
62}
Anthony Barbier8db83182018-02-27 13:08:00 +000063
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010064void CLTuner::set_tuner_mode(CLTunerMode mode)
65{
66 _tuner_mode = mode;
67}
68CLTunerMode CLTuner::get_tuner_mode() const
69{
70 return _tuner_mode;
71}
72
Georgios Pinitasc0d1c862018-03-23 15:13:15 +000073void CLTuner::tune_kernel_static(ICLKernel &kernel)
74{
75 ARM_COMPUTE_UNUSED(kernel);
76}
77
78void CLTuner::tune_kernel_dynamic(ICLKernel &kernel)
Gian Marcode691f02017-09-08 16:13:11 +010079{
Georgios Pinitas9c82e012020-07-17 12:47:56 +010080 tune_kernel_dynamic(kernel, {}, {});
81}
82
83void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, const InputTensorMap &inputs, const OutputTensorMap &outputs)
84{
Giorgio Arena5d42b462019-07-26 15:54:20 +010085 // Get the configuration ID from the kernel and append GPU target name and number of available compute units
86 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 +000087
Giorgio Arena5d42b462019-07-26 15:54:20 +010088 // 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
89 if(kernel.config_id() != arm_compute::default_config_id)
Gian Marco85e6f512018-02-01 16:57:48 +000090 {
Anthony Barbier8db83182018-02-27 13:08:00 +000091 auto p = _lws_table.find(config_id);
92
93 if(p == _lws_table.end())
94 {
95 if(_tune_new_kernels)
96 {
97 // Find the optimal LWS for the kernel
Georgios Pinitas9c82e012020-07-17 12:47:56 +010098 cl::NDRange opt_lws = find_optimal_lws(kernel, inputs, outputs);
Anthony Barbier8db83182018-02-27 13:08:00 +000099
100 // Insert the optimal LWS in the table
101 add_lws_to_table(config_id, opt_lws);
102
103 // Set Local-Workgroup-Size
104 kernel.set_lws_hint(opt_lws);
105 }
106 }
107 else
108 {
109 // Set Local-Workgroup-Size
110 kernel.set_lws_hint(p->second);
111 }
112 }
113}
114
115void CLTuner::add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws)
116{
117 _lws_table.emplace(kernel_id, optimal_lws);
118}
119
Georgios Pinitas9c82e012020-07-17 12:47:56 +0100120cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel, const InputTensorMap &inputs, const OutputTensorMap &outputs)
Anthony Barbier8db83182018-02-27 13:08:00 +0000121{
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000122 // Profiling queue
123 cl::CommandQueue queue_profiler;
124
125 // Extract real OpenCL function to intercept
Anthony Barbier8db83182018-02-27 13:08:00 +0000126 if(real_clEnqueueNDRangeKernel == nullptr)
127 {
128 real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
Gian Marco85e6f512018-02-01 16:57:48 +0000129 }
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000130
131 // Get the default queue
132 cl::CommandQueue default_queue = CLScheduler::get().queue();
133
134 // Check if we can use the OpenCL timer with the default queue
135 cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>();
136
137 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
138 {
139 // Set the queue for profiling
140 queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
141 }
142 else
143 {
144 queue_profiler = default_queue;
145 }
146
Gian Marco85e6f512018-02-01 16:57:48 +0000147 // Start intercepting enqueues:
Anthony Barbier48c19f12018-04-20 11:31:52 +0100148 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,
149 const cl_event * event_wait_list, cl_event * event)
150 {
Anthony Barbier48c19f12018-04-20 11:31:52 +0100151 if(this->kernel_event_is_set())
152 {
153 // 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.
154 return CL_SUCCESS;
155 }
156 cl_event tmp;
157 cl_int retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
158
159 // Set OpenCL event
160 this->set_cl_kernel_event(tmp);
161
Vidhya Sudhan Loganathanca65af32019-02-07 11:14:42 +0000162 if(event != nullptr)
163 {
164 //return cl_event from the intercepted call
165 clRetainEvent(tmp);
166 *event = tmp;
167 }
Anthony Barbier48c19f12018-04-20 11:31:52 +0100168 return retval;
169 };
170 CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor;
Gian Marcode691f02017-09-08 16:13:11 +0100171
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100172 cl::NDRange gws = ICLKernel::gws_from_window(kernel.window());
Gian Marcode691f02017-09-08 16:13:11 +0100173
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100174 // Run the kernel with default lws to be used as baseline
Georgios Pinitas9c82e012020-07-17 12:47:56 +0100175 const bool inject_memory = !inputs.empty();
176 inject_memory ? kernel.run_op(inputs, outputs, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler);
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100177
178 queue_profiler.finish();
179
180 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
181 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
182 cl_ulong min_exec_time = end - start;
183 _kernel_event = nullptr;
184
Gian Marco85e6f512018-02-01 16:57:48 +0000185 cl::NDRange opt_lws = cl::NullRange;
Gian Marcode691f02017-09-08 16:13:11 +0100186
Georgios Pinitas9c82e012020-07-17 12:47:56 +0100187 // Construct the list of LWS values to be tested based on the tuner mode.
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100188 auto lws_list = cl_tuner::CLLWSListFactory::get_lws_list(_tuner_mode, gws);
189 for(size_t i = 0; i < lws_list->size(); ++i)
Gian Marco85e6f512018-02-01 16:57:48 +0000190 {
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100191 cl::NDRange lws_test = (*lws_list)[i];
192 auto x = lws_test[0];
193 auto y = lws_test[1];
194 auto z = lws_test[2];
Gian Marco Iodicedeaed2d2019-05-14 17:11:53 +0100195 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 +0100196
197 if(invalid_lws)
Gian Marcode691f02017-09-08 16:13:11 +0100198 {
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100199 continue;
200 }
Gian Marco85e6f512018-02-01 16:57:48 +0000201
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100202 //Set the Local-Workgroup-Size
203 kernel.set_lws_hint(lws_test);
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000204
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100205 // Run the kernel
Georgios Pinitas9c82e012020-07-17 12:47:56 +0100206 inject_memory ? kernel.run_op(inputs, outputs, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler);
Gian Marco85e6f512018-02-01 16:57:48 +0000207
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100208 queue_profiler.finish();
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000209
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100210 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
211 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
212 const cl_ulong diff = end - start;
213 _kernel_event = nullptr;
Gian Marco Iodicedeaed2d2019-05-14 17:11:53 +0100214
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100215 // Check the execution time
216 if(diff < min_exec_time)
217 {
218 min_exec_time = diff;
219 opt_lws = cl::NDRange(x, y, z);
Gian Marcode691f02017-09-08 16:13:11 +0100220 }
221 }
222
Gian Marco85e6f512018-02-01 16:57:48 +0000223 // Restore real function
Anthony Barbier8db83182018-02-27 13:08:00 +0000224 CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel;
225
Gian Marcode691f02017-09-08 16:13:11 +0100226 return opt_lws;
227}
228
229void CLTuner::import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table)
230{
231 _lws_table.clear();
232 _lws_table = lws_table;
233}
234
Anthony Barbier8b811952018-02-28 13:47:58 +0000235const std::unordered_map<std::string, cl::NDRange> &CLTuner::lws_table() const
Gian Marcode691f02017-09-08 16:13:11 +0100236{
237 return _lws_table;
Gian Marco85e6f512018-02-01 16:57:48 +0000238}
239
Anthony Barbier8b811952018-02-28 13:47:58 +0000240void CLTuner::load_from_file(const std::string &filename)
Gian Marco85e6f512018-02-01 16:57:48 +0000241{
Anthony Barbier8b811952018-02-28 13:47:58 +0000242 std::ifstream fs;
Anthony Barbierf5dcf792018-02-28 18:04:45 +0000243 fs.exceptions(std::ifstream::badbit);
Anthony Barbier8b811952018-02-28 13:47:58 +0000244 fs.open(filename, std::ios::in);
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000245 if(!fs.is_open())
Anthony Barbier8db83182018-02-27 13:08:00 +0000246 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100247 ARM_COMPUTE_ERROR_VAR("Failed to open '%s' (%s [%d])", filename.c_str(), strerror(errno), errno);
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000248 }
249 std::string line;
250 while(!std::getline(fs, line).fail())
251 {
252 std::istringstream ss(line);
253 std::string token;
254 if(std::getline(ss, token, ';').fail())
Anthony Barbier8db83182018-02-27 13:08:00 +0000255 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100256 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 Barbier317fa7f2018-03-01 10:11:22 +0000257 }
258 std::string kernel_id = token;
259 cl::NDRange lws(1, 1, 1);
260 for(int i = 0; i < 3; i++)
261 {
Anthony Barbierf5dcf792018-02-28 18:04:45 +0000262 if(std::getline(ss, token, ';').fail())
Anthony Barbier8db83182018-02-27 13:08:00 +0000263 {
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100264 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 Barbier8db83182018-02-27 13:08:00 +0000265 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000266 lws.get()[i] = support::cpp11::stoi(token);
Anthony Barbier8db83182018-02-27 13:08:00 +0000267 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000268
269 // If all dimensions are 0: reset to NullRange (i.e nullptr)
270 if(lws[0] == 0 && lws[1] == 0 && lws[2] == 0)
271 {
272 lws = cl::NullRange;
273 }
274 add_lws_to_table(kernel_id, lws);
Anthony Barbier8db83182018-02-27 13:08:00 +0000275 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000276 fs.close();
Gian Marco85e6f512018-02-01 16:57:48 +0000277}
278
Anthony Barbier8b811952018-02-28 13:47:58 +0000279void CLTuner::save_to_file(const std::string &filename) const
Gian Marco85e6f512018-02-01 16:57:48 +0000280{
Anthony Barbier8b811952018-02-28 13:47:58 +0000281 std::ofstream fs;
282 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
283 fs.open(filename, std::ios::out);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100284 for(auto const &kernel_data : _lws_table)
Anthony Barbier8db83182018-02-27 13:08:00 +0000285 {
286 fs << kernel_data.first << ";" << kernel_data.second[0] << ";" << kernel_data.second[1] << ";" << kernel_data.second[2] << std::endl;
287 }
288 fs.close();
289}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000290} // namespace arm_compute