Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 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 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLMeanStdDevKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 27 | #include "arm_compute/core/CL/ICLTensor.h" |
| 28 | #include "arm_compute/core/CL/OpenCL.h" |
| 29 | #include "arm_compute/core/Error.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include <cmath> |
| 37 | #include <set> |
| 38 | #include <string> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | CLMeanStdDevKernel::CLMeanStdDevKernel() |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 43 | : _input(nullptr), _mean(nullptr), _stddev(nullptr), _global_sum(nullptr), _global_sum_squared(nullptr), _border_size(0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 47 | BorderSize CLMeanStdDevKernel::border_size() const |
| 48 | { |
| 49 | return _border_size; |
| 50 | } |
| 51 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | void CLMeanStdDevKernel::configure(const ICLImage *input, float *mean, cl::Buffer *global_sum, float *stddev, cl::Buffer *global_sum_squared) |
| 53 | { |
| 54 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 55 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 56 | ARM_COMPUTE_ERROR_ON(nullptr == mean); |
| 57 | ARM_COMPUTE_ERROR_ON(nullptr == global_sum); |
| 58 | ARM_COMPUTE_ERROR_ON(stddev && nullptr == global_sum_squared); |
| 59 | |
| 60 | _input = input; |
| 61 | _mean = mean; |
| 62 | _stddev = stddev; |
| 63 | _global_sum = global_sum; |
| 64 | _global_sum_squared = global_sum_squared; |
| 65 | |
| 66 | // Create kernel |
| 67 | std::set<std::string> build_opts; |
| 68 | |
| 69 | if(_stddev != nullptr) |
| 70 | { |
| 71 | build_opts.insert("-DSTDDEV"); |
| 72 | } |
| 73 | |
| 74 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("mean_stddev_accumulate", build_opts)); |
| 75 | |
| 76 | // Set fixed arguments |
| 77 | unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input parameters |
| 78 | |
| 79 | _kernel.setArg(idx++, static_cast<cl_uint>(input->info()->dimension(1))); |
| 80 | _kernel.setArg(idx++, *_global_sum); |
| 81 | |
| 82 | if(_stddev != nullptr) |
| 83 | { |
| 84 | _kernel.setArg(idx++, *_global_sum_squared); |
| 85 | } |
| 86 | |
| 87 | // Configure kernel window |
| 88 | constexpr unsigned int num_elems_processed_per_iteration_x = 8; |
| 89 | const unsigned int num_elems_processed_per_iteration_y = input->info()->dimension(1); |
| 90 | |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 91 | _border_size = BorderSize(ceil_to_multiple(input->info()->dimension(0), num_elems_processed_per_iteration_x) - input->info()->dimension(0)); |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 92 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 94 | AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 95 | update_window_and_padding(win, input_access); |
| 96 | |
| 97 | ICLKernel::configure(win); |
| 98 | } |
| 99 | |
| 100 | void CLMeanStdDevKernel::run(const Window &window, cl::CommandQueue &queue) |
| 101 | { |
| 102 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 103 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 104 | |
| 105 | // Clear sums |
| 106 | static const cl_ulong zero = 0; |
| 107 | queue.enqueueWriteBuffer(*_global_sum, CL_FALSE, 0, sizeof(cl_ulong), &zero); |
| 108 | |
| 109 | if(_stddev != nullptr) |
| 110 | { |
| 111 | queue.enqueueWriteBuffer(*_global_sum_squared, CL_FALSE, 0, sizeof(cl_ulong), &zero); |
| 112 | } |
| 113 | |
| 114 | Window slice = window.first_slice_window_2D(); |
| 115 | |
| 116 | do |
| 117 | { |
| 118 | unsigned int idx = 0; |
| 119 | add_2D_tensor_argument(idx, _input, slice); |
| 120 | // Set slice step equal to height to force gws[1] to 1, |
| 121 | // as each thread calculates the sum across all rows and columns equal to the number of elements processed by each work-item |
| 122 | slice.set_dimension_step(Window::DimY, _input->info()->dimension(1)); |
| 123 | enqueue(queue, *this, slice); |
| 124 | } |
| 125 | while(window.slide_window_slice_2D(slice)); |
| 126 | |
| 127 | // Calculate mean and stddev |
| 128 | cl_ulong global_sum = 0; |
| 129 | cl_ulong global_sum_squared = 0; |
| 130 | const float num_pixels = _input->info()->dimension(0) * _input->info()->dimension(1); |
| 131 | |
| 132 | queue.enqueueReadBuffer(*_global_sum, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum)); |
| 133 | const float mean = global_sum / num_pixels; |
| 134 | *_mean = mean; |
| 135 | |
| 136 | if(_stddev != nullptr) |
| 137 | { |
| 138 | queue.enqueueReadBuffer(*_global_sum_squared, CL_TRUE, 0, sizeof(cl_ulong), static_cast<void *>(&global_sum_squared)); |
| 139 | *_stddev = std::sqrt((global_sum_squared / num_pixels) - (mean * mean)); |
| 140 | } |
| 141 | } |