blob: 3e545c61aac1288981bd24693d258a707e098e2b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +00002 * Copyright (c) 2016-2019 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"
32
Gian Marcode691f02017-09-08 16:13:11 +010033#include <string>
34
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035namespace arm_compute
36{
SiCong Li3e363692017-07-04 15:02:10 +010037template <typename T>
38class ICLArray;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039class ICLTensor;
40class Window;
41
42/** Common interface for all the OpenCL kernels */
43class ICLKernel : public IKernel
44{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000045private:
46 /** Returns the number of arguments enqueued per array object.
47 *
48 * @return The number of arguments enqueued per array object.
49 */
50 template <unsigned int dimension_size>
51 constexpr static unsigned int num_arguments_per_array()
52 {
53 return num_arguments_per_tensor<dimension_size>();
54 }
55 /** Returns the number of arguments enqueued per tensor object.
56 *
57 * @return The number of arguments enqueued per tensor object.
58 */
59 template <unsigned int dimension_size>
60 constexpr static unsigned int num_arguments_per_tensor()
61 {
62 return 2 + 2 * dimension_size;
63 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +010064 using IKernel::configure; //Prevent children from calling IKernel::configure() directly
Anthony Barbier5a65cfd2018-08-10 14:10:08 +010065protected:
66 /** Configure the kernel's window and local workgroup size hint.
67 *
68 * @param[in] window The maximum window which will be returned by window()
69 * @param[in] lws_hint (Optional) Local-Workgroup-Size to use.
70 */
Anthony Barbierb6eb3532018-08-08 13:20:04 +010071 void configure_internal(const Window &window, cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange())
72 {
73 _lws_hint = lws_hint;
74 IKernel::configure(window);
75 }
76
Anthony Barbier5a65cfd2018-08-10 14:10:08 +010077public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 /** Constructor */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000079 ICLKernel()
Anthony Barbierb6eb3532018-08-08 13:20:04 +010080 : _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 +000081 {
82 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 /** Returns a reference to the OpenCL kernel of this object.
84 *
85 * @return A reference to the OpenCL kernel of this object.
86 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000087 cl::Kernel &kernel()
88 {
89 return _kernel;
90 }
SiCong Li3e363692017-07-04 15:02:10 +010091 /** Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx.
92 *
93 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
94 * @param[in] array Array to set as an argument of the object's kernel.
95 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
96 * @param[in] num_dimensions Number of dimensions of the @p array.
97 * @param[in] window Window the kernel will be executed on.
98 */
99 template <typename T>
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000100 void add_1D_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
101 {
102 add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
103 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
105 *
106 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
107 * @param[in] tensor Tensor to set as an argument of the object's kernel.
108 * @param[in] window Window the kernel will be executed on.
109 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000110 void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
111 {
112 add_tensor_argument<1>(idx, tensor, window);
113 }
Michalis Spyroue1651a52019-07-11 15:00:49 +0100114 /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true.
115 *
116 * @param[in] cond Condition to check
117 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
118 * @param[in] tensor Tensor to set as an argument of the object's kernel.
119 * @param[in] window Window the kernel will be executed on.
120 */
121 void add_1D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
122 {
123 if(cond)
124 {
125 add_1D_tensor_argument(idx, tensor, window);
126 }
127 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
129 *
130 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
131 * @param[in] tensor Tensor to set as an argument of the object's kernel.
132 * @param[in] window Window the kernel will be executed on.
133 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000134 void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
135 {
136 add_tensor_argument<2>(idx, tensor, window);
137 }
Michalis Spyroue1651a52019-07-11 15:00:49 +0100138 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true.
139 *
140 * @param[in] cond Condition to check
141 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
142 * @param[in] tensor Tensor to set as an argument of the object's kernel.
143 * @param[in] window Window the kernel will be executed on.
144 */
145 void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
146 {
147 if(cond)
148 {
149 add_2D_tensor_argument(idx, tensor, window);
150 }
151 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 /** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
153 *
154 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
155 * @param[in] tensor Tensor to set as an argument of the object's kernel.
156 * @param[in] window Window the kernel will be executed on.
157 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000158 void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
159 {
160 add_tensor_argument<3>(idx, tensor, window);
161 }
steniu01868e5412017-07-17 23:16:00 +0100162 /** Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
163 *
164 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
165 * @param[in] tensor Tensor to set as an argument of the object's kernel.
166 * @param[in] window Window the kernel will be executed on.
167 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000168 void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
169 {
170 add_tensor_argument<4>(idx, tensor, window);
171 }
SiCong Li3e363692017-07-04 15:02:10 +0100172 /** Returns the number of arguments enqueued per 1D array object.
173 *
174 * @return The number of arguments enqueues per 1D array object.
175 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000176 constexpr static unsigned int num_arguments_per_1D_array()
177 {
178 return num_arguments_per_array<1>();
179 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180 /** Returns the number of arguments enqueued per 1D tensor object.
181 *
182 * @return The number of arguments enqueues per 1D tensor object.
183 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000184 constexpr static unsigned int num_arguments_per_1D_tensor()
185 {
186 return num_arguments_per_tensor<1>();
187 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188 /** Returns the number of arguments enqueued per 2D tensor object.
189 *
190 * @return The number of arguments enqueues per 2D tensor object.
191 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000192 constexpr static unsigned int num_arguments_per_2D_tensor()
193 {
194 return num_arguments_per_tensor<2>();
195 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 /** Returns the number of arguments enqueued per 3D tensor object.
197 *
198 * @return The number of arguments enqueues per 3D tensor object.
199 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000200 constexpr static unsigned int num_arguments_per_3D_tensor()
201 {
202 return num_arguments_per_tensor<3>();
203 }
steniu01868e5412017-07-17 23:16:00 +0100204 /** Returns the number of arguments enqueued per 4D tensor object.
205 *
206 * @return The number of arguments enqueues per 4D tensor object.
207 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000208 constexpr static unsigned int num_arguments_per_4D_tensor()
209 {
210 return num_arguments_per_tensor<4>();
211 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212 /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
213 *
214 * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
215 *
216 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
217 * @param[in,out] queue Command queue on which to enqueue the kernel.
218 */
219 virtual void run(const Window &window, cl::CommandQueue &queue) = 0;
220 /** Add the passed parameters to the object's kernel's arguments starting from the index idx.
221 *
222 * @param[in,out] idx Index at which to start adding the arguments. Will be incremented by the number of kernel arguments set.
223 * @param[in] value Value to set as an argument of the object's kernel.
224 */
225 template <typename T>
226 void add_argument(unsigned int &idx, T value)
227 {
228 _kernel.setArg(idx++, value);
229 }
230
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100231 /** Set the Local-Workgroup-Size hint
232 *
233 * @note This method should be called after the configuration of the kernel
234 *
235 * @param[in] lws_hint Local-Workgroup-Size to use
236 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100237 void set_lws_hint(const cl::NDRange &lws_hint)
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100238 {
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100239 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); // lws_hint will be overwritten by configure()
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100240 _lws_hint = lws_hint;
241 }
242
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000243 /** Return the Local-Workgroup-Size hint
244 *
245 * @return Current lws hint
246 */
247 cl::NDRange lws_hint() const
248 {
249 return _lws_hint;
250 }
251
Gian Marcode691f02017-09-08 16:13:11 +0100252 /** Get the configuration ID
253 *
254 * @note The configuration ID can be used by the caller to distinguish different calls of the same OpenCL kernel
255 * In particular, this method can be used by CLScheduler to keep track of the best LWS for each configuration of the same kernel.
256 * The configuration ID should be provided only for the kernels potentially affected by the LWS geometry
257 *
258 * @note This method should be called after the configuration of the kernel
259 *
260 * @return configuration id string
261 */
262 const std::string &config_id() const
263 {
264 return _config_id;
265 }
266
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267 /** Set the targeted GPU architecture
268 *
269 * @param[in] target The targeted GPU architecture
270 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000271 void set_target(GPUTarget target)
272 {
273 _target = target;
274 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100275
276 /** Set the targeted GPU architecture according to the CL device
277 *
278 * @param[in] device A CL device
279 */
280 void set_target(cl::Device &device);
281
282 /** Get the targeted GPU architecture
283 *
284 * @return The targeted GPU architecture.
285 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000286 GPUTarget get_target() const
287 {
288 return _target;
289 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100291 /** Get the maximum workgroup size for the device the CLKernelLibrary uses.
292 *
293 * @return The maximum workgroup size value.
294 */
295 size_t get_max_workgroup_size();
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100296 /** Get the global work size given an execution window
297 *
298 * @param[in] window Execution window
299 *
300 * @return Global work size of the given execution window
301 */
302 static cl::NDRange gws_from_window(const Window &window);
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100303
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100304private:
SiCong Li3e363692017-07-04 15:02:10 +0100305 /** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
306 *
307 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
308 * @param[in] array Array to set as an argument of the object's kernel.
309 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
310 * @param[in] num_dimensions Number of dimensions of the @p array.
311 * @param[in] window Window the kernel will be executed on.
312 */
313 template <typename T, unsigned int dimension_size>
314 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 +0100315 /** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx.
316 *
317 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
318 * @param[in] tensor Tensor to set as an argument of the object's kernel.
319 * @param[in] window Window the kernel will be executed on.
320 */
321 template <unsigned int dimension_size>
322 void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323
324protected:
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100325 cl::Kernel _kernel; /**< OpenCL kernel to run */
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100326 GPUTarget _target; /**< The targeted GPU */
327 std::string _config_id; /**< Configuration ID */
328 size_t _max_workgroup_size; /**< The maximum workgroup size for this kernel */
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100329private:
330 cl::NDRange _lws_hint; /**< Local workgroup size hint for the OpenCL kernel */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331};
332
333/** Add the kernel to the command queue with the given window.
334 *
335 * @note Depending on the size of the window, this might translate into several jobs being enqueued.
336 *
337 * @note If kernel->kernel() is empty then the function will return without adding anything to the queue.
338 *
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000339 * @param[in,out] queue OpenCL command queue.
340 * @param[in] kernel Kernel to enqueue
341 * @param[in] window Window the kernel has to process.
342 * @param[in] lws_hint (Optional) Local workgroup size requested. Default is based on the device target.
343 * @param[in] use_dummy_work_items (Optional) Use dummy work items in order to have two dimensional power of two NDRange. Default is false
344 * Note: it is kernel responsibility to check if the work-item is out-of-range
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100345 *
346 * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed.
347 */
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000348void 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 +0100349
Alex Gildayc357c472018-03-21 13:54:09 +0000350/** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
351 *
352 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
353 * @param[in] array Array to set as an argument of the object's kernel.
354 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
355 * @param[in] num_dimensions Number of dimensions of the @p array.
356 * @param[in] window Window the kernel will be executed on.
357 */
SiCong Li3e363692017-07-04 15:02:10 +0100358template <typename T, unsigned int dimension_size>
359void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
360{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000361 ARM_COMPUTE_ERROR_ON(array == nullptr);
362
SiCong Li3e363692017-07-04 15:02:10 +0100363 // Calculate offset to the start of the window
364 unsigned int offset_first_element = 0;
365
366 for(unsigned int n = 0; n < num_dimensions; ++n)
367 {
368 offset_first_element += window[n].start() * strides[n];
369 }
370
371 unsigned int idx_start = idx;
372 _kernel.setArg(idx++, array->cl_buffer());
373
374 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
375 {
376 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
377 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
378 }
379
380 _kernel.setArg<cl_uint>(idx++, offset_first_element);
381
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100382 ARM_COMPUTE_ERROR_ON_MSG_VAR(idx_start + num_arguments_per_array<dimension_size>() != idx,
383 "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 +0100384 ARM_COMPUTE_UNUSED(idx_start);
385}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100386}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000387#endif /*ARM_COMPUTE_ICLKERNEL_H */