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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLMinMaxLocationKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 31 | #include "src/core/helpers/AutoConfiguration.h" |
| 32 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 33 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | #include <climits> |
| 36 | |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 39 | inline int32_t FloatFlip(float val) |
| 40 | { |
| 41 | static_assert(sizeof(float) == sizeof(int32_t), "Float must be same size as int32_t"); |
| 42 | int32_t int_val = 0; |
| 43 | |
| 44 | memcpy(&int_val, &val, sizeof(float)); |
| 45 | int_val = (int_val >= 0) ? int_val : int_val ^ 0x7FFFFFFF; |
| 46 | return int_val; |
| 47 | } |
| 48 | |
| 49 | inline float IFloatFlip(int32_t val) |
| 50 | { |
| 51 | static_assert(sizeof(float) == sizeof(int32_t), "Float must be same size as int32_t"); |
| 52 | float flt_val = 0.f; |
| 53 | |
| 54 | val = (val >= 0) ? val : val ^ 0x7FFFFFFF; |
| 55 | memcpy(&flt_val, &val, sizeof(float)); |
| 56 | return flt_val; |
| 57 | } |
| 58 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | CLMinMaxKernel::CLMinMaxKernel() |
| 60 | : _input(nullptr), _min_max(), _data_type_max_min() |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | void CLMinMaxKernel::configure(const ICLImage *input, cl::Buffer *min_max) |
| 65 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 66 | configure(CLKernelLibrary::get().get_compile_context(), input, min_max); |
| 67 | } |
| 68 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 69 | 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] | 70 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 71 | 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] | 72 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 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; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 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; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | default: |
| 94 | ARM_COMPUTE_ERROR("You called with the wrong image data types"); |
| 95 | } |
| 96 | |
| 97 | // Set kernel build options |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 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 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 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 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | |
| 117 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 118 | _kernel = create_kernel(compile_context, "minmax", build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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); |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 123 | _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] | 124 | |
| 125 | // Configure kernel window |
| 126 | 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] | 127 | 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] | 128 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void CLMinMaxKernel::run(const Window &window, cl::CommandQueue &queue) |
| 132 | { |
| 133 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 134 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 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 | |
| 139 | Window slice = window.first_slice_window_2D(); |
| 140 | do |
| 141 | { |
| 142 | unsigned int idx = 0; |
| 143 | add_2D_tensor_argument(idx, _input, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 144 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | } |
| 146 | while(window.slide_window_slice_2D(slice)); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 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 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | CLMinMaxLocationKernel::CLMinMaxLocationKernel() |
| 172 | : _input(nullptr), _min_max_count(nullptr) |
| 173 | { |
| 174 | } |
| 175 | |
| 176 | void CLMinMaxLocationKernel::configure(const ICLImage *input, cl::Buffer *min_max, cl::Buffer *min_max_count, ICLCoordinates2DArray *min_loc, ICLCoordinates2DArray *max_loc) |
| 177 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 178 | configure(CLKernelLibrary::get().get_compile_context(), input, min_max, min_max_count, min_loc, max_loc); |
| 179 | } |
| 180 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 181 | 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] | 182 | ICLCoordinates2DArray *max_loc) |
| 183 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 184 | 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] | 185 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 186 | ARM_COMPUTE_ERROR_ON(min_max == nullptr); |
| 187 | ARM_COMPUTE_ERROR_ON(min_max_count == nullptr && min_loc == nullptr && max_loc == nullptr); |
| 188 | |
| 189 | _input = input; |
| 190 | _min_max_count = min_max_count; |
| 191 | |
| 192 | // Set kernel build options |
| 193 | std::set<std::string> build_opts; |
| 194 | build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 195 | build_opts.emplace((min_max_count != nullptr) ? "-DCOUNT_MIN_MAX" : ""); |
| 196 | build_opts.emplace((min_loc != nullptr) ? "-DLOCATE_MIN" : ""); |
| 197 | build_opts.emplace((max_loc != nullptr) ? "-DLOCATE_MAX" : ""); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 198 | if(input->info()->data_type() == DataType::F32) |
| 199 | { |
| 200 | build_opts.emplace("-DIS_DATA_TYPE_FLOAT"); |
| 201 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 202 | |
| 203 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 204 | _kernel = create_kernel(compile_context, "minmaxloc", build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | |
| 206 | // Set static arguments |
| 207 | unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters |
| 208 | _kernel.setArg(idx++, *min_max); |
| 209 | _kernel.setArg(idx++, *min_max_count); |
| 210 | if(min_loc != nullptr) |
| 211 | { |
| 212 | _kernel.setArg(idx++, min_loc->cl_buffer()); |
| 213 | _kernel.setArg<cl_uint>(idx++, min_loc->max_num_values()); |
| 214 | } |
| 215 | if(max_loc != nullptr) |
| 216 | { |
| 217 | _kernel.setArg(idx++, max_loc->cl_buffer()); |
| 218 | _kernel.setArg<cl_uint>(idx++, max_loc->max_num_values()); |
| 219 | } |
| 220 | |
| 221 | // Configure kernel window |
| 222 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 223 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 224 | 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] | 225 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void CLMinMaxLocationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 229 | { |
| 230 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 231 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 232 | |
| 233 | static const unsigned int zero_count = 0; |
| 234 | queue.enqueueWriteBuffer(*_min_max_count, CL_FALSE, 0 * sizeof(zero_count), sizeof(zero_count), &zero_count); |
| 235 | queue.enqueueWriteBuffer(*_min_max_count, CL_FALSE, 1 * sizeof(zero_count), sizeof(zero_count), &zero_count); |
| 236 | |
| 237 | Window slice = window.first_slice_window_2D(); |
| 238 | do |
| 239 | { |
| 240 | unsigned int idx = 0; |
| 241 | add_2D_tensor_argument(idx, _input, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 242 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 243 | } |
| 244 | while(window.slide_window_slice_2D(slice)); |
| 245 | } |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 246 | } // namespace arm_compute |