39 : real_clEnqueueNDRangeKernel(nullptr), _tuning_params_table(), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuning_info(tuning_info)
45 return _kernel_event() !=
nullptr;
49 _kernel_event = kernel_event;
58 return _tune_new_kernels;
83 if(kernel.
config_id() != arm_compute::default_config_id)
85 auto p = _tuning_params_table.find(config_id);
87 if(p == _tuning_params_table.end())
92 CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, tensors);
119 _tuning_params_table.emplace(kernel_id, optimal_tuning_params);
125 cl::CommandQueue queue_profiler;
137 cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>();
139 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
142 queue_profiler = cl::CommandQueue(
CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
146 queue_profiler = default_queue;
150 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,
151 const cl_event * event_wait_list, cl_event * event)
159 cl_int retval = this->
real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
177 const bool inject_memory = !tensors.
empty();
178 inject_memory ? kernel.run_op(tensors, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler);
180 queue_profiler.finish();
182 const cl_ulong start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
183 const cl_ulong
end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
184 cl_ulong min_exec_time = end - start;
185 _kernel_event =
nullptr;
191 for(
size_t i = 0; i < tuning_list->size(); ++i)
195 cl::NDRange lws_test = tuning_test.
get_lws();
196 auto x = lws_test[0];
197 auto y = lws_test[1];
198 auto z = lws_test[2];
199 const bool invalid_lws = (x * y * z > kernel.get_max_workgroup_size()) || (x == 1 && y == 1 && z == 1);
206 kernel.set_lws_hint(lws_test);
209 cl_int wbsm_test = tuning_test.
get_wbsm();
210 kernel.set_wbsm_hint(wbsm_test);
214 inject_memory ? kernel.run_op(tensors, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler);
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 const cl_ulong diff = end - start;
221 _kernel_event =
nullptr;
224 if(diff < min_exec_time)
226 min_exec_time = diff;
237 return opt_tuning_params;
242 return _tuning_params_table;
247 _tuning_params_table.clear();
254 fs.exceptions(std::ifstream::badbit);
255 fs.open(filename, std::ios::in);
261 bool header_line =
true;
262 while(!std::getline(fs, line).fail())
267 size_t pos_lws = line.find(
"lws");
268 size_t pos_wbsm = line.find(
"wbsm");
270 if(pos_lws != std::string::npos || pos_wbsm != std::string::npos)
273 if(pos_wbsm != std::string::npos)
279 if(std::getline(fs, line).fail())
287 size_t pos = line.find(
";");
288 if(pos == std::string::npos)
292 std::string kernel_id = line.substr(0, pos);
293 line.erase(0, pos + 1);
305 if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty())
310 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
311 fs.open(filename, std::ios::out);
312 std::string header_string =
"";
313 header_string +=
"lws";
316 if(!header_string.empty())
318 header_string +=
" ";
320 header_string +=
"wbsm";
322 fs << header_string << std::endl;
323 for(
auto const &kernel_data : _tuning_params_table)
326 fs << kernel_data.first << tun_pams.
to_string(_tuning_info) << std::endl;
static cl::NDRange gws_from_window(const Window &window)
Get the global work size given an execution window.
void set_tuner_mode(CLTunerMode mode)
Set OpenCL tuner mode.
void set_cl_kernel_event(cl_event kernel_event)
Set the OpenCL kernel event.
cl::NDRange get_lws() const
bool tune_wbsm
Flag to tune the batches of work groups distributed to compute units.
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.
bool empty() const
Checks if pack is empty.
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_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-2021 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)
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
std::string to_string(CLTuningInfo tuning_info)
CLTunerMode tuner_mode
Parameter to select the level (granularity) of the tuning.
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.