Compute Library
 21.02
ICLKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021 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 #include "src/core/CL/ICLKernel.h"
25 
28 #include "src/core/helpers/Utils.h"
29 
30 #include <cstddef>
31 
32 void arm_compute::enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint, bool use_dummy_work_items)
33 {
34  if(kernel.kernel()() == nullptr)
35  {
36  return;
37  }
38 
39  for(unsigned int i = 0; i < Coordinates::num_max_dimensions; ++i)
40  {
41  ARM_COMPUTE_ERROR_ON(window[i].step() == 0);
42  // Make sure that dimensions > Z are 1
43  ARM_COMPUTE_ERROR_ON((i >= 3) && ((window[i].end() - window[i].start()) != 1));
44  }
45 
46  cl::NDRange gws = ICLKernel::gws_from_window(window);
47 
48  // Check for empty NDRange
49  if(gws.dimensions() == 0)
50  {
51  return;
52  }
53 
54  // Use dummy work-items
55  if(use_dummy_work_items)
56  {
57  gws.get()[0] = get_next_power_two(gws[0]);
58  gws.get()[1] = get_next_power_two(gws[1]);
59  }
60 
61  cl::NDRange valid_lws;
62  if(lws_hint[0] * lws_hint[1] * lws_hint[2] > kernel.get_max_workgroup_size())
63  {
64  valid_lws = cl::NullRange;
65  }
66  else
67  {
68  valid_lws = lws_hint;
69  }
70 
71  cl::NDRange lws = cl::NullRange;
72 
73  if((valid_lws[0] <= gws[0]) && (valid_lws[1] <= gws[1]) && (valid_lws[2] <= gws[2]))
74  {
75  lws = valid_lws;
76  }
77 
78  if(CLKernelLibrary::get().is_wbsm_supported())
79  {
80  set_wbsm(kernel.kernel(), kernel.wbsm_hint());
81  }
82  queue.enqueueNDRangeKernel(kernel.kernel(), cl::NullRange, gws, lws);
83 }
84 
85 namespace arm_compute
86 {
87 template <unsigned int dimension_size>
88 void ICLKernel::add_tensor_argument(unsigned &idx, const ICLTensor *tensor, const Window &window)
89 {
90  ARM_COMPUTE_ERROR_ON(tensor == nullptr);
91 
92  const ITensorInfo *info = tensor->info();
93  const Strides &strides = info->strides_in_bytes();
94 
95  // Calculate offset to the start of the window
96  unsigned int offset_first_element = info->offset_first_element_in_bytes();
97 
98  for(unsigned int n = 0; n < info->num_dimensions(); ++n)
99  {
100  offset_first_element += (window.is_broadcasted(n) ? 0 : window[n].start()) * strides[n];
101  }
102 
103  unsigned int idx_start = idx;
104  _kernel.setArg(idx++, tensor->cl_buffer());
105 
106  for(unsigned int d = 0; d < dimension_size; ++d)
107  {
108  _kernel.setArg<cl_uint>(idx++, strides[d]);
109  _kernel.setArg<cl_uint>(idx++, strides[d] * window[d].step());
110  }
111 
112  _kernel.setArg<cl_uint>(idx++, offset_first_element);
113 
114  ARM_COMPUTE_ERROR_ON_MSG_VAR(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
115  "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
116  ARM_COMPUTE_UNUSED(idx_start);
117 }
118 
119 #ifndef DOXYGEN_SKIP_THIS
120 template void ICLKernel::add_tensor_argument<1>(unsigned &idx, const ICLTensor *tensor, const Window &window);
121 template void ICLKernel::add_tensor_argument<2>(unsigned &idx, const ICLTensor *tensor, const Window &window);
122 template void ICLKernel::add_tensor_argument<3>(unsigned &idx, const ICLTensor *tensor, const Window &window);
123 template void ICLKernel::add_tensor_argument<4>(unsigned &idx, const ICLTensor *tensor, const Window &window);
124 #endif /* DOXYGEN_SKIP_THIS */
125 
126 void ICLKernel::set_target(cl::Device &device)
127 {
128  _target = get_target_from_device(device);
129 }
130 
132 {
133  if(_max_workgroup_size == 0)
134  {
135  _max_workgroup_size = CLKernelLibrary::get().max_local_workgroup_size(_kernel);
136  }
137  return _max_workgroup_size;
138 }
139 
141 {
142  if((window.x().end() - window.x().start()) == 0 || (window.y().end() - window.y().start()) == 0)
143  {
144  return cl::NullRange;
145  }
146 
147  cl::NDRange gws((window.x().end() - window.x().start()) / window.x().step(),
148  (window.y().end() - window.y().start()) / window.y().step(),
149  (window.z().end() - window.z().start()) / window.z().step());
150 
151  return gws;
152 }
153 } // namespace arm_compute
static cl::NDRange gws_from_window(const Window &window)
Get the global work size given an execution window.
Definition: ICLKernel.cpp:140
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
unsigned int get_next_power_two(unsigned int x)
Given an integer value, this function returns the next power of two.
Definition: Utils.h:74
cl::Kernel & kernel()
Returns a reference to the OpenCL kernel of this object.
Definition: ICLKernel.h:101
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:104
void set_wbsm(cl::Kernel &kernel, cl_int wbsm_hint)
Definition: CLHelpers.cpp:430
cl_int wbsm_hint() const
Return the workgroup batch size modifier hint.
Definition: ICLKernel.h:297
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg,...)
Definition: Error.h:457
constexpr const Dimension & z() const
Alias to access the third dimension of the window.
Definition: Window.h:163
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:46
Copyright (c) 2017-2021 Arm Limited.
size_t max_local_workgroup_size(const cl::Kernel &kernel) const
Find the maximum number of local work items in a workgroup can be supported for the kernel...
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
GPUTarget get_target_from_device(const cl::Device &device)
Helper function to get the GPU target from CL device.
Definition: CLHelpers.cpp:221
bool is_broadcasted(size_t dimension) const
Return whether a dimension has been broadcasted.
Definition: Window.inl:62
Strides of an item in bytes.
Definition: Strides.h:37
virtual size_t offset_first_element_in_bytes() const =0
The offset from the beginning of the memory allocation to the first element of the tensor...
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
constexpr int step
Definition: fp32.cpp:35
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:154
size_t get_max_workgroup_size()
Get the maximum workgroup size for the device the CLKernelLibrary uses.
Definition: ICLKernel.cpp:131
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 ...
void set_target(GPUTarget target)
Set the targeted GPU architecture.
Definition: ICLKernel.h:321
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
Describe a multidimensional execution window.
Definition: Window.h:39
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145