Adding PocketFFT support in FFT module since kissfft has some flaw in accuracy and performance

This commit is contained in:
Guoqiang QI
2022-05-11 17:44:22 +00:00
committed by Rasmus Munk Larsen
parent 73d65dbc43
commit 00b75375e7
8 changed files with 390 additions and 273 deletions

View File

@@ -29,10 +29,19 @@
* The default implementation is based on kissfft. It is a small, free, and
* reasonably efficient default.
*
* There are currently two implementation backend:
* There are currently four implementation backend:
*
* - kissfft(https://github.com/mborgerding/kissfft) : Simple and not so fast, BSD-3-Clause.
* It is a mixed-radix Fast Fourier Transform based up on the principle, "Keep It Simple, Stupid."
* Notice that:kissfft fails to handle "atypically-sized" inputs(i.e., sizes with large factors),a workaround is using fftw or pocketfft.
* - fftw (http://www.fftw.org) : faster, GPL -- incompatible with Eigen in LGPL form, bigger code size.
* - MKL (http://en.wikipedia.org/wiki/Math_Kernel_Library) : fastest, commercial -- may be incompatible with Eigen in GPL form.
* - pocketfft (https://gitlab.mpcdf.mpg.de/mtr/pocketfft) : faster than kissfft, BSD 3-clause.
* It is a heavily modified implementation of FFTPack, with the following advantages:
* 1.strictly C++11 compliant
* 2.more accurate twiddle factor computation
* 3.very fast plan generation
* 4.worst case complexity for transform sizes with large prime factors is N*log(N), because Bluestein's algorithm is used for these cases.
*
* \section FFTDesign Design
*
@@ -85,9 +94,16 @@
namespace Eigen {
template <typename T> struct default_fft_impl : public internal::imklfft_impl {};
}
#else
#elif defined EIGEN_POCKETFFT_DEFAULT
// internal::pocketfft_impl: a heavily modified implementation of FFTPack, with many advantages.
# include<pocketfft_hdronly.h>
# include"src/FFT/ei_pocketfft_impl.h"
namespace Eigen {
template <typename T>
struct default_fft_impl : public internal::pocketfft_impl<T> {};
}
#else
// internal::kissfft_impl: small, free, reasonably efficient default, derived from kissfft
//
# include "src/FFT/ei_kissfft_impl.h"
namespace Eigen {
template <typename T>
@@ -195,13 +211,13 @@ class FFT
m_impl.fwd(dst,src,static_cast<int>(nfft));
}
/*
#if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT
inline
void fwd2(Complex * dst, const Complex * src, int n0,int n1)
{
m_impl.fwd2(dst,src,n0,n1);
}
*/
#endif
template <typename Input_>
inline
@@ -354,8 +370,7 @@ class FFT
}
/*
// TODO: multi-dimensional FFTs
#if defined EIGEN_FFTW_DEFAULT || defined EIGEN_POCKETFFT_DEFAULT
inline
void inv2(Complex * dst, const Complex * src, int n0,int n1)
{
@@ -363,7 +378,8 @@ class FFT
if ( HasFlag( Unscaled ) == false)
scale(dst,1./(n0*n1),n0*n1);
}
*/
#endif
inline
impl_type & impl() {return m_impl;}

View File

@@ -0,0 +1,69 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
using namespace pocketfft;
using namespace pocketfft::detail;
namespace Eigen {
namespace internal {
template<typename _Scalar>
struct pocketfft_impl
{
typedef _Scalar Scalar;
typedef std::complex<Scalar> Complex;
inline void clear() {}
inline void fwd(Complex* dst, const Scalar* src, int nfft){
const shape_t shape_{ static_cast<size_t>(nfft) };
const shape_t axes_{ 0 };
const stride_t stride_in{ sizeof(Scalar) };
const stride_t stride_out{ sizeof(Complex) };
r2c(shape_, stride_in, stride_out, axes_, FORWARD, src, dst, static_cast<Scalar>(1));
}
inline void fwd(Complex* dst, const Complex* src, int nfft){
const shape_t shape_{ static_cast<size_t>(nfft) };
const shape_t axes_{ 0 };
const stride_t stride_{ sizeof(Complex) };
c2c(shape_, stride_, stride_, axes_, FORWARD, src, dst, static_cast<Scalar>(1));
}
inline void inv(Scalar* dst, const Complex* src, int nfft){
const shape_t shape_{ static_cast<size_t>(nfft) };
const shape_t axes_{ 0 };
const stride_t stride_in{ sizeof(Complex) };
const stride_t stride_out{ sizeof(Scalar) };
c2r(shape_, stride_in, stride_out, axes_, BACKWARD, src, dst, static_cast<Scalar>(1));
}
inline void inv(Complex* dst, const Complex* src, int nfft){
const shape_t shape_{ static_cast<size_t>(nfft) };
const shape_t axes_{ 0 };
const stride_t stride_{ sizeof(Complex) };
c2c(shape_, stride_, stride_, axes_, BACKWARD, src, dst, static_cast<Scalar>(1));
}
inline void fwd2(Complex* dst, const Complex* src, int nfft0, int nfft1){
const shape_t shape_{ static_cast<size_t>(nfft0), static_cast<size_t>(nfft1) };
const shape_t axes_{ 0, 1 };
const stride_t stride_{ static_cast<ptrdiff_t>(sizeof(Complex)*nfft1), static_cast<ptrdiff_t>(sizeof(Complex)) };
c2c(shape_, stride_, stride_, axes_, FORWARD, src, dst, static_cast<Scalar>(1));
}
inline void inv2(Complex* dst, const Complex* src, int nfft0, int nfft1){
const shape_t shape_{ static_cast<size_t>(nfft0), static_cast<size_t>(nfft1) };
const shape_t axes_{ 0, 1 };
const stride_t stride_{ static_cast<ptrdiff_t>(sizeof(Complex)*nfft1), static_cast<ptrdiff_t>(sizeof(Complex)) };
c2c(shape_, stride_, stride_, axes_, BACKWARD, src, dst, static_cast<Scalar>(1));
}
};
} // namespace internal
} // namespace Eigen