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