Compute Library
 19.08
CLMeanStdDevKernel Class Reference

Interface for the kernel to calculate mean and standard deviation of input image pixels. More...

#include <CLMeanStdDevKernel.h>

Collaboration diagram for CLMeanStdDevKernel:
[legend]

Public Member Functions

 CLMeanStdDevKernel ()
 Default constructor. More...
 
 CLMeanStdDevKernel (const CLMeanStdDevKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLMeanStdDevKerneloperator= (const CLMeanStdDevKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLMeanStdDevKernel (CLMeanStdDevKernel &&)=default
 Allow instances of this class to be moved. More...
 
CLMeanStdDevKerneloperator= (CLMeanStdDevKernel &&)=default
 Allow instances of this class to be moved. More...
 
void configure (const ICLImage *input, float *mean, cl::Buffer *global_sum, float *stddev=nullptr, cl::Buffer *global_sum_squared=nullptr)
 Initialise the kernel's input and outputs. 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...
 
BorderSize border_size () const override
 The size of the border for that kernel. 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...
 
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...
 
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<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...
 
template<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
- 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...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, float *mean, cl::Buffer *global_sum, float *stddev=nullptr, cl::Buffer *global_sum_squared=nullptr)
 Static function to check if given info will lead to a valid configuration of CLMeanStdDevKernel. More...
 
- 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 for the kernel to calculate mean and standard deviation of input image pixels.

Definition at line 40 of file CLMeanStdDevKernel.h.

Constructor & Destructor Documentation

◆ CLMeanStdDevKernel() [1/3]

Default constructor.

Definition at line 43 of file CLMeanStdDevKernel.cpp.

44  : _input(nullptr), _mean(nullptr), _stddev(nullptr), _global_sum(nullptr), _global_sum_squared(nullptr), _border_size(0)
45 {
46 }

◆ CLMeanStdDevKernel() [2/3]

CLMeanStdDevKernel ( const CLMeanStdDevKernel )
delete

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

◆ CLMeanStdDevKernel() [3/3]

Allow instances of this class to be moved.

Member Function Documentation

◆ border_size()

BorderSize border_size ( ) const
overridevirtual

The size of the border for that kernel.

Returns
The width in number of elements of the border.

Reimplemented from IKernel.

Definition at line 48 of file CLMeanStdDevKernel.cpp.

49 {
50  return _border_size;
51 }

Referenced by CLMeanStdDev::configure().

◆ configure()

void configure ( const ICLImage input,
float *  mean,
cl::Buffer *  global_sum,
float *  stddev = nullptr,
cl::Buffer *  global_sum_squared = nullptr 
)

Initialise the kernel's input and outputs.

Parameters
[in]inputInput image. Data types supported: U8.
[out]meanInput average pixel value.
[out]global_sumKeeps global sum of pixel values (Buffer size: 1 cl_ulong).
[out]stddev(Optional) Output standard deviation of pixel values.
[out]global_sum_squared(Optional if stddev is not set, required if stddev is set) Keeps global sum of squared pixel values (Buffer size: 1 cl_ulong).

Definition at line 66 of file CLMeanStdDevKernel.cpp.

67 {
68  ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, global_sum);
69  ARM_COMPUTE_ERROR_ON(stddev && nullptr == global_sum_squared);
70  ARM_COMPUTE_ERROR_THROW_ON(CLMeanStdDevKernel::validate(input->info(), mean, global_sum, stddev, global_sum_squared));
71 
72  _input = input;
73  _mean = mean;
74  _stddev = stddev;
75  _global_sum = global_sum;
76  _global_sum_squared = global_sum_squared;
77 
78  // Create kernel
79  std::set<std::string> build_opts;
80 
81  if(_stddev != nullptr)
82  {
83  build_opts.insert("-DSTDDEV");
84  }
85 
86  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("mean_stddev_accumulate", build_opts));
87 
88  // Set fixed arguments
89  unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input parameters
90 
91  _kernel.setArg(idx++, static_cast<cl_uint>(input->info()->dimension(1)));
92  _kernel.setArg(idx++, *_global_sum);
93 
94  if(_stddev != nullptr)
95  {
96  _kernel.setArg(idx++, *_global_sum_squared);
97  }
98 
99  // Configure kernel window
100  constexpr unsigned int num_elems_processed_per_iteration_x = 8;
101  const unsigned int num_elems_processed_per_iteration_y = input->info()->dimension(1);
102 
103  _border_size = BorderSize(ceil_to_multiple(input->info()->dimension(0), num_elems_processed_per_iteration_x) - input->info()->dimension(0));
104 
105  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
106  AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
107  update_window_and_padding(win, input_access);
108 
109  ICLKernel::configure_internal(win);
110 }
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
Definition: Types.h:259
#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.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
Implementation of a rectangular access pattern.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: Helpers.h:402
static Status validate(const ITensorInfo *input, float *mean, cl::Buffer *global_sum, float *stddev=nullptr, cl::Buffer *global_sum_squared=nullptr)
Static function to check if given info will lead to a valid configuration of CLMeanStdDevKernel.
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:66
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:192
std::unique_ptr< Kernel > create_kernel()
Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object.
Definition: Helpers.h:86
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Describe a multidimensional execution window.
Definition: Window.h:39

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::calculate_max_window(), arm_compute::ceil_to_multiple(), arm_compute::create_kernel(), ITensorInfo::dimension(), CLKernelLibrary::get(), ITensor::info(), ICLKernel::num_arguments_per_2D_tensor(), arm_compute::update_window_and_padding(), and CLMeanStdDevKernel::validate().

