1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
/*
* Copyright (c) 2016-2020 Arm Limited.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef ARM_COMPUTE_ICLKERNEL_H
#define ARM_COMPUTE_ICLKERNEL_H
#include "arm_compute/core/CL/CLKernelLibrary.h"
#include "arm_compute/core/CL/CLTypes.h"
#include "arm_compute/core/CL/OpenCL.h"
#include "arm_compute/core/GPUTarget.h"
#include "arm_compute/core/IKernel.h"
#include "arm_compute/core/experimental/Types.h"
#include <string>
namespace arm_compute
{
template <typename T>
class ICLArray;
class ICLTensor;
class Window;
/** Common interface for all the OpenCL kernels */
class ICLKernel : public IKernel
{
private:
/** Returns the number of arguments enqueued per array object.
*
* @return The number of arguments enqueued per array object.
*/
template <unsigned int dimension_size>
constexpr static unsigned int num_arguments_per_array()
{
return num_arguments_per_tensor<dimension_size>();
}
/** Returns the number of arguments enqueued per tensor object.
*
* @return The number of arguments enqueued per tensor object.
*/
template <unsigned int dimension_size>
constexpr static unsigned int num_arguments_per_tensor()
{
return 2 + 2 * dimension_size;
}
using IKernel::configure; //Prevent children from calling IKernel::configure() directly
protected:
/** Configure the kernel's window and local workgroup size hint.
*
* @param[in] window The maximum window which will be returned by window()
* @param[in] lws_hint (Optional) Local-Workgroup-Size to use.
*/
void configure_internal(const Window &window, cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange())
{
_lws_hint = lws_hint;
IKernel::configure(window);
}
public:
/** Constructor */
ICLKernel()
: _kernel(nullptr), _target(GPUTarget::MIDGARD), _config_id(arm_compute::default_config_id), _max_workgroup_size(0), _lws_hint()
{
}
/** Returns a reference to the OpenCL kernel of this object.
*
* @return A reference to the OpenCL kernel of this object.
*/
cl::Kernel &kernel()
{
return _kernel;
}
/** Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx.
*
* @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] array Array to set as an argument of the object's kernel.
* @param[in] strides @ref Strides object containing stride of each dimension in bytes.
* @param[in] num_dimensions Number of dimensions of the @p array.
* @param[in] window Window the kernel will be executed on.
*/
template <typename T>
void add_1D_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
{
add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
}
/** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
*
* @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] tensor Tensor to set as an argument of the object's kernel.
* @param[in] window Window the kernel will be executed on.
*/
void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
{
add_tensor_argument<1>(idx, tensor, window);
}
/** Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true.
*
* @param[in] cond Condition to check
* @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] tensor Tensor to set as an argument of the object's kernel.
* @param[in] window Window the kernel will be executed on.
*/
void add_1D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
{
if(cond)
{
add_1D_tensor_argument(idx, tensor, window);
}
}
/** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
*
* @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] tensor Tensor to set as an argument of the object's kernel.
* @param[in] window Window the kernel will be executed on.
*/
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
{
add_tensor_argument<2>(idx, tensor, window);
}
/** Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true.
*
* @param[in] cond Condition to check
* @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] tensor Tensor to set as an argument of the object's kernel.
* @param[in] window Window the kernel will be executed on.
*/
void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
{
if(cond)
{
add_2D_tensor_argument(idx, tensor, window);
}
}
/** Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
*
* @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] tensor Tensor to set as an argument of the object's kernel.
* @param[in] window Window the kernel will be executed on.
*/
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
{
add_tensor_argument<3>(idx, tensor, window);
}
/** Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
*
* @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] tensor Tensor to set as an argument of the object's kernel.
* @param[in] window Window the kernel will be executed on.
*/
void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
{
add_tensor_argument<4>(idx, tensor, window);
}
/** Returns the number of arguments enqueued per 1D array object.
*
* @return The number of arguments enqueues per 1D array object.
*/
constexpr static unsigned int num_arguments_per_1D_array()
{
return num_arguments_per_array<1>();
}
/** Returns the number of arguments enqueued per 1D tensor object.
*
* @return The number of arguments enqueues per 1D tensor object.
*/
constexpr static unsigned int num_arguments_per_1D_tensor()
{
return num_arguments_per_tensor<1>();
}
/** Returns the number of arguments enqueued per 2D tensor object.
*
* @return The number of arguments enqueues per 2D tensor object.
*/
constexpr static unsigned int num_arguments_per_2D_tensor()
{
return num_arguments_per_tensor<2>();
}
/** Returns the number of arguments enqueued per 3D tensor object.
*
* @return The number of arguments enqueues per 3D tensor object.
*/
constexpr static unsigned int num_arguments_per_3D_tensor()
{
return num_arguments_per_tensor<3>();
}
/** Returns the number of arguments enqueued per 4D tensor object.
*
* @return The number of arguments enqueues per 4D tensor object.
*/
constexpr static unsigned int num_arguments_per_4D_tensor()
{
return num_arguments_per_tensor<4>();
}
/** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
*
* @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
*
* @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
* @param[in,out] queue Command queue on which to enqueue the kernel.
*/
virtual void run(const Window &window, cl::CommandQueue &queue)
{
ARM_COMPUTE_UNUSED(window, queue);
}
/** Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
*
* @note The queue is *not* flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
*
* @param[in] tensors A vector containing the tensors to operato on.
* @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
* @param[in,out] queue Command queue on which to enqueue the kernel.
*/
virtual void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
{
ARM_COMPUTE_UNUSED(tensors, window, queue);
}
/** Add the passed parameters to the object's kernel's arguments starting from the index idx.
*
* @param[in,out] idx Index at which to start adding the arguments. Will be incremented by the number of kernel arguments set.
* @param[in] value Value to set as an argument of the object's kernel.
*/
template <typename T>
void add_argument(unsigned int &idx, T value)
{
_kernel.setArg(idx++, value);
}
/** Set the Local-Workgroup-Size hint
*
* @note This method should be called after the configuration of the kernel
*
* @param[in] lws_hint Local-Workgroup-Size to use
*/
void set_lws_hint(const cl::NDRange &lws_hint)
{
ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); // lws_hint will be overwritten by configure()
_lws_hint = lws_hint;
}
/** Return the Local-Workgroup-Size hint
*
* @return Current lws hint
*/
cl::NDRange lws_hint() const
{
return _lws_hint;
}
/** Get the configuration ID
*
* @note The configuration ID can be used by the caller to distinguish different calls of the same OpenCL kernel
* In particular, this method can be used by CLScheduler to keep track of the best LWS for each configuration of the same kernel.
* The configuration ID should be provided only for the kernels potentially affected by the LWS geometry
*
* @note This method should be called after the configuration of the kernel
*
* @return configuration id string
*/
const std::string &config_id() const
{
return _config_id;
}
/** Set the targeted GPU architecture
*
* @param[in] target The targeted GPU architecture
*/
void set_target(GPUTarget target)
{
_target = target;
}
/** Set the targeted GPU architecture according to the CL device
*
* @param[in] device A CL device
*/
void set_target(cl::Device &device);
/** Get the targeted GPU architecture
*
* @return The targeted GPU architecture.
*/
GPUTarget get_target() const
{
return _target;
}
/** Get the maximum workgroup size for the device the CLKernelLibrary uses.
*
* @return The maximum workgroup size value.
*/
size_t get_max_workgroup_size();
/** Get the global work size given an execution window
*
* @param[in] window Execution window
*
* @return Global work size of the given execution window
*/
static cl::NDRange gws_from_window(const Window &window);
private:
/** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
*
* @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] array Array to set as an argument of the object's kernel.
* @param[in] strides @ref Strides object containing stride of each dimension in bytes.
* @param[in] num_dimensions Number of dimensions of the @p array.
* @param[in] window Window the kernel will be executed on.
*/
template <typename T, unsigned int dimension_size>
void add_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window);
/** Add the passed tensor's parameters to the object's kernel's arguments starting from the index idx.
*
* @param[in,out] idx Index at which to start adding the tensor's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] tensor Tensor to set as an argument of the object's kernel.
* @param[in] window Window the kernel will be executed on.
*/
template <unsigned int dimension_size>
void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
protected:
cl::Kernel _kernel; /**< OpenCL kernel to run */
GPUTarget _target; /**< The targeted GPU */
std::string _config_id; /**< Configuration ID */
size_t _max_workgroup_size; /**< The maximum workgroup size for this kernel */
private:
cl::NDRange _lws_hint; /**< Local workgroup size hint for the OpenCL kernel */
};
/** Add the kernel to the command queue with the given window.
*
* @note Depending on the size of the window, this might translate into several jobs being enqueued.
*
* @note If kernel->kernel() is empty then the function will return without adding anything to the queue.
*
* @param[in,out] queue OpenCL command queue.
* @param[in] kernel Kernel to enqueue
* @param[in] window Window the kernel has to process.
* @param[in] lws_hint (Optional) Local workgroup size requested. Default is based on the device target.
* @param[in] use_dummy_work_items (Optional) Use dummy work items in order to have two dimensional power of two NDRange. Default is false
* Note: it is kernel responsibility to check if the work-item is out-of-range
*
* @note If any dimension of the lws is greater than the global workgroup size then no lws will be passed.
*/
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint = CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items = false);
/** Add the passed array's parameters to the object's kernel's arguments starting from the index idx.
*
* @param[in,out] idx Index at which to start adding the array's arguments. Will be incremented by the number of kernel arguments set.
* @param[in] array Array to set as an argument of the object's kernel.
* @param[in] strides @ref Strides object containing stride of each dimension in bytes.
* @param[in] num_dimensions Number of dimensions of the @p array.
* @param[in] window Window the kernel will be executed on.
*/
template <typename T, unsigned int dimension_size>
void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
{
ARM_COMPUTE_ERROR_ON(array == nullptr);
// Calculate offset to the start of the window
unsigned int offset_first_element = 0;
for(unsigned int n = 0; n < num_dimensions; ++n)
{
offset_first_element += window[n].start() * strides[n];
}
unsigned int idx_start = idx;
_kernel.setArg(idx++, array->cl_buffer());
for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
{
_kernel.setArg<cl_uint>(idx++, strides[dimension]);
_kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
}
_kernel.setArg<cl_uint>(idx++, offset_first_element);
ARM_COMPUTE_ERROR_ON_MSG_VAR(idx_start + num_arguments_per_array<dimension_size>() != idx,
"add_%dD_array_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_array<dimension_size>());
ARM_COMPUTE_UNUSED(idx_start);
}
}
#endif /*ARM_COMPUTE_ICLKERNEL_H */
|