Compute Library
 19.08
CLMinMaxKernel Class Reference

Interface for the kernel to perform min max search on an image. More...

#include <CLMinMaxLocationKernel.h>

Collaboration diagram for CLMinMaxKernel:
[legend]

Public Member Functions

 CLMinMaxKernel ()
 Default constructor. More...
 
 CLMinMaxKernel (const CLMinMaxKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLMinMaxKerneloperator= (const CLMinMaxKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLMinMaxKernel (CLMinMaxKernel &&)=default
 Allow instances of this class to be moved. More...
 
CLMinMaxKerneloperator= (CLMinMaxKernel &&)=default
 Allow instances of this class to be moved. More...
 
void configure (const ICLImage *input, cl::Buffer *min_max)
 Initialise the kernel's input and output. 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...
 
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...
 
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 for the kernel to perform min max search on an image.

Definition at line 39 of file CLMinMaxLocationKernel.h.

Constructor & Destructor Documentation

◆ CLMinMaxKernel() [1/3]

Default constructor.

Definition at line 57 of file CLMinMaxLocationKernel.cpp.

58  : _input(nullptr), _min_max(), _data_type_max_min()
59 {
60 }

◆ CLMinMaxKernel() [2/3]

CLMinMaxKernel ( const CLMinMaxKernel )
delete

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

◆ CLMinMaxKernel() [3/3]

CLMinMaxKernel ( CLMinMaxKernel &&  )
default

Allow instances of this class to be moved.

Member Function Documentation

◆ configure()

void configure ( const ICLImage input,
cl::Buffer *  min_max 
)

Initialise the kernel's input and output.

Parameters
[in]inputInput Image. Data types supported: U8/S16/F32.
[out]min_maxBuffer of 2 elements to store the min value at position 0 and the max value at position 1. Data type supported: S32 if input type is U8/S16, F32 if input type is F32.

Definition at line 62 of file CLMinMaxLocationKernel.cpp.

63 {
66  ARM_COMPUTE_ERROR_ON(min_max == nullptr);
67 
68  _input = input;
69  _min_max = min_max;
70  const unsigned int num_elems_processed_per_iteration = input->info()->dimension(0);
71 
72  switch(input->info()->data_type())
73  {
74  case DataType::U8:
75  _data_type_max_min[0] = UCHAR_MAX;
76  _data_type_max_min[1] = 0;
77  break;
78  case DataType::S16:
79  _data_type_max_min[0] = SHRT_MAX;
80  _data_type_max_min[1] = SHRT_MIN;
81  break;
82  case DataType::F32:
83  _data_type_max_min[0] = FloatFlip(std::numeric_limits<float>::max());
84  _data_type_max_min[1] = FloatFlip(std::numeric_limits<float>::lowest());
85  break;
86  default:
87  ARM_COMPUTE_ERROR("You called with the wrong image data types");
88  }
89 
90  // Set kernel build options
91  std::set<std::string> build_opts{ "-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()) };
92 
93  if(num_elems_processed_per_iteration % max_cl_vector_width != 0)
94  {
95  build_opts.emplace("-DNON_MULTIPLE_OF_16");
96  }
97 
98  if(input->info()->data_type() == DataType::F32)
99  {
100  build_opts.emplace("-DDATA_TYPE_MAX=" + support::cpp11::to_string(std::numeric_limits<float>::max()));
101  build_opts.emplace("-DDATA_TYPE_MIN=" + support::cpp11::to_string(std::numeric_limits<float>::lowest()));
102  build_opts.emplace("-DIS_DATA_TYPE_FLOAT");
103  }
104  else
105  {
106  build_opts.emplace("-DDATA_TYPE_MAX=" + support::cpp11::to_string(_data_type_max_min[0]));
107  build_opts.emplace("-DDATA_TYPE_MIN=" + support::cpp11::to_string(_data_type_max_min[1]));
108  }
109 
110  // Create kernel
111  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("minmax", build_opts));
112 
113  // Set fixed arguments
114  unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters
115  _kernel.setArg(idx++, *_min_max);
116  _kernel.setArg<cl_int>(idx++, static_cast<cl_int>(input->info()->dimension(0)));
117 
118  // Configure kernel window
119  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
120  update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, ceil_to_multiple(num_elems_processed_per_iteration, 16)));
121  ICLKernel::configure_internal(win);
122 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
1 channel, 1 U8 per channel
std::string to_string(T &&value)
Convert integer and float values to string.
1 channel, 1 F32 per channel
#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.
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
#define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t)
Definition: Validate.h:855
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: Helpers.h:402
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
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:35
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
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:789
int32_t FloatFlip(float val)

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D, arm_compute::calculate_max_window(), arm_compute::ceil_to_multiple(), arm_compute::create_kernel(), ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::F32, arm_compute::FloatFlip(), CLKernelLibrary::get(), arm_compute::get_cl_type_from_data_type(), ITensor::info(), arm_compute::support::cpp11::lowest(), ICLKernel::num_arguments_per_2D_tensor(), arm_compute::S16, arm_compute::support::cpp11::to_string(), arm_compute::U8, and arm_compute::update_window_and_padding().

Referenced by CLMinMaxLocation::configure().

◆ operator=() [1/2]

CLMinMaxKernel& operator= ( const CLMinMaxKernel )
delete

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

◆ operator=() [2/2]

CLMinMaxKernel& operator= ( CLMinMaxKernel &&  )
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 124 of file CLMinMaxLocationKernel.cpp.

125 {
128 
129  // Reset mininum and maximum values
130  queue.enqueueWriteBuffer(*_min_max, CL_FALSE /* blocking */, 0, _data_type_max_min.size() * sizeof(int), _data_type_max_min.data());
131 
133  do
134  {
135  unsigned int idx = 0;
136  add_2D_tensor_argument(idx, _input, slice);
137  enqueue(queue, *this, slice);
138  }
140 
141  cl_int min = 0;
142  cl_int max = 0;
143  queue.enqueueReadBuffer(*_min_max, CL_TRUE /* blocking */, 0 * sizeof(cl_int), sizeof(cl_int), static_cast<int *>(&min));
144  queue.enqueueReadBuffer(*_min_max, CL_TRUE /* blocking */, 1 * sizeof(cl_int), sizeof(cl_int), static_cast<int *>(&max));
145 
146  if(_input->info()->data_type() == DataType::F32)
147  {
148  std::array<float, 2> min_max =
149  {
150  {
151  IFloatFlip(min),
152  IFloatFlip(max)
153  }
154  };
155  queue.enqueueWriteBuffer(*_min_max, CL_TRUE /* blocking */, 0, min_max.size() * sizeof(float), min_max.data());
156  }
157  else
158  {
159  std::array<int32_t, 2> min_max = { { min, max } };
160  queue.enqueueWriteBuffer(*_min_max, CL_TRUE /* blocking */, 0, min_max.size() * sizeof(int32_t), min_max.data());
161  }
162 }
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
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
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
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.
float IFloatFlip(int32_t val)
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
#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::data_type(), arm_compute::enqueue(), arm_compute::F32, Window::first_slice_window_2D(), arm_compute::IFloatFlip(), ITensor::info(), arm_compute::test::validation::reference::slice(), Window::slide_window_slice_2D(), and IKernel::window().


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