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