blob: 16e5113c6231ccc7022db7b80cc4127c184a84ff [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/CLNonLinearFilterKernel.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/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"
36
37#include <algorithm>
38#include <cmath>
39#include <cstdlib>
40#include <set>
41#include <sstream>
42#include <string>
43
44using namespace arm_compute;
45
46CLNonLinearFilterKernel::CLNonLinearFilterKernel()
47 : _border_size(0)
48{
49}
50
51BorderSize CLNonLinearFilterKernel::border_size() const
52{
53 return _border_size;
54}
55
56void CLNonLinearFilterKernel::configure(const ICLTensor *input, ICLTensor *output, NonLinearFilterFunction function,
57 unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask,
58 bool border_undefined)
59{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010060 configure(CLKernelLibrary::get().get_compile_context(), input, output, function, mask_size, pattern, mask, border_undefined);
61}
62
Manuel Bottini679fc962020-04-21 16:08:53 +010063void CLNonLinearFilterKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, NonLinearFilterFunction function,
Manuel Bottini4c6bd512020-04-08 10:15:51 +010064 unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask,
65 bool border_undefined)
66{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
68 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
69 ARM_COMPUTE_ERROR_ON(mask_size != 3 && mask_size != 5);
70 ARM_COMPUTE_ERROR_ON_MSG(pattern == MatrixPattern::OTHER, "MatrixPattern::OTHER is not supported!");
71 ARM_COMPUTE_UNUSED(mask);
72
73 _input = input;
74 _output = output;
75 _border_size = BorderSize(mask_size / 2);
76
77 // Define build options
78 std::set<std::string> build_opts;
79 build_opts.emplace("-D" + string_from_non_linear_filter_function(function));
80
81 // Define kernel
82 std::string pattern_name = string_from_matrix_pattern(pattern);
83 std::transform(pattern_name.begin(), pattern_name.end(), pattern_name.begin(), ::tolower);
84 std::stringstream ss;
85 ss << "non_linear_filter_" << pattern_name << mask_size << "x" << mask_size;
86
87 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +010088 _kernel = create_kernel(compile_context, ss.str(), build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089
90 // Configure kernel window
91 constexpr unsigned int num_elems_processed_per_iteration = 8;
92 constexpr unsigned int num_elems_read_per_iteration = 16;
93 const unsigned int num_rows_read_per_iteration = mask_size;
94
95 Window win = calculate_max_window(*input->info(), num_elems_processed_per_iteration, border_undefined, border_size());
96
97 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration);
98 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
99
100 update_window_and_padding(win, input_access, output_access);
101
102 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
103
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100104 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105}