Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 32 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | |
| 34 | #include <climits> |
| 35 | |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 38 | inline int32_t FloatFlip(float val) |
| 39 | { |
| 40 | static_assert(sizeof(float) == sizeof(int32_t), "Float must be same size as int32_t"); |
| 41 | int32_t int_val = 0; |
| 42 | |
| 43 | memcpy(&int_val, &val, sizeof(float)); |
| 44 | int_val = (int_val >= 0) ? int_val : int_val ^ 0x7FFFFFFF; |
| 45 | return int_val; |
| 46 | } |
| 47 | |
| 48 | inline float IFloatFlip(int32_t val) |
| 49 | { |
| 50 | static_assert(sizeof(float) == sizeof(int32_t), "Float must be same size as int32_t"); |
| 51 | float flt_val = 0.f; |
| 52 | |
| 53 | val = (val >= 0) ? val : val ^ 0x7FFFFFFF; |
| 54 | memcpy(&flt_val, &val, sizeof(float)); |
| 55 | return flt_val; |
| 56 | } |
| 57 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | CLMinMaxKernel::CLMinMaxKernel() |
| 59 | : _input(nullptr), _min_max(), _data_type_max_min() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | void CLMinMaxKernel::configure(const ICLImage *input, cl::Buffer *min_max) |
| 64 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 65 | configure(CLKernelLibrary::get().get_compile_context(), input, min_max); |
| 66 | } |
| 67 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 68 | void CLMinMaxKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, cl::Buffer *min_max) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 69 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 70 | 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] | 71 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 72 | ARM_COMPUTE_ERROR_ON(min_max == nullptr); |
| 73 | |
| 74 | _input = input; |
| 75 | _min_max = min_max; |
| 76 | const unsigned int num_elems_processed_per_iteration = input->info()->dimension(0); |
| 77 | |
| 78 | switch(input->info()->data_type()) |
| 79 | { |
| 80 | case DataType::U8: |
| 81 | _data_type_max_min[0] = UCHAR_MAX; |
| 82 | _data_type_max_min[1] = 0; |
| 83 | break; |
| 84 | case DataType::S16: |
| 85 | _data_type_max_min[0] = SHRT_MAX; |
| 86 | _data_type_max_min[1] = SHRT_MIN; |
| 87 | break; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 88 | case DataType::F32: |
| 89 | _data_type_max_min[0] = FloatFlip(std::numeric_limits<float>::max()); |
| 90 | _data_type_max_min[1] = FloatFlip(std::numeric_limits<float>::lowest()); |
| 91 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | default: |
| 93 | ARM_COMPUTE_ERROR("You called with the wrong image data types"); |
| 94 | } |
| 95 | |
| 96 | // Set kernel build options |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 97 | std::set<std::string> build_opts{ "-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()) }; |
| 98 | |
| 99 | if(num_elems_processed_per_iteration % max_cl_vector_width != 0) |
| 100 | { |
| 101 | build_opts.emplace("-DNON_MULTIPLE_OF_16"); |
| 102 | } |
| 103 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 104 | if(input->info()->data_type() == DataType::F32) |
| 105 | { |
| 106 | build_opts.emplace("-DDATA_TYPE_MAX=" + support::cpp11::to_string(std::numeric_limits<float>::max())); |
| 107 | build_opts.emplace("-DDATA_TYPE_MIN=" + support::cpp11::to_string(std::numeric_limits<float>::lowest())); |
| 108 | build_opts.emplace("-DIS_DATA_TYPE_FLOAT"); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | build_opts.emplace("-DDATA_TYPE_MAX=" + support::cpp11::to_string(_data_type_max_min[0])); |
| 113 | build_opts.emplace("-DDATA_TYPE_MIN=" + support::cpp11::to_string(_data_type_max_min[1])); |
| 114 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | |
| 116 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 117 | _kernel = create_kernel(compile_context, "minmax", build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
| 119 | // Set fixed arguments |
| 120 | unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters |
| 121 | _kernel.setArg(idx++, *_min_max); |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 122 | _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] | 123 | |
| 124 | // Configure kernel window |
| 125 | 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] | 126 | 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] | 127 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void CLMinMaxKernel::run(const Window &window, cl::CommandQueue &queue) |
| 131 | { |
| 132 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 133 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 134 | |
| 135 | // Reset mininum and maximum values |
| 136 | queue.enqueueWriteBuffer(*_min_max, CL_FALSE /* blocking */, 0, _data_type_max_min.size() * sizeof(int), _data_type_max_min.data()); |
| 137 | |
| 138 | Window slice = window.first_slice_window_2D(); |
| 139 | do |
| 140 | { |
| 141 | unsigned int idx = 0; |
| 142 | add_2D_tensor_argument(idx, _input, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 143 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | } |
| 145 | while(window.slide_window_slice_2D(slice)); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 146 | |
| 147 | cl_int min = 0; |
| 148 | cl_int max = 0; |
| 149 | queue.enqueueReadBuffer(*_min_max, CL_TRUE /* blocking */, 0 * sizeof(cl_int), sizeof(cl_int), static_cast<int *>(&min)); |
| 150 | queue.enqueueReadBuffer(*_min_max, CL_TRUE /* blocking */, 1 * sizeof(cl_int), sizeof(cl_int), static_cast<int *>(&max)); |
| 151 | |
| 152 | if(_input->info()->data_type() == DataType::F32) |
| 153 | { |
| 154 | std::array<float, 2> min_max = |
| 155 | { |
| 156 | { |
| 157 | IFloatFlip(min), |
| 158 | IFloatFlip(max) |
| 159 | } |
| 160 | }; |
| 161 | queue.enqueueWriteBuffer(*_min_max, CL_TRUE /* blocking */, 0, min_max.size() * sizeof(float), min_max.data()); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | std::array<int32_t, 2> min_max = { { min, max } }; |
| 166 | queue.enqueueWriteBuffer(*_min_max, CL_TRUE /* blocking */, 0, min_max.size() * sizeof(int32_t), min_max.data()); |
| 167 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | CLMinMaxLocationKernel::CLMinMaxLocationKernel() |
| 171 | : _input(nullptr), _min_max_count(nullptr) |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | void CLMinMaxLocationKernel::configure(const ICLImage *input, cl::Buffer *min_max, cl::Buffer *min_max_count, ICLCoordinates2DArray *min_loc, ICLCoordinates2DArray *max_loc) |
| 176 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 177 | configure(CLKernelLibrary::get().get_compile_context(), input, min_max, min_max_count, min_loc, max_loc); |
| 178 | } |
| 179 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 180 | void CLMinMaxLocationKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, cl::Buffer *min_max, cl::Buffer *min_max_count, ICLCoordinates2DArray *min_loc, |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 181 | ICLCoordinates2DArray *max_loc) |
| 182 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 183 | 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] | 184 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 185 | ARM_COMPUTE_ERROR_ON(min_max == nullptr); |
| 186 | ARM_COMPUTE_ERROR_ON(min_max_count == nullptr && min_loc == nullptr && max_loc == nullptr); |
| 187 | |
| 188 | _input = input; |
| 189 | _min_max_count = min_max_count; |
| 190 | |
| 191 | // Set kernel build options |
| 192 | std::set<std::string> build_opts; |
| 193 | build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 194 | build_opts.emplace((min_max_count != nullptr) ? "-DCOUNT_MIN_MAX" : ""); |
| 195 | build_opts.emplace((min_loc != nullptr) ? "-DLOCATE_MIN" : ""); |
| 196 | build_opts.emplace((max_loc != nullptr) ? "-DLOCATE_MAX" : ""); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 197 | if(input->info()->data_type() == DataType::F32) |
| 198 | { |
| 199 | build_opts.emplace("-DIS_DATA_TYPE_FLOAT"); |
| 200 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | |
| 202 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 203 | _kernel = create_kernel(compile_context, "minmaxloc", build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | |
| 205 | // Set static arguments |
| 206 | unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters |
| 207 | _kernel.setArg(idx++, *min_max); |
| 208 | _kernel.setArg(idx++, *min_max_count); |
| 209 | if(min_loc != nullptr) |
| 210 | { |
| 211 | _kernel.setArg(idx++, min_loc->cl_buffer()); |
| 212 | _kernel.setArg<cl_uint>(idx++, min_loc->max_num_values()); |
| 213 | } |
| 214 | if(max_loc != nullptr) |
| 215 | { |
| 216 | _kernel.setArg(idx++, max_loc->cl_buffer()); |
| 217 | _kernel.setArg<cl_uint>(idx++, max_loc->max_num_values()); |
| 218 | } |
| 219 | |
| 220 | // Configure kernel window |
| 221 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 222 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 223 | 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] | 224 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void CLMinMaxLocationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 228 | { |
| 229 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 230 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 231 | |
| 232 | static const unsigned int zero_count = 0; |
| 233 | queue.enqueueWriteBuffer(*_min_max_count, CL_FALSE, 0 * sizeof(zero_count), sizeof(zero_count), &zero_count); |
| 234 | queue.enqueueWriteBuffer(*_min_max_count, CL_FALSE, 1 * sizeof(zero_count), sizeof(zero_count), &zero_count); |
| 235 | |
| 236 | Window slice = window.first_slice_window_2D(); |
| 237 | do |
| 238 | { |
| 239 | unsigned int idx = 0; |
| 240 | add_2D_tensor_argument(idx, _input, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 241 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 242 | } |
| 243 | while(window.slide_window_slice_2D(slice)); |
| 244 | } |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 245 | } // namespace arm_compute |