Compute Library
 21.02
IGCKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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  */
25 
26 #include "arm_compute/core/Error.h"
31 #include "arm_compute/core/Utils.h"
35 
36 #include <cstddef>
37 #include <sstream>
38 
39 using namespace arm_compute;
40 
41 void arm_compute::enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws)
42 {
43  ARM_COMPUTE_UNUSED(kernel);
44 
45  if(kernel.kernel().get_program() == 0)
46  {
47  return;
48  }
49 
50  ARM_COMPUTE_ERROR_ON((0 == (window.x().end() - window.x().start())) || (0 == (window.y().end() - window.y().start())));
51 
52  ARM_COMPUTE_ERROR_ON_MSG_VAR((((window.x().end() - window.x().start()) % (window.x().step() * lws[0])) != 0),
53  "window x end =%d, start=%d, step=%d, lws x=%zu", window.x().end(), window.x().start(), window.x().step(), lws[0]);
54  ARM_COMPUTE_ERROR_ON_MSG_VAR((((window.y().end() - window.y().start()) % (window.y().step() * lws[1])) != 0),
55  "window y end =%d, start=%d, step=%d, lws y=%zu", window.y().end(), window.y().start(), window.y().step(), lws[1]);
56  ARM_COMPUTE_ERROR_ON_MSG_VAR((((window.z().end() - window.z().start()) % (window.z().step() * lws[2])) != 0),
57  "window z end =%d, start=%d, step=%d, lws z=%zu", window.z().end(), window.z().start(), window.z().step(), lws[2]);
58 
59  ARM_COMPUTE_GL_CHECK(glDispatchCompute(((window.x().end() - window.x().start()) / window.x().step()) / lws[0],
60  ((window.y().end() - window.y().start()) / window.y().step()) / lws[1],
61  ((window.z().end() - window.z().start()) / window.z().step()) / lws[2]));
62 }
63 
65  : _kernel(), _lws_hint(gles::NDRange(1U, 1U, 1U)), _target(GPUTarget::MIDGARD)
66 {
67 }
68 
70 {
71  return _kernel;
72 }
73 
74 template <unsigned int dimension_size>
75 unsigned int IGCKernel::num_arguments_per_tensor() const
76 {
77  // Rounding up the tensor attributes structure in compute shader to a multiple of a vec4
78  return ceil_to_multiple(1 + 2 * dimension_size, 4);
79 }
80 
81 template <unsigned int dimension_size>
82 void IGCKernel::add_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
83 {
84  ARM_COMPUTE_ERROR_ON(tensor == nullptr);
85 
86  const ITensorInfo *info = tensor->info();
87  const Strides &strides = info->strides_in_bytes();
88 
89  // Calculate offset to the start of the window
90  unsigned int offset_first_element = info->offset_first_element_in_bytes();
91 
92  for(unsigned int n = 0; n < info->num_dimensions(); ++n)
93  {
94  offset_first_element += window[n].start() * strides[n];
95  }
96 
97  unsigned int idx_start = idx;
98 
99  for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
100  {
101  _kernel.set_argument(idx++, strides[dimension]);
102  _kernel.set_argument(idx++, strides[dimension] * window[dimension].step());
103  }
104 
105  _kernel.set_argument(idx++, offset_first_element);
106 
107  // Rounding up the tensor attributes structure in compute shader to a multiple of a vec4
108  unsigned int idx_end = ceil_to_multiple(idx, 4);
109  for(unsigned int i = idx; i < idx_end; ++i)
110  {
111  _kernel.set_argument(i, 0);
112  }
113  idx = idx_end;
114 
115  ARM_COMPUTE_GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding_point, tensor->gc_buffer()));
116 
117  ARM_COMPUTE_ERROR_ON_MSG_VAR(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
118  "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
119  ARM_COMPUTE_UNUSED(idx_start);
120 }
121 
122 void IGCKernel::add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
123 {
124  add_tensor_argument<1>(idx, tensor, binding_point, window);
125 }
126 
127 void IGCKernel::add_2D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
128 {
129  add_tensor_argument<2>(idx, tensor, binding_point, window);
130 }
131 
132 void IGCKernel::add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
133 {
134  add_tensor_argument<3>(idx, tensor, binding_point, window);
135 }
136 
138 {
139  return num_arguments_per_tensor<1>();
140 }
141 
143 {
144  return num_arguments_per_tensor<2>();
145 }
146 
148 {
149  return num_arguments_per_tensor<3>();
150 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
#define ARM_COMPUTE_GL_CHECK(x)
Definition: OpenGLES.h:45
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
unsigned int num_arguments_per_2D_tensor() const
Returns the number of arguments enqueued per 2D tensor object.
Definition: IGCKernel.cpp:142
void add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 3D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: IGCKernel.cpp:132
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
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:104
#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
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg,...)
Definition: Error.h:457
GCKernel class.
unsigned int num_arguments_per_1D_tensor() const
Returns the number of arguments enqueued per 1D tensor object.
Definition: IGCKernel.cpp:137
constexpr const Dimension & z() const
Alias to access the third dimension of the window.
Definition: Window.h:163
unsigned int num_arguments_per_3D_tensor() const
Returns the number of arguments enqueued per 3D tensor object.
Definition: IGCKernel.cpp:147
GCKernel & kernel()
Returns a reference to the GLES kernel of this object.
Definition: IGCKernel.cpp:69
Copyright (c) 2017-2021 Arm Limited.
virtual GLuint gc_buffer() const =0
Interface to be implemented by the child class to return the tensor&#39;s gles compute buffer id...
void GL_APIENTRY glBindBufferBase(GLenum target, GLuint index, GLuint buffer)
Definition: OpenGLES.cpp:503
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
Definition: Utils.h:71
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void add_2D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 2D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: IGCKernel.cpp:127
void GL_APIENTRY glDispatchCompute(GLuint num_groups_x, GLuint num_groups_y, GLuint num_groups_z)
Definition: OpenGLES.cpp:542
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)
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
constexpr int step
Definition: fp32.cpp:35
GLuint get_program() const
Get program id.
Common interface for all the GLES kernels.
Definition: IGCKernel.h:39
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:154
Class interface for specifying NDRange values.
Definition: OpenGLES.h:53
Interface to enqueue GLES kernels and get/set the GLES CommandQueue.
IGCKernel()
Constructor.
Definition: IGCKernel.cpp:64
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
void set_argument(unsigned int idx, T value)
Set argument value at index of shader params.
void add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 1D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: IGCKernel.cpp:122
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145