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