blob: 2d7081956bbc185e57a34e99a085269148d61e94 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recas0021d752017-12-18 14:42:56 +00002 * Copyright (c) 2016-2018 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 */
24#ifndef __ARM_COMPUTE_ICLKERNEL_H__
25#define __ARM_COMPUTE_ICLKERNEL_H__
26
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"
30#include "arm_compute/core/IKernel.h"
31
Gian Marcode691f02017-09-08 16:13:11 +010032#include <string>
33
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034namespace arm_compute
35{
SiCong Li3e363692017-07-04 15:02:10 +010036template <typename T>
37class ICLArray;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038class ICLTensor;
39class Window;
40
41/** Common interface for all the OpenCL kernels */
42class ICLKernel : public IKernel
43{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000044private:
45 /** Returns the number of arguments enqueued per array object.
46 *
47 * @return The number of arguments enqueued per array object.
48 */
49 template <unsigned int dimension_size>
50 constexpr static unsigned int num_arguments_per_array()
51 {
52 return num_arguments_per_tensor<dimension_size>();
53 }
54 /** Returns the number of arguments enqueued per tensor object.
55 *
56 * @return The number of arguments enqueued per tensor object.
57 */
58 template <unsigned int dimension_size>
59 constexpr static unsigned int num_arguments_per_tensor()
60 {
61 return 2 + 2 * dimension_size;
62 }
63
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064public:
65 /** Constructor */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000066 ICLKernel()
67 : _kernel(nullptr), _lws_hint(CLKernelLibrary::get().default_ndrange()), _target(GPUTarget::MIDGARD), _config_id(arm_compute::default_config_id), _max_workgroup_size(0)
68 {
69 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 /** Returns a reference to the OpenCL kernel of this object.
71 *
72 * @return A reference to the OpenCL kernel of this object.
73 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000074 cl::Kernel &kernel()
75 {
76 return _kernel;
77 }
SiCong Li3e363692017-07-04 15:02:10 +010078 /** Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx.
79 *
80 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
81 * @param[in] array Array to set as an argument of the object's kernel.
82 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
83 * @param[in] num_dimensions Number of dimensions of the @p array.
84 * @param[in] window Window the kernel will be executed on.
85 */
86 template <typename T>
Diego Lopez Recas0021d752017-12-18 14:42:56 +000087 void add_1D_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
88 {
89 add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
90 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 /** Add the passed 1D tensor'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 tensor's arguments. Will be incremented by the number of kernel arguments set.
94 * @param[in] tensor Tensor to set as an argument of the object's kernel.
95 * @param[in] window Window the kernel will be executed on.
96 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000097 void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
98 {
99 add_tensor_argument<1>(idx, tensor, window);
100 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
102 *
103 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
104 * @param[in] tensor Tensor to set as an argument of the object's kernel.
105 * @param[in] window Window the kernel will be executed on.
106 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000107 void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
108 {
109 add_tensor_argument<2>(idx, tensor, window);
110 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 /** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
112 *
113 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
114 * @param[in] tensor Tensor to set as an argument of the object's kernel.
115 * @param[in] window Window the kernel will be executed on.
116 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000117 void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
118 {
119 add_tensor_argument<3>(idx, tensor, window);
120 }
steniu01868e5412017-07-17 23:16:00 +0100121 /** Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
122 *
123 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
124 * @param[in] tensor Tensor to set as an argument of the object's kernel.
125 * @param[in] window Window the kernel will be executed on.
126 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000127 void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
128 {
129 add_tensor_argument<4>(idx, tensor, window);
130 }
SiCong Li3e363692017-07-04 15:02:10 +0100131 /** Returns the number of arguments enqueued per 1D array object.
132 *
133 * @return The number of arguments enqueues per 1D array object.
134 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000135 constexpr static unsigned int num_arguments_per_1D_array()
136 {
137 return num_arguments_per_array<1>();
138 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 /** Returns the number of arguments enqueued per 1D tensor object.
140 *
141 * @return The number of arguments enqueues per 1D tensor object.
142 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000143 constexpr static unsigned int num_arguments_per_1D_tensor()
144 {
145 return num_arguments_per_tensor<1>();
146 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 /** Returns the number of arguments enqueued per 2D tensor object.
148 *
149 * @return The number of arguments enqueues per 2D tensor object.
150 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000151 constexpr static unsigned int num_arguments_per_2D_tensor()
152 {
153 return num_arguments_per_tensor<2>();
154 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 /** Returns the number of arguments enqueued per 3D tensor object.
156 *
157 * @return The number of arguments enqueues per 3D tensor object.
158 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000159 constexpr static unsigned int num_arguments_per_3D_tensor()
160 {
161 return num_arguments_per_tensor<3>();
162 }
steniu01868e5412017-07-17 23:16:00 +0100163 /** Returns the number of arguments enqueued per 4D tensor object.
164 *
165 * @return The number of arguments enqueues per 4D tensor object.
166 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000167 constexpr static unsigned int num_arguments_per_4D_tensor()
168 {
169 return num_arguments_per_tensor<4>();
170 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
172 *
173 * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
174 *
175 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
176 * @param[in,out] queue Command queue on which to enqueue the kernel.
177 */
178 virtual void run(const Window &window, cl::CommandQueue &queue) = 0;
179 /** Add the passed parameters to the object's kernel's arguments starting from the index idx.
180 *
181 * @param[in,out] idx Index at which to start adding the arguments. Will be incremented by the number of kernel arguments set.
182 * @param[in] value Value to set as an argument of the object's kernel.
183 */
184 template <typename T>
185 void add_argument(unsigned int &idx, T value)
186 {
187 _kernel.setArg(idx++, value);
188 }
189
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100190 /** Set the Local-Workgroup-Size hint
191 *
192 * @note This method should be called after the configuration of the kernel
193 *
194 * @param[in] lws_hint Local-Workgroup-Size to use
195 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100196 void set_lws_hint(const cl::NDRange &lws_hint)
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100197 {
198 _lws_hint = lws_hint;
199 }
200
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000201 /** Return the Local-Workgroup-Size hint
202 *
203 * @return Current lws hint
204 */
205 cl::NDRange lws_hint() const
206 {
207 return _lws_hint;
208 }
209
Gian Marcode691f02017-09-08 16:13:11 +0100210 /** Get the configuration ID
211 *
212 * @note The configuration ID can be used by the caller to distinguish different calls of the same OpenCL kernel
213 * In particular, this method can be used by CLScheduler to keep track of the best LWS for each configuration of the same kernel.
214 * The configuration ID should be provided only for the kernels potentially affected by the LWS geometry
215 *
216 * @note This method should be called after the configuration of the kernel
217 *
218 * @return configuration id string
219 */
220 const std::string &config_id() const
221 {
222 return _config_id;
223 }
224
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225 /** Set the targeted GPU architecture
226 *
227 * @param[in] target The targeted GPU architecture
228 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000229 void set_target(GPUTarget target)
230 {
231 _target = target;
232 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233
234 /** Set the targeted GPU architecture according to the CL device
235 *
236 * @param[in] device A CL device
237 */
238 void set_target(cl::Device &device);
239
240 /** Get the targeted GPU architecture
241 *
242 * @return The targeted GPU architecture.
243 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000244 GPUTarget get_target() const
245 {
246 return _target;
247 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100249 /** Get the maximum workgroup size for the device the CLKernelLibrary uses.
250 *
251 * @return The maximum workgroup size value.
252 */
253 size_t get_max_workgroup_size();
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100254 /** Get the global work size given an execution window
255 *
256 * @param[in] window Execution window
257 *
258 * @return Global work size of the given execution window
259 */
260 static cl::NDRange gws_from_window(const Window &window);
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100261
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262private:
SiCong Li3e363692017-07-04 15:02:10 +0100263 /** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
264 *
265 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
266 * @param[in] array Array to set as an argument of the object's kernel.
267 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
268 * @param[in] num_dimensions Number of dimensions of the @p array.
269 * @param[in] window Window the kernel will be executed on.
270 */
271 template <typename T, unsigned int dimension_size>
272 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 +0100273 /** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx.
274 *
275 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
276 * @param[in] tensor Tensor to set as an argument of the object's kernel.
277 * @param[in] window Window the kernel will be executed on.
278 */
279 template <unsigned int dimension_size>
280 void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100281
282protected:
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100283 cl::Kernel _kernel; /**< OpenCL kernel to run */
284 cl::NDRange _lws_hint; /**< Local workgroup size hint for the OpenCL kernel */
285 GPUTarget _target; /**< The targeted GPU */
286 std::string _config_id; /**< Configuration ID */
287 size_t _max_workgroup_size; /**< The maximum workgroup size for this kernel */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288};
289
290/** Add the kernel to the command queue with the given window.
291 *
292 * @note Depending on the size of the window, this might translate into several jobs being enqueued.
293 *
294 * @note If kernel->kernel() is empty then the function will return without adding anything to the queue.
295 *
296 * @param[in,out] queue OpenCL command queue.
297 * @param[in] kernel Kernel to enqueue
298 * @param[in] window Window the kernel has to process.
Michalis Spyroua9676112018-02-22 18:07:43 +0000299 * @param[in] lws_hint Local workgroup size requested. Default is based on the device target.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100300 *
301 * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed.
302 */
steniu015f910722017-08-23 10:15:22 +0100303void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint = CLKernelLibrary::get().default_ndrange());
SiCong Li3e363692017-07-04 15:02:10 +0100304
Alex Gildayc357c472018-03-21 13:54:09 +0000305/** 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 */
SiCong Li3e363692017-07-04 15:02:10 +0100313template <typename T, unsigned int dimension_size>
314void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
315{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000316 ARM_COMPUTE_ERROR_ON(array == nullptr);
317
SiCong Li3e363692017-07-04 15:02:10 +0100318 // Calculate offset to the start of the window
319 unsigned int offset_first_element = 0;
320
321 for(unsigned int n = 0; n < num_dimensions; ++n)
322 {
323 offset_first_element += window[n].start() * strides[n];
324 }
325
326 unsigned int idx_start = idx;
327 _kernel.setArg(idx++, array->cl_buffer());
328
329 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
330 {
331 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
332 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
333 }
334
335 _kernel.setArg<cl_uint>(idx++, offset_first_element);
336
337 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_array<dimension_size>() != idx,
338 "add_%dD_array_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_array<dimension_size>());
339 ARM_COMPUTE_UNUSED(idx_start);
340}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100341}
342#endif /*__ARM_COMPUTE_ICLKERNEL_H__ */