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;
88 if(kernel.
config_id() != arm_compute::default_config_id)
90 auto p = _tuning_params_table.find(config_id);
92 if(p == _tuning_params_table.end())
97 CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, tensors);
129 _tuning_params_table.emplace(kernel_id, optimal_tuning_params);
135 cl::CommandQueue queue_profiler;
147 cl_command_queue_properties props = default_queue.getInfo<CL_QUEUE_PROPERTIES>();
149 if((props & CL_QUEUE_PROFILING_ENABLE) == 0)
152 queue_profiler = cl::CommandQueue(
CLScheduler::get().context(), props | CL_QUEUE_PROFILING_ENABLE);
156 queue_profiler = default_queue;
160 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,
161 const cl_event * event_wait_list, cl_event * event)
169 cl_int retval = this->
real_clEnqueueNDRangeKernel(command_queue, kernel, work_dim, gwo, gws, lws, num_events_in_wait_list, event_wait_list, &tmp);
187 const bool inject_memory = !tensors.
empty();
188 inject_memory ? kernel.run_op(tensors, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler);
190 queue_profiler.finish();
192 const cl_ulong
start = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
193 const cl_ulong
end = _kernel_event.getProfilingInfo<CL_PROFILING_COMMAND_END>();
194 cl_ulong min_exec_time = end -
start;
195 _kernel_event =
nullptr;
201 for(
size_t i = 0; i < tuning_list->size(); ++i)
205 cl::NDRange lws_test = tuning_test.
get_lws();
206 auto x = lws_test[0];
207 auto y = lws_test[1];
208 auto z = lws_test[2];
209 const bool invalid_lws = (x * y * z > kernel.get_max_workgroup_size()) || (x == 1 && y == 1 && z == 1);
216 kernel.set_lws_hint(lws_test);
219 cl_int wbsm_test = tuning_test.
get_wbsm();
220 kernel.set_wbsm_hint(wbsm_test);
224 inject_memory ? kernel.run_op(tensors, kernel.window(), queue_profiler) : kernel.run(kernel.window(), queue_profiler);
226 queue_profiler.finish();
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;
231 _kernel_event =
nullptr;
234 if(diff < min_exec_time)
236 min_exec_time = diff;
247 return opt_tuning_params;
252 _tuning_params_table.clear();
253 for(
auto && params : lws_table)
262 for(
auto && params : _tuning_params_table)
264 _lws_table.emplace(params.first, params.second.get_lws());
271 return _tuning_params_table;
276 _tuning_params_table.clear();
283 fs.exceptions(std::ifstream::badbit);
284 fs.open(filename, std::ios::in);
290 bool header_line =
true;
291 while(!std::getline(fs, line).fail())
296 size_t pos_lws = line.find(
"lws");
297 size_t pos_wbsm = line.find(
"wbsm");
299 if(pos_lws != std::string::npos || pos_wbsm != std::string::npos)
302 if(pos_wbsm != std::string::npos)
308 if(std::getline(fs, line).fail())
316 size_t pos = line.find(
";");
317 if(pos == std::string::npos)
321 std::string kernel_id = line.substr(0, pos);
322 line.erase(0, pos + 1);
334 if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty())
339 fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
340 fs.open(filename, std::ios::out);
341 std::string header_string =
"";
342 header_string +=
"lws";
345 if(!header_string.empty())
347 header_string +=
" ";
349 header_string +=
"wbsm";
351 fs << header_string << std::endl;
352 for(
auto const &kernel_data : _tuning_params_table)
355 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.
const std::unordered_map< std::string, cl::NDRange > & lws_table()
Give read access to the LWS table.
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 import_lws_table(const std::unordered_map< std::string, cl::NDRange > &lws_table)
Import LWS table.
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.
void add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws)
Manually add a LWS for a kernel.
CLTunerMode get_tuner_mode() const
Get the current OpenCL tuner mode.