arm_compute v17.04
[platform/upstream/armcl.git] / src / core / CL / kernels / CLNonLinearFilterKernel.cpp
1 /*
2  * Copyright (c) 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/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
44 using namespace arm_compute;
45
46 CLNonLinearFilterKernel::CLNonLinearFilterKernel()
47     : _border_size(0)
48 {
49 }
50
51 BorderSize CLNonLinearFilterKernel::border_size() const
52 {
53     return _border_size;
54 }
55
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)
59 {
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);
65
66     _input       = input;
67     _output      = output;
68     _border_size = BorderSize(mask_size / 2);
69
70     // Define build options
71     std::set<std::string> build_opts;
72     build_opts.emplace("-D" + string_from_non_linear_filter_function(function));
73
74     // Define kernel
75     std::string pattern_name = string_from_matrix_pattern(pattern);
76     std::transform(pattern_name.begin(), pattern_name.end(), pattern_name.begin(), ::tolower);
77     std::stringstream ss;
78     ss << "non_linear_filter_" << pattern_name << mask_size << "x" << mask_size;
79
80     // Create kernel
81     _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(ss.str(), build_opts));
82
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;
87
88     Window win = calculate_max_window(*input->info(), num_elems_processed_per_iteration, border_undefined, border_size());
89
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);
92
93     update_window_and_padding(win, input_access, output_access);
94
95     output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
96
97     ICLKernel::configure(win);
98 }