blob: 675cfc19a9a1d4c9804cce8591a20e9b80fc582d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLMinMaxLocationKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
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 Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000033#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35#include <climits>
36
Moritz Pflanzer4726fdf2017-09-23 10:47:54 +010037namespace arm_compute
38{
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +010039inline 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
49inline 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 Barbier6ff3b192017-09-04 18:44:23 +010059CLMinMaxKernel::CLMinMaxKernel()
60 : _input(nullptr), _min_max(), _data_type_max_min()
61{
62}
63
64void CLMinMaxKernel::configure(const ICLImage *input, cl::Buffer *min_max)
65{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010066 configure(CLKernelLibrary::get().get_compile_context(), input, min_max);
67}
68
Manuel Bottini679fc962020-04-21 16:08:53 +010069void CLMinMaxKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, cl::Buffer *min_max)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010070{
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +010071 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 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 Giorgioef4b4ae2017-07-04 17:19:43 +010089 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 Barbier6ff3b192017-09-04 18:44:23 +010093 default:
94 ARM_COMPUTE_ERROR("You called with the wrong image data types");
95 }
96
97 // Set kernel build options
Moritz Pflanzer4726fdf2017-09-23 10:47:54 +010098 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 Giorgioef4b4ae2017-07-04 17:19:43 +0100105 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 Barbier6ff3b192017-09-04 18:44:23 +0100116
117 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100118 _kernel = create_kernel(compile_context, "minmax", build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
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 Pflanzer4726fdf2017-09-23 10:47:54 +0100123 _kernel.setArg<cl_int>(idx++, static_cast<cl_int>(input->info()->dimension(0)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
125 // Configure kernel window
126 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
Moritz Pflanzer4726fdf2017-09-23 10:47:54 +0100127 update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, ceil_to_multiple(num_elems_processed_per_iteration, 16)));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100128 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129}
130
131void 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 Pinitas275f99c2019-08-23 12:44:11 +0100144 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 }
146 while(window.slide_window_slice_2D(slice));
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100147
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 Barbier6ff3b192017-09-04 18:44:23 +0100169}
170
171CLMinMaxLocationKernel::CLMinMaxLocationKernel()
172 : _input(nullptr), _min_max_count(nullptr)
173{
174}
175
176void CLMinMaxLocationKernel::configure(const ICLImage *input, cl::Buffer *min_max, cl::Buffer *min_max_count, ICLCoordinates2DArray *min_loc, ICLCoordinates2DArray *max_loc)
177{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100178 configure(CLKernelLibrary::get().get_compile_context(), input, min_max, min_max_count, min_loc, max_loc);
179}
180
Manuel Bottini679fc962020-04-21 16:08:53 +0100181void CLMinMaxLocationKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, cl::Buffer *min_max, cl::Buffer *min_max_count, ICLCoordinates2DArray *min_loc,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100182 ICLCoordinates2DArray *max_loc)
183{
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100184 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 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 Giorgioef4b4ae2017-07-04 17:19:43 +0100198 if(input->info()->data_type() == DataType::F32)
199 {
200 build_opts.emplace("-DIS_DATA_TYPE_FLOAT");
201 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202
203 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100204 _kernel = create_kernel(compile_context, "minmaxloc", build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205
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 Barbierb6eb3532018-08-08 13:20:04 +0100225 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226}
227
228void 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 Pinitas275f99c2019-08-23 12:44:11 +0100242 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243 }
244 while(window.slide_window_slice_2D(slice));
245}
Moritz Pflanzer4726fdf2017-09-23 10:47:54 +0100246} // namespace arm_compute