blob: 56baad82c37a8ce2fab42bd376ae9a3eed6f82d8 [file] [log] [blame]
Gian Marcode691f02017-09-08 16:13:11 +01001/*
Gian Marco Iodicea74923c2019-01-31 17:06:54 +00002 * Copyright (c) 2017-2019 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"
25
26#include "arm_compute/core/CL/ICLKernel.h"
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"
29
Anthony Barbier317fa7f2018-03-01 10:11:22 +000030#include <cerrno>
Anthony Barbier8db83182018-02-27 13:08:00 +000031#include <fstream>
32#include <iostream>
Gian Marcode691f02017-09-08 16:13:11 +010033#include <limits>
34#include <string>
35
Gian Marco Iodicea74923c2019-01-31 17:06:54 +000036namespace arm_compute
37{
38namespace
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 */
48void 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 Marcode691f02017-09-08 16:13:11 +010067
Anthony Barbier8db83182018-02-27 13:08:00 +000068CLTuner::CLTuner(bool tune_new_kernels)
Georgios Pinitas4632e5e2019-02-06 14:47:59 +000069 : real_clEnqueueNDRangeKernel(nullptr), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels)
Gian Marcode691f02017-09-08 16:13:11 +010070{
71}
72
Anthony Barbierf5dcf792018-02-28 18:04:45 +000073bool CLTuner::kernel_event_is_set() const
74{
75 return _kernel_event() != nullptr;
76}
Gian Marco85e6f512018-02-01 16:57:48 +000077void CLTuner::set_cl_kernel_event(cl_event kernel_event)
78{
79 _kernel_event = kernel_event;
80}
81
Anthony Barbier8db83182018-02-27 13:08:00 +000082void CLTuner::set_tune_new_kernels(bool tune_new_kernels)
83{
84 _tune_new_kernels = tune_new_kernels;
85}
Anthony Barbier8b811952018-02-28 13:47:58 +000086bool CLTuner::tune_new_kernels() const
87{
88 return _tune_new_kernels;
89}
Anthony Barbier8db83182018-02-27 13:08:00 +000090
Georgios Pinitasc0d1c862018-03-23 15:13:15 +000091void CLTuner::tune_kernel_static(ICLKernel &kernel)
92{
93 ARM_COMPUTE_UNUSED(kernel);
94}
95
96void CLTuner::tune_kernel_dynamic(ICLKernel &kernel)
Gian Marcode691f02017-09-08 16:13:11 +010097{
Anthony Barbier8db83182018-02-27 13:08:00 +000098 // 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 Marco85e6f512018-02-01 16:57:48 +0000103 {
Anthony Barbier8db83182018-02-27 13:08:00 +0000104 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
128void 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
133cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel)
134{
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000135 // Profiling queue
136 cl::CommandQueue queue_profiler;
137
138 // Extract real OpenCL function to intercept
Anthony Barbier8db83182018-02-27 13:08:00 +0000139 if(real_clEnqueueNDRangeKernel == nullptr)
140 {
141 real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
Gian Marco85e6f512018-02-01 16:57:48 +0000142 }
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000143
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 Marco85e6f512018-02-01 16:57:48 +0000160 // Start intercepting enqueues:
Anthony Barbier48c19f12018-04-20 11:31:52 +0100161 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 Marcode691f02017-09-08 16:13:11 +0100180
Gian Marco85e6f512018-02-01 16:57:48 +0000181 cl_ulong min_exec_time = std::numeric_limits<cl_ulong>::max();
Gian Marcode691f02017-09-08 16:13:11 +0100182
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000183 cl::NDRange gws = ICLKernel::gws_from_window(kernel.window());
Gian Marco85e6f512018-02-01 16:57:48 +0000184 cl::NDRange opt_lws = cl::NullRange;
Gian Marcode691f02017-09-08 16:13:11 +0100185
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000186 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 Marcoc78d4bc2018-01-25 13:49:44 +0000189
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000190 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 Marco85e6f512018-02-01 16:57:48 +0000200 {
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000201 for(const auto &y : lws_y)
Gian Marcode691f02017-09-08 16:13:11 +0100202 {
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000203 for(const auto &x : lws_x)
Gian Marcode691f02017-09-08 16:13:11 +0100204 {
Gian Marco85e6f512018-02-01 16:57:48 +0000205 cl::NDRange lws_test = cl::NDRange(x, y, z);
206
Gian Marco Iodicea74923c2019-01-31 17:06:54 +0000207 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 Marco85e6f512018-02-01 16:57:48 +0000210
211 if(invalid_lws)
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000212 {
213 continue;
214 }
215
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000216 //Set the Local-Workgroup-Size
217 kernel.set_lws_hint(lws_test);
218
Gian Marco85e6f512018-02-01 16:57:48 +0000219 // Run the kernel
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000220 kernel.run(kernel.window(), queue_profiler);
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000221
Georgios Pinitas4632e5e2019-02-06 14:47:59 +0000222 queue_profiler.finish();
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000223
Gian Marco85e6f512018-02-01 16:57:48 +0000224 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 Barbierf5dcf792018-02-28 18:04:45 +0000227 _kernel_event = nullptr;
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000228
229 // Check the execution time
Gian Marco85e6f512018-02-01 16:57:48 +0000230 if(diff < min_exec_time)
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000231 {
Gian Marco85e6f512018-02-01 16:57:48 +0000232 min_exec_time = diff;
Gian Marcoc78d4bc2018-01-25 13:49:44 +0000233 opt_lws = cl::NDRange(x, y, z);
234 }
Gian Marcode691f02017-09-08 16:13:11 +0100235 }
236 }
237 }
238
Gian Marco85e6f512018-02-01 16:57:48 +0000239 // Restore real function
Anthony Barbier8db83182018-02-27 13:08:00 +0000240 CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel;
241
Gian Marcode691f02017-09-08 16:13:11 +0100242 return opt_lws;
243}
244
245void 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 Barbier8b811952018-02-28 13:47:58 +0000251const std::unordered_map<std::string, cl::NDRange> &CLTuner::lws_table() const
Gian Marcode691f02017-09-08 16:13:11 +0100252{
253 return _lws_table;
Gian Marco85e6f512018-02-01 16:57:48 +0000254}
255
Anthony Barbier8b811952018-02-28 13:47:58 +0000256void CLTuner::load_from_file(const std::string &filename)
Gian Marco85e6f512018-02-01 16:57:48 +0000257{
Anthony Barbier8b811952018-02-28 13:47:58 +0000258 std::ifstream fs;
Anthony Barbierf5dcf792018-02-28 18:04:45 +0000259 fs.exceptions(std::ifstream::badbit);
Anthony Barbier8b811952018-02-28 13:47:58 +0000260 fs.open(filename, std::ios::in);
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000261 if(!fs.is_open())
Anthony Barbier8db83182018-02-27 13:08:00 +0000262 {
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000263 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 Barbier8db83182018-02-27 13:08:00 +0000271 {
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000272 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 Barbierf5dcf792018-02-28 18:04:45 +0000278 if(std::getline(ss, token, ';').fail())
Anthony Barbier8db83182018-02-27 13:08:00 +0000279 {
Anthony Barbierf5dcf792018-02-28 18:04:45 +0000280 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 Barbier8db83182018-02-27 13:08:00 +0000281 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000282 lws.get()[i] = support::cpp11::stoi(token);
Anthony Barbier8db83182018-02-27 13:08:00 +0000283 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000284
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 Barbier8db83182018-02-27 13:08:00 +0000291 }
Anthony Barbier317fa7f2018-03-01 10:11:22 +0000292 fs.close();
Gian Marco85e6f512018-02-01 16:57:48 +0000293}
294
Anthony Barbier8b811952018-02-28 13:47:58 +0000295void CLTuner::save_to_file(const std::string &filename) const
Gian Marco85e6f512018-02-01 16:57:48 +0000296{
Anthony Barbier8b811952018-02-28 13:47:58 +0000297 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 Barbier8db83182018-02-27 13:08:00 +0000301 {
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 Iodicea74923c2019-01-31 17:06:54 +0000306} // namespace arm_compute