2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/core/CL/kernels/CLNonLinearFilterKernel.h"
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/Helpers.h"
32 #include "arm_compute/core/TensorInfo.h"
33 #include "arm_compute/core/Utils.h"
34 #include "arm_compute/core/Validate.h"
35 #include "arm_compute/core/Window.h"
44 using namespace arm_compute;
46 CLNonLinearFilterKernel::CLNonLinearFilterKernel()
51 BorderSize CLNonLinearFilterKernel::border_size() const
56 void CLNonLinearFilterKernel::configure(const ICLTensor *input, ICLTensor *output, NonLinearFilterFunction function,
57 unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask,
58 bool border_undefined)
60 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
61 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
62 ARM_COMPUTE_ERROR_ON(mask_size != 3 && mask_size != 5);
63 ARM_COMPUTE_ERROR_ON_MSG(pattern == MatrixPattern::OTHER, "MatrixPattern::OTHER is not supported!");
64 ARM_COMPUTE_UNUSED(mask);
68 _border_size = BorderSize(mask_size / 2);
70 // Define build options
71 std::set<std::string> build_opts;
72 build_opts.emplace("-D" + string_from_non_linear_filter_function(function));
75 std::string pattern_name = string_from_matrix_pattern(pattern);
76 std::transform(pattern_name.begin(), pattern_name.end(), pattern_name.begin(), ::tolower);
78 ss << "non_linear_filter_" << pattern_name << mask_size << "x" << mask_size;
81 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(ss.str(), build_opts));
83 // Configure kernel window
84 constexpr unsigned int num_elems_processed_per_iteration = 8;
85 constexpr unsigned int num_elems_read_per_iteration = 16;
86 const unsigned int num_rows_read_per_iteration = mask_size;
88 Window win = calculate_max_window(*input->info(), num_elems_processed_per_iteration, border_undefined, border_size());
90 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
91 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
93 update_window_and_padding(win, input_access, output_access);
95 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
97 ICLKernel::configure(win);