blob: d2610539d1c53485ec40ed309203b1506e3224c2 [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/CLFillBorderKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.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/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
36#include <cstdint>
37#include <set>
38#include <sstream>
39#include <string>
40
41using namespace arm_compute;
42
43CLFillBorderKernel::CLFillBorderKernel()
44 : ICLKernel(), _tensor(nullptr)
45{
46}
47
48bool CLFillBorderKernel::is_parallelisable() const
49{
50 return false;
51}
52
53template <class T>
54void CLFillBorderKernel::set_constant_border(unsigned int idx, const PixelValue &constant_border_value)
55{
56 T value;
57 constant_border_value.get(value);
58 ICLKernel::add_argument<T>(idx, static_cast<T>(value));
59}
60
61void CLFillBorderKernel::configure(ICLTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value)
62{
63 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
64 ARM_COMPUTE_ERROR_ON(tensor->info()->num_channels() != 1);
65
66 border_size.limit(tensor->info()->padding());
67
68 // If there is no border: early exit
69 if(border_size.empty() || border_mode == BorderMode::UNDEFINED)
70 {
71 return;
72 }
73
74 // Select appropriate kernel
75 std::string kernel_name = "fill_image_borders_" + lower_string(string_from_border_mode(border_mode));
76
77 // Define select type required by replicate border > 1
78 const DataType dt = tensor->info()->data_type();
Georgios Pinitasac4e8732017-07-05 17:02:25 +010079 std::string select_type = get_underlying_cl_type_from_data_type(dt);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 if(is_data_type_float(dt))
81 {
82 select_type = (DataType::F32 == dt) ? "int" : "short";
83 }
84
85 // Define build options
86 std::set<std::string> build_opts;
Georgios Pinitasac4e8732017-07-05 17:02:25 +010087 build_opts.emplace(("-DDATA_TYPE=" + get_underlying_cl_type_from_data_type(dt)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 build_opts.emplace(("-DSELECT_TYPE=" + select_type));
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +010089 build_opts.emplace(("-DBORDER_SIZE_TOP=" + support::cpp11::to_string(border_size.top)));
90 build_opts.emplace(("-DBORDER_SIZE_BOTTOM=" + support::cpp11::to_string(border_size.bottom)));
91 build_opts.emplace(("-DBORDER_SIZE_LEFT=" + support::cpp11::to_string(border_size.left)));
92 build_opts.emplace(("-DBORDER_SIZE_RIGHT=" + support::cpp11::to_string(border_size.right)));
steniu010c7614f2017-06-23 17:00:26 +010093 if(is_data_type_fixed_point(tensor->info()->data_type()))
94 {
95 build_opts.emplace("-DFIXED_POINT_POSITION");
96 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097
98 // Create kernel
99 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
100 _tensor = tensor;
101
102 // Create static kernel arguments
103 const unsigned int valid_width = tensor->info()->valid_region().shape[0];
104 const unsigned int valid_height = tensor->info()->valid_region().shape[1];
105 const cl_int2 valid_region_coords =
106 {
107 {
108 static_cast<cl_int>(tensor->info()->valid_region().anchor[0]),
109 static_cast<cl_int>(tensor->info()->valid_region().anchor[1]),
110 }
111 };
112 const unsigned int total_valid_width = border_size.left + valid_width + border_size.right;
113
114 // Set static kernel arguments
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100115 unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 ICLKernel::add_argument<cl_uint>(idx, valid_width);
117 ICLKernel::add_argument<cl_uint>(idx, valid_height);
118 ICLKernel::add_argument<cl_int2>(idx, valid_region_coords);
119 if(BorderMode::CONSTANT == border_mode)
120 {
121 switch(dt)
122 {
123 case DataType::U8:
124 set_constant_border<uint8_t>(idx, constant_border_value);
125 break;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100126 case DataType::QS8:
127 case DataType::S8:
128 set_constant_border<int8_t>(idx, constant_border_value);
129 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 case DataType::U16:
131 set_constant_border<uint16_t>(idx, constant_border_value);
132 break;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100133 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 case DataType::S16:
135 set_constant_border<int16_t>(idx, constant_border_value);
136 break;
137 case DataType::U32:
138 set_constant_border<uint32_t>(idx, constant_border_value);
139 break;
140 case DataType::S32:
141 set_constant_border<int32_t>(idx, constant_border_value);
142 break;
143 case DataType::F32:
144 static_assert(sizeof(float) == 4, "Float must be 32 bit");
145 set_constant_border<float>(idx, constant_border_value);
146 break;
147 case DataType::F16:
148 static_assert(sizeof(cl_half) == 2, "Half must be 16 bit");
149 set_constant_border<cl_half>(idx, constant_border_value);
150 break;
151 default:
152 ARM_COMPUTE_ERROR("Not handled");
153 }
154 }
155
156 // Configure kernel window
157 Window win;
158 win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height));
159 win.set(Window::DimY, Window::Dimension(0, 1, 1));
SiCong Li86b53332017-08-23 11:02:43 +0100160 win.use_tensor_dimensions(tensor->info()->tensor_shape(), Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 ICLKernel::configure(win);
162}
163
164void CLFillBorderKernel::run(const Window &window, cl::CommandQueue &queue)
165{
166 // Border mode undefined or border width == 0
167 if(_kernel() == nullptr)
168 {
169 return;
170 }
171
172 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
173 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
174
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100175 Window slice = window.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176
177 do
178 {
179 unsigned int idx = 0;
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100180 add_3D_tensor_argument(idx, _tensor, slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 enqueue(queue, *this, slice, cl::NullRange);
182 }
Anthony Barbier7ff47a32017-07-11 16:54:04 +0100183 while(window.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184}