blob: a262d6b95ca67cbd731d222087bb407232072884 [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Jenkins514be652019-02-28 12:25:18 +00002 * Copyright (c) 2017-2019 ARM Limited.
Kaizen8938bd32017-09-28 14:38:23 +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"
Anthony Barbier06ea0482018-02-22 15:45:35 +000027#include "arm_compute/core/Error.h"
Kaizen8938bd32017-09-28 14:38:23 +010028#include "arm_compute/runtime/CL/CLScheduler.h"
29
Jenkinsc3f34a42018-03-02 12:38:09 +000030#include <cerrno>
31#include <fstream>
32#include <iostream>
Kaizen8938bd32017-09-28 14:38:23 +010033#include <limits>
34#include <string>
35
Jenkins514be652019-02-28 12:25:18 +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
Kaizen8938bd32017-09-28 14:38:23 +010067
Jenkinsc3f34a42018-03-02 12:38:09 +000068CLTuner::CLTuner(bool tune_new_kernels)
Jenkins514be652019-02-28 12:25:18 +000069 : real_clEnqueueNDRangeKernel(nullptr), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels)
Jenkinsc3f34a42018-03-02 12:38:09 +000070{
71}
72
73bool CLTuner::kernel_event_is_set() const
74{
75 return _kernel_event() != nullptr;
76}
Anthony Barbier06ea0482018-02-22 15:45:35 +000077void CLTuner::set_cl_kernel_event(cl_event kernel_event)
78{
79 _kernel_event = kernel_event;
80}
81
Jenkinsc3f34a42018-03-02 12:38:09 +000082void CLTuner::set_tune_new_kernels(bool tune_new_kernels)
83{
84 _tune_new_kernels = tune_new_kernels;
85}
86bool CLTuner::tune_new_kernels() const
87{
88 return _tune_new_kernels;
89}
90
Jenkinsb3a371b2018-05-23 11:36:53 +010091void CLTuner::tune_kernel_static(ICLKernel &kernel)
92{
93 ARM_COMPUTE_UNUSED(kernel);
94}
95
96void CLTuner::tune_kernel_dynamic(ICLKernel &kernel)
Kaizen8938bd32017-09-28 14:38:23 +010097{
Jenkinsc3f34a42018-03-02 12:38:09 +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)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000103 {
Jenkinsc3f34a42018-03-02 12:38:09 +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{
Jenkins514be652019-02-28 12:25:18 +0000135 // Profiling queue
136 cl::CommandQueue queue_profiler;
137
138 // Extract real OpenCL function to intercept
Jenkinsc3f34a42018-03-02 12:38:09 +0000139 if(real_clEnqueueNDRangeKernel == nullptr)
140 {
141 real_clEnqueueNDRangeKernel = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000142 }
Jenkins514be652019-02-28 12:25:18 +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
Anthony Barbier06ea0482018-02-22 15:45:35 +0000160 // Start intercepting enqueues:
Jenkinsb3a371b2018-05-23 11:36:53 +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 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100164 if(this->kernel_event_is_set())
165 {
166 // 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.
167 return CL_SUCCESS;
168 }
169 cl_event tmp;
170 cl_int retval = this->real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
171
172 // Set OpenCL event
173 this->set_cl_kernel_event(tmp);
174
Jenkins514be652019-02-28 12:25:18 +0000175 if(event != nullptr)
176 {
177 //return cl_event from the intercepted call
178 clRetainEvent(tmp);
179 *event = tmp;
180 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100181 return retval;
182 };
183 CLSymbols::get().clEnqueueNDRangeKernel_ptr = interceptor;
Kaizen8938bd32017-09-28 14:38:23 +0100184
Anthony Barbier06ea0482018-02-22 15:45:35 +0000185 cl_ulong min_exec_time = std::numeric_limits<cl_ulong>::max();
Kaizen8938bd32017-09-28 14:38:23 +0100186
Jenkins514be652019-02-28 12:25:18 +0000187 cl::NDRange gws = ICLKernel::gws_from_window(kernel.window());
Anthony Barbier06ea0482018-02-22 15:45:35 +0000188 cl::NDRange opt_lws = cl::NullRange;
Kaizen8938bd32017-09-28 14:38:23 +0100189
Jenkins514be652019-02-28 12:25:18 +0000190 const unsigned int lws_x_max = std::min(static_cast<unsigned int>(gws[0]), 64u);
191 const unsigned int lws_y_max = std::min(static_cast<unsigned int>(gws[1]), 32u);
192 const unsigned int lws_z_max = std::min(static_cast<unsigned int>(gws[2]), 32u);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000193
Jenkins514be652019-02-28 12:25:18 +0000194 std::vector<unsigned int> lws_x;
195 std::vector<unsigned int> lws_y;
196 std::vector<unsigned int> lws_z;
197
198 // Initialize the LWS values to test
199 initialize_lws_values(lws_x, gws[0], lws_x_max, gws[2] > 16);
200 initialize_lws_values(lws_y, gws[1], lws_y_max, gws[2] > 16);
201 initialize_lws_values(lws_z, gws[2], lws_z_max, false);
202
203 for(const auto &z : lws_z)
Kaizen8938bd32017-09-28 14:38:23 +0100204 {
Jenkins514be652019-02-28 12:25:18 +0000205 for(const auto &y : lws_y)
Kaizen8938bd32017-09-28 14:38:23 +0100206 {
Jenkins514be652019-02-28 12:25:18 +0000207 for(const auto &x : lws_x)
Kaizen8938bd32017-09-28 14:38:23 +0100208 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000209 cl::NDRange lws_test = cl::NDRange(x, y, z);
210
Jenkins514be652019-02-28 12:25:18 +0000211 bool invalid_lws = (x * y * z > kernel.get_max_workgroup_size()) || (x == 1 && y == 1 && z == 1);
212
213 invalid_lws = invalid_lws || (x > gws[0]) || (y > gws[1]) || (z > gws[2]);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000214
215 if(invalid_lws)
216 {
217 continue;
218 }
219
220 //Set the Local-Workgroup-Size
221 kernel.set_lws_hint(lws_test);
222
223 // Run the kernel
Jenkins514be652019-02-28 12:25:18 +0000224 kernel.run(kernel.window(), queue_profiler);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000225
Jenkins514be652019-02-28 12:25:18 +0000226 queue_profiler.finish();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000227
228 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;
Jenkinsc3f34a42018-03-02 12:38:09 +0000231 _kernel_event = nullptr;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000232
233 // Check the execution time
234 if(diff < min_exec_time)
235 {
236 min_exec_time = diff;
237 opt_lws = cl::NDRange(x, y, z);
238 }
Kaizen8938bd32017-09-28 14:38:23 +0100239 }
240 }
241 }
242
Anthony Barbier06ea0482018-02-22 15:45:35 +0000243 // Restore real function
Jenkinsc3f34a42018-03-02 12:38:09 +0000244 CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000245
Kaizen8938bd32017-09-28 14:38:23 +0100246 return opt_lws;
247}
248
249void CLTuner::import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table)
250{
251 _lws_table.clear();
252 _lws_table = lws_table;
253}
254
Jenkinsc3f34a42018-03-02 12:38:09 +0000255const std::unordered_map<std::string, cl::NDRange> &CLTuner::lws_table() const
Kaizen8938bd32017-09-28 14:38:23 +0100256{
257 return _lws_table;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000258}
259
Jenkinsc3f34a42018-03-02 12:38:09 +0000260void CLTuner::load_from_file(const std::string &filename)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000261{
Jenkinsc3f34a42018-03-02 12:38:09 +0000262 std::ifstream fs;
263 fs.exceptions(std::ifstream::badbit);
264 fs.open(filename, std::ios::in);
265 if(!fs.is_open())
266 {
267 ARM_COMPUTE_ERROR("Failed to open '%s' (%s [%d])", filename.c_str(), strerror(errno), errno);
268 }
269 std::string line;
270 while(!std::getline(fs, line).fail())
271 {
272 std::istringstream ss(line);
273 std::string token;
274 if(std::getline(ss, token, ';').fail())
275 {
276 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());
277 }
278 std::string kernel_id = token;
279 cl::NDRange lws(1, 1, 1);
280 for(int i = 0; i < 3; i++)
281 {
282 if(std::getline(ss, token, ';').fail())
283 {
284 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());
285 }
286 lws.get()[i] = support::cpp11::stoi(token);
287 }
288
289 // If all dimensions are 0: reset to NullRange (i.e nullptr)
290 if(lws[0] == 0 && lws[1] == 0 && lws[2] == 0)
291 {
292 lws = cl::NullRange;
293 }
294 add_lws_to_table(kernel_id, lws);
295 }
296 fs.close();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000297}
298
Jenkinsc3f34a42018-03-02 12:38:09 +0000299void CLTuner::save_to_file(const std::string &filename) const
Anthony Barbier06ea0482018-02-22 15:45:35 +0000300{
Jenkinsc3f34a42018-03-02 12:38:09 +0000301 std::ofstream fs;
302 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
303 fs.open(filename, std::ios::out);
304 for(auto kernel_data : _lws_table)
305 {
306 fs << kernel_data.first << ";" << kernel_data.second[0] << ";" << kernel_data.second[1] << ";" << kernel_data.second[2] << std::endl;
307 }
308 fs.close();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000309}
Jenkins514be652019-02-28 12:25:18 +0000310} // namespace arm_compute