blob: 150dd62a89adb5f73b72d4f3ddd673ba0aef3581 [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"
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 Barbier6ff3b192017-09-04 18:44:23 +010065public:
Anthony Barbierb6eb3532018-08-08 13:20:04 +010066 void configure_internal(const Window &window, cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange())
67 {
68 _lws_hint = lws_hint;
69 IKernel::configure(window);
70 }
71
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 /** Constructor */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000073 ICLKernel()
Anthony Barbierb6eb3532018-08-08 13:20:04 +010074 : _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 +000075 {
76 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 /** Returns a reference to the OpenCL kernel of this object.
78 *
79 * @return A reference to the OpenCL kernel of this object.
80 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000081 cl::Kernel &kernel()
82 {
83 return _kernel;
84 }
SiCong Li3e363692017-07-04 15:02:10 +010085 /** Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx.
86 *
87 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
88 * @param[in] array Array to set as an argument of the object's kernel.
89 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
90 * @param[in] num_dimensions Number of dimensions of the @p array.
91 * @param[in] window Window the kernel will be executed on.
92 */
93 template <typename T>
Diego Lopez Recas0021d752017-12-18 14:42:56 +000094 void add_1D_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
95 {
96 add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
97 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
99 *
100 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
101 * @param[in] tensor Tensor to set as an argument of the object's kernel.
102 * @param[in] window Window the kernel will be executed on.
103 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000104 void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
105 {
106 add_tensor_argument<1>(idx, tensor, window);
107 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
109 *
110 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
111 * @param[in] tensor Tensor to set as an argument of the object's kernel.
112 * @param[in] window Window the kernel will be executed on.
113 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000114 void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
115 {
116 add_tensor_argument<2>(idx, tensor, window);
117 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 /** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
119 *
120 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
121 * @param[in] tensor Tensor to set as an argument of the object's kernel.
122 * @param[in] window Window the kernel will be executed on.
123 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000124 void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
125 {
126 add_tensor_argument<3>(idx, tensor, window);
127 }
steniu01868e5412017-07-17 23:16:00 +0100128 /** Add the passed 4D 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_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
135 {
136 add_tensor_argument<4>(idx, tensor, window);
137 }
SiCong Li3e363692017-07-04 15:02:10 +0100138 /** Returns the number of arguments enqueued per 1D array object.
139 *
140 * @return The number of arguments enqueues per 1D array object.
141 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000142 constexpr static unsigned int num_arguments_per_1D_array()
143 {
144 return num_arguments_per_array<1>();
145 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 /** Returns the number of arguments enqueued per 1D tensor object.
147 *
148 * @return The number of arguments enqueues per 1D tensor object.
149 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000150 constexpr static unsigned int num_arguments_per_1D_tensor()
151 {
152 return num_arguments_per_tensor<1>();
153 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 /** Returns the number of arguments enqueued per 2D tensor object.
155 *
156 * @return The number of arguments enqueues per 2D tensor object.
157 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000158 constexpr static unsigned int num_arguments_per_2D_tensor()
159 {
160 return num_arguments_per_tensor<2>();
161 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 /** Returns the number of arguments enqueued per 3D tensor object.
163 *
164 * @return The number of arguments enqueues per 3D tensor object.
165 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000166 constexpr static unsigned int num_arguments_per_3D_tensor()
167 {
168 return num_arguments_per_tensor<3>();
169 }
steniu01868e5412017-07-17 23:16:00 +0100170 /** Returns the number of arguments enqueued per 4D tensor object.
171 *
172 * @return The number of arguments enqueues per 4D tensor object.
173 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000174 constexpr static unsigned int num_arguments_per_4D_tensor()
175 {
176 return num_arguments_per_tensor<4>();
177 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
179 *
180 * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
181 *
182 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
183 * @param[in,out] queue Command queue on which to enqueue the kernel.
184 */
185 virtual void run(const Window &window, cl::CommandQueue &queue) = 0;
186 /** Add the passed parameters to the object's kernel's arguments starting from the index idx.
187 *
188 * @param[in,out] idx Index at which to start adding the arguments. Will be incremented by the number of kernel arguments set.
189 * @param[in] value Value to set as an argument of the object's kernel.
190 */
191 template <typename T>
192 void add_argument(unsigned int &idx, T value)
193 {
194 _kernel.setArg(idx++, value);
195 }
196
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100197 /** Set the Local-Workgroup-Size hint
198 *
199 * @note This method should be called after the configuration of the kernel
200 *
201 * @param[in] lws_hint Local-Workgroup-Size to use
202 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100203 void set_lws_hint(const cl::NDRange &lws_hint)
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100204 {
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100205 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); // lws_hint will be overwritten by configure()
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100206 _lws_hint = lws_hint;
207 }
208
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000209 /** Return the Local-Workgroup-Size hint
210 *
211 * @return Current lws hint
212 */
213 cl::NDRange lws_hint() const
214 {
215 return _lws_hint;
216 }
217
Gian Marcode691f02017-09-08 16:13:11 +0100218 /** Get the configuration ID
219 *
220 * @note The configuration ID can be used by the caller to distinguish different calls of the same OpenCL kernel
221 * In particular, this method can be used by CLScheduler to keep track of the best LWS for each configuration of the same kernel.
222 * The configuration ID should be provided only for the kernels potentially affected by the LWS geometry
223 *
224 * @note This method should be called after the configuration of the kernel
225 *
226 * @return configuration id string
227 */
228 const std::string &config_id() const
229 {
230 return _config_id;
231 }
232
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233 /** Set the targeted GPU architecture
234 *
235 * @param[in] target The targeted GPU architecture
236 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000237 void set_target(GPUTarget target)
238 {
239 _target = target;
240 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241
242 /** Set the targeted GPU architecture according to the CL device
243 *
244 * @param[in] device A CL device
245 */
246 void set_target(cl::Device &device);
247
248 /** Get the targeted GPU architecture
249 *
250 * @return The targeted GPU architecture.
251 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000252 GPUTarget get_target() const
253 {
254 return _target;
255 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100256
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100257 /** Get the maximum workgroup size for the device the CLKernelLibrary uses.
258 *
259 * @return The maximum workgroup size value.
260 */
261 size_t get_max_workgroup_size();
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100262 /** Get the global work size given an execution window
263 *
264 * @param[in] window Execution window
265 *
266 * @return Global work size of the given execution window
267 */
268 static cl::NDRange gws_from_window(const Window &window);
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100269
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270private:
SiCong Li3e363692017-07-04 15:02:10 +0100271 /** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
272 *
273 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
274 * @param[in] array Array to set as an argument of the object's kernel.
275 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
276 * @param[in] num_dimensions Number of dimensions of the @p array.
277 * @param[in] window Window the kernel will be executed on.
278 */
279 template <typename T, unsigned int dimension_size>
280 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 +0100281 /** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx.
282 *
283 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
284 * @param[in] tensor Tensor to set as an argument of the object's kernel.
285 * @param[in] window Window the kernel will be executed on.
286 */
287 template <unsigned int dimension_size>
288 void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289
290protected:
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100291 cl::Kernel _kernel; /**< OpenCL kernel to run */
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100292 GPUTarget _target; /**< The targeted GPU */
293 std::string _config_id; /**< Configuration ID */
294 size_t _max_workgroup_size; /**< The maximum workgroup size for this kernel */
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100295private:
296 cl::NDRange _lws_hint; /**< Local workgroup size hint for the OpenCL kernel */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297};
298
299/** Add the kernel to the command queue with the given window.
300 *
301 * @note Depending on the size of the window, this might translate into several jobs being enqueued.
302 *
303 * @note If kernel->kernel() is empty then the function will return without adding anything to the queue.
304 *
305 * @param[in,out] queue OpenCL command queue.
306 * @param[in] kernel Kernel to enqueue
307 * @param[in] window Window the kernel has to process.
Michalis Spyroua9676112018-02-22 18:07:43 +0000308 * @param[in] lws_hint Local workgroup size requested. Default is based on the device target.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309 *
310 * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed.
311 */
steniu015f910722017-08-23 10:15:22 +0100312void 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 +0100313
Alex Gildayc357c472018-03-21 13:54:09 +0000314/** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
315 *
316 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
317 * @param[in] array Array to set as an argument of the object's kernel.
318 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
319 * @param[in] num_dimensions Number of dimensions of the @p array.
320 * @param[in] window Window the kernel will be executed on.
321 */
SiCong Li3e363692017-07-04 15:02:10 +0100322template <typename T, unsigned int dimension_size>
323void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
324{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000325 ARM_COMPUTE_ERROR_ON(array == nullptr);
326
SiCong Li3e363692017-07-04 15:02:10 +0100327 // Calculate offset to the start of the window
328 unsigned int offset_first_element = 0;
329
330 for(unsigned int n = 0; n < num_dimensions; ++n)
331 {
332 offset_first_element += window[n].start() * strides[n];
333 }
334
335 unsigned int idx_start = idx;
336 _kernel.setArg(idx++, array->cl_buffer());
337
338 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
339 {
340 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
341 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
342 }
343
344 _kernel.setArg<cl_uint>(idx++, offset_first_element);
345
346 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_array<dimension_size>() != idx,
347 "add_%dD_array_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_array<dimension_size>());
348 ARM_COMPUTE_UNUSED(idx_start);
349}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100350}
351#endif /*__ARM_COMPUTE_ICLKERNEL_H__ */