Compute Library
 21.02
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 configure (const CLCompileContext &compile_context, 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...
 
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 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 59 of file CLMinMaxLocationKernel.cpp.

60  : _input(nullptr), _min_max(), _data_type_max_min()
61 {
62 }

◆ 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() [1/2]

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 64 of file CLMinMaxLocationKernel.cpp.

References CLKernelLibrary::get().

65 {
66  configure(CLKernelLibrary::get().get_compile_context(), input, min_max);
67 }
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
void configure(const ICLImage *input, cl::Buffer *min_max)
Initialise the kernel&#39;s input and output.

◆ configure() [2/2]

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

Initialise the kernel's input and output.

Parameters
[in]compile_contextThe compile context to be used.
[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 69 of file CLMinMaxLocationKernel.cpp.

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(), arm_compute::get_cl_type_from_data_type(), ITensor::info(), arm_compute::test::validation::input, arm_compute::support::cpp11::lowest(), ICLKernel::num_arguments_per_2D_tensor(), num_elems_processed_per_iteration, arm_compute::S16, arm_compute::support::cpp11::to_string(), arm_compute::U8, and arm_compute::update_window_and_padding().

70 {
73  ARM_COMPUTE_ERROR_ON(min_max == nullptr);
74 
75  _input = input;
76  _min_max = min_max;
77  const unsigned int num_elems_processed_per_iteration = input->info()->dimension(0);
78 
79  switch(input->info()->data_type())
80  {
81  case DataType::U8:
82  _data_type_max_min[0] = UCHAR_MAX;
83  _data_type_max_min[1] = 0;
84  break;
85  case DataType::S16:
86  _data_type_max_min[0] = SHRT_MAX;
87  _data_type_max_min[1] = SHRT_MIN;
88  break;
89  case DataType::F32:
90  _data_type_max_min[0] = FloatFlip(std::numeric_limits<float>::max());
91  _data_type_max_min[1] = FloatFlip(std::numeric_limits<float>::lowest());
92  break;
93  default:
94  ARM_COMPUTE_ERROR("You called with the wrong image data types");
95  }
96 
97  // Set kernel build options
98  std::set<std::string> build_opts{ "-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()) };
99 
100  if(num_elems_processed_per_iteration % max_cl_vector_width != 0)
101  {
102  build_opts.emplace("-DNON_MULTIPLE_OF_16");
103  }
104 
105  if(input->info()->data_type() == DataType::F32)
106  {
107  build_opts.emplace("-DDATA_TYPE_MAX=" + support::cpp11::to_string(std::numeric_limits<float>::max()));
108  build_opts.emplace("-DDATA_TYPE_MIN=" + support::cpp11::to_string(std::numeric_limits<float>::lowest()));
109  build_opts.emplace("-DIS_DATA_TYPE_FLOAT");
110  }
111  else
112  {
113  build_opts.emplace("-DDATA_TYPE_MAX=" + support::cpp11::to_string(_data_type_max_min[0]));
114  build_opts.emplace("-DDATA_TYPE_MIN=" + support::cpp11::to_string(_data_type_max_min[1]));
115  }
116 
117  // Create kernel
118  _kernel = create_kernel(compile_context, "minmax", build_opts);
119 
120  // Set fixed arguments
121  unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters
122  _kernel.setArg(idx++, *_min_max);
123  _kernel.setArg<cl_int>(idx++, static_cast<cl_int>(input->info()->dimension(0)));
124 
125  // Configure kernel window
126  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
127  update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, ceil_to_multiple(num_elems_processed_per_iteration, 16)));
128  ICLKernel::configure_internal(win);
129 }
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t)
Definition: Validate.h:856
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
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:466
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
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
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
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:37
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:206
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
unsigned int num_elems_processed_per_iteration
int32_t FloatFlip(float val)

◆ 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.

Reimplemented from ICLKernel.

Definition at line 131 of file CLMinMaxLocationKernel.cpp.

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(), ICLKernel::lws_hint(), arm_compute::test::validation::reference::slice(), Window::slide_window_slice_2D(), and IKernel::window().

132 {
135 
136  // Reset mininum and maximum values
137  queue.enqueueWriteBuffer(*_min_max, CL_FALSE /* blocking */, 0, _data_type_max_min.size() * sizeof(int), _data_type_max_min.data());
138 
140  do
141  {
142  unsigned int idx = 0;
143  add_2D_tensor_argument(idx, _input, slice);
144  enqueue(queue, *this, slice, lws_hint());
145  }
146  while(window.slide_window_slice_2D(slice));
147 
148  cl_int min = 0;
149  cl_int max = 0;
150  queue.enqueueReadBuffer(*_min_max, CL_TRUE /* blocking */, 0 * sizeof(cl_int), sizeof(cl_int), static_cast<int *>(&min));
151  queue.enqueueReadBuffer(*_min_max, CL_TRUE /* blocking */, 1 * sizeof(cl_int), sizeof(cl_int), static_cast<int *>(&max));
152 
153  if(_input->info()->data_type() == DataType::F32)
154  {
155  std::array<float, 2> min_max =
156  {
157  {
158  IFloatFlip(min),
159  IFloatFlip(max)
160  }
161  };
162  queue.enqueueWriteBuffer(*_min_max, CL_TRUE /* blocking */, 0, min_max.size() * sizeof(float), min_max.data());
163  }
164  else
165  {
166  std::array<int32_t, 2> min_max = { { min, max } };
167  queue.enqueueWriteBuffer(*_min_max, CL_TRUE /* blocking */, 0, min_max.size() * sizeof(int32_t), min_max.data());
168  }
169 }
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
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
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:323
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
float IFloatFlip(int32_t val)
#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
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

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