blob: adb998d2f56766c98b8a2d83cd87b6d053bab657 [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/CLHistogramKernel.h"
25
26#include "arm_compute/core/CL/CLKernelLibrary.h"
27#include "arm_compute/core/CL/ICLDistribution1D.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38#include <cstring>
39#include <string>
40
41using namespace arm_compute;
42
43// each thread handle 16 pixels
44constexpr signed int pixels_per_item = 16;
45
46// local work group size in X dimension
47constexpr unsigned int local_x_size = 16;
48
49CLHistogramKernel::CLHistogramKernel()
50 : _input(nullptr), _output(nullptr)
51{
52}
53
54void CLHistogramKernel::configure(const ICLImage *input, ICLDistribution1D *output)
55{
56 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
57 ARM_COMPUTE_ERROR_ON(nullptr == output);
58
59 // Check input size
60 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
61
62 // Check offset
63 ARM_COMPUTE_ERROR_ON_MSG(0 > output->offset() || output->offset() > 256, "Offset is larger than the image value range.");
64
65 // Check range
66 ARM_COMPUTE_ERROR_ON_MSG(output->range() > 256 /* max range */, "Range larger than the image value range.");
67
68 _input = input;
69 _output = output;
70
71 if(_input->info()->dimension(0) < pixels_per_item)
72 {
73 return;
74 }
75
76 unsigned int num_bins = _output->num_bins();
77 unsigned int window_size = _output->window();
78 unsigned int offset = _output->offset();
79 unsigned int range = _output->range();
80 unsigned int offrange = offset + range;
81 unsigned int bin_size = _output->size();
82 unsigned int buffer_size = bin_size + 1; // We need one extra place for pixels that don't meet the conditions
83
84 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010085 bool is_fixed_size = (256 == num_bins) && (1 == window_size) && (0 == offset) && (256 == offrange);
86 const std::string kernel_name = is_fixed_size ? "hist_local_kernel_fixed" : "hist_local_kernel";
87 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
89 // Set static kernel arguments
90 unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters
91 _kernel.setArg(idx++, buffer_size, nullptr);
92 _kernel.setArg(idx++, _output->cl_buffer());
93 if(!is_fixed_size)
94 {
95 _kernel.setArg<cl_uint>(idx++, num_bins);
96 _kernel.setArg<cl_uint>(idx++, offset);
97 _kernel.setArg<cl_uint>(idx++, range);
98 _kernel.setArg<cl_uint>(idx++, offrange);
99 }
100
101 // We only run histogram on Image, therefore only 2 dimensions here
102 unsigned int end_position = (_input->info()->dimension(0) / pixels_per_item) * pixels_per_item;
103
104 // Configure kernel window
105 Window win;
106 win.set(0, Window::Dimension(0, end_position, pixels_per_item));
107 win.set(1, Window::Dimension(0, _input->info()->dimension(1)));
108
109 update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, pixels_per_item));
110
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100111 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100112
113 // Set config_id for enabling LWS tuning
114 _config_id = kernel_name;
115 _config_id += "_";
116 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
117 _config_id += "_";
118 _config_id += support::cpp11::to_string(input->info()->dimension(0));
119 _config_id += "_";
120 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121}
122
123void CLHistogramKernel::run(const Window &window, cl::CommandQueue &queue)
124{
125 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
126 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
127
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100128 // TODO (COMPMID-679): Add CLMemFill
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 _output->map(queue, true);
130 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
131 memset(_output->buffer(), 0, _output->size());
132 _output->unmap(queue);
133
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100134 if(_input->info()->dimension(0) < pixels_per_item)
135 {
136 return;
137 }
138
139 Window slice = window.first_slice_window_2D();
140 const unsigned int gws_x = (window.x().end() - window.x().start()) / window.x().step();
141 cl::NDRange lws = (local_x_size < gws_x) ? cl::NDRange(local_x_size, 1) : cl::NDRange(1, 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142
143 do
144 {
145 /* Run the core part which has width can be divided by 16 */
146 unsigned int idx = 0;
147 add_2D_tensor_argument(idx, _input, slice);
148
149 enqueue(queue, *this, slice, lws);
150 }
151 while(window.slide_window_slice_2D(slice));
152}
153
154CLHistogramBorderKernel::CLHistogramBorderKernel()
155 : _input(nullptr), _output(nullptr)
156{
157}
158
159void CLHistogramBorderKernel::configure(const ICLImage *input, ICLDistribution1D *output)
160{
161 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
162 ARM_COMPUTE_ERROR_ON(nullptr == output);
163
164 // Check input size
165 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
166
167 // Check offset
168 ARM_COMPUTE_ERROR_ON_MSG(0 > output->offset() || output->offset() > 256, "Offset is larger than the image value range.");
169
170 // Check range
171 ARM_COMPUTE_ERROR_ON_MSG(output->range() > 256 /* max range */, "Range larger than the image value range.");
172
173 // We only run histogram on Image, therefore only 2 dimensions here
174 unsigned int start_position = (input->info()->dimension(0) / pixels_per_item) * pixels_per_item;
175
176 if(start_position >= input->info()->dimension(0))
177 {
178 return; // no need to run histogram border kernel
179 }
180
181 _input = input;
182 _output = output;
183
184 unsigned int num_bins = _output->num_bins();
185 unsigned int window_size = _output->window();
186 unsigned int offset = _output->offset();
187 unsigned int range = _output->range();
188 unsigned int offrange = offset + range;
189
190 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100191 bool is_fixed_size = (256 == num_bins) && (1 == window_size) && (0 == offset) && (256 == offrange);
192 const std::string kernel_name = is_fixed_size ? "hist_border_kernel_fixed" : "hist_border_kernel";
193 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194
195 // Set static kernel arguments
196 unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters
197 _kernel.setArg(idx++, _output->cl_buffer());
198 if(!is_fixed_size)
199 {
200 _kernel.setArg<cl_uint>(idx++, num_bins);
201 _kernel.setArg<cl_uint>(idx++, offset);
202 _kernel.setArg<cl_uint>(idx++, range);
203 _kernel.setArg<cl_uint>(idx++, offrange);
204 }
205
206 // Configure kernel window
207 Window win;
208 win.set(0, Window::Dimension(start_position, _input->info()->dimension(0)));
209 win.set(1, Window::Dimension(0, _input->info()->dimension(1)));
210 update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, 1));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100211 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100212
213 // Set config_id for enabling LWS tuning
214 _config_id = kernel_name;
215 _config_id += "_";
216 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
217 _config_id += "_";
218 _config_id += support::cpp11::to_string(input->info()->dimension(0));
219 _config_id += "_";
220 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221}
222
223void CLHistogramBorderKernel::run(const Window &window, cl::CommandQueue &queue)
224{
225 if(window.x().start() >= window.x().end())
226 {
227 return;
228 }
229
230 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
231
232 cl::NDRange lws = cl::NDRange(1, 1);
233
234 Window slice = window.first_slice_window_2D();
235
236 do
237 {
238 /* Run the border part which has width cannot be divided by 16 */
239 unsigned int idx = 0;
240 add_2D_tensor_argument(idx, _input, slice);
241
242 enqueue(queue, *this, slice, lws);
243 }
244 while(window.slide_window_slice_2D(slice));
245}