blob: ebdfd2741f9727f2373469301357cb44f01741ea [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-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 */
24#include "arm_compute/core/CL/kernels/CLFastCornersKernel.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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000032#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34#include <set>
35#include <string>
36
37using namespace arm_compute;
38
39CLFastCornersKernel::CLFastCornersKernel()
40 : ICLKernel(), _input(nullptr), _output(nullptr)
41{
42}
43
44BorderSize CLFastCornersKernel::border_size() const
45{
46 return BorderSize(3);
47}
48
49void CLFastCornersKernel::configure(const ICLImage *input, ICLImage *output, float threshold, bool non_max_suppression, BorderMode border_mode)
50{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010051 configure(CLKernelLibrary::get().get_compile_context(), input, output, threshold, non_max_suppression, border_mode);
52}
53
Manuel Bottini256c0b92020-04-21 13:29:30 +010054void CLFastCornersKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, ICLImage *output, float threshold, bool non_max_suppression, BorderMode border_mode)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010055{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
57 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
58 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
59 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
60 ARM_COMPUTE_ERROR_ON_MSG(border_mode != BorderMode::UNDEFINED, "Not implemented");
61
62 _input = input;
63 _output = output;
64
65 // Create build options
66 std::set<std::string> build_opts;
67
68 if(non_max_suppression)
69 {
70 build_opts.emplace("-DUSE_MAXSUPPRESSION");
71 }
72
73 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010074 const std::string kernel_name = std::string("fast_corners");
Manuel Bottini4c6bd512020-04-08 10:15:51 +010075 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
77 // Set static kernel arguments
78 unsigned int idx = 2 * num_arguments_per_2D_tensor(); // Skip the input and output parameters
79 _kernel.setArg<cl_float>(idx, static_cast<float>(threshold));
80
81 // Configure kernel window
82 constexpr unsigned int num_elems_processed_per_iteration = 1;
83 constexpr unsigned int num_elems_read_per_iteration = 7;
84 constexpr unsigned int num_rows_read_per_iteration = 3;
85
86 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_mode == BorderMode::UNDEFINED, BorderSize(3));
87
88 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
89 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
90
91 update_window_and_padding(win, input_access, output_access);
92
93 output_access.set_valid_region(win, input->info()->valid_region(), border_mode == BorderMode::UNDEFINED, border_size());
94
Anthony Barbierb6eb3532018-08-08 13:20:04 +010095 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010096
97 // Set config_id for enabling LWS tuning
98 _config_id = kernel_name;
99 _config_id += "_";
100 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
101 _config_id += "_";
102 _config_id += support::cpp11::to_string(input->info()->dimension(0));
103 _config_id += "_";
104 _config_id += support::cpp11::to_string(input->info()->dimension(1));
105 _config_id += "_";
106 _config_id += support::cpp11::to_string(output->info()->dimension(0));
107 _config_id += "_";
108 _config_id += support::cpp11::to_string(output->info()->dimension(1));
109 _config_id += "_";
110 _config_id += support::cpp11::to_string(non_max_suppression);
111 _config_id += "_";
112 _config_id += lower_string(string_from_border_mode(border_mode));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113}
114
115void CLFastCornersKernel::run(const Window &window, cl::CommandQueue &queue)
116{
117 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
118 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
119
120 Window slice = window.first_slice_window_2D();
121
122 do
123 {
124 unsigned int idx = 0;
125 add_2D_tensor_argument(idx, _input, slice);
126 add_2D_tensor_argument(idx, _output, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100127 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 }
129 while(window.slide_window_slice_2D(slice));
130}
131
132CLCopyToArrayKernel::CLCopyToArrayKernel()
133 : ICLKernel(), _input(nullptr), _corners(nullptr), _num_buffer(nullptr)
134{
135}
136
137void CLCopyToArrayKernel::configure(const ICLImage *input, bool update_number, ICLKeyPointArray *corners, cl::Buffer *num_buffers)
138{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100139 configure(CLKernelLibrary::get().get_compile_context(), input, update_number, corners, num_buffers);
140}
141
Manuel Bottini256c0b92020-04-21 13:29:30 +0100142void CLCopyToArrayKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, bool update_number, ICLKeyPointArray *corners, cl::Buffer *num_buffers)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100143{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
145 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
146 ARM_COMPUTE_ERROR_ON(corners == nullptr);
147 ARM_COMPUTE_ERROR_ON(num_buffers == nullptr);
148
149 _input = input;
150 _corners = corners;
151 _num_buffer = num_buffers;
152
153 std::set<std::string> build_opts;
154
155 if(update_number)
156 {
157 build_opts.emplace("-DUPDATE_NUMBER");
158 }
159
160 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100161 const std::string kernel_name = std::string("copy_to_keypoint");
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100162 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163
164 //Get how many pixels skipped in the x dimension in the previous stages
165 unsigned int offset = _input->info()->valid_region().anchor.x();
166
167 // Set static kernel arguments
168 unsigned int idx = num_arguments_per_2D_tensor(); // Skip the input and output parameters
Abe Mbise25a340f2017-12-19 13:00:58 +0000169 _kernel.setArg<unsigned int>(idx++, _corners->max_num_values());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 _kernel.setArg<cl_uint>(idx++, offset);
171 _kernel.setArg(idx++, *_num_buffer);
172 _kernel.setArg(idx++, _corners->cl_buffer());
173
174 // Configure kernel window
175 constexpr unsigned int num_elems_processed_per_iteration = 1;
176 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
177 update_window_and_padding(win,
178 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100179 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100180
181 // Set config_id for enabling LWS tuning
182 _config_id = kernel_name;
183 _config_id += "_";
184 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
185 _config_id += "_";
186 _config_id += support::cpp11::to_string(input->info()->dimension(0));
187 _config_id += "_";
188 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189}
190
191void CLCopyToArrayKernel::run(const Window &window, cl::CommandQueue &queue)
192{
193 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
194 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
195
196 //Initialise the _num_buffer as it used as both input and output
197 static const unsigned int zero_init = 0;
198 queue.enqueueWriteBuffer(*_num_buffer, CL_FALSE, 0, sizeof(unsigned int), &zero_init);
199
200 Window slice = window.first_slice_window_2D();
201
202 do
203 {
204 unsigned int idx = 0;
205 add_2D_tensor_argument(idx, _input, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100206 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207 }
208 while(window.slide_window_slice_2D(slice));
209}