blob: a24cd8c798906726a564abccac29f7cde6623c3b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 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"
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +010030#include "arm_compute/core/GPUTarget.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/IKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "arm_compute/core/Validate.h"
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010033#include "arm_compute/core/experimental/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Gian Marcode691f02017-09-08 16:13:11 +010035#include <string>
36
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037namespace arm_compute
38{
SiCong Li3e363692017-07-04 15:02:10 +010039template <typename T>
40class ICLArray;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041class ICLTensor;
42class Window;
43
44/** Common interface for all the OpenCL kernels */
45class ICLKernel : public IKernel
46{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000047private:
48 /** Returns the number of arguments enqueued per array object.
49 *
50 * @return The number of arguments enqueued per array object.
51 */
52 template <unsigned int dimension_size>
53 constexpr static unsigned int num_arguments_per_array()
54 {
55 return num_arguments_per_tensor<dimension_size>();
56 }
57 /** Returns the number of arguments enqueued per tensor object.
58 *
59 * @return The number of arguments enqueued per tensor object.
60 */
61 template <unsigned int dimension_size>
62 constexpr static unsigned int num_arguments_per_tensor()
63 {
64 return 2 + 2 * dimension_size;
65 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +010066 using IKernel::configure; //Prevent children from calling IKernel::configure() directly
Anthony Barbier5a65cfd2018-08-10 14:10:08 +010067protected:
68 /** Configure the kernel's window and local workgroup size hint.
69 *
70 * @param[in] window The maximum window which will be returned by window()
71 * @param[in] lws_hint (Optional) Local-Workgroup-Size to use.
72 */
Anthony Barbierb6eb3532018-08-08 13:20:04 +010073 void configure_internal(const Window &window, cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange())
74 {
75 _lws_hint = lws_hint;
76 IKernel::configure(window);
77 }
78
Anthony Barbier5a65cfd2018-08-10 14:10:08 +010079public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 /** Constructor */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000081 ICLKernel()
Anthony Barbierb6eb3532018-08-08 13:20:04 +010082 : _kernel(nullptr), _target(GPUTarget::MIDGARD), _config_id(arm_compute::default_config_id), _max_workgroup_size(0), _lws_hint()
Diego Lopez Recas0021d752017-12-18 14:42:56 +000083 {
84 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 /** Returns a reference to the OpenCL kernel of this object.
86 *
87 * @return A reference to the OpenCL kernel of this object.
88 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000089 cl::Kernel &kernel()
90 {
91 return _kernel;
92 }
SiCong Li3e363692017-07-04 15:02:10 +010093 /** Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx.
94 *
95 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
96 * @param[in] array Array to set as an argument of the object's kernel.
97 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
98 * @param[in] num_dimensions Number of dimensions of the @p array.
99 * @param[in] window Window the kernel will be executed on.
100 */
101 template <typename T>
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000102 void add_1D_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
103 {
104 add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
105 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
107 *
108 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
109 * @param[in] tensor Tensor to set as an argument of the object's kernel.
110 * @param[in] window Window the kernel will be executed on.
111 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000112 void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
113 {
114 add_tensor_argument<1>(idx, tensor, window);
115 }
Michalis Spyroue1651a52019-07-11 15:00:49 +0100116 /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true.
117 *
118 * @param[in] cond Condition to check
119 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
120 * @param[in] tensor Tensor to set as an argument of the object's kernel.
121 * @param[in] window Window the kernel will be executed on.
122 */
123 void add_1D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
124 {
125 if(cond)
126 {
127 add_1D_tensor_argument(idx, tensor, window);
128 }
129 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
131 *
132 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
133 * @param[in] tensor Tensor to set as an argument of the object's kernel.
134 * @param[in] window Window the kernel will be executed on.
135 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000136 void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
137 {
138 add_tensor_argument<2>(idx, tensor, window);
139 }
Michalis Spyroue1651a52019-07-11 15:00:49 +0100140 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true.
141 *
142 * @param[in] cond Condition to check
143 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
144 * @param[in] tensor Tensor to set as an argument of the object's kernel.
145 * @param[in] window Window the kernel will be executed on.
146 */
147 void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
148 {
149 if(cond)
150 {
151 add_2D_tensor_argument(idx, tensor, window);
152 }
153 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 /** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
155 *
156 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
157 * @param[in] tensor Tensor to set as an argument of the object's kernel.
158 * @param[in] window Window the kernel will be executed on.
159 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000160 void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
161 {
162 add_tensor_argument<3>(idx, tensor, window);
163 }
steniu01868e5412017-07-17 23:16:00 +0100164 /** Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
165 *
166 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
167 * @param[in] tensor Tensor to set as an argument of the object's kernel.
168 * @param[in] window Window the kernel will be executed on.
169 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000170 void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
171 {
172 add_tensor_argument<4>(idx, tensor, window);
173 }
SiCong Li3e363692017-07-04 15:02:10 +0100174 /** Returns the number of arguments enqueued per 1D array object.
175 *
176 * @return The number of arguments enqueues per 1D array object.
177 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000178 constexpr static unsigned int num_arguments_per_1D_array()
179 {
180 return num_arguments_per_array<1>();
181 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 /** Returns the number of arguments enqueued per 1D tensor object.
183 *
184 * @return The number of arguments enqueues per 1D tensor object.
185 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000186 constexpr static unsigned int num_arguments_per_1D_tensor()
187 {
188 return num_arguments_per_tensor<1>();
189 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 /** Returns the number of arguments enqueued per 2D tensor object.
191 *
192 * @return The number of arguments enqueues per 2D tensor object.
193 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000194 constexpr static unsigned int num_arguments_per_2D_tensor()
195 {
196 return num_arguments_per_tensor<2>();
197 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 /** Returns the number of arguments enqueued per 3D tensor object.
199 *
200 * @return The number of arguments enqueues per 3D tensor object.
201 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000202 constexpr static unsigned int num_arguments_per_3D_tensor()
203 {
204 return num_arguments_per_tensor<3>();
205 }
steniu01868e5412017-07-17 23:16:00 +0100206 /** Returns the number of arguments enqueued per 4D tensor object.
207 *
208 * @return The number of arguments enqueues per 4D tensor object.
209 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000210 constexpr static unsigned int num_arguments_per_4D_tensor()
211 {
212 return num_arguments_per_tensor<4>();
213 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
215 *
216 * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
217 *
218 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
219 * @param[in,out] queue Command queue on which to enqueue the kernel.
220 */
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100221 virtual void run(const Window &window, cl::CommandQueue &queue)
222 {
223 ARM_COMPUTE_UNUSED(window, queue);
224 }
225 /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
226 *
227 * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
228 *
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100229 * @param[in] tensors A vector containing the tensors to operato on.
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100230 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
231 * @param[in,out] queue Command queue on which to enqueue the kernel.
232 */
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100233 virtual void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100234 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100235 ARM_COMPUTE_UNUSED(tensors, window, queue);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100236 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237 /** Add the passed parameters to the object's kernel's arguments starting from the index idx.
238 *
239 * @param[in,out] idx Index at which to start adding the arguments. Will be incremented by the number of kernel arguments set.
240 * @param[in] value Value to set as an argument of the object's kernel.
241 */
242 template <typename T>
243 void add_argument(unsigned int &idx, T value)
244 {
245 _kernel.setArg(idx++, value);
246 }
247
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100248 /** Set the Local-Workgroup-Size hint
249 *
250 * @note This method should be called after the configuration of the kernel
251 *
252 * @param[in] lws_hint Local-Workgroup-Size to use
253 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100254 void set_lws_hint(const cl::NDRange &lws_hint)
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100255 {
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100256 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); // lws_hint will be overwritten by configure()
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100257 _lws_hint = lws_hint;
258 }
259
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000260 /** Return the Local-Workgroup-Size hint
261 *
262 * @return Current lws hint
263 */
264 cl::NDRange lws_hint() const
265 {
266 return _lws_hint;
267 }
268
Gian Marcode691f02017-09-08 16:13:11 +0100269 /** Get the configuration ID
270 *
271 * @note The configuration ID can be used by the caller to distinguish different calls of the same OpenCL kernel
272 * In particular, this method can be used by CLScheduler to keep track of the best LWS for each configuration of the same kernel.
273 * The configuration ID should be provided only for the kernels potentially affected by the LWS geometry
274 *
275 * @note This method should be called after the configuration of the kernel
276 *
277 * @return configuration id string
278 */
279 const std::string &config_id() const
280 {
281 return _config_id;
282 }
283
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284 /** Set the targeted GPU architecture
285 *
286 * @param[in] target The targeted GPU architecture
287 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000288 void set_target(GPUTarget target)
289 {
290 _target = target;
291 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292
293 /** Set the targeted GPU architecture according to the CL device
294 *
295 * @param[in] device A CL device
296 */
297 void set_target(cl::Device &device);
298
299 /** Get the targeted GPU architecture
300 *
301 * @return The targeted GPU architecture.
302 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000303 GPUTarget get_target() const
304 {
305 return _target;
306 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100308 /** Get the maximum workgroup size for the device the CLKernelLibrary uses.
309 *
310 * @return The maximum workgroup size value.
311 */
312 size_t get_max_workgroup_size();
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100313 /** Get the global work size given an execution window
314 *
315 * @param[in] window Execution window
316 *
317 * @return Global work size of the given execution window
318 */
319 static cl::NDRange gws_from_window(const Window &window);
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100320
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321private:
SiCong Li3e363692017-07-04 15:02:10 +0100322 /** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
323 *
324 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
325 * @param[in] array Array to set as an argument of the object's kernel.
326 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
327 * @param[in] num_dimensions Number of dimensions of the @p array.
328 * @param[in] window Window the kernel will be executed on.
329 */
330 template <typename T, unsigned int dimension_size>
331 void add_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100332 /** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx.
333 *
334 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
335 * @param[in] tensor Tensor to set as an argument of the object's kernel.
336 * @param[in] window Window the kernel will be executed on.
337 */
338 template <unsigned int dimension_size>
339 void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100340
341protected:
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100342 cl::Kernel _kernel; /**< OpenCL kernel to run */
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100343 GPUTarget _target; /**< The targeted GPU */
344 std::string _config_id; /**< Configuration ID */
345 size_t _max_workgroup_size; /**< The maximum workgroup size for this kernel */
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100346private:
347 cl::NDRange _lws_hint; /**< Local workgroup size hint for the OpenCL kernel */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100348};
349
350/** Add the kernel to the command queue with the given window.
351 *
352 * @note Depending on the size of the window, this might translate into several jobs being enqueued.
353 *
354 * @note If kernel->kernel() is empty then the function will return without adding anything to the queue.
355 *
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000356 * @param[in,out] queue OpenCL command queue.
357 * @param[in] kernel Kernel to enqueue
358 * @param[in] window Window the kernel has to process.
359 * @param[in] lws_hint (Optional) Local workgroup size requested. Default is based on the device target.
360 * @param[in] use_dummy_work_items (Optional) Use dummy work items in order to have two dimensional power of two NDRange. Default is false
361 * Note: it is kernel responsibility to check if the work-item is out-of-range
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100362 *
363 * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed.
364 */
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000365void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint = CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items = false);
SiCong Li3e363692017-07-04 15:02:10 +0100366
Alex Gildayc357c472018-03-21 13:54:09 +0000367/** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
368 *
369 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
370 * @param[in] array Array to set as an argument of the object's kernel.
371 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
372 * @param[in] num_dimensions Number of dimensions of the @p array.
373 * @param[in] window Window the kernel will be executed on.
374 */
SiCong Li3e363692017-07-04 15:02:10 +0100375template <typename T, unsigned int dimension_size>
376void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
377{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000378 ARM_COMPUTE_ERROR_ON(array == nullptr);
379
SiCong Li3e363692017-07-04 15:02:10 +0100380 // Calculate offset to the start of the window
381 unsigned int offset_first_element = 0;
382
383 for(unsigned int n = 0; n < num_dimensions; ++n)
384 {
385 offset_first_element += window[n].start() * strides[n];
386 }
387
388 unsigned int idx_start = idx;
389 _kernel.setArg(idx++, array->cl_buffer());
390
391 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
392 {
393 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
394 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
395 }
396
397 _kernel.setArg<cl_uint>(idx++, offset_first_element);
398
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100399 ARM_COMPUTE_ERROR_ON_MSG_VAR(idx_start + num_arguments_per_array<dimension_size>() != idx,
400 "add_%dD_array_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_array<dimension_size>());
SiCong Li3e363692017-07-04 15:02:10 +0100401 ARM_COMPUTE_UNUSED(idx_start);
402}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000404#endif /*ARM_COMPUTE_ICLKERNEL_H */