Referenced by CLMeanStdDev::configure().

◆ operator=() [1/2]

CLMeanStdDevKernel& operator= ( const CLMeanStdDevKernel )
delete

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

◆ operator=() [2/2]

CLMeanStdDevKernel& operator= ( CLMeanStdDevKernel &&  )
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.

Implements ICLKernel.

Definition at line 112 of file CLMeanStdDevKernel.cpp.

113 {
116 
117  // Clear sums
118  static const cl_ulong zero = 0;
119  queue.enqueueWriteBuffer(*_global_sum, CL_FALSE, 0, sizeof(cl_ulong), &zero);
120 
121  if(_stddev != nullptr)
122  {
123  queue.enqueueWriteBuffer(*_global_sum_squared, CL_FALSE, 0, sizeof(cl_ulong), &zero);
124  }
125 
127 
128  do
129  {
130  unsigned int idx = 0;
131  add_2D_tensor_argument(idx, _input, slice);
132  // Set slice step equal to height to force gws[1] to 1,
133  // as each thread calculates the sum across all rows and columns equal to the number of elements processed by each work-item
134  slice.set_dimension_step(Window::DimY, _input->info()->dimension(1));
135  enqueue(queue, *this, slice);
136  }
138 
139  // Calculate mean and stddev
140  cl_ulong global_sum = 0;
141  cl_ulong global_sum_squared = 0;
142  const float num_pixels = _input->info()->dimension(0) * _input->info()->dimension(1);
143 
144  queue.enqueueReadBuffer(*_global_sum, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum));
145  const float mean = global_sum / num_pixels;
146  *_mean = mean;
147 
148  if(_stddev != nullptr)
149  {
150  queue.enqueueReadBuffer(*_global_sum_squared, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum_squared));
151  *_stddev = std::sqrt((global_sum_squared / num_pixels) - (mean * mean));
152  }
153 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:267
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
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
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:307
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
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
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

References ICLKernel::add_2D_tensor_argument(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ITensorInfo::dimension(), Window::DimY, arm_compute::enqueue(), Window::first_slice_window_2D(), ITensor::info(), arm_compute::test::validation::reference::slice(), Window::slide_window_slice_2D(), and IKernel::window().

◆ validate()

Status validate ( const ITensorInfo input,
float *  mean,
cl::Buffer *  global_sum,
float *  stddev = nullptr,
cl::Buffer *  global_sum_squared = nullptr 
)
static

Static function to check if given info will lead to a valid configuration of CLMeanStdDevKernel.

Parameters
[in]inputInput image info. Data types supported: U8.
[in]meanInput average pixel value.
[in]global_sumKeeps global sum of pixel values.
[in]stddev(Optional) Output standard deviation of pixel values.
[in]global_sum_squared(Optional if stddev is not set, required if stddev is set) Keeps global sum of squared pixel values.
Returns
a status

Definition at line 53 of file CLMeanStdDevKernel.cpp.

54 {
55  ARM_COMPUTE_UNUSED(mean);
56  ARM_COMPUTE_UNUSED(stddev);
57  ARM_COMPUTE_UNUSED(global_sum);
58  ARM_COMPUTE_UNUSED(global_sum_squared);
62 
63  return Status{};
64 }
1 channel, 1 U8 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:791
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON_TENSOR_NOT_2D(t)
Definition: Validate.h:857
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
#define ARM_COMPUTE_RETURN_ERROR_ON_INT64_BASE_ATOMICS_UNSUPPORTED()
Definition: CLValidate.h:57

References ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_INT64_BASE_ATOMICS_UNSUPPORTED, ARM_COMPUTE_RETURN_ERROR_ON_TENSOR_NOT_2D, ARM_COMPUTE_UNUSED, and arm_compute::U8.

Referenced by CLMeanStdDevKernel::configure(), and CLMeanStdDev::validate().


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