Compute Library
 21.02
CLHistogramKernel Class Reference

Interface to run the histogram kernel. More...

#include <CLHistogramKernel.h>

Collaboration diagram for CLHistogramKernel:
[legend]

Public Member Functions

 CLHistogramKernel ()
 Constructor. More...
 
 CLHistogramKernel (const CLHistogramKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLHistogramKerneloperator= (const CLHistogramKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLHistogramKernel (CLHistogramKernel &&)=default
 Allow instances of this class to be moved. More...
 
CLHistogramKerneloperator= (CLHistogramKernel &&)=default
 Allow instances of this class to be moved. More...
 
void configure (const ICLImage *input, ICLDistribution1D *output)
 Initialise the kernel's input, output and border mode. More...
 
void configure (const CLCompileContext &compile_context, const ICLImage *input, ICLDistribution1D *output)
 Initialise the kernel's input, output and border mode. More...
 
void run (const Window &window, cl::CommandQueue &queue) override
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. More...
 
- Public Member Functions inherited from ICLKernel
 ICLKernel ()
 Constructor. More...
 
cl::Kernel & kernel ()
 Returns a reference to the OpenCL kernel of this object. More...
 
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 the passed 1D array's parameters to the object's kernel's arguments starting from the index idx. More...
 
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. More...
 
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 if the condition is true. More...
 
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. More...
 
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 if the condition is true. More...
 
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. More...
 
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. More...
 
virtual void run_op (ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. More...
 
template<typename T >
void add_argument (unsigned int &idx, T value)
 Add the passed parameters to the object's kernel's arguments starting from the index idx. More...
 
void set_lws_hint (const cl::NDRange &lws_hint)
 Set the Local-Workgroup-Size hint. More...
 
cl::NDRange lws_hint () const
 Return the Local-Workgroup-Size hint. More...
 
void set_wbsm_hint (const cl_int &wbsm_hint)
 Set the workgroup batch size modifier hint. More...
 
cl_int wbsm_hint () const
 Return the workgroup batch size modifier hint. More...
 
const std::string & config_id () const
 Get the configuration ID. More...
 
void set_target (GPUTarget target)
 Set the targeted GPU architecture. More...
 
void set_target (cl::Device &device)
 Set the targeted GPU architecture according to the CL device. More...
 
GPUTarget get_target () const
 Get the targeted GPU architecture. More...
 
size_t get_max_workgroup_size ()
 Get the maximum workgroup size for the device the CLKernelLibrary uses. More...
 
template<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
template<typename T , unsigned int dimension_size>
void add_array_argument (unsigned &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
 Add the passed array's parameters to the object's kernel's arguments starting from the index idx. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from ICLKernel
static constexpr unsigned int num_arguments_per_1D_array ()
 Returns the number of arguments enqueued per 1D array object. More...
 
static constexpr unsigned int num_arguments_per_1D_tensor ()
 Returns the number of arguments enqueued per 1D tensor object. More...
 
static constexpr unsigned int num_arguments_per_2D_tensor ()
 Returns the number of arguments enqueued per 2D tensor object. More...
 
static constexpr unsigned int num_arguments_per_3D_tensor ()
 Returns the number of arguments enqueued per 3D tensor object. More...
 
static constexpr unsigned int num_arguments_per_4D_tensor ()
 Returns the number of arguments enqueued per 4D tensor object. More...
 
static cl::NDRange gws_from_window (const Window &window)
 Get the global work size given an execution window. More...
 

Detailed Description

Interface to run the histogram kernel.

This kernel processes the part of image with width can be divided by 16. If the image width is not a multiple of 16, remaining pixels have to be processed with the CLHistogramBorderKernel

Definition at line 38 of file CLHistogramKernel.h.

Constructor & Destructor Documentation

◆ CLHistogramKernel() [1/3]

Constructor.

Definition at line 47 of file CLHistogramKernel.cpp.

48  : _input(nullptr), _output(nullptr)
49 {
50 }

◆ CLHistogramKernel() [2/3]

CLHistogramKernel ( const CLHistogramKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ CLHistogramKernel() [3/3]

Allow instances of this class to be moved.

Member Function Documentation

◆ configure() [1/2]

void configure ( const ICLImage input,
ICLDistribution1D output 
)

Initialise the kernel's input, output and border mode.

Parameters
[in]inputSource image. Data types supported: U8.
[out]outputDestination distribution.

Definition at line 52 of file CLHistogramKernel.cpp.

References CLKernelLibrary::get().

Referenced by CLHistogram::configure().

53 {
54  configure(CLKernelLibrary::get().get_compile_context(), input, output);
55 }
void configure(const ICLImage *input, ICLDistribution1D *output)
Initialise the kernel&#39;s input, output and border mode.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.

◆ configure() [2/2]

void configure ( const CLCompileContext compile_context,
const ICLImage input,
ICLDistribution1D output 
)

Initialise the kernel's input, output and border mode.

Parameters
[in]compile_contextThe compile context to be used.
[in]inputSource image. Data types supported: U8.
[out]outputDestination distribution.

Definition at line 57 of file CLHistogramKernel.cpp.

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_ON_MSG, ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D, ICLDistribution1D::cl_buffer(), arm_compute::create_kernel(), ITensorInfo::data_type(), ITensorInfo::dimension(), ITensor::info(), arm_compute::test::validation::input, kernel_name, arm_compute::lower_string(), ICLKernel::num_arguments_per_2D_tensor(), IDistribution1D::num_bins(), IDistribution1D::offset(), offset(), pixels_per_item, arm_compute::test::validation::reference::range(), IDistribution1D::range(), Window::set(), IDistribution1D::size(), arm_compute::string_from_data_type(), arm_compute::support::cpp11::to_string(), arm_compute::U8, arm_compute::update_window_and_padding(), and IDistribution1D::window().

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 }
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:846
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
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
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
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
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
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
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 ...
size_t size() const override
Returns the total size in bytes of the distribution.
Describe a multidimensional execution window.
Definition: Window.h:39
int32_t offset() const
Returns the offset of the distribution.

◆ operator=() [1/2]

CLHistogramKernel& operator= ( const CLHistogramKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

CLHistogramKernel& operator= ( CLHistogramKernel &&  )
default

Allow instances of this class to be moved.

◆ run()

void run ( const Window window,
cl::CommandQueue &  queue 
)
overridevirtual

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.
Parameters
[in]windowRegion on which to execute the kernel. (Must be a valid region of the window returned by window()).
[in,out]queueCommand queue on which to enqueue the kernel.

Reimplemented from ICLKernel.

Definition at line 126 of file CLHistogramKernel.cpp.

References ICLKernel::add_2D_tensor_argument(), ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ICLDistribution1D::buffer(), ITensorInfo::dimension(), Window::Dimension::end(), arm_compute::enqueue(), Window::first_slice_window_2D(), ITensor::info(), local_x_size, ICLDistribution1D::map(), pixels_per_item, IDistribution1D::size(), arm_compute::test::validation::reference::slice(), Window::slide_window_slice_2D(), Window::Dimension::start(), Window::Dimension::step(), ICLDistribution1D::unmap(), IKernel::window(), and Window::x().

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 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
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.
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
#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
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
void map(cl::CommandQueue &q, bool blocking=true)
Enqueue a map operation of the allocated buffer on the given queue.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w)
Definition: Validate.h:183
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
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
uint32_t * buffer() const override
Returns a pointer to the start of the distribution.
constexpr signed int pixels_per_item
size_t size() const override
Returns the total size in bytes of the distribution.
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
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

The documentation for this class was generated from the following files: