blob: b8a4e8619de80598fadb9079ba24da28720c73a9 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010056 configure(CLKernelLibrary::get().get_compile_context(), input, output);
57}
58
Manuel Bottini679fc962020-04-21 16:08:53 +010059void CLHistogramKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, ICLDistribution1D *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010060{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
62 ARM_COMPUTE_ERROR_ON(nullptr == output);
63
64 // Check input size
65 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
66
67 // Check offset
68 ARM_COMPUTE_ERROR_ON_MSG(0 > output->offset() || output->offset() > 256, "Offset is larger than the image value range.");
69
70 // Check range
71 ARM_COMPUTE_ERROR_ON_MSG(output->range() > 256 /* max range */, "Range larger than the image value range.");
72
73 _input = input;
74 _output = output;
75
76 if(_input->info()->dimension(0) < pixels_per_item)
77 {
78 return;
79 }
80
81 unsigned int num_bins = _output->num_bins();
82 unsigned int window_size = _output->window();
83 unsigned int offset = _output->offset();
84 unsigned int range = _output->range();
85 unsigned int offrange = offset + range;
86 unsigned int bin_size = _output->size();
87 unsigned int buffer_size = bin_size + 1; // We need one extra place for pixels that don't meet the conditions
88
89 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010090 bool is_fixed_size = (256 == num_bins) && (1 == window_size) && (0 == offset) && (256 == offrange);
91 const std::string kernel_name = is_fixed_size ? "hist_local_kernel_fixed" : "hist_local_kernel";
Manuel Bottini4c6bd512020-04-08 10:15:51 +010092 _kernel = create_kernel(compile_context, kernel_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
94 // Set static kernel arguments
95 unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters
96 _kernel.setArg(idx++, buffer_size, nullptr);
97 _kernel.setArg(idx++, _output->cl_buffer());
98 if(!is_fixed_size)
99 {
100 _kernel.setArg<cl_uint>(idx++, num_bins);
101 _kernel.setArg<cl_uint>(idx++, offset);
102 _kernel.setArg<cl_uint>(idx++, range);
103 _kernel.setArg<cl_uint>(idx++, offrange);
104 }
105
106 // We only run histogram on Image, therefore only 2 dimensions here
107 unsigned int end_position = (_input->info()->dimension(0) / pixels_per_item) * pixels_per_item;
108
109 // Configure kernel window
110 Window win;
111 win.set(0, Window::Dimension(0, end_position, pixels_per_item));
112 win.set(1, Window::Dimension(0, _input->info()->dimension(1)));
113
114 update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, pixels_per_item));
115
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100116 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100117
118 // Set config_id for enabling LWS tuning
119 _config_id = kernel_name;
120 _config_id += "_";
121 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
122 _config_id += "_";
123 _config_id += support::cpp11::to_string(input->info()->dimension(0));
124 _config_id += "_";
125 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126}
127
128void CLHistogramKernel::run(const Window &window, cl::CommandQueue &queue)
129{
130 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
131 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
132
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100133 // TODO (COMPMID-679): Add CLMemFill
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 _output->map(queue, true);
135 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
136 memset(_output->buffer(), 0, _output->size());
137 _output->unmap(queue);
138
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100139 if(_input->info()->dimension(0) < pixels_per_item)
140 {
141 return;
142 }
143
144 Window slice = window.first_slice_window_2D();
145 const unsigned int gws_x = (window.x().end() - window.x().start()) / window.x().step();
146 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 +0100147
148 do
149 {
150 /* Run the core part which has width can be divided by 16 */
151 unsigned int idx = 0;
152 add_2D_tensor_argument(idx, _input, slice);
153
154 enqueue(queue, *this, slice, lws);
155 }
156 while(window.slide_window_slice_2D(slice));
157}
158
159CLHistogramBorderKernel::CLHistogramBorderKernel()
160 : _input(nullptr), _output(nullptr)
161{
162}
163
164void CLHistogramBorderKernel::configure(const ICLImage *input, ICLDistribution1D *output)
165{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100166 configure(CLKernelLibrary::get().get_compile_context(), input, output);
167}
168
Manuel Bottini679fc962020-04-21 16:08:53 +0100169void CLHistogramBorderKernel::configure(const CLCompileContext &compile_context, const ICLImage *input, ICLDistribution1D *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100170{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
172 ARM_COMPUTE_ERROR_ON(nullptr == output);
173
174 // Check input size
175 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
176
177 // Check offset
178 ARM_COMPUTE_ERROR_ON_MSG(0 > output->offset() || output->offset() > 256, "Offset is larger than the image value range.");
179
180 // Check range
181 ARM_COMPUTE_ERROR_ON_MSG(output->range() > 256 /* max range */, "Range larger than the image value range.");
182
183 // We only run histogram on Image, therefore only 2 dimensions here
184 unsigned int start_position = (input->info()->dimension(0) / pixels_per_item) * pixels_per_item;
185
186 if(start_position >= input->info()->dimension(0))
187 {
188 return; // no need to run histogram border kernel
189 }
190
191 _input = input;
192 _output = output;
193
194 unsigned int num_bins = _output->num_bins();
195 unsigned int window_size = _output->window();
196 unsigned int offset = _output->offset();
197 unsigned int range = _output->range();
198 unsigned int offrange = offset + range;
199
200 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100201 bool is_fixed_size = (256 == num_bins) && (1 == window_size) && (0 == offset) && (256 == offrange);
202 const std::string kernel_name = is_fixed_size ? "hist_border_kernel_fixed" : "hist_border_kernel";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100203 _kernel = create_kernel(compile_context, kernel_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204
205 // Set static kernel arguments
206 unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters
207 _kernel.setArg(idx++, _output->cl_buffer());
208 if(!is_fixed_size)
209 {
210 _kernel.setArg<cl_uint>(idx++, num_bins);
211 _kernel.setArg<cl_uint>(idx++, offset);
212 _kernel.setArg<cl_uint>(idx++, range);
213 _kernel.setArg<cl_uint>(idx++, offrange);
214 }
215
216 // Configure kernel window
217 Window win;
218 win.set(0, Window::Dimension(start_position, _input->info()->dimension(0)));
219 win.set(1, Window::Dimension(0, _input->info()->dimension(1)));
220 update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, 1));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100221 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100222
223 // Set config_id for enabling LWS tuning
224 _config_id = kernel_name;
225 _config_id += "_";
226 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
227 _config_id += "_";
228 _config_id += support::cpp11::to_string(input->info()->dimension(0));
229 _config_id += "_";
230 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231}
232
233void CLHistogramBorderKernel::run(const Window &window, cl::CommandQueue &queue)
234{
235 if(window.x().start() >= window.x().end())
236 {
237 return;
238 }
239
240 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
241
242 cl::NDRange lws = cl::NDRange(1, 1);
243
244 Window slice = window.first_slice_window_2D();
245
246 do
247 {
248 /* Run the border part which has width cannot be divided by 16 */
249 unsigned int idx = 0;
250 add_2D_tensor_argument(idx, _input, slice);
251
252 enqueue(queue, *this, slice, lws);
253 }
254 while(window.slide_window_slice_2D(slice));
255}