2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/runtime/CL/CLTuner.h"
26 #include "arm_compute/core/CL/ICLKernel.h"
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/runtime/CL/CLScheduler.h"
33 using namespace arm_compute;
36 : real_function(nullptr), _lws_table(), _queue(), _queue_profiler(), _kernel_event()
40 void CLTuner::set_cl_kernel_event(cl_event kernel_event)
42 _kernel_event = kernel_event;
45 void CLTuner::tune_kernel(ICLKernel &kernel)
47 if(real_function == nullptr)
49 real_function = CLSymbols::get().clEnqueueNDRangeKernel_ptr;
51 // Get the default queue
52 _queue = CLScheduler::get().queue();
54 // Check if we can use the OpenCL timer with the default queue
55 cl_command_queue_properties props = _queue.getInfo<CL_QUEUE_PROPERTIES>();
57 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
59 // Set the queue for profiling
60 _queue_profiler = cl::CommandQueue(CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
64 _queue_profiler = _queue;
68 // Get the configuration ID from the kernel
69 const std::string &config_id = kernel.config_id();
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)
74 auto p = _lws_table.find(config_id);
76 if(p == _lws_table.end())
79 CLScheduler::get().set_queue(_queue_profiler);
81 // Find the optimal LWS for the kernel
82 cl::NDRange opt_lws = find_optimal_lws(kernel);
84 // Insert the optimal LWS in the table
85 _lws_table.emplace(config_id, opt_lws);
87 // Set Local-Workgroup-Size
88 kernel.set_lws_hint(opt_lws);
91 CLScheduler::get().set_queue(_queue);
95 // Set Local-Workgroup-Size
96 kernel.set_lws_hint(p->second);
101 cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel)
103 // Start intercepting enqueues:
104 CLSymbols::get().clEnqueueNDRangeKernel_ptr = Interceptor(*this);
106 cl_ulong min_exec_time = std::numeric_limits<cl_ulong>::max();
108 cl::NDRange opt_lws = cl::NullRange;
110 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;
117 // First run using the default LWS
119 cl::NDRange lws_test = cl::NullRange;
121 kernel.set_lws_hint(lws_test);
124 kernel.run(kernel.window(), _queue_profiler);
126 CLScheduler::get().sync();
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;
132 min_exec_time = diff;
135 for(int z = 1; z <= z_end; ++z)
137 for(int y = 1; y <= y_end; ++y)
139 for(int x = 1; x <= x_end; ++x)
141 cl::NDRange lws_test = cl::NDRange(x, y, z);
143 const bool invalid_lws = (x * y * z > static_cast<int>(kernel.get_max_workgroup_size())) || (x == 1 && y == 1 && z == 1);
150 //Set the Local-Workgroup-Size
151 kernel.set_lws_hint(lws_test);
154 kernel.run(kernel.window(), _queue_profiler);
156 CLScheduler::get().sync();
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;
162 // Check the execution time
163 if(diff < min_exec_time)
165 min_exec_time = diff;
166 opt_lws = cl::NDRange(x, y, z);
172 // Restore real function
173 CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_function;
178 void CLTuner::import_lws_table(const std::unordered_map<std::string, cl::NDRange> &lws_table)
181 _lws_table = lws_table;
184 const std::unordered_map<std::string, cl::NDRange> &CLTuner::export_lws_table()
189 Interceptor::Interceptor(CLTuner &tuner)
194 cl_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)
197 ARM_COMPUTE_ERROR_ON_MSG(event != nullptr, "Not supported");
198 ARM_COMPUTE_UNUSED(event);
201 cl_int retval = _tuner.real_function(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
204 _tuner.set_cl_kernel_event(tmp);