blob: 79ab3b7197908995ef64b1ac8a78c63154f01eb4 [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{
53 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
54 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
55 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
56 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
57 ARM_COMPUTE_ERROR_ON_MSG(border_mode != BorderMode::UNDEFINED, "Not implemented");
58
59 _input = input;
60 _output = output;
61
62 // Create build options
63 std::set<std::string> build_opts;
64
65 if(non_max_suppression)
66 {
67 build_opts.emplace("-DUSE_MAXSUPPRESSION");
68 }
69
70 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010071 const std::string kernel_name = std::string("fast_corners");
72 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073
74 // Set static kernel arguments
75 unsigned int idx = 2 * num_arguments_per_2D_tensor(); // Skip the input and output parameters
76 _kernel.setArg<cl_float>(idx, static_cast<float>(threshold));
77
78 // Configure kernel window
79 constexpr unsigned int num_elems_processed_per_iteration = 1;
80 constexpr unsigned int num_elems_read_per_iteration = 7;
81 constexpr unsigned int num_rows_read_per_iteration = 3;
82
83 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_mode == BorderMode::UNDEFINED, BorderSize(3));
84
85 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
86 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
87
88 update_window_and_padding(win, input_access, output_access);
89
90 output_access.set_valid_region(win, input->info()->valid_region(), border_mode == BorderMode::UNDEFINED, border_size());
91
Anthony Barbierb6eb3532018-08-08 13:20:04 +010092 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010093
94 // Set config_id for enabling LWS tuning
95 _config_id = kernel_name;
96 _config_id += "_";
97 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
98 _config_id += "_";
99 _config_id += support::cpp11::to_string(input->info()->dimension(0));
100 _config_id += "_";
101 _config_id += support::cpp11::to_string(input->info()->dimension(1));
102 _config_id += "_";
103 _config_id += support::cpp11::to_string(output->info()->dimension(0));
104 _config_id += "_";
105 _config_id += support::cpp11::to_string(output->info()->dimension(1));
106 _config_id += "_";
107 _config_id += support::cpp11::to_string(non_max_suppression);
108 _config_id += "_";
109 _config_id += lower_string(string_from_border_mode(border_mode));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110}
111
112void CLFastCornersKernel::run(const Window &window, cl::CommandQueue &queue)
113{
114 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
115 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
116
117 Window slice = window.first_slice_window_2D();
118
119 do
120 {
121 unsigned int idx = 0;
122 add_2D_tensor_argument(idx, _input, slice);
123 add_2D_tensor_argument(idx, _output, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100124 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 }
126 while(window.slide_window_slice_2D(slice));
127}
128
129CLCopyToArrayKernel::CLCopyToArrayKernel()
130 : ICLKernel(), _input(nullptr), _corners(nullptr), _num_buffer(nullptr)
131{
132}
133
134void CLCopyToArrayKernel::configure(const ICLImage *input, bool update_number, ICLKeyPointArray *corners, cl::Buffer *num_buffers)
135{
136 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
137 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
138 ARM_COMPUTE_ERROR_ON(corners == nullptr);
139 ARM_COMPUTE_ERROR_ON(num_buffers == nullptr);
140
141 _input = input;
142 _corners = corners;
143 _num_buffer = num_buffers;
144
145 std::set<std::string> build_opts;
146
147 if(update_number)
148 {
149 build_opts.emplace("-DUPDATE_NUMBER");
150 }
151
152 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100153 const std::string kernel_name = std::string("copy_to_keypoint");
154 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
156 //Get how many pixels skipped in the x dimension in the previous stages
157 unsigned int offset = _input->info()->valid_region().anchor.x();
158
159 // Set static kernel arguments
160 unsigned int idx = num_arguments_per_2D_tensor(); // Skip the input and output parameters
Abe Mbise25a340f2017-12-19 13:00:58 +0000161 _kernel.setArg<unsigned int>(idx++, _corners->max_num_values());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 _kernel.setArg<cl_uint>(idx++, offset);
163 _kernel.setArg(idx++, *_num_buffer);
164 _kernel.setArg(idx++, _corners->cl_buffer());
165
166 // Configure kernel window
167 constexpr unsigned int num_elems_processed_per_iteration = 1;
168 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
169 update_window_and_padding(win,
170 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100171 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100172
173 // Set config_id for enabling LWS tuning
174 _config_id = kernel_name;
175 _config_id += "_";
176 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
177 _config_id += "_";
178 _config_id += support::cpp11::to_string(input->info()->dimension(0));
179 _config_id += "_";
180 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181}
182
183void CLCopyToArrayKernel::run(const Window &window, cl::CommandQueue &queue)
184{
185 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
186 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
187
188 //Initialise the _num_buffer as it used as both input and output
189 static const unsigned int zero_init = 0;
190 queue.enqueueWriteBuffer(*_num_buffer, CL_FALSE, 0, sizeof(unsigned int), &zero_init);
191
192 Window slice = window.first_slice_window_2D();
193
194 do
195 {
196 unsigned int idx = 0;
197 add_2D_tensor_argument(idx, _input, slice);
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100198 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 }
200 while(window.slide_window_slice_2D(slice));
201}