blob: 4c2086c1c6fc5714dd81dd5875224aaa99da257f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * 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"
29#include "arm_compute/core/Error.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/IAccessWindow.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000034#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36#include <set>
37#include <string>
38
39using namespace arm_compute;
40
41CLFastCornersKernel::CLFastCornersKernel()
42 : ICLKernel(), _input(nullptr), _output(nullptr)
43{
44}
45
46BorderSize CLFastCornersKernel::border_size() const
47{
48 return BorderSize(3);
49}
50
51void CLFastCornersKernel::configure(const ICLImage *input, ICLImage *output, float threshold, bool non_max_suppression, BorderMode border_mode)
52{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010053 configure(CLKernelLibrary::get().get_compile_context(), input, output, threshold, non_max_suppression, border_mode);
54}
55
Manuel Bottini256c0b92020-04-21 13:29:30 +010056void 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 +010057{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
59 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
60 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
61 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
62 ARM_COMPUTE_ERROR_ON_MSG(border_mode != BorderMode::UNDEFINED, "Not implemented");
63
64 _input = input;
65 _output = output;
66
67 // Create build options
68 std::set<std::string> build_opts;
69
70 if(non_max_suppression)
71 {
72 build_opts.emplace("-DUSE_MAXSUPPRESSION");
73 }
74
75 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010076 const std::string kernel_name = std::string("fast_corners");
Manuel Bottini4c6bd512020-04-08 10:15:51 +010077 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078
79 // Set static kernel arguments
80 unsigned int idx = 2 * num_arguments_per_2D_tensor(); // Skip the input and output parameters
81 _kernel.setArg<cl_float>(idx, static_cast<float>(threshold));
82
83 // Configure kernel window
84 constexpr unsigned int num_elems_processed_per_iteration = 1;
85 constexpr unsigned int num_elems_read_per_iteration = 7;
86 constexpr unsigned int num_rows_read_per_iteration = 3;
87
88 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_mode == BorderMode::UNDEFINED, BorderSize(3));
89
90 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
91 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
92
93 update_window_and_padding(win, input_access, output_access);
94
95 output_access.set_valid_region(win, input->info()->valid_region(), border_mode == BorderMode::UNDEFINED, border_size());
96
Anthony Barbierb6eb3532018-08-08 13:20:04 +010097 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010098
99 // Set config_id for enabling LWS tuning
100 _config_id = kernel_name;
101 _config_id += "_";
102 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
103 _config_id += "_";
104 _config_id += support::cpp11::to_string(input->info()->dimension(0));
105 _config_id += "_";
106 _config_id += support::cpp11::to_string(input->info()->dimension(1));
107 _config_id += "_";
108 _config_id += support::cpp11::to_string(output->info()->dimension(0));
109 _config_id += "_";
110 _config_id += support::cpp11::to_string(output->info()->dimension(1));
111 _config_id += "_";
112 _config_id += support::cpp11::to_string(non_max_suppression);
113 _config_id += "_";
114 _config_id += lower_string(string_from_border_mode(border_mode));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115}
116
117void CLFastCornersKernel::run(const Window &window, cl::CommandQueue &queue)
118{
119 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
120 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
121
122 Window slice = window.first_slice_window_2D();
123
124 do
125 {
126 unsigned int idx = 0;
127 add_2D_tensor_argument(idx, _input, slice);
128 add_2D_tensor_argument(idx, _output, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100129 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 }
131 while(window.slide_window_slice_2D(slice));
132}
133
134CLCopyToArrayKernel::CLCopyToArrayKernel()
135 : ICLKernel(), _input(nullptr), _corners(nullptr), _num_buffer(nullptr)
136{
137}
138
139void CLCopyToArrayKernel::configure(const ICLImage *input, bool update_number, ICLKeyPointArray *corners, cl::Buffer *num_buffers)
140{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100141 configure(CLKernelLibrary::get().get_compile_context(), input, update_number, corners, num_buffers);
142}
143
Manuel Bottini256c0b92020-04-21 13:29:30 +0100144void 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 +0100145{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
147 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
148 ARM_COMPUTE_ERROR_ON(corners == nullptr);
149 ARM_COMPUTE_ERROR_ON(num_buffers == nullptr);
150
151 _input = input;
152 _corners = corners;
153 _num_buffer = num_buffers;
154
155 std::set<std::string> build_opts;
156
157 if(update_number)
158 {
159 build_opts.emplace("-DUPDATE_NUMBER");
160 }
161
162 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100163 const std::string kernel_name = std::string("copy_to_keypoint");
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100164 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
166 //Get how many pixels skipped in the x dimension in the previous stages
167 unsigned int offset = _input->info()->valid_region().anchor.x();
168
169 // Set static kernel arguments
170 unsigned int idx = num_arguments_per_2D_tensor(); // Skip the input and output parameters
Abe Mbise25a340f2017-12-19 13:00:58 +0000171 _kernel.setArg<unsigned int>(idx++, _corners->max_num_values());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 _kernel.setArg<cl_uint>(idx++, offset);
173 _kernel.setArg(idx++, *_num_buffer);
174 _kernel.setArg(idx++, _corners->cl_buffer());
175
176 // Configure kernel window
177 constexpr unsigned int num_elems_processed_per_iteration = 1;
178 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
179 update_window_and_padding(win,
180 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100181 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100182
183 // Set config_id for enabling LWS tuning
184 _config_id = kernel_name;
185 _config_id += "_";
186 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
187 _config_id += "_";
188 _config_id += support::cpp11::to_string(input->info()->dimension(0));
189 _config_id += "_";
190 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191}
192
193void CLCopyToArrayKernel::run(const Window &window, cl::CommandQueue &queue)
194{
195 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
196 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
197
198 //Initialise the _num_buffer as it used as both input and output
199 static const unsigned int zero_init = 0;
200 queue.enqueueWriteBuffer(*_num_buffer, CL_FALSE, 0, sizeof(unsigned int), &zero_init);
201
202 Window slice = window.first_slice_window_2D();
203
204 do
205 {
206 unsigned int idx = 0;
207 add_2D_tensor_argument(idx, _input, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100208 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 }
210 while(window.slide_window_slice_2D(slice));
211}