blob: 9e970641c15e4b2448924c8c9f8d142acc980950 [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 }
64
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065public:
66 /** Constructor */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000067 ICLKernel()
68 : _kernel(nullptr), _lws_hint(CLKernelLibrary::get().default_ndrange()), _target(GPUTarget::MIDGARD), _config_id(arm_compute::default_config_id), _max_workgroup_size(0)
69 {
70 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 /** Returns a reference to the OpenCL kernel of this object.
72 *
73 * @return A reference to the OpenCL kernel of this object.
74 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000075 cl::Kernel &kernel()
76 {
77 return _kernel;
78 }
SiCong Li3e363692017-07-04 15:02:10 +010079 /** Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx.
80 *
81 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
82 * @param[in] array Array to set as an argument of the object's kernel.
83 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
84 * @param[in] num_dimensions Number of dimensions of the @p array.
85 * @param[in] window Window the kernel will be executed on.
86 */
87 template <typename T>
Diego Lopez Recas0021d752017-12-18 14:42:56 +000088 void add_1D_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
89 {
90 add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
91 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
93 *
94 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
95 * @param[in] tensor Tensor to set as an argument of the object's kernel.
96 * @param[in] window Window the kernel will be executed on.
97 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000098 void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
99 {
100 add_tensor_argument<1>(idx, tensor, window);
101 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
103 *
104 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
105 * @param[in] tensor Tensor to set as an argument of the object's kernel.
106 * @param[in] window Window the kernel will be executed on.
107 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000108 void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
109 {
110 add_tensor_argument<2>(idx, tensor, window);
111 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 /** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
113 *
114 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
115 * @param[in] tensor Tensor to set as an argument of the object's kernel.
116 * @param[in] window Window the kernel will be executed on.
117 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000118 void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
119 {
120 add_tensor_argument<3>(idx, tensor, window);
121 }
steniu01868e5412017-07-17 23:16:00 +0100122 /** Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
123 *
124 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
125 * @param[in] tensor Tensor to set as an argument of the object's kernel.
126 * @param[in] window Window the kernel will be executed on.
127 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000128 void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
129 {
130 add_tensor_argument<4>(idx, tensor, window);
131 }
SiCong Li3e363692017-07-04 15:02:10 +0100132 /** Returns the number of arguments enqueued per 1D array object.
133 *
134 * @return The number of arguments enqueues per 1D array object.
135 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000136 constexpr static unsigned int num_arguments_per_1D_array()
137 {
138 return num_arguments_per_array<1>();
139 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 /** Returns the number of arguments enqueued per 1D tensor object.
141 *
142 * @return The number of arguments enqueues per 1D tensor object.
143 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000144 constexpr static unsigned int num_arguments_per_1D_tensor()
145 {
146 return num_arguments_per_tensor<1>();
147 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 /** Returns the number of arguments enqueued per 2D tensor object.
149 *
150 * @return The number of arguments enqueues per 2D tensor object.
151 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000152 constexpr static unsigned int num_arguments_per_2D_tensor()
153 {
154 return num_arguments_per_tensor<2>();
155 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 /** Returns the number of arguments enqueued per 3D tensor object.
157 *
158 * @return The number of arguments enqueues per 3D tensor object.
159 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000160 constexpr static unsigned int num_arguments_per_3D_tensor()
161 {
162 return num_arguments_per_tensor<3>();
163 }
steniu01868e5412017-07-17 23:16:00 +0100164 /** Returns the number of arguments enqueued per 4D tensor object.
165 *
166 * @return The number of arguments enqueues per 4D tensor object.
167 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000168 constexpr static unsigned int num_arguments_per_4D_tensor()
169 {
170 return num_arguments_per_tensor<4>();
171 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
173 *
174 * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
175 *
176 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
177 * @param[in,out] queue Command queue on which to enqueue the kernel.
178 */
179 virtual void run(const Window &window, cl::CommandQueue &queue) = 0;
180 /** Add the passed 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 arguments. Will be incremented by the number of kernel arguments set.
183 * @param[in] value Value to set as an argument of the object's kernel.
184 */
185 template <typename T>
186 void add_argument(unsigned int &idx, T value)
187 {
188 _kernel.setArg(idx++, value);
189 }
190
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100191 /** Set the Local-Workgroup-Size hint
192 *
193 * @note This method should be called after the configuration of the kernel
194 *
195 * @param[in] lws_hint Local-Workgroup-Size to use
196 */
Anthony Barbierd727e852018-04-20 11:05:29 +0100197 void set_lws_hint(const cl::NDRange &lws_hint)
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100198 {
199 _lws_hint = lws_hint;
200 }
201
Georgios Pinitasc0d1c862018-03-23 15:13:15 +0000202 /** Return the Local-Workgroup-Size hint
203 *
204 * @return Current lws hint
205 */
206 cl::NDRange lws_hint() const
207 {
208 return _lws_hint;
209 }
210
Gian Marcode691f02017-09-08 16:13:11 +0100211 /** Get the configuration ID
212 *
213 * @note The configuration ID can be used by the caller to distinguish different calls of the same OpenCL kernel
214 * In particular, this method can be used by CLScheduler to keep track of the best LWS for each configuration of the same kernel.
215 * The configuration ID should be provided only for the kernels potentially affected by the LWS geometry
216 *
217 * @note This method should be called after the configuration of the kernel
218 *
219 * @return configuration id string
220 */
221 const std::string &config_id() const
222 {
223 return _config_id;
224 }
225
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 /** Set the targeted GPU architecture
227 *
228 * @param[in] target The targeted GPU architecture
229 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000230 void set_target(GPUTarget target)
231 {
232 _target = target;
233 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234
235 /** Set the targeted GPU architecture according to the CL device
236 *
237 * @param[in] device A CL device
238 */
239 void set_target(cl::Device &device);
240
241 /** Get the targeted GPU architecture
242 *
243 * @return The targeted GPU architecture.
244 */
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000245 GPUTarget get_target() const
246 {
247 return _target;
248 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100250 /** Get the maximum workgroup size for the device the CLKernelLibrary uses.
251 *
252 * @return The maximum workgroup size value.
253 */
254 size_t get_max_workgroup_size();
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100255 /** Get the global work size given an execution window
256 *
257 * @param[in] window Execution window
258 *
259 * @return Global work size of the given execution window
260 */
261 static cl::NDRange gws_from_window(const Window &window);
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100262
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100263private:
SiCong Li3e363692017-07-04 15:02:10 +0100264 /** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
265 *
266 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
267 * @param[in] array Array to set as an argument of the object's kernel.
268 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
269 * @param[in] num_dimensions Number of dimensions of the @p array.
270 * @param[in] window Window the kernel will be executed on.
271 */
272 template <typename T, unsigned int dimension_size>
273 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 +0100274 /** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx.
275 *
276 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
277 * @param[in] tensor Tensor to set as an argument of the object's kernel.
278 * @param[in] window Window the kernel will be executed on.
279 */
280 template <unsigned int dimension_size>
281 void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282
283protected:
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100284 cl::Kernel _kernel; /**< OpenCL kernel to run */
285 cl::NDRange _lws_hint; /**< Local workgroup size hint for the OpenCL kernel */
286 GPUTarget _target; /**< The targeted GPU */
287 std::string _config_id; /**< Configuration ID */
288 size_t _max_workgroup_size; /**< The maximum workgroup size for this kernel */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289};
290
291/** Add the kernel to the command queue with the given window.
292 *
293 * @note Depending on the size of the window, this might translate into several jobs being enqueued.
294 *
295 * @note If kernel->kernel() is empty then the function will return without adding anything to the queue.
296 *
297 * @param[in,out] queue OpenCL command queue.
298 * @param[in] kernel Kernel to enqueue
299 * @param[in] window Window the kernel has to process.
Michalis Spyroua9676112018-02-22 18:07:43 +0000300 * @param[in] lws_hint Local workgroup size requested. Default is based on the device target.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301 *
302 * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed.
303 */
steniu015f910722017-08-23 10:15:22 +0100304void 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 +0100305
Alex Gildayc357c472018-03-21 13:54:09 +0000306/** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
307 *
308 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
309 * @param[in] array Array to set as an argument of the object's kernel.
310 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
311 * @param[in] num_dimensions Number of dimensions of the @p array.
312 * @param[in] window Window the kernel will be executed on.
313 */
SiCong Li3e363692017-07-04 15:02:10 +0100314template <typename T, unsigned int dimension_size>
315void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
316{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000317 ARM_COMPUTE_ERROR_ON(array == nullptr);
318
SiCong Li3e363692017-07-04 15:02:10 +0100319 // Calculate offset to the start of the window
320 unsigned int offset_first_element = 0;
321
322 for(unsigned int n = 0; n < num_dimensions; ++n)
323 {
324 offset_first_element += window[n].start() * strides[n];
325 }
326
327 unsigned int idx_start = idx;
328 _kernel.setArg(idx++, array->cl_buffer());
329
330 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
331 {
332 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
333 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
334 }
335
336 _kernel.setArg<cl_uint>(idx++, offset_first_element);
337
338 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_array<dimension_size>() != idx,
339 "add_%dD_array_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_array<dimension_size>());
340 ARM_COMPUTE_UNUSED(idx_start);
341}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100342}
343#endif /*__ARM_COMPUTE_ICLKERNEL_H__ */