blob: 9119940bc513c83c44b2aca6961ba21beed0c7bf [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();
183
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184private:
SiCong Li3e363692017-07-04 15:02:10 +0100185 /** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
186 *
187 * @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
188 * @param[in] array Array to set as an argument of the object's kernel.
189 * @param[in] strides @ref Strides object containing stride of each dimension in bytes.
190 * @param[in] num_dimensions Number of dimensions of the @p array.
191 * @param[in] window Window the kernel will be executed on.
192 */
193 template <typename T, unsigned int dimension_size>
194 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 +0100195 /** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx.
196 *
197 * @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
198 * @param[in] tensor Tensor to set as an argument of the object's kernel.
199 * @param[in] window Window the kernel will be executed on.
200 */
201 template <unsigned int dimension_size>
202 void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
SiCong Li3e363692017-07-04 15:02:10 +0100203 /** Returns the number of arguments enqueued per array object.
204 *
205 * @return The number of arguments enqueued per array object.
206 */
207 template <unsigned int dimension_size>
208 unsigned int num_arguments_per_array() const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 /** Returns the number of arguments enqueued per tensor object.
210 *
211 * @return The number of arguments enqueued per tensor object.
212 */
213 template <unsigned int dimension_size>
214 unsigned int num_arguments_per_tensor() const;
215
216protected:
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100217 cl::Kernel _kernel; /**< OpenCL kernel to run */
218 cl::NDRange _lws_hint; /**< Local workgroup size hint for the OpenCL kernel */
219 GPUTarget _target; /**< The targeted GPU */
220 std::string _config_id; /**< Configuration ID */
221 size_t _max_workgroup_size; /**< The maximum workgroup size for this kernel */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222};
223
224/** Add the kernel to the command queue with the given window.
225 *
226 * @note Depending on the size of the window, this might translate into several jobs being enqueued.
227 *
228 * @note If kernel->kernel() is empty then the function will return without adding anything to the queue.
229 *
230 * @param[in,out] queue OpenCL command queue.
231 * @param[in] kernel Kernel to enqueue
232 * @param[in] window Window the kernel has to process.
Abel Bernabeu5a6e0532017-09-28 09:53:45 +0100233 * @param[in] lws_hint Local workgroup size requested, by default (128,1).
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234 *
235 * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed.
236 */
steniu015f910722017-08-23 10:15:22 +0100237void 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 +0100238
239template <typename T, unsigned int dimension_size>
240void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
241{
242 // Calculate offset to the start of the window
243 unsigned int offset_first_element = 0;
244
245 for(unsigned int n = 0; n < num_dimensions; ++n)
246 {
247 offset_first_element += window[n].start() * strides[n];
248 }
249
250 unsigned int idx_start = idx;
251 _kernel.setArg(idx++, array->cl_buffer());
252
253 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
254 {
255 _kernel.setArg<cl_uint>(idx++, strides[dimension]);
256 _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
257 }
258
259 _kernel.setArg<cl_uint>(idx++, offset_first_element);
260
261 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_array<dimension_size>() != idx,
262 "add_%dD_array_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_array<dimension_size>());
263 ARM_COMPUTE_UNUSED(idx_start);
264}
265
266template <typename T>
267void ICLKernel::add_1D_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
268{
269 add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
270}
271
272template <unsigned int dimension_size>
273unsigned int ICLKernel::num_arguments_per_array() const
274{
275 return num_arguments_per_tensor<dimension_size>();
276}
277
278template <unsigned int dimension_size>
279unsigned int ICLKernel::num_arguments_per_tensor() const
280{
281 return 2 + 2 * dimension_size;
282}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100283}
284#endif /*__ARM_COMPUTE_ICLKERNEL_H__ */