Compute Library
 21.02
CLMeanStdDevKernel.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 
32 #include "src/core/CL/CLValidate.h"
34 
35 #include <cmath>
36 #include <set>
37 #include <string>
38 
39 using namespace arm_compute;
40 
42  : _input(nullptr), _mean(nullptr), _stddev(nullptr), _global_sum(nullptr), _global_sum_squared(nullptr), _border_size(0)
43 {
44 }
45 
47 {
48  return _border_size;
49 }
50 
51 Status CLMeanStdDevKernel::validate(const ITensorInfo *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared)
52 {
53  ARM_COMPUTE_UNUSED(mean);
54  ARM_COMPUTE_UNUSED(stddev);
55  ARM_COMPUTE_UNUSED(global_sum);
56  ARM_COMPUTE_UNUSED(global_sum_squared);
60 
61  return Status{};
62 }
63 
64 void CLMeanStdDevKernel::configure(const ICLImage *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared)
65 {
66  configure(CLKernelLibrary::get().get_compile_context(), input, mean, global_sum, stddev, global_sum_squared);
67 }
68 
69 void CLMeanStdDevKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared)
70 {
71  ARM_COMPUTE_ERROR_ON_NULLPTR(input, mean, global_sum);
72  ARM_COMPUTE_ERROR_ON(stddev && nullptr == global_sum_squared);
73  ARM_COMPUTE_ERROR_THROW_ON(CLMeanStdDevKernel::validate(input->info(), mean, global_sum, stddev, global_sum_squared));
74 
75  _input = input;
76  _mean = mean;
77  _stddev = stddev;
78  _global_sum = global_sum;
79  _global_sum_squared = global_sum_squared;
80 
81  // Create kernel
82  std::set<std::string> build_opts;
83 
84  if(_stddev != nullptr)
85  {
86  build_opts.insert("-DSTDDEV");
87  }
88 
89  _kernel = create_kernel(compile_context, "mean_stddev_accumulate", build_opts);
90 
91  // Set fixed arguments
92  unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input parameters
93 
94  _kernel.setArg(idx++, static_cast<cl_uint>(input->info()->dimension(1)));
95  _kernel.setArg(idx++, *_global_sum);
96 
97  if(_stddev != nullptr)
98  {
99  _kernel.setArg(idx++, *_global_sum_squared);
100  }
101 
102  // Configure kernel window
103  constexpr unsigned int num_elems_processed_per_iteration_x = 8;
104  const unsigned int num_elems_processed_per_iteration_y = input->info()->dimension(1);
105 
106  _border_size = BorderSize(ceil_to_multiple(input->info()->dimension(0), num_elems_processed_per_iteration_x) - input->info()->dimension(0));
107 
108  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
109  AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
110  update_window_and_padding(win, input_access);
111 
112  ICLKernel::configure_internal(win);
113 }
114 
115 void CLMeanStdDevKernel::run(const Window &window, cl::CommandQueue &queue)
116 {
119 
120  // Clear sums
121  static const cl_ulong zero = 0;
122  queue.enqueueWriteBuffer(*_global_sum, CL_FALSE, 0, sizeof(cl_ulong), &zero);
123 
124  if(_stddev != nullptr)
125  {
126  queue.enqueueWriteBuffer(*_global_sum_squared, CL_FALSE, 0, sizeof(cl_ulong), &zero);
127  }
128 
130 
131  do
132  {
133  unsigned int idx = 0;
134  add_2D_tensor_argument(idx, _input, slice);
135  // Set slice step equal to height to force gws[1] to 1,
136  // as each thread calculates the sum across all rows and columns equal to the number of elements processed by each work-item
137  slice.set_dimension_step(Window::DimY, _input->info()->dimension(1));
138  enqueue(queue, *this, slice, lws_hint());
139  }
140  while(window.slide_window_slice_2D(slice));
141 
142  // Calculate mean and stddev
143  cl_ulong global_sum = 0;
144  cl_ulong global_sum_squared = 0;
145  const float num_pixels = _input->info()->dimension(0) * _input->info()->dimension(1);
146 
147  queue.enqueueReadBuffer(*_global_sum, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum));
148  const float mean = global_sum / num_pixels;
149  *_mean = mean;
150 
151  if(_stddev != nullptr)
152  {
153  queue.enqueueReadBuffer(*_global_sum_squared, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum_squared));
154  *_stddev = std::sqrt((global_sum_squared / num_pixels) - (mean * mean));
155  }
156 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
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.
Container for 2D border size.
Definition: Types.h:273
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
1 channel, 1 U8 per channel
void configure(const ICLImage *input, float *mean, cl::Buffer *global_sum, float *stddev=nullptr, cl::Buffer *global_sum_squared=nullptr)
Initialise the kernel&#39;s input and outputs.
#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.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
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 run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
Implementation of a rectangular access pattern.
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
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
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:71
#define ARM_COMPUTE_RETURN_ERROR_ON_TENSOR_NOT_2D(t)
Definition: Validate.h:858
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
#define ARM_COMPUTE_RETURN_ERROR_ON_INT64_BASE_ATOMICS_UNSUPPORTED()
Definition: CLValidate.h:58
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
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
CLCompileContext class.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void set_dimension_step(size_t dimension, int step)
Set the step of a given dimension.
Definition: Window.inl:167
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
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Wrapper to configure the Khronos OpenCL C++ header.
BorderSize border_size() const override
The size of the border for that kernel.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
CLMeanStdDevKernel()
Default constructor.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)