Compute Library
 21.02
CLHistogramKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 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 
34 #include "support/StringSupport.h"
35 
36 #include <cstring>
37 #include <string>
38 
39 using namespace arm_compute;
40 
41 // each thread handle 16 pixels
42 constexpr signed int pixels_per_item = 16;
43 
44 // local work group size in X dimension
45 constexpr unsigned int local_x_size = 16;
46 
48  : _input(nullptr), _output(nullptr)
49 {
50 }
51 
53 {
54  configure(CLKernelLibrary::get().get_compile_context(), input, output);
55 }
56 
57 void CLHistogramKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, ICLDistribution1D *output)
58 {
60  ARM_COMPUTE_ERROR_ON(nullptr == output);
61 
62  // Check input size
64 
65  // Check offset
66  ARM_COMPUTE_ERROR_ON_MSG(0 > output->offset() || output->offset() > 256, "Offset is larger than the image value range.");
67 
68  // Check range
69  ARM_COMPUTE_ERROR_ON_MSG(output->range() > 256 /* max range */, "Range larger than the image value range.");
70 
71  _input = input;
72  _output = output;
73 
74  if(_input->info()->dimension(0) < pixels_per_item)
75  {
76  return;
77  }
78 
79  unsigned int num_bins = _output->num_bins();
80  unsigned int window_size = _output->window();
81  unsigned int offset = _output->offset();
82  unsigned int range = _output->range();
83  unsigned int offrange = offset + range;
84  unsigned int bin_size = _output->size();
85  unsigned int buffer_size = bin_size + 1; // We need one extra place for pixels that don't meet the conditions
86 
87  // Create kernel
88  bool is_fixed_size = (256 == num_bins) && (1 == window_size) && (0 == offset) && (256 == offrange);
89  const std::string kernel_name = is_fixed_size ? "hist_local_kernel_fixed" : "hist_local_kernel";
90  _kernel = create_kernel(compile_context, kernel_name);
91 
92  // Set static kernel arguments
93  unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters
94  _kernel.setArg(idx++, buffer_size, nullptr);
95  _kernel.setArg(idx++, _output->cl_buffer());
96  if(!is_fixed_size)
97  {
98  _kernel.setArg<cl_uint>(idx++, num_bins);
99  _kernel.setArg<cl_uint>(idx++, offset);
100  _kernel.setArg<cl_uint>(idx++, range);
101  _kernel.setArg<cl_uint>(idx++, offrange);
102  }
103 
104  // We only run histogram on Image, therefore only 2 dimensions here
105  unsigned int end_position = (_input->info()->dimension(0) / pixels_per_item) * pixels_per_item;
106 
107  // Configure kernel window
108  Window win;
109  win.set(0, Window::Dimension(0, end_position, pixels_per_item));
110  win.set(1, Window::Dimension(0, _input->info()->dimension(1)));
111 
113 
114  ICLKernel::configure_internal(win);
115 
116  // Set config_id for enabling LWS tuning
117  _config_id = kernel_name;
118  _config_id += "_";
119  _config_id += lower_string(string_from_data_type(input->info()->data_type()));
120  _config_id += "_";
121  _config_id += support::cpp11::to_string(input->info()->dimension(0));
122  _config_id += "_";
123  _config_id += support::cpp11::to_string(input->info()->dimension(1));
124 }
125 
126 void CLHistogramKernel::run(const Window &window, cl::CommandQueue &queue)
127 {
130 
131  // TODO (COMPMID-679): Add CLMemFill
132  _output->map(queue, true);
133  ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
134  memset(_output->buffer(), 0, _output->size());
135  _output->unmap(queue);
136 
137  if(_input->info()->dimension(0) < pixels_per_item)
138  {
139  return;
140  }
141 
143  const unsigned int gws_x = (window.x().end() - window.x().start()) / window.x().step();
144  cl::NDRange lws = (local_x_size < gws_x) ? cl::NDRange(local_x_size, 1) : cl::NDRange(1, 1);
145 
146  do
147  {
148  /* Run the core part which has width can be divided by 16 */
149  unsigned int idx = 0;
150  add_2D_tensor_argument(idx, _input, slice);
151 
152  enqueue(queue, *this, slice, lws);
153  }
154  while(window.slide_window_slice_2D(slice));
155 }
156 
158  : _input(nullptr), _output(nullptr)
159 {
160 }
161 
163 {
164  configure(CLKernelLibrary::get().get_compile_context(), input, output);
165 }
166 
168 {
170  ARM_COMPUTE_ERROR_ON(nullptr == output);
171 
172  // Check input size
174 
175  // Check offset
176  ARM_COMPUTE_ERROR_ON_MSG(0 > output->offset() || output->offset() > 256, "Offset is larger than the image value range.");
177 
178  // Check range
179  ARM_COMPUTE_ERROR_ON_MSG(output->range() > 256 /* max range */, "Range larger than the image value range.");
180 
181  // We only run histogram on Image, therefore only 2 dimensions here
182  unsigned int start_position = (input->info()->dimension(0) / pixels_per_item) * pixels_per_item;
183 
184  if(start_position >= input->info()->dimension(0))
185  {
186  return; // no need to run histogram border kernel
187  }
188 
189  _input = input;
190  _output = output;
191 
192  unsigned int num_bins = _output->num_bins();
193  unsigned int window_size = _output->window();
194  unsigned int offset = _output->offset();
195  unsigned int range = _output->range();
196  unsigned int offrange = offset + range;
197 
198  // Create kernel
199  bool is_fixed_size = (256 == num_bins) && (1 == window_size) && (0 == offset) && (256 == offrange);
200  const std::string kernel_name = is_fixed_size ? "hist_border_kernel_fixed" : "hist_border_kernel";
201  _kernel = create_kernel(compile_context, kernel_name);
202 
203  // Set static kernel arguments
204  unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters
205  _kernel.setArg(idx++, _output->cl_buffer());
206  if(!is_fixed_size)
207  {
208  _kernel.setArg<cl_uint>(idx++, num_bins);
209  _kernel.setArg<cl_uint>(idx++, offset);
210  _kernel.setArg<cl_uint>(idx++, range);
211  _kernel.setArg<cl_uint>(idx++, offrange);
212  }
213 
214  // Configure kernel window
215  Window win;
216  win.set(0, Window::Dimension(start_position, _input->info()->dimension(0)));
217  win.set(1, Window::Dimension(0, _input->info()->dimension(1)));
219  ICLKernel::configure_internal(win);
220 
221  // Set config_id for enabling LWS tuning
222  _config_id = kernel_name;
223  _config_id += "_";
224  _config_id += lower_string(string_from_data_type(input->info()->data_type()));
225  _config_id += "_";
226  _config_id += support::cpp11::to_string(input->info()->dimension(0));
227  _config_id += "_";
228  _config_id += support::cpp11::to_string(input->info()->dimension(1));
229 }
230 
231 void CLHistogramBorderKernel::run(const Window &window, cl::CommandQueue &queue)
232 {
233  if(window.x().start() >= window.x().end())
234  {
235  return;
236  }
237 
239 
240  cl::NDRange lws = cl::NDRange(1, 1);
241 
243 
244  do
245  {
246  /* Run the border part which has width cannot be divided by 16 */
247  unsigned int idx = 0;
248  add_2D_tensor_argument(idx, _input, slice);
249 
250  enqueue(queue, *this, slice, lws);
251  }
252  while(window.slide_window_slice_2D(slice));
253 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:846
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
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t)
Definition: Validate.h:856
void configure(const ICLImage *input, ICLDistribution1D *output)
Initialise the kernel&#39;s input, output and border mode.
void unmap(cl::CommandQueue &q)
Enqueue an unmap operation of the allocated and mapped buffer on the given queue. ...
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:104
1 channel, 1 U8 per channel
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
uint32_t range() const
Returns the range of the distribution.
#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.
uint32_t window() const
Returns the window of the distribution, which is the range divided by the number of bins...
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:350
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
Copyright (c) 2017-2021 Arm Limited.
void map(cl::CommandQueue &q, bool blocking=true)
Enqueue a map operation of the allocated buffer on the given queue.
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:403
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:135
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w)
Definition: Validate.h:183
SimpleTensor< T > range(SimpleTensor< T > &dst, float start, const size_t num_of_elements, float step)
Definition: Range.cpp:50
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Implementation of a row access pattern.
std::string kernel_name
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:206
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
size_t num_bins() const
Returns the number of bins that the distribution has.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
CLCompileContext class.
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, 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: ICLKernel.h:148
ICLDistribution1D interface class.
uint32_t * buffer() const override
Returns a pointer to the start of the distribution.
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
constexpr signed int pixels_per_item
virtual cl::Buffer & cl_buffer()=0
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Wrapper to configure the Khronos OpenCL C++ header.
size_t size() const override
Returns the total size in bytes of the distribution.
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
void configure(const ICLImage *input, ICLDistribution1D *output)
Initialise the kernel&#39;s input, output and border mode.
constexpr unsigned int local_x_size
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
int32_t offset() const
Returns the offset of the distribution.
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145