Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 41 | using namespace arm_compute; |
| 42 | |
| 43 | CLFillBorderKernel::CLFillBorderKernel() |
| 44 | : ICLKernel(), _tensor(nullptr) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | bool CLFillBorderKernel::is_parallelisable() const |
| 49 | { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | template <class T> |
| 54 | void 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 | |
| 61 | void 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(); |
| 79 | std::string select_type = get_cl_type_from_data_type(dt); |
| 80 | 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; |
| 87 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(dt))); |
| 88 | build_opts.emplace(("-DSELECT_TYPE=" + select_type)); |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 89 | 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))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
| 94 | // Create kernel |
| 95 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
| 96 | _tensor = tensor; |
| 97 | |
| 98 | // Create static kernel arguments |
| 99 | const unsigned int valid_width = tensor->info()->valid_region().shape[0]; |
| 100 | const unsigned int valid_height = tensor->info()->valid_region().shape[1]; |
| 101 | const cl_int2 valid_region_coords = |
| 102 | { |
| 103 | { |
| 104 | static_cast<cl_int>(tensor->info()->valid_region().anchor[0]), |
| 105 | static_cast<cl_int>(tensor->info()->valid_region().anchor[1]), |
| 106 | } |
| 107 | }; |
| 108 | const unsigned int total_valid_width = border_size.left + valid_width + border_size.right; |
| 109 | |
| 110 | // Set static kernel arguments |
| 111 | unsigned int idx = num_arguments_per_2D_tensor(); //Skip the tensor parameters |
| 112 | ICLKernel::add_argument<cl_uint>(idx, valid_width); |
| 113 | ICLKernel::add_argument<cl_uint>(idx, valid_height); |
| 114 | ICLKernel::add_argument<cl_int2>(idx, valid_region_coords); |
| 115 | if(BorderMode::CONSTANT == border_mode) |
| 116 | { |
| 117 | switch(dt) |
| 118 | { |
| 119 | case DataType::U8: |
| 120 | set_constant_border<uint8_t>(idx, constant_border_value); |
| 121 | break; |
| 122 | case DataType::U16: |
| 123 | set_constant_border<uint16_t>(idx, constant_border_value); |
| 124 | break; |
| 125 | case DataType::S16: |
| 126 | set_constant_border<int16_t>(idx, constant_border_value); |
| 127 | break; |
| 128 | case DataType::U32: |
| 129 | set_constant_border<uint32_t>(idx, constant_border_value); |
| 130 | break; |
| 131 | case DataType::S32: |
| 132 | set_constant_border<int32_t>(idx, constant_border_value); |
| 133 | break; |
| 134 | case DataType::F32: |
| 135 | static_assert(sizeof(float) == 4, "Float must be 32 bit"); |
| 136 | set_constant_border<float>(idx, constant_border_value); |
| 137 | break; |
| 138 | case DataType::F16: |
| 139 | static_assert(sizeof(cl_half) == 2, "Half must be 16 bit"); |
| 140 | set_constant_border<cl_half>(idx, constant_border_value); |
| 141 | break; |
| 142 | default: |
| 143 | ARM_COMPUTE_ERROR("Not handled"); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Configure kernel window |
| 148 | Window win; |
| 149 | win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height)); |
| 150 | win.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 151 | win.use_tensor_dimensions(tensor->info(), Window::DimZ); |
| 152 | ICLKernel::configure(win); |
| 153 | } |
| 154 | |
| 155 | void CLFillBorderKernel::run(const Window &window, cl::CommandQueue &queue) |
| 156 | { |
| 157 | // Border mode undefined or border width == 0 |
| 158 | if(_kernel() == nullptr) |
| 159 | { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 164 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 165 | |
| 166 | Window slice = window.first_slice_window_2D(); |
| 167 | |
| 168 | do |
| 169 | { |
| 170 | unsigned int idx = 0; |
| 171 | add_2D_tensor_argument(idx, _tensor, slice); |
| 172 | enqueue(queue, *this, slice, cl::NullRange); |
| 173 | } |
| 174 | while(window.slide_window_slice_2D(slice)); |
| 175 | } |