Compute Library
 19.08
ICLKernel.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019 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 
32 
33 #include <string>
34 
35 namespace arm_compute
36 {
37 template <typename T>
38 class ICLArray;
39 class ICLTensor;
40 class Window;
41 
43 class ICLKernel : public IKernel
44 {
45 private:
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  }
59  template <unsigned int dimension_size>
60  constexpr static unsigned int num_arguments_per_tensor()
61  {
62  return 2 + 2 * dimension_size;
63  }
64  using IKernel::configure; //Prevent children from calling IKernel::configure() directly
65 protected:
71  void configure_internal(const Window &window, cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange())
72  {
73  _lws_hint = lws_hint;
74  IKernel::configure(window);
75  }
76 
77 public:
80  : _kernel(nullptr), _target(GPUTarget::MIDGARD), _config_id(arm_compute::default_config_id), _max_workgroup_size(0), _lws_hint()
81  {
82  }
87  cl::Kernel &kernel()
88  {
89  return _kernel;
90  }
99  template <typename T>
100  void add_1D_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
101  {
102  add_array_argument<T, 1>(idx, array, strides, num_dimensions, window);
103  }
110  void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
111  {
112  add_tensor_argument<1>(idx, tensor, window);
113  }
121  void add_1D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
122  {
123  if(cond)
124  {
125  add_1D_tensor_argument(idx, tensor, window);
126  }
127  }
134  void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
135  {
136  add_tensor_argument<2>(idx, tensor, window);
137  }
145  void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
146  {
147  if(cond)
148  {
149  add_2D_tensor_argument(idx, tensor, window);
150  }
151  }
158  void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
159  {
160  add_tensor_argument<3>(idx, tensor, window);
161  }
168  void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
169  {
170  add_tensor_argument<4>(idx, tensor, window);
171  }
176  constexpr static unsigned int num_arguments_per_1D_array()
177  {
178  return num_arguments_per_array<1>();
179  }
184  constexpr static unsigned int num_arguments_per_1D_tensor()
185  {
186  return num_arguments_per_tensor<1>();
187  }
192  constexpr static unsigned int num_arguments_per_2D_tensor()
193  {
194  return num_arguments_per_tensor<2>();
195  }
200  constexpr static unsigned int num_arguments_per_3D_tensor()
201  {
202  return num_arguments_per_tensor<3>();
203  }
208  constexpr static unsigned int num_arguments_per_4D_tensor()
209  {
210  return num_arguments_per_tensor<4>();
211  }
219  virtual void run(const Window &window, cl::CommandQueue &queue) = 0;
225  template <typename T>
226  void add_argument(unsigned int &idx, T value)
227  {
228  _kernel.setArg(idx++, value);
229  }
230 
237  void set_lws_hint(const cl::NDRange &lws_hint)
238  {
239  ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); // lws_hint will be overwritten by configure()
240  _lws_hint = lws_hint;
241  }
242 
247  cl::NDRange lws_hint() const
248  {
249  return _lws_hint;
250  }
251 
262  const std::string &config_id() const
263  {
264  return _config_id;
265  }
266 
271  void set_target(GPUTarget target)
272  {
273  _target = target;
274  }
275 
280  void set_target(cl::Device &device);
281 
287  {
288  return _target;
289  }
290 
295  size_t get_max_workgroup_size();
302  static cl::NDRange gws_from_window(const Window &window);
303 
304 private:
313  template <typename T, unsigned int dimension_size>
314  void add_array_argument(unsigned int &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window);
321  template <unsigned int dimension_size>
322  void add_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window);
323 
324 protected:
325  cl::Kernel _kernel;
326  GPUTarget _target;
327  std::string _config_id;
328  size_t _max_workgroup_size;
329 private:
330  cl::NDRange _lws_hint;
331 };
332 
348 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);
349 
358 template <typename T, unsigned int dimension_size>
359 void ICLKernel::add_array_argument(unsigned &idx, const ICLArray<T> *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
360 {
361  ARM_COMPUTE_ERROR_ON(array == nullptr);
362 
363  // Calculate offset to the start of the window
364  unsigned int offset_first_element = 0;
365 
366  for(unsigned int n = 0; n < num_dimensions; ++n)
367  {
368  offset_first_element += window[n].start() * strides[n];
369  }
370 
371  unsigned int idx_start = idx;
372  _kernel.setArg(idx++, array->cl_buffer());
373 
374  for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
375  {
376  _kernel.setArg<cl_uint>(idx++, strides[dimension]);
377  _kernel.setArg<cl_uint>(idx++, strides[dimension] * window[dimension].step());
378  }
379 
380  _kernel.setArg<cl_uint>(idx++, offset_first_element);
381 
382  ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_array<dimension_size>() != idx,
383  "add_%dD_array_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_array<dimension_size>());
384  ARM_COMPUTE_UNUSED(idx_start);
385 }
386 }
387 #endif /*__ARM_COMPUTE_ICLKERNEL_H__ */
static constexpr unsigned int num_arguments_per_1D_tensor()
Returns the number of arguments enqueued per 1D tensor object.
Definition: ICLKernel.h:184
static cl::NDRange gws_from_window(const Window &window)
Get the global work size given an execution window.
Definition: ICLKernel.cpp:141
Common information for all the kernels.
Definition: IKernel.h:33
void add_1D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx ...
Definition: ICLKernel.h:121
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
static constexpr unsigned int num_arguments_per_1D_array()
Returns the number of arguments enqueued per 1D array object.
Definition: ICLKernel.h:176
void add_2D_tensor_argument_if(bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx ...
Definition: ICLKernel.h:145
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 kernel to the command queue with the given window.
Definition: ICLKernel.cpp:39
cl::Kernel & kernel()
Returns a reference to the OpenCL kernel of this object.
Definition: ICLKernel.h:87
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:247
void set_lws_hint(const cl::NDRange &lws_hint)
Set the Local-Workgroup-Size hint.
Definition: ICLKernel.h:237
void add_argument(unsigned int &idx, T value)
Add the passed parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:226
void add_1D_array_argument(unsigned int &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:100
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:43
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:158
Copyright (c) 2017-2018 ARM Limited.
const std::string & config_id() const
Get the configuration ID.
Definition: ICLKernel.h:262
Interface for OpenCL Array.
Definition: ICLArray.h:35
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:200
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
GPUTarget get_target() const
Get the targeted GPU architecture.
Definition: ICLKernel.h:286
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:192
static constexpr unsigned int num_arguments_per_4D_tensor()
Returns the number of arguments enqueued per 4D tensor object.
Definition: ICLKernel.h:208
Strides of an item in bytes.
Definition: Strides.h:37
virtual void run(const Window &window, cl::CommandQueue &queue)=0
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:134
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
ICLKernel()
Constructor.
Definition: ICLKernel.h:79
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
size_t get_max_workgroup_size()
Get the maximum workgroup size for the device the CLKernelLibrary uses.
Definition: ICLKernel.cpp:132
void set_target(GPUTarget target)
Set the targeted GPU architecture.
Definition: ICLKernel.h:271
void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:110
void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:168
virtual const cl::Buffer & cl_buffer() const =0
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328