40 : real_clEnqueueNDRangeKernel(nullptr), _tuning_params_table(), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuning_info(tuning_info)
44 struct CLTuner::IKernelData
46 virtual ~IKernelData() =
default;
47 virtual void do_run(
ICLKernel &kernel, cl::CommandQueue &queue) = 0;
49 struct DefaultKernelData :
public CLTuner::IKernelData
55 ~DefaultKernelData()
override =
default;
56 void do_run(
ICLKernel &kernel, cl::CommandQueue &queue)
override 58 const bool inject_memory = !_tensors.empty();
68 return _kernel_event() !=
nullptr;
72 _kernel_event = kernel_event;
81 return _tune_new_kernels;
86 _tuning_info.tuner_mode =
mode;
100 void CLTuner::do_tune_kernel_dynamic(
ICLKernel &kernel, IKernelData *data)
106 if(kernel.
config_id() != arm_compute::default_config_id)
108 auto p = _tuning_params_table.find(config_id);
110 if(p == _tuning_params_table.end())
112 if(_tune_new_kernels)
115 CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, data);
122 if(_tuning_info.tune_wbsm)
132 if(_tuning_info.tune_wbsm)
141 DefaultKernelData data{ tensors };
143 do_tune_kernel_dynamic(kernel, &data);
148 _tuning_params_table.emplace(kernel_id, optimal_tuning_params);
154 cl::CommandQueue queue_profiler;
166 cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>();
168 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
171 queue_profiler = cl::CommandQueue(
CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
175 queue_profiler = default_queue;
179 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,
180 const cl_event * event_wait_list, cl_event * event)
188 cl_int retval = this->
real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
204 data->do_run(kernel, queue_profiler);
211 cl::NDRange gws = kernel.get_cached_gws();
213 "[CLTuner] Kernel with config_id '%s' uses %s as the upper-bound for lws search",
214 kernel.config_id().c_str(),
to_string(gws).c_str());
216 queue_profiler.finish();
218 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
219 const cl_ulong
end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
220 cl_ulong min_exec_time = end - start;
221 _kernel_event =
nullptr;
227 for(
size_t i = 0; i < tuning_list->size(); ++i)
231 cl::NDRange lws_test = tuning_test.
get_lws();
232 auto x = lws_test[0];
233 auto y = lws_test[1];
234 auto z = lws_test[2];
235 const bool invalid_lws = (x * y * z > kernel.get_max_workgroup_size()) || (x == 1 && y == 1 && z == 1);
242 kernel.set_lws_hint(lws_test);
245 cl_int wbsm_test = tuning_test.
get_wbsm();
246 kernel.set_wbsm_hint(wbsm_test);
249 "[CLTuner] Trying LWS: %s, WBSM: %d",
250 to_string(kernel.lws_hint()).c_str(), kernel.wbsm_hint());
253 data->do_run(kernel, queue_profiler);
255 queue_profiler.finish();
257 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
258 const cl_ulong end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
259 const cl_ulong diff = end - start;
260 _kernel_event =
nullptr;
263 if(diff < min_exec_time)
265 min_exec_time = diff;
267 if(_tuning_info.tune_wbsm)
276 return opt_tuning_params;
281 return _tuning_params_table;
286 _tuning_params_table.clear();
293 fs.exceptions(std::ifstream::badbit);
294 fs.open(filename, std::ios::in);
300 bool header_line =
true;
301 while(!std::getline(fs, line).fail())
306 size_t pos_lws = line.find(
"lws");
307 size_t pos_wbsm = line.find(
"wbsm");
308 _tuning_info.tune_wbsm =
false;
309 if(pos_lws != std::string::npos || pos_wbsm != std::string::npos)
312 if(pos_wbsm != std::string::npos)
314 _tuning_info.tune_wbsm =
true;
318 if(std::getline(fs, line).fail())
326 size_t pos = line.find(
";");
327 if(pos == std::string::npos)
331 std::string kernel_id = line.substr(0, pos);
332 line.erase(0, pos + 1);
344 if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty())
349 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
350 fs.open(filename, std::ios::out);
351 std::string header_string =
"";
352 header_string +=
"lws";
353 if(_tuning_info.tune_wbsm)
355 if(!header_string.empty())
357 header_string +=
" ";
359 header_string +=
"wbsm";
361 fs << header_string << std::endl;
362 for(
auto const &kernel_data : _tuning_params_table)
365 fs << kernel_data.first << tun_pams.
to_string(_tuning_info) << std::endl;
void set_tuner_mode(CLTunerMode mode)
Set OpenCL tuner mode.
const Window & window() const
The maximum window the kernel can be executed on.
void set_cl_kernel_event(cl_event kernel_event)
Set the OpenCL kernel event.
cl::NDRange get_lws() const
virtual void run(const Window &window, cl::CommandQueue &queue)
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
const std::unordered_map< std::string, CLTuningParams > & tuning_params_table() const
Give read access to the tuning params table.
static CLScheduler & get()
Access the scheduler singleton.
void import_tuning_params(const std::unordered_map< std::string, CLTuningParams > &tuning_params_table)
Import tuning parameters table.
void tune_kernel_static(ICLKernel &kernel) override
Tune OpenCL kernel statically.
void tune_kernel_dynamic(ICLKernel &kernel) override
Tune OpenCL kernel dynamically.
void set_lws_hint(const cl::NDRange &lws_hint)
Set the Local-Workgroup-Size hint.
std::string to_string(T &&value)
Convert integer and float values to string.
void load_from_file(const std::string &filename)
Load the tuning parameters table from file.
#define ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(log_level, fmt,...)
Log a message with format to the logger.
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
< OpenCL tuner parameters
const std::string & string_from_target(GPUTarget target)
Translates a given gpu device target to string.
cl_int clRetainEvent(cl_event event)
Common interface for all the OpenCL kernels.
Copyright (c) 2017-2023 Arm Limited.
CLTuner(bool tune_new_kernels=true, CLTuningInfo tuning_info=CLTuningInfo())
Constructor.
std::function< decltype(clEnqueueNDRangeKernel)> real_clEnqueueNDRangeKernel
clEnqueueNDRangeKernel symbol
cl_uint get_num_compute_units()
Return the maximum number of compute units in the device.
void set_lws(cl::NDRange lws)
const std::string & config_id() const
Get the configuration ID.
bool kernel_event_is_set() const
Is the kernel_event set ?
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
std::function< decltype(clEnqueueNDRangeKernel)> clEnqueueNDRangeKernel_ptr
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
bool save_to_file(const std::string &filename) const
Save the content of the tuning parameters table to file.
void add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params)
Manually add tuning parameters for a kernel.
void set_tune_new_kernels(bool tune_new_kernels)
Setter for tune_new_kernels option.
GPUTarget get_target() const
Get the targeted GPU architecture.
void set_wbsm(cl_int wbsm)
void end(TokenStream &in, bool &valid)
std::string to_string(const T &val)
Fallback method: try to use std::to_string:
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
std::string to_string(CLTuningInfo tuning_info)
CLTunerMode
< OpenCL tuner modes
std::unique_ptr< ICLTuningParametersList > get_tuning_parameters_list(CLTuningInfo tuning_info, const cl::NDRange &gws)
Construct an ICLTuningParametersList object for the given tuner mode and gws configuration.
void set_wbsm_hint(const cl_int &wbsm_hint)
Set the workgroup batch size modifier hint.
bool tune_new_kernels() const
Tune kernels that are not in the tuning parameters table.
bool from_string(CLTuningInfo tuning_info, std::string tuning_params_string)
static CLSymbols & get()
Get the static instance of CLSymbols.
virtual void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...