Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 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 Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 34 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
| 36 | #include <set> |
| 37 | #include <string> |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | CLFastCornersKernel::CLFastCornersKernel() |
| 42 | : ICLKernel(), _input(nullptr), _output(nullptr) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | BorderSize CLFastCornersKernel::border_size() const |
| 47 | { |
| 48 | return BorderSize(3); |
| 49 | } |
| 50 | |
| 51 | void CLFastCornersKernel::configure(const ICLImage *input, ICLImage *output, float threshold, bool non_max_suppression, BorderMode border_mode) |
| 52 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 53 | configure(CLKernelLibrary::get().get_compile_context(), input, output, threshold, non_max_suppression, border_mode); |
| 54 | } |
| 55 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 56 | void CLFastCornersKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, ICLImage *output, float threshold, bool non_max_suppression, BorderMode border_mode) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 57 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | 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 Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 76 | const std::string kernel_name = std::string("fast_corners"); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 77 | _kernel = create_kernel(compile_context, kernel_name, build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | |
| 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 Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 97 | ICLKernel::configure_internal(win); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 98 | |
| 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void 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 Giorgio | 6b9f388 | 2019-07-01 16:37:04 +0100 | [diff] [blame] | 129 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | } |
| 131 | while(window.slide_window_slice_2D(slice)); |
| 132 | } |
| 133 | |
| 134 | CLCopyToArrayKernel::CLCopyToArrayKernel() |
| 135 | : ICLKernel(), _input(nullptr), _corners(nullptr), _num_buffer(nullptr) |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | void CLCopyToArrayKernel::configure(const ICLImage *input, bool update_number, ICLKeyPointArray *corners, cl::Buffer *num_buffers) |
| 140 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 141 | configure(CLKernelLibrary::get().get_compile_context(), input, update_number, corners, num_buffers); |
| 142 | } |
| 143 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 144 | void CLCopyToArrayKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, bool update_number, ICLKeyPointArray *corners, cl::Buffer *num_buffers) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 145 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | 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 Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 163 | const std::string kernel_name = std::string("copy_to_keypoint"); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 164 | _kernel = create_kernel(compile_context, kernel_name, build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | |
| 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 Mbise | 25a340f | 2017-12-19 13:00:58 +0000 | [diff] [blame] | 171 | _kernel.setArg<unsigned int>(idx++, _corners->max_num_values()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | _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 Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 181 | ICLKernel::configure_internal(win); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 182 | |
| 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void 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 Giorgio | 6b9f388 | 2019-07-01 16:37:04 +0100 | [diff] [blame] | 208 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | } |
| 210 | while(window.slide_window_slice_2D(slice)); |
| 211 | } |