blob: 6aebef15a567350511f5c46337aaaff53d749612 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCong Li47f177e2023-02-22 17:24:09 +00002 * Copyright (c) 2016-2023 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44: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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_ICLKERNEL_H
25#define ARM_COMPUTE_ICLKERNEL_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
steniu015f910722017-08-23 10:15:22 +010027#include "arm_compute/core/CL/CLKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/CLTypes.h"
29#include "arm_compute/core/CL/OpenCL.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "arm_compute/core/experimental/Types.h"
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +010031#include "arm_compute/core/GPUTarget.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/IKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "arm_compute/core/Validate.h"
Manuel Bottinibe9f9f92021-01-25 15:07:17 +000034#include "arm_compute/runtime/CL/CLTuningParams.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Giorgio Arenaba2dd822021-07-28 16:10:03 +010036#include "src/core/CL/DefaultLWSHeuristics.h"
37
Gian Marcode691f02017-09-08 16:13:11 +010038#include <string>
39
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040namespace arm_compute
41{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010042namespace
43{
44bool is_same_lws(cl::NDRange lws0, cl::NDRange lws1)
45{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046 if (lws0.dimensions() != lws1.dimensions())
Giorgio Arena4a95bba2021-06-28 11:00:27 +010047 {
48 return false;
49 }
50
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010051 for (size_t i = 0; i < lws0.dimensions(); ++i)
Giorgio Arena4a95bba2021-06-28 11:00:27 +010052 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 if (lws0.get()[i] != lws1.get()[i])
Giorgio Arena4a95bba2021-06-28 11:00:27 +010054 {
55 return false;
56 }
57 }
58
59 return true;
60}
61} // namespace
SiCong Li3e363692017-07-04 15:02:10 +010062template <typename T>
63class ICLArray;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064class ICLTensor;
65class Window;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066/** Common interface for all the OpenCL kernels */
67class ICLKernel : public IKernel
68{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000069private:
70 /** Returns the number of arguments enqueued per array object.
71 *
72 * @return The number of arguments enqueued per array object.
73 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 template <unsigned int dimension_size>
Diego Lopez Recas0021d752017-12-18 14:42:56 +000075 constexpr static unsigned int num_arguments_per_array()
76 {
77 return num_arguments_per_tensor<dimension_size>();
78 }
79 /** Returns the number of arguments enqueued per tensor object.
80 *
81 * @return The number of arguments enqueued per tensor object.
82 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 template <unsigned int dimension_size>
Diego Lopez Recas0021d752017-12-18 14:42:56 +000084 constexpr static unsigned int num_arguments_per_tensor()
85 {
86 return 2 + 2 * dimension_size;
87 }
Giorgio Arena4a95bba2021-06-28 11:00:27 +010088
SiCong Li47f177e2023-02-22 17:24:09 +000089 /** Get default lws for the kernel
90 *
91 * @param[in] window Execution window used by the kernel
92 * @param[in] use_dummy_work_items If the kernel uses dummy workloads
93 *
94 * @return cl::NDRange
95 */
96 cl::NDRange default_lws_tune(const Window &window, bool use_dummy_work_items)
Giorgio Arena4a95bba2021-06-28 11:00:27 +010097 {
SiCong Li47f177e2023-02-22 17:24:09 +000098 return get_default_lws_for_type(_type, gws_from_window(window, use_dummy_work_items));
Giorgio Arena4a95bba2021-06-28 11:00:27 +010099 }
100
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100101 using IKernel::configure; //Prevent children from calling IKernel::configure() directly
Anthony Barbier5a65cfd2018-08-10 14:10:08 +0100102protected:
103 /** Configure the kernel's window and local workgroup size hint.
104 *
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000105 * @param[in] window The maximum window which will be returned by window()
106 * @param[in] lws_hint Local-Workgroup-Size to use.
107 * @param[in] wbsm_hint (Optional) Workgroup-Batch-Size-Modifier to use.
Anthony Barbier5a65cfd2018-08-10 14:10:08 +0100108 */
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000109 void configure_internal(const Window &window, cl::NDRange lws_hint, cl_int wbsm_hint = 0)
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100110 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000111 configure_internal(window, CLTuningParams(lws_hint, wbsm_hint));
112 }
113
114 /** Configure the kernel's window and tuning parameters hints.
115 *
116 * @param[in] window The maximum window which will be returned by window()
117 * @param[in] tuning_params_hint (Optional) Tuning parameters to use.
118 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 void configure_internal(const Window &window,
120 CLTuningParams tuning_params_hint = CLTuningParams(CLKernelLibrary::get().default_ndrange(),
121 0))
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000122 {
123 _tuning_params_hint = tuning_params_hint;
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100124
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 if (is_same_lws(_tuning_params_hint.get_lws(), CLKernelLibrary::get().default_ndrange()))
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100126 {
SiCong Li47f177e2023-02-22 17:24:09 +0000127 // Disable use_dummy_work_items at configure time. Because dummy work items only affect gws size, which
128 // will be recalculated with use_dummy_work_items flag at run time again anyway.
129 _tuning_params_hint.set_lws(default_lws_tune(window, false /* use_dummy_work_items */));
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100130 }
131
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100132 IKernel::configure(window);
133 }
134
Anthony Barbier5a65cfd2018-08-10 14:10:08 +0100135public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 /** Constructor */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000137 ICLKernel()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100138 : _kernel(nullptr),
139 _target(GPUTarget::MIDGARD),
140 _config_id(arm_compute::default_config_id),
141 _max_workgroup_size(0),
142 _type(CLKernelType::UNKNOWN),
143 _tuning_params_hint(),
144 _cached_gws(cl::NullRange)
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000145 {
146 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 /** Returns a reference to the OpenCL kernel of this object.
148 *
149 * @return A reference to the OpenCL kernel of this object.
150 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000151 cl::Kernel &kernel()
152 {
153 return _kernel;
154 }
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100155 /** Returns the CL kernel type
156 *
157 * @return The CL kernel type
158 */
159 CLKernelType type() const
160 {
161 return _type;
162 }
SiCong Li3e363692017-07-04 15:02:10 +0100163 /** Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx.
164 *
165 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
166 * @param[in] array Array to set as an argument of the object's kernel.
167 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
168 * @param[in] num_dimensions Number of dimensions of the @p array.
169 * @param[in] window Window the kernel will be executed on.
170 */
171 template <typename T>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 void add_1D_array_argument(unsigned int &idx,
173 const ICLArray<T> *array,
174 const Strides &strides,
175 unsigned int num_dimensions,
176 const Window &window)
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000177 {
178 add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
179 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180 /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
181 *
182 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
183 * @param[in] tensor Tensor to set as an argument of the object's kernel.
184 * @param[in] window Window the kernel will be executed on.
185 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000186 void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
187 {
188 add_tensor_argument<1>(idx, tensor, window);
189 }
Michalis Spyroue1651a52019-07-11 15:00:49 +0100190 /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true.
191 *
192 * @param[in] cond Condition to check
193 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
194 * @param[in] tensor Tensor to set as an argument of the object's kernel.
195 * @param[in] window Window the kernel will be executed on.
196 */
197 void add_1D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
198 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100199 if (cond)
Michalis Spyroue1651a52019-07-11 15:00:49 +0100200 {
201 add_1D_tensor_argument(idx, tensor, window);
202 }
203 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
205 *
206 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
207 * @param[in] tensor Tensor to set as an argument of the object's kernel.
208 * @param[in] window Window the kernel will be executed on.
209 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000210 void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
211 {
212 add_tensor_argument<2>(idx, tensor, window);
213 }
Michalis Spyroue1651a52019-07-11 15:00:49 +0100214 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true.
215 *
216 * @param[in] cond Condition to check
217 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
218 * @param[in] tensor Tensor to set as an argument of the object's kernel.
219 * @param[in] window Window the kernel will be executed on.
220 */
221 void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
222 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 if (cond)
Michalis Spyroue1651a52019-07-11 15:00:49 +0100224 {
225 add_2D_tensor_argument(idx, tensor, window);
226 }
227 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228 /** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
229 *
230 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
231 * @param[in] tensor Tensor to set as an argument of the object's kernel.
232 * @param[in] window Window the kernel will be executed on.
233 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000234 void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
235 {
236 add_tensor_argument<3>(idx, tensor, window);
237 }
steniu01868e5412017-07-17 23:16:00 +0100238 /** Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
239 *
240 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
241 * @param[in] tensor Tensor to set as an argument of the object's kernel.
242 * @param[in] window Window the kernel will be executed on.
243 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000244 void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
245 {
246 add_tensor_argument<4>(idx, tensor, window);
247 }
ramelg0137515692022-02-26 22:06:20 +0000248 /** Add the passed 5D tensor's parameters to the object's kernel's arguments starting from the index idx.
249 *
250 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
251 * @param[in] tensor Tensor to set as an argument of the object's kernel.
252 * @param[in] window Window the kernel will be executed on.
253 */
254 void add_5D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
255 {
256 add_tensor_argument<5>(idx, tensor, window);
257 }
Adnan AlSinan17975a62021-11-08 17:46:39 +0000258
Gian Marco Iodice4fb56702021-11-10 11:18:50 +0000259 /** Add the passed NHW 3D tensor's parameters to the object's kernel's arguments by passing strides, dimensions and the offset to the first valid element in bytes.
260 *
261 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
262 * @param[in] tensor Tensor to set as an argument of the object's kernel.
263 */
264 void add_3d_tensor_nhw_argument(unsigned int &idx, const ICLTensor *tensor);
265
266 /** Returns the number of arguments enqueued per NHW 3D Tensor object.
267 *
268 * @return The number of arguments enqueued per NHW 3D Tensor object.
269 */
270 constexpr static unsigned int num_arguments_per_3d_tensor_nhw()
271 {
272 constexpr unsigned int no_args_per_3d_tensor_nhw = 7u;
273 return no_args_per_3d_tensor_nhw;
274 }
275
Adnan AlSinan17975a62021-11-08 17:46:39 +0000276 /** Add the passed NHWC 4D tensor's parameters to the object's kernel's arguments by passing strides, dimensions and the offset to the first valid element in bytes.
277 *
278 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
279 * @param[in] tensor Tensor to set as an argument of the object's kernel.
280 */
281 void add_4d_tensor_nhwc_argument(unsigned int &idx, const ICLTensor *tensor);
282
283 /** Returns the number of arguments enqueued per NHWC 4D Tensor object.
284 *
285 * @return The number of arguments enqueued per NHWC 4D Tensor object.
286 */
287 constexpr static unsigned int num_arguments_per_4d_tensor_nhwc()
288 {
289 constexpr unsigned int no_args_per_4d_tensor_nhwc = 9u;
290 return no_args_per_4d_tensor_nhwc;
291 }
292
SiCong Li3e363692017-07-04 15:02:10 +0100293 /** Returns the number of arguments enqueued per 1D array object.
294 *
295 * @return The number of arguments enqueues per 1D array object.
296 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000297 constexpr static unsigned int num_arguments_per_1D_array()
298 {
299 return num_arguments_per_array<1>();
300 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301 /** Returns the number of arguments enqueued per 1D tensor object.
302 *
303 * @return The number of arguments enqueues per 1D tensor object.
304 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000305 constexpr static unsigned int num_arguments_per_1D_tensor()
306 {
307 return num_arguments_per_tensor<1>();
308 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309 /** Returns the number of arguments enqueued per 2D tensor object.
310 *
311 * @return The number of arguments enqueues per 2D tensor object.
312 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000313 constexpr static unsigned int num_arguments_per_2D_tensor()
314 {
315 return num_arguments_per_tensor<2>();
316 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100317 /** Returns the number of arguments enqueued per 3D tensor object.
318 *
319 * @return The number of arguments enqueues per 3D tensor object.
320 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000321 constexpr static unsigned int num_arguments_per_3D_tensor()
322 {
323 return num_arguments_per_tensor<3>();
324 }
steniu01868e5412017-07-17 23:16:00 +0100325 /** Returns the number of arguments enqueued per 4D tensor object.
326 *
327 * @return The number of arguments enqueues per 4D tensor object.
328 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000329 constexpr static unsigned int num_arguments_per_4D_tensor()
330 {
331 return num_arguments_per_tensor<4>();
332 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100333 /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
334 *
335 * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
336 *
337 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
338 * @param[in,out] queue Command queue on which to enqueue the kernel.
339 */
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100340 virtual void run(const Window &window, cl::CommandQueue &queue)
341 {
342 ARM_COMPUTE_UNUSED(window, queue);
343 }
344 /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
345 *
346 * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
347 *
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100348 * @param[in] tensors A vector containing the tensors to operato on.
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100349 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
350 * @param[in,out] queue Command queue on which to enqueue the kernel.
351 */
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100352 virtual void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100353 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100354 ARM_COMPUTE_UNUSED(tensors, window, queue);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100355 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356 /** Add the passed parameters to the object's kernel's arguments starting from the index idx.
357 *
358 * @param[in,out] idx Index at which to start adding the arguments. Will be incremented by the number of kernel arguments set.
359 * @param[in] value Value to set as an argument of the object's kernel.
360 */
361 template <typename T>
362 void add_argument(unsigned int &idx, T value)
363 {
364 _kernel.setArg(idx++, value);
365 }
366
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100367 /** Set the Local-Workgroup-Size hint
368 *
369 * @note This method should be called after the configuration of the kernel
370 *
371 * @param[in] lws_hint Local-Workgroup-Size to use
372 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100373 void set_lws_hint(const cl::NDRange &lws_hint)
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100374 {
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100375 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); // lws_hint will be overwritten by configure()
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000376 _tuning_params_hint.set_lws(lws_hint);
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100377 }
378
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000379 /** Return the Local-Workgroup-Size hint
380 *
381 * @return Current lws hint
382 */
383 cl::NDRange lws_hint() const
384 {
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000385 return _tuning_params_hint.get_lws();
386 }
387
388 /** Set the workgroup batch size modifier hint
389 *
390 * @note This method should be called after the configuration of the kernel
391 *
392 * @param[in] wbsm_hint workgroup batch size modifier value
393 */
394 void set_wbsm_hint(const cl_int &wbsm_hint)
395 {
396 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); // wbsm_hint will be overwritten by configure()
397 _tuning_params_hint.set_wbsm(wbsm_hint);
398 }
399
400 /** Return the workgroup batch size modifier hint
401 *
402 * @return Current wbsm hint
403 */
404 cl_int wbsm_hint() const
405 {
406 return _tuning_params_hint.get_wbsm();
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000407 }
408
Gian Marcode691f02017-09-08 16:13:11 +0100409 /** Get the configuration ID
410 *
411 * @note The configuration ID can be used by the caller to distinguish different calls of the same OpenCL kernel
412 * In particular, this method can be used by CLScheduler to keep track of the best LWS for each configuration of the same kernel.
413 * The configuration ID should be provided only for the kernels potentially affected by the LWS geometry
414 *
415 * @note This method should be called after the configuration of the kernel
416 *
417 * @return configuration id string
418 */
419 const std::string &config_id() const
420 {
421 return _config_id;
422 }
423
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100424 /** Set the targeted GPU architecture
425 *
426 * @param[in] target The targeted GPU architecture
427 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000428 void set_target(GPUTarget target)
429 {
430 _target = target;
431 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100432
433 /** Set the targeted GPU architecture according to the CL device
434 *
435 * @param[in] device A CL device
436 */
437 void set_target(cl::Device &device);
438
439 /** Get the targeted GPU architecture
440 *
441 * @return The targeted GPU architecture.
442 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000443 GPUTarget get_target() const
444 {
445 return _target;
446 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100447
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100448 /** Get the maximum workgroup size for the device the CLKernelLibrary uses.
449 *
450 * @return The maximum workgroup size value.
451 */
452 size_t get_max_workgroup_size();
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100453 /** Get the global work size given an execution window
454 *
SiCong Li47f177e2023-02-22 17:24:09 +0000455 * @param[in] window Execution window
456 * @param[in] use_dummy_work_items If the kernel uses dummy work items
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100457 *
458 * @return Global work size of the given execution window
459 */
SiCong Li47f177e2023-02-22 17:24:09 +0000460 static cl::NDRange gws_from_window(const Window &window, bool use_dummy_work_items);
461
462 /** Get the cached gws used to enqueue this kernel
463 *
464 * @return Latest global work size of the kernel
465 */
466 cl::NDRange get_cached_gws() const;
467
468 /** Cache the latest gws used to enqueue this kernel
469 *
470 * @param[in] gws Latest global work size of the kernel
471 */
472 void cache_gws(const cl::NDRange &gws);
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100473
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100474private:
SiCong Li3e363692017-07-04 15:02:10 +0100475 /** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
476 *
477 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
478 * @param[in] array Array to set as an argument of the object's kernel.
479 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
480 * @param[in] num_dimensions Number of dimensions of the @p array.
481 * @param[in] window Window the kernel will be executed on.
482 */
483 template <typename T, unsigned int dimension_size>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100484 void add_array_argument(unsigned int &idx,
485 const ICLArray<T> *array,
486 const Strides &strides,
487 unsigned int num_dimensions,
488 const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100489 /** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx.
490 *
491 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
492 * @param[in] tensor Tensor to set as an argument of the object's kernel.
493 * @param[in] window Window the kernel will be executed on.
494 */
495 template <unsigned int dimension_size>
496 void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100497
498protected:
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100499 cl::Kernel _kernel; /**< OpenCL kernel to run */
500 GPUTarget _target; /**< The targeted GPU */
501 std::string _config_id; /**< Configuration ID */
502 size_t _max_workgroup_size; /**< The maximum workgroup size for this kernel */
503 CLKernelType _type; /**< The CL kernel type */
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100504private:
Manuel Bottinibe9f9f92021-01-25 15:07:17 +0000505 CLTuningParams _tuning_params_hint; /**< Tuning parameters hint for the OpenCL kernel */
SiCong Li47f177e2023-02-22 17:24:09 +0000506 cl::NDRange _cached_gws; /**< Latest GWS used to enqueue this kernel */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100507};
508
509/** Add the kernel to the command queue with the given window.
510 *
511 * @note Depending on the size of the window, this might translate into several jobs being enqueued.
512 *
513 * @note If kernel->kernel() is empty then the function will return without adding anything to the queue.
514 *
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000515 * @param[in,out] queue OpenCL command queue.
516 * @param[in] kernel Kernel to enqueue
517 * @param[in] window Window the kernel has to process.
518 * @param[in] lws_hint (Optional) Local workgroup size requested. Default is based on the device target.
519 * @param[in] use_dummy_work_items (Optional) Use dummy work items in order to have two dimensional power of two NDRange. Default is false
520 * Note: it is kernel responsibility to check if the work-item is out-of-range
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100521 *
522 * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed.
523 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100524void enqueue(cl::CommandQueue &queue,
525 ICLKernel &kernel,
526 const Window &window,
527 const cl::NDRange &lws_hint = CLKernelLibrary::get().default_ndrange(),
528 bool use_dummy_work_items = false);
SiCong Li3e363692017-07-04 15:02:10 +0100529
Alex Gildayc357c472018-03-21 13:54:09 +0000530/** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
531 *
532 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
533 * @param[in] array Array to set as an argument of the object's kernel.
534 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
535 * @param[in] num_dimensions Number of dimensions of the @p array.
536 * @param[in] window Window the kernel will be executed on.
537 */
SiCong Li3e363692017-07-04 15:02:10 +0100538template <typename T, unsigned int dimension_size>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100539void ICLKernel::add_array_argument(
540 unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
SiCong Li3e363692017-07-04 15:02:10 +0100541{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000542 ARM_COMPUTE_ERROR_ON(array == nullptr);
543
SiCong Li3e363692017-07-04 15:02:10 +0100544 // Calculate offset to the start of the window
545 unsigned int offset_first_element = 0;
546
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100547 for (unsigned int n = 0; n < num_dimensions; ++n)
SiCong Li3e363692017-07-04 15:02:10 +0100548 {
549 offset_first_element += window[n].start() * strides[n];
550 }
551
552 unsigned int idx_start = idx;
553 _kernel.setArg(idx++, array->cl_buffer());
554
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100555 for (unsigned int dimension = 0; dimension < dimension_size; dimension++)
SiCong Li3e363692017-07-04 15:02:10 +0100556 {
557 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
558 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
559 }
560
561 _kernel.setArg<cl_uint>(idx++, offset_first_element);
562
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100563 ARM_COMPUTE_ERROR_ON_MSG_VAR(idx_start + num_arguments_per_array<dimension_size>() != idx,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100564 "add_%dD_array_argument() is supposed to add exactly %d arguments to the kernel",
565 dimension_size, num_arguments_per_array<dimension_size>());
SiCong Li3e363692017-07-04 15:02:10 +0100566 ARM_COMPUTE_UNUSED(idx_start);
567}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100568} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000569#endif /*ARM_COMPUTE_ICLKERNEL_H */