blob: cf5b5bce2db31d3de66d4ebded0b209d8b400ecf [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 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
Kaizen8938bd32017-09-28 14:38:23 +010030#include <limits>
31#include <string>
32
33using namespace arm_compute;
34
35CLTuner::CLTuner()
Anthony Barbier06ea0482018-02-22 15:45:35 +000036 : real_function(nullptr), _lws_table(), _queue(), _queue_profiler(), _kernel_event()
Kaizen8938bd32017-09-28 14:38:23 +010037{
38}
39
Anthony Barbier06ea0482018-02-22 15:45:35 +000040void CLTuner::set_cl_kernel_event(cl_event kernel_event)
41{
42 _kernel_event = kernel_event;
43}
44
Kaizen8938bd32017-09-28 14:38:23 +010045void CLTuner::tune_kernel(ICLKernel &kernel)
46{
Anthony Barbier06ea0482018-02-22 15:45:35 +000047 if(real_function == nullptr)
48 {
49 real_function = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
50
51 // Get the default queue
52 _queue = CLScheduler::get().queue();
53
54 // Check if we can use the OpenCL timer with the default queue
55 cl_command_queue_properties props = _queue.getInfo<CL_QUEUE_PROPERTIES>();
56
57 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
58 {
59 // Set the queue for profiling
60 _queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
61 }
62 else
63 {
64 _queue_profiler = _queue;
65 }
66 }
67
Kaizen8938bd32017-09-28 14:38:23 +010068 // Get the configuration ID from the kernel
69 const std::string &config_id = kernel.config_id();
70
71 // 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
72 if(config_id != arm_compute::default_config_id)
73 {
74 auto p = _lws_table.find(config_id);
75
76 if(p == _lws_table.end())
77 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000078 // Set profiler queue
79 CLScheduler::get().set_queue(_queue_profiler);
80
Kaizen8938bd32017-09-28 14:38:23 +010081 // Find the optimal LWS for the kernel
82 cl::NDRange opt_lws = find_optimal_lws(kernel);
83
84 // Insert the optimal LWS in the table
85 _lws_table.emplace(config_id, opt_lws);
86
87 // Set Local-Workgroup-Size
88 kernel.set_lws_hint(opt_lws);
Anthony Barbier06ea0482018-02-22 15:45:35 +000089
90 // Restore queue
91 CLScheduler::get().set_queue(_queue);
Kaizen8938bd32017-09-28 14:38:23 +010092 }
93 else
94 {
95 // Set Local-Workgroup-Size
96 kernel.set_lws_hint(p->second);
97 }
98 }
99}
100
101cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel)
102{
Anthony Barbier06ea0482018-02-22 15:45:35 +0000103 // Start intercepting enqueues:
104 CLSymbols::get().clEnqueueNDRangeKernel_ptr = Interceptor(*this);
Kaizen8938bd32017-09-28 14:38:23 +0100105
Anthony Barbier06ea0482018-02-22 15:45:35 +0000106 cl_ulong min_exec_time = std::numeric_limits<cl_ulong>::max();
Kaizen8938bd32017-09-28 14:38:23 +0100107
Anthony Barbier06ea0482018-02-22 15:45:35 +0000108 cl::NDRange opt_lws = cl::NullRange;
Kaizen8938bd32017-09-28 14:38:23 +0100109
Anthony Barbier06ea0482018-02-22 15:45:35 +0000110 const int x_step = std::max(1, kernel.window().x().step());
111 const int y_step = std::max(1, kernel.window().y().step());
112 const int z_step = std::max(1, kernel.window().z().step());
113 const int x_end = kernel.window().x().end() - kernel.window().x().start() / x_step > 1 ? 16 : 1;
114 const int y_end = kernel.window().y().end() - kernel.window().y().start() / y_step > 1 ? 16 : 1;
115 const int z_end = kernel.window().z().end() - kernel.window().z().start() / z_step > 1 ? 8 : 1;
116
117 // First run using the default LWS
Kaizen8938bd32017-09-28 14:38:23 +0100118 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000119 cl::NDRange lws_test = cl::NullRange;
120
121 kernel.set_lws_hint(lws_test);
122
123 // Run the kernel
124 kernel.run(kernel.window(), _queue_profiler);
125
126 CLScheduler::get().sync();
127
128 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
129 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
130 const cl_ulong diff = end - start;
131
132 min_exec_time = diff;
133 }
134
135 for(int z = 1; z <= z_end; ++z)
136 {
137 for(int y = 1; y <= y_end; ++y)
Kaizen8938bd32017-09-28 14:38:23 +0100138 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000139 for(int x = 1; x <= x_end; ++x)
Kaizen8938bd32017-09-28 14:38:23 +0100140 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000141 cl::NDRange lws_test = cl::NDRange(x, y, z);
142
143 const bool invalid_lws = (x * y * z > static_cast<int>(kernel.get_max_workgroup_size())) || (x == 1 && y == 1 && z == 1);
144
145 if(invalid_lws)
146 {
147 continue;
148 }
149
150 //Set the Local-Workgroup-Size
151 kernel.set_lws_hint(lws_test);
152
153 // Run the kernel
154 kernel.run(kernel.window(), _queue_profiler);
155
156 CLScheduler::get().sync();
157
158 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
159 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
160 const cl_ulong diff = end - start;
161
162 // Check the execution time
163 if(diff < min_exec_time)
164 {
165 min_exec_time = diff;
166 opt_lws = cl::NDRange(x, y, z);
167 }
Kaizen8938bd32017-09-28 14:38:23 +0100168 }
169 }
170 }
171
Anthony Barbier06ea0482018-02-22 15:45:35 +0000172 // Restore real function
173 CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_function;
174
Kaizen8938bd32017-09-28 14:38:23 +0100175 return opt_lws;
176}
177
178void CLTuner::import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table)
179{
180 _lws_table.clear();
181 _lws_table = lws_table;
182}
183
184const std::unordered_map<std::string, cl::NDRange> &CLTuner::export_lws_table()
185{
186 return _lws_table;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000187}
188
189Interceptor::Interceptor(CLTuner &tuner)
190 : _tuner(tuner)
191{
192}
193
194cl_int Interceptor::operator()(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,
195 const cl_event *event_wait_list, cl_event *event)
196{
197 ARM_COMPUTE_ERROR_ON_MSG(event != nullptr, "Not supported");
198 ARM_COMPUTE_UNUSED(event);
199
200 cl_event tmp;
201 cl_int retval = _tuner.real_function(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
202
203 // Set OpenCL event
204 _tuner.set_cl_kernel_event(tmp);
205
206 return retval;
207}