Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLTypes.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | #include "arm_compute/core/IKernel.h" |
| 31 | |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 32 | #include <string> |
| 33 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 36 | template <typename T> |
| 37 | class ICLArray; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | class ICLTensor; |
| 39 | class Window; |
| 40 | |
| 41 | /** Common interface for all the OpenCL kernels */ |
| 42 | class ICLKernel : public IKernel |
| 43 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 44 | private: |
| 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | public: |
| 65 | /** Constructor */ |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 66 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 74 | cl::Kernel &kernel() |
| 75 | { |
| 76 | return _kernel; |
| 77 | } |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 78 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 87 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 97 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 107 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 117 | void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window) |
| 118 | { |
| 119 | add_tensor_argument<3>(idx, tensor, window); |
| 120 | } |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 121 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 127 | 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 Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 131 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 135 | constexpr static unsigned int num_arguments_per_1D_array() |
| 136 | { |
| 137 | return num_arguments_per_array<1>(); |
| 138 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 143 | constexpr static unsigned int num_arguments_per_1D_tensor() |
| 144 | { |
| 145 | return num_arguments_per_tensor<1>(); |
| 146 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 151 | constexpr static unsigned int num_arguments_per_2D_tensor() |
| 152 | { |
| 153 | return num_arguments_per_tensor<2>(); |
| 154 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 159 | constexpr static unsigned int num_arguments_per_3D_tensor() |
| 160 | { |
| 161 | return num_arguments_per_tensor<3>(); |
| 162 | } |
steniu01 | 868e541 | 2017-07-17 23:16:00 +0100 | [diff] [blame] | 163 | /** 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 167 | constexpr static unsigned int num_arguments_per_4D_tensor() |
| 168 | { |
| 169 | return num_arguments_per_tensor<4>(); |
| 170 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | /** 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 Iodice | 9331aeb | 2017-08-10 17:11:08 +0100 | [diff] [blame] | 190 | /** 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 Barbier | d727e85 | 2018-04-20 11:05:29 +0100 | [diff] [blame] | 196 | void set_lws_hint(const cl::NDRange &lws_hint) |
Gian Marco Iodice | 9331aeb | 2017-08-10 17:11:08 +0100 | [diff] [blame] | 197 | { |
| 198 | _lws_hint = lws_hint; |
| 199 | } |
| 200 | |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 201 | /** 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 Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 210 | /** 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | /** Set the targeted GPU architecture |
| 226 | * |
| 227 | * @param[in] target The targeted GPU architecture |
| 228 | */ |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 229 | void set_target(GPUTarget target) |
| 230 | { |
| 231 | _target = target; |
| 232 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 233 | |
| 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 Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 244 | GPUTarget get_target() const |
| 245 | { |
| 246 | return _target; |
| 247 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 248 | |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 249 | /** 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 Pinitas | 1f378ee | 2017-10-27 13:37:16 +0100 | [diff] [blame] | 254 | /** 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 Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 261 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 262 | private: |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 263 | /** 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 273 | /** 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | |
| 282 | protected: |
Abel Bernabeu | 5a6e053 | 2017-09-28 09:53:45 +0100 | [diff] [blame] | 283 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 288 | }; |
| 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 Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 299 | * @param[in] lws_hint Local workgroup size requested. Default is based on the device target. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 300 | * |
| 301 | * @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed. |
| 302 | */ |
steniu01 | 5f91072 | 2017-08-23 10:15:22 +0100 | [diff] [blame] | 303 | void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint = CLKernelLibrary::get().default_ndrange()); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 304 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 305 | /** 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 Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 313 | template <typename T, unsigned int dimension_size> |
| 314 | void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window) |
| 315 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 316 | ARM_COMPUTE_ERROR_ON(array == nullptr); |
| 317 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 318 | // 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 341 | } |
| 342 | #endif /*__ARM_COMPUTE_ICLKERNEL_H__ */ |