blob: 7b715abb36eb2f35477a0d58a9a7044ee09cb047 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
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/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
84 bool is_fixed_size = (256 == num_bins) && (1 == window_size) && (0 == offset) && (256 == offrange);
85 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));
87
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
110 ICLKernel::configure(win);
111}
112
113void CLHistogramKernel::run(const Window &window, cl::CommandQueue &queue)
114{
115 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
116 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
117
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100118 // TODO (COMPMID-679): Add CLMemFill
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 _output->map(queue, true);
120 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
121 memset(_output->buffer(), 0, _output->size());
122 _output->unmap(queue);
123
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100124 if(_input->info()->dimension(0) < pixels_per_item)
125 {
126 return;
127 }
128
129 Window slice = window.first_slice_window_2D();
130 const unsigned int gws_x = (window.x().end() - window.x().start()) / window.x().step();
131 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 +0100132
133 do
134 {
135 /* Run the core part which has width can be divided by 16 */
136 unsigned int idx = 0;
137 add_2D_tensor_argument(idx, _input, slice);
138
139 enqueue(queue, *this, slice, lws);
140 }
141 while(window.slide_window_slice_2D(slice));
142}
143
144CLHistogramBorderKernel::CLHistogramBorderKernel()
145 : _input(nullptr), _output(nullptr)
146{
147}
148
149void CLHistogramBorderKernel::configure(const ICLImage *input, ICLDistribution1D *output)
150{
151 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
152 ARM_COMPUTE_ERROR_ON(nullptr == output);
153
154 // Check input size
155 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
156
157 // Check offset
158 ARM_COMPUTE_ERROR_ON_MSG(0 > output->offset() || output->offset() > 256, "Offset is larger than the image value range.");
159
160 // Check range
161 ARM_COMPUTE_ERROR_ON_MSG(output->range() > 256 /* max range */, "Range larger than the image value range.");
162
163 // We only run histogram on Image, therefore only 2 dimensions here
164 unsigned int start_position = (input->info()->dimension(0) / pixels_per_item) * pixels_per_item;
165
166 if(start_position >= input->info()->dimension(0))
167 {
168 return; // no need to run histogram border kernel
169 }
170
171 _input = input;
172 _output = output;
173
174 unsigned int num_bins = _output->num_bins();
175 unsigned int window_size = _output->window();
176 unsigned int offset = _output->offset();
177 unsigned int range = _output->range();
178 unsigned int offrange = offset + range;
179
180 // Create kernel
181 bool is_fixed_size = (256 == num_bins) && (1 == window_size) && (0 == offset) && (256 == offrange);
182 std::string kernel_name = is_fixed_size ? "hist_border_kernel_fixed" : "hist_border_kernel";
183 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name));
184
185 // Set static kernel arguments
186 unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters
187 _kernel.setArg(idx++, _output->cl_buffer());
188 if(!is_fixed_size)
189 {
190 _kernel.setArg<cl_uint>(idx++, num_bins);
191 _kernel.setArg<cl_uint>(idx++, offset);
192 _kernel.setArg<cl_uint>(idx++, range);
193 _kernel.setArg<cl_uint>(idx++, offrange);
194 }
195
196 // Configure kernel window
197 Window win;
198 win.set(0, Window::Dimension(start_position, _input->info()->dimension(0)));
199 win.set(1, Window::Dimension(0, _input->info()->dimension(1)));
200 update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, 1));
201 ICLKernel::configure(win);
202}
203
204void CLHistogramBorderKernel::run(const Window &window, cl::CommandQueue &queue)
205{
206 if(window.x().start() >= window.x().end())
207 {
208 return;
209 }
210
211 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
212
213 cl::NDRange lws = cl::NDRange(1, 1);
214
215 Window slice = window.first_slice_window_2D();
216
217 do
218 {
219 /* Run the border part which has width cannot be divided by 16 */
220 unsigned int idx = 0;
221 add_2D_tensor_argument(idx, _input, slice);
222
223 enqueue(queue, *this, slice, lws);
224 }
225 while(window.slide_window_slice_2D(slice));
226}