Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/CLMinMaxLocationKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
| 32 | |
| 33 | #include <climits> |
| 34 | |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 37 | inline int32_t FloatFlip(float val) |
| 38 | { |
| 39 | static_assert(sizeof(float) == sizeof(int32_t), "Float must be same size as int32_t"); |
| 40 | int32_t int_val = 0; |
| 41 | |
| 42 | memcpy(&int_val, &val, sizeof(float)); |
| 43 | int_val = (int_val >= 0) ? int_val : int_val ^ 0x7FFFFFFF; |
| 44 | return int_val; |
| 45 | } |
| 46 | |
| 47 | inline float IFloatFlip(int32_t val) |
| 48 | { |
| 49 | static_assert(sizeof(float) == sizeof(int32_t), "Float must be same size as int32_t"); |
| 50 | float flt_val = 0.f; |
| 51 | |
| 52 | val = (val >= 0) ? val : val ^ 0x7FFFFFFF; |
| 53 | memcpy(&flt_val, &val, sizeof(float)); |
| 54 | return flt_val; |
| 55 | } |
| 56 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | CLMinMaxKernel::CLMinMaxKernel() |
| 58 | : _input(nullptr), _min_max(), _data_type_max_min() |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | void CLMinMaxKernel::configure(const ICLImage *input, cl::Buffer *min_max) |
| 63 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 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; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 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; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | default: |
| 87 | ARM_COMPUTE_ERROR("You called with the wrong image data types"); |
| 88 | } |
| 89 | |
| 90 | // Set kernel build options |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 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 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 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 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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); |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 116 | _kernel.setArg<cl_int>(idx++, static_cast<cl_int>(input->info()->dimension(0))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | |
| 118 | // Configure kernel window |
| 119 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 120 | update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, ceil_to_multiple(num_elems_processed_per_iteration, 16))); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 121 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void CLMinMaxKernel::run(const Window &window, cl::CommandQueue &queue) |
| 125 | { |
| 126 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 127 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 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 | |
| 132 | Window slice = window.first_slice_window_2D(); |
| 133 | do |
| 134 | { |
| 135 | unsigned int idx = 0; |
| 136 | add_2D_tensor_argument(idx, _input, slice); |
| 137 | enqueue(queue, *this, slice); |
| 138 | } |
| 139 | while(window.slide_window_slice_2D(slice)); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 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 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | CLMinMaxLocationKernel::CLMinMaxLocationKernel() |
| 165 | : _input(nullptr), _min_max_count(nullptr) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | void CLMinMaxLocationKernel::configure(const ICLImage *input, cl::Buffer *min_max, cl::Buffer *min_max_count, ICLCoordinates2DArray *min_loc, ICLCoordinates2DArray *max_loc) |
| 170 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 171 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 173 | ARM_COMPUTE_ERROR_ON(min_max == nullptr); |
| 174 | ARM_COMPUTE_ERROR_ON(min_max_count == nullptr && min_loc == nullptr && max_loc == nullptr); |
| 175 | |
| 176 | _input = input; |
| 177 | _min_max_count = min_max_count; |
| 178 | |
| 179 | // Set kernel build options |
| 180 | std::set<std::string> build_opts; |
| 181 | build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 182 | build_opts.emplace((min_max_count != nullptr) ? "-DCOUNT_MIN_MAX" : ""); |
| 183 | build_opts.emplace((min_loc != nullptr) ? "-DLOCATE_MIN" : ""); |
| 184 | build_opts.emplace((max_loc != nullptr) ? "-DLOCATE_MAX" : ""); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 185 | if(input->info()->data_type() == DataType::F32) |
| 186 | { |
| 187 | build_opts.emplace("-DIS_DATA_TYPE_FLOAT"); |
| 188 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | |
| 190 | // Create kernel |
| 191 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("minmaxloc", build_opts)); |
| 192 | |
| 193 | // Set static arguments |
| 194 | unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters |
| 195 | _kernel.setArg(idx++, *min_max); |
| 196 | _kernel.setArg(idx++, *min_max_count); |
| 197 | if(min_loc != nullptr) |
| 198 | { |
| 199 | _kernel.setArg(idx++, min_loc->cl_buffer()); |
| 200 | _kernel.setArg<cl_uint>(idx++, min_loc->max_num_values()); |
| 201 | } |
| 202 | if(max_loc != nullptr) |
| 203 | { |
| 204 | _kernel.setArg(idx++, max_loc->cl_buffer()); |
| 205 | _kernel.setArg<cl_uint>(idx++, max_loc->max_num_values()); |
| 206 | } |
| 207 | |
| 208 | // Configure kernel window |
| 209 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 210 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 211 | update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration)); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 212 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void CLMinMaxLocationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 216 | { |
| 217 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 218 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 219 | |
| 220 | static const unsigned int zero_count = 0; |
| 221 | queue.enqueueWriteBuffer(*_min_max_count, CL_FALSE, 0 * sizeof(zero_count), sizeof(zero_count), &zero_count); |
| 222 | queue.enqueueWriteBuffer(*_min_max_count, CL_FALSE, 1 * sizeof(zero_count), sizeof(zero_count), &zero_count); |
| 223 | |
| 224 | Window slice = window.first_slice_window_2D(); |
| 225 | do |
| 226 | { |
| 227 | unsigned int idx = 0; |
| 228 | add_2D_tensor_argument(idx, _input, slice); |
| 229 | enqueue(queue, *this, slice); |
| 230 | } |
| 231 | while(window.slide_window_slice_2D(slice)); |
| 232 | } |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 233 | } // namespace arm_compute |