blob: 6b6735ae581898cbb2bfd2648f559b84ffa3094b [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/runtime/CL/functions/CLFFT1D.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/Validate.h"
28#include "arm_compute/core/utils/helpers/fft.h"
29#include "arm_compute/runtime/CL/CLScheduler.h"
30
31namespace arm_compute
32{
33CLFFT1D::CLFFT1D(std::shared_ptr<IMemoryManager> memory_manager)
34 : _memory_group(std::move(memory_manager)), _digit_reversed_input(), _digit_reverse_indices(), _digit_reverse_kernel(), _fft_kernels(), _num_ffts(0)
35{
36}
37
38void CLFFT1D::configure(const ICLTensor *input, ICLTensor *output, const FFT1DInfo &config)
39{
40 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
41 ARM_COMPUTE_ERROR_THROW_ON(CLFFT1D::validate(input->info(), output->info(), config));
42
43 // Decompose size to radix factors
44 const auto supported_radix = CLFFTRadixStageKernel::supported_radix();
45 const unsigned int N = input->info()->tensor_shape()[config.axis];
46 const auto decomposed_vector = arm_compute::helpers::fft::decompose_stages(N, supported_radix);
47 ARM_COMPUTE_ERROR_ON(decomposed_vector.empty());
48
49 // Configure digit reverse
50 TensorInfo digit_reverse_indices_info(TensorShape(input->info()->tensor_shape()[config.axis]), 1, DataType::U32);
51 _digit_reverse_indices.allocator()->init(digit_reverse_indices_info);
52 _memory_group.manage(&_digit_reversed_input);
53 _digit_reverse_kernel.configure(input, &_digit_reversed_input, &_digit_reverse_indices, config.axis);
54
55 // Create and configure FFT kernels
56 unsigned int Nx = 1;
57 _num_ffts = decomposed_vector.size();
58 _fft_kernels = arm_compute::support::cpp14::make_unique<CLFFTRadixStageKernel[]>(_num_ffts);
59 for(unsigned int i = 0; i < _num_ffts; ++i)
60 {
61 const unsigned int radix_for_stage = decomposed_vector.at(i);
62
63 FFTRadixStageKernelDescriptor fft_kernel_desc;
64 fft_kernel_desc.axis = config.axis;
65 fft_kernel_desc.radix = radix_for_stage;
66 fft_kernel_desc.Nx = Nx;
67 fft_kernel_desc.is_first_stage = (i == 0);
68 _fft_kernels[i].configure(&_digit_reversed_input, i == (_num_ffts - 1) ? output : nullptr, fft_kernel_desc);
69
70 Nx *= radix_for_stage;
71 }
72
73 // Allocate tensors
74 _digit_reversed_input.allocator()->allocate();
75 _digit_reverse_indices.allocator()->allocate();
76
77 // Init digit reverse indices
78 const auto digit_reverse_cpu = arm_compute::helpers::fft::digit_reverse_indices(N, decomposed_vector);
79 _digit_reverse_indices.map(CLScheduler::get().queue(), true);
80 std::copy_n(digit_reverse_cpu.data(), N, reinterpret_cast<unsigned int *>(_digit_reverse_indices.buffer()));
81 _digit_reverse_indices.unmap(CLScheduler::get().queue());
82}
83
84Status CLFFT1D::validate(const ITensorInfo *input, const ITensorInfo *output, const FFT1DInfo &config)
85{
86 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
87 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32);
88 ARM_COMPUTE_RETURN_ERROR_ON(config.axis != 0);
89
90 // Check if FFT is decomposable
91 const auto supported_radix = CLFFTRadixStageKernel::supported_radix();
92 const unsigned int N = input->tensor_shape()[config.axis];
93 const auto decomposed_vector = arm_compute::helpers::fft::decompose_stages(N, supported_radix);
94 ARM_COMPUTE_RETURN_ERROR_ON(decomposed_vector.empty());
95
96 // Checks performed when output is configured
97 if((output != nullptr) && (output->total_size() != 0))
98 {
99 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
100 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
101 }
102
103 return Status{};
104}
105
106void CLFFT1D::run()
107{
108 _memory_group.acquire();
109
110 CLScheduler::get().enqueue(_digit_reverse_kernel, false);
111
112 for(unsigned int i = 0; i < _num_ffts; ++i)
113 {
114 CLScheduler::get().enqueue(_fft_kernels[i], i == (_num_ffts - 1));
115 }
116
117 _memory_group.release();
118}
119} // namespace arm_compute