blob: a1bc3eb8d26d954b27f891f46a5b359e63db8e15 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
3 *
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{
44public:
45 /** Constructor */
46 ICLKernel();
47 /** Returns a reference to the OpenCL kernel of this object.
48 *
49 * @return A reference to the OpenCL kernel of this object.
50 */
51 cl::Kernel &kernel();
SiCong Li3e363692017-07-04 15:02:10 +010052 /** Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx.
53 *
54 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
55 * @param[in] array Array to set as an argument of the object's kernel.
56 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
57 * @param[in] num_dimensions Number of dimensions of the @p array.
58 * @param[in] window Window the kernel will be executed on.
59 */
60 template <typename T>
61 void add_1D_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 +010062 /** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
63 *
64 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
65 * @param[in] tensor Tensor to set as an argument of the object's kernel.
66 * @param[in] window Window the kernel will be executed on.
67 */
68 void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
69 /** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
70 *
71 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
72 * @param[in] tensor Tensor to set as an argument of the object's kernel.
73 * @param[in] window Window the kernel will be executed on.
74 */
75 void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
76 /** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
77 *
78 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
79 * @param[in] tensor Tensor to set as an argument of the object's kernel.
80 * @param[in] window Window the kernel will be executed on.
81 */
82 void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
steniu01868e5412017-07-17 23:16:00 +010083 /** Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
84 *
85 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
86 * @param[in] tensor Tensor to set as an argument of the object's kernel.
87 * @param[in] window Window the kernel will be executed on.
88 */
89 void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
SiCong Li3e363692017-07-04 15:02:10 +010090 /** Returns the number of arguments enqueued per 1D array object.
91 *
92 * @return The number of arguments enqueues per 1D array object.
93 */
94 unsigned int num_arguments_per_1D_array() const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 /** Returns the number of arguments enqueued per 1D tensor object.
96 *
97 * @return The number of arguments enqueues per 1D tensor object.
98 */
99 unsigned int num_arguments_per_1D_tensor() const;
100 /** Returns the number of arguments enqueued per 2D tensor object.
101 *
102 * @return The number of arguments enqueues per 2D tensor object.
103 */
104 unsigned int num_arguments_per_2D_tensor() const;
105 /** Returns the number of arguments enqueued per 3D tensor object.
106 *
107 * @return The number of arguments enqueues per 3D tensor object.
108 */
109 unsigned int num_arguments_per_3D_tensor() const;
steniu01868e5412017-07-17 23:16:00 +0100110 /** Returns the number of arguments enqueued per 4D tensor object.
111 *
112 * @return The number of arguments enqueues per 4D tensor object.
113 */
114 unsigned int num_arguments_per_4D_tensor() const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 /** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
116 *
117 * @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
118 *
119 * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
120 * @param[in,out] queue Command queue on which to enqueue the kernel.
121 */
122 virtual void run(const Window &window, cl::CommandQueue &queue) = 0;
123 /** Add the passed parameters to the object's kernel's arguments starting from the index idx.
124 *
125 * @param[in,out] idx Index at which to start adding the arguments. Will be incremented by the number of kernel arguments set.
126 * @param[in] value Value to set as an argument of the object's kernel.
127 */
128 template <typename T>
129 void add_argument(unsigned int &idx, T value)
130 {
131 _kernel.setArg(idx++, value);
132 }
133
Gian Marco Iodice9331aeb2017-08-10 17:11:08 +0100134 /** Set the Local-Workgroup-Size hint
135 *
136 * @note This method should be called after the configuration of the kernel
137 *
138 * @param[in] lws_hint Local-Workgroup-Size to use
139 */
140 void set_lws_hint(cl::NDRange &lws_hint)
141 {
142 _lws_hint = lws_hint;
143 }
144
Gian Marcode691f02017-09-08 16:13:11 +0100145 /** Get the configuration ID
146 *
147 * @note The configuration ID can be used by the caller to distinguish different calls of the same OpenCL kernel
148 * In particular, this method can be used by CLScheduler to keep track of the best LWS for each configuration of the same kernel.
149 * The configuration ID should be provided only for the kernels potentially affected by the LWS geometry
150 *
151 * @note This method should be called after the configuration of the kernel
152 *
153 * @return configuration id string
154 */
155 const std::string &config_id() const
156 {
157 return _config_id;
158 }
159
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 /** Set the targeted GPU architecture
161 *
162 * @param[in] target The targeted GPU architecture
163 */
164 void set_target(GPUTarget target);
165
166 /** Set the targeted GPU architecture according to the CL device
167 *
168 * @param[in] device A CL device
169 */
170 void set_target(cl::Device &device);
171
172 /** Get the targeted GPU architecture
173 *
174 * @return The targeted GPU architecture.
175 */
176 GPUTarget get_target() const;
177
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100178 /** Get the maximum workgroup size for the device the CLKernelLibrary uses.
179 *
180 * @return The maximum workgroup size value.
181 */
182 size_t get_max_workgroup_size();
Georgios Pinitas1f378ee2017-10-27 13:37:16 +0100183 /** Get the global work size given an execution window
184 *
185 * @param[in] window Execution window
186 *
187 * @return Global work size of the given execution window
188 */
189 static cl::NDRange gws_from_window(const Window &window);
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100190
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191private:
SiCong Li3e363692017-07-04 15:02:10 +0100192 /** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
193 *
194 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
195 * @param[in] array Array to set as an argument of the object's kernel.
196 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
197 * @param[in] num_dimensions Number of dimensions of the @p array.
198 * @param[in] window Window the kernel will be executed on.
199 */
200 template <typename T, unsigned int dimension_size>
201 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 +0100202 /** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx.
203 *
204 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
205 * @param[in] tensor Tensor to set as an argument of the object's kernel.
206 * @param[in] window Window the kernel will be executed on.
207 */
208 template <unsigned int dimension_size>
209 void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
SiCong Li3e363692017-07-04 15:02:10 +0100210 /** Returns the number of arguments enqueued per array object.
211 *
212 * @return The number of arguments enqueued per array object.
213 */
214 template <unsigned int dimension_size>
215 unsigned int num_arguments_per_array() const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 /** Returns the number of arguments enqueued per tensor object.
217 *
218 * @return The number of arguments enqueued per tensor object.
219 */
220 template <unsigned int dimension_size>
221 unsigned int num_arguments_per_tensor() const;
222
223protected:
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100224 cl::Kernel _kernel; /**< OpenCL kernel to run */
225 cl::NDRange _lws_hint; /**< Local workgroup size hint for the OpenCL kernel */
226 GPUTarget _target; /**< The targeted GPU */
227 std::string _config_id; /**< Configuration ID */
228 size_t _max_workgroup_size; /**< The maximum workgroup size for this kernel */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229};
230
231/** Add the kernel to the command queue with the given window.
232 *
233 * @note Depending on the size of the window, this might translate into several jobs being enqueued.
234 *
235 * @note If kernel->kernel() is empty then the function will return without adding anything to the queue.
236 *
237 * @param[in,out] queue OpenCL command queue.
238 * @param[in] kernel Kernel to enqueue
239 * @param[in] window Window the kernel has to process.
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100240 * @param[in] lws_hint Local workgroup size requested, by default (128,1).
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241 *
242 * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed.
243 */
steniu015f910722017-08-23 10:15:22 +0100244void 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 +0100245
246template <typename T, unsigned int dimension_size>
247void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
248{
249 // Calculate offset to the start of the window
250 unsigned int offset_first_element = 0;
251
252 for(unsigned int n = 0; n < num_dimensions; ++n)
253 {
254 offset_first_element += window[n].start() * strides[n];
255 }
256
257 unsigned int idx_start = idx;
258 _kernel.setArg(idx++, array->cl_buffer());
259
260 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
261 {
262 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
263 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
264 }
265
266 _kernel.setArg<cl_uint>(idx++, offset_first_element);
267
268 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_array<dimension_size>() != idx,
269 "add_%dD_array_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_array<dimension_size>());
270 ARM_COMPUTE_UNUSED(idx_start);
271}
272
273template <typename T>
274void ICLKernel::add_1D_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
275{
276 add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
277}
278
279template <unsigned int dimension_size>
280unsigned int ICLKernel::num_arguments_per_array() const
281{
282 return num_arguments_per_tensor<dimension_size>();
283}
284
285template <unsigned int dimension_size>
286unsigned int ICLKernel::num_arguments_per_tensor() const
287{
288 return 2 + 2 * dimension_size;
289}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290}
291#endif /*__ARM_COMPUTE_ICLKERNEL_H__ */