blob: 87a12b9da93399f2a39b02da146fbc435e441367 [file] [log] [blame]
Georgios Pinitas0bc78492019-03-18 20:07:37 +00001/*
2 * Copyright (c) 2019 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/CLFFTRadixStageKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/CLValidate.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Window.h"
34
35#include <cmath>
36
37namespace arm_compute
38{
39namespace
40{
41Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const FFTRadixStageKernelDescriptor &config)
42{
43 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
44 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32);
45 ARM_COMPUTE_RETURN_ERROR_ON(config.axis != 0);
46 ARM_COMPUTE_RETURN_ERROR_ON(CLFFTRadixStageKernel::supported_radix().count(config.radix) == 0);
47
48 // Checks performed when output is configured
49 if((output != nullptr) && (output->total_size() != 0))
50 {
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
53 }
54
55 return Status{};
56}
57
58std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const FFTRadixStageKernelDescriptor &config)
59{
60 if(output != nullptr)
61 {
62 auto_init_if_empty(*output, *input);
63 }
64
65 Window win = calculate_max_window(*input, Steps(config.radix));
66 if(output != nullptr)
67 {
68 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
69 }
70
71 return std::make_pair(Status{}, win);
72}
73} // namespace
74
75CLFFTRadixStageKernel::CLFFTRadixStageKernel()
76 : _input(nullptr), _output(nullptr), _run_in_place(false)
77{
78}
79
80void CLFFTRadixStageKernel::configure(ICLTensor *input, ICLTensor *output, const FFTRadixStageKernelDescriptor &config)
81{
82 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
83 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr, config));
84
85 _input = input;
86 _output = output;
87 _run_in_place = (output == nullptr) || (output == input);
88
89 // Create build options
90 CLBuildOptions build_opts;
91 build_opts.add_option_if(_run_in_place, "-DIN_PLACE");
92
93 // Create kernel
94 std::string kernel_name = "fft";
95 kernel_name += "_radix_" + support::cpp11::to_string(config.radix);
96 kernel_name += (config.is_first_stage) ? "_first_stage" : "";
97 kernel_name += "_axis_" + support::cpp11::to_string(config.axis);
98 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
99
100 // Set static arguments if not the first stage
101 if(!config.is_first_stage)
102 {
103 const unsigned int Ni = config.Nx * config.radix;
104 const float exp_const = (-2.0 * M_PI) / static_cast<float>(Ni);
105 unsigned int idx = (1 + (_run_in_place ? 0 : 1)) * num_arguments_per_3D_tensor(); // Skip the input and output parameters
106 _kernel.setArg<cl_uint>(idx++, config.Nx);
107 _kernel.setArg<cl_uint>(idx++, Ni);
108 _kernel.setArg<cl_float>(idx++, exp_const);
109 }
110
111 // Configure kernel window
112 auto win_config = validate_and_configure_window(input->info(), (_run_in_place) ? nullptr : output->info(), config);
113 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
114 ICLKernel::configure_internal(win_config.second);
115
116 // Set config_id for enabling LWS tuning
117 _config_id = kernel_name;
118 _config_id += "_";
119 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
120 _config_id += "_";
121 _config_id += support::cpp11::to_string(input->info()->dimension(0));
122 _config_id += "_";
123 _config_id += support::cpp11::to_string(input->info()->dimension(1));
124}
125
126Status CLFFTRadixStageKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const FFTRadixStageKernelDescriptor &config)
127{
128 const bool run_in_place = (output == nullptr) || (output == input);
129 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, config));
130 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(),
131 (run_in_place) ? nullptr : output->clone().get(),
132 config)
133 .first);
134
135 return Status{};
136}
137
138std::set<unsigned int> CLFFTRadixStageKernel::supported_radix()
139{
140 return std::set<unsigned int> { 2, 3, 4, 5, 7, 8 };
141}
142
143void CLFFTRadixStageKernel::run(const Window &window, cl::CommandQueue &queue)
144{
145 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
146 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
147
148 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
149 Window slice = collapsed.first_slice_window_3D();
150
151 do
152 {
153 unsigned int idx = 0;
154 add_3D_tensor_argument(idx, _input, slice);
155 if(!_run_in_place)
156 {
157 add_3D_tensor_argument(idx, _output, slice);
158 }
159 enqueue(queue, *this, slice, lws_hint());
160 }
161 while(collapsed.slide_window_slice_3D(slice));
162}
163} // namespace arm_compute