blob: d03a80dbdfa2f5709edc6eea01cf95c7f520497e [file]
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2009 Mark Borgerding mark a borgerding net
//
// 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/.
// SPDX-License-Identifier: MPL-2.0
#ifndef EIGEN_FFT_FFTW_IMPL_H
#define EIGEN_FFT_FFTW_IMPL_H
// IWYU pragma: private
#include "./InternalHeaderCheck.h"
#include <memory>
#include <mutex>
#include <tuple>
namespace Eigen {
namespace internal {
// FFTW uses non-const arguments,
// so const_cast is needed for all the args it uses.
//
// This should be safe as long as
// 1. we use FFTW_ESTIMATE for all our planning
// see the FFTW docs section 4.3.2 "Planner Flags"
// 2. fftw_complex is compatible with std::complex
// This assumes std::complex<T> layout is array of size 2 with real,imag
template <typename T>
inline T *fftw_cast(const T *p) {
return const_cast<T *>(p);
}
inline fftw_complex *fftw_cast(const std::complex<double> *p) {
return const_cast<fftw_complex *>(reinterpret_cast<const fftw_complex *>(p));
}
inline fftwf_complex *fftw_cast(const std::complex<float> *p) {
return const_cast<fftwf_complex *>(reinterpret_cast<const fftwf_complex *>(p));
}
inline fftwl_complex *fftw_cast(const std::complex<long double> *p) {
return const_cast<fftwl_complex *>(reinterpret_cast<const fftwl_complex *>(p));
}
// The FFTW planner is not thread-safe: fftw_execute and its new-array variants,
// which is what this backend runs transforms through, are the only entry points
// that may be called concurrently (FFTW manual, "Thread safety"), so plan
// creation and destruction are serialized through this mutex. A template
// static gives the header-only definition; std::mutex is
// constexpr-constructible, so the mutex is ready before any thread starts.
// The planner state it stands in for is one per process, so the mutex must be
// too: the explicit default visibility is what keeps the definition from being
// bound locally under -fvisibility=hidden, where each library planning through
// Eigen would get a mutex of its own and serialize nothing between them. The
// module documentation covers what no header can reach, which needs FFTW's own
// fftw_make_planner_thread_safe().
#if EIGEN_HAS_ATTRIBUTE(visibility) && !EIGEN_OS_WIN
#define EIGEN_FFTW_PLANNER_MUTEX_VISIBILITY __attribute__((visibility("default")))
#else
#define EIGEN_FFTW_PLANNER_MUTEX_VISIBILITY
#endif
template <typename Dummy = void>
struct fftw_planner_lock {
static EIGEN_FFTW_PLANNER_MUTEX_VISIBILITY std::mutex mutex;
};
template <typename Dummy>
EIGEN_FFTW_PLANNER_MUTEX_VISIBILITY std::mutex fftw_planner_lock<Dummy>::mutex;
inline std::mutex &fftw_planner_mutex() { return fftw_planner_lock<>::mutex; }
template <typename PlanFactory>
inline decltype(auto) fftw_make_plan(PlanFactory factory) {
std::lock_guard<std::mutex> lock(fftw_planner_mutex());
return factory();
}
template <typename T>
struct fftw_plan {};
template <>
struct fftw_plan<float> {
using scalar_type = float;
using complex_type = fftwf_complex;
std::shared_ptr<fftwf_plan_s> m_plan;
fftw_plan() = default;
void set_plan(fftwf_plan p) {
m_plan.reset(p, [](fftwf_plan plan) {
std::lock_guard<std::mutex> lock(fftw_planner_mutex());
fftwf_destroy_plan(plan);
});
}
inline void fwd(complex_type *dst, complex_type *src, int nfft) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftwf_plan_dft_1d(nfft, src, dst, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwf_execute_dft(m_plan.get(), src, dst);
}
inline void inv(complex_type *dst, complex_type *src, int nfft) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftwf_plan_dft_1d(nfft, src, dst, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwf_execute_dft(m_plan.get(), src, dst);
}
inline void fwd(complex_type *dst, scalar_type *src, int nfft) {
if (!m_plan)
set_plan(
fftw_make_plan([&] { return fftwf_plan_dft_r2c_1d(nfft, src, dst, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwf_execute_dft_r2c(m_plan.get(), src, dst);
}
inline void inv(scalar_type *dst, complex_type *src, int nfft) {
if (!m_plan)
set_plan(
fftw_make_plan([&] { return fftwf_plan_dft_c2r_1d(nfft, src, dst, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwf_execute_dft_c2r(m_plan.get(), src, dst);
}
inline void fwd2(complex_type *dst, complex_type *src, int n0, int n1) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftwf_plan_dft_2d(n0, n1, src, dst, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwf_execute_dft(m_plan.get(), src, dst);
}
inline void inv2(complex_type *dst, complex_type *src, int n0, int n1) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftwf_plan_dft_2d(n0, n1, src, dst, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwf_execute_dft(m_plan.get(), src, dst);
}
};
template <>
struct fftw_plan<double> {
using scalar_type = double;
using complex_type = fftw_complex;
std::shared_ptr<fftw_plan_s> m_plan;
fftw_plan() = default;
void set_plan(::fftw_plan p) {
m_plan.reset(p, [](::fftw_plan plan) {
std::lock_guard<std::mutex> lock(fftw_planner_mutex());
fftw_destroy_plan(plan);
});
}
inline void fwd(complex_type *dst, complex_type *src, int nfft) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftw_plan_dft_1d(nfft, src, dst, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftw_execute_dft(m_plan.get(), src, dst);
}
inline void inv(complex_type *dst, complex_type *src, int nfft) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftw_plan_dft_1d(nfft, src, dst, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftw_execute_dft(m_plan.get(), src, dst);
}
inline void fwd(complex_type *dst, scalar_type *src, int nfft) {
if (!m_plan)
set_plan(
fftw_make_plan([&] { return fftw_plan_dft_r2c_1d(nfft, src, dst, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftw_execute_dft_r2c(m_plan.get(), src, dst);
}
inline void inv(scalar_type *dst, complex_type *src, int nfft) {
if (!m_plan)
set_plan(
fftw_make_plan([&] { return fftw_plan_dft_c2r_1d(nfft, src, dst, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftw_execute_dft_c2r(m_plan.get(), src, dst);
}
inline void fwd2(complex_type *dst, complex_type *src, int n0, int n1) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftw_plan_dft_2d(n0, n1, src, dst, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftw_execute_dft(m_plan.get(), src, dst);
}
inline void inv2(complex_type *dst, complex_type *src, int n0, int n1) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftw_plan_dft_2d(n0, n1, src, dst, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftw_execute_dft(m_plan.get(), src, dst);
}
};
template <>
struct fftw_plan<long double> {
using scalar_type = long double;
using complex_type = fftwl_complex;
std::shared_ptr<fftwl_plan_s> m_plan;
fftw_plan() = default;
void set_plan(fftwl_plan p) {
m_plan.reset(p, [](fftwl_plan plan) {
std::lock_guard<std::mutex> lock(fftw_planner_mutex());
fftwl_destroy_plan(plan);
});
}
inline void fwd(complex_type *dst, complex_type *src, int nfft) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftwl_plan_dft_1d(nfft, src, dst, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwl_execute_dft(m_plan.get(), src, dst);
}
inline void inv(complex_type *dst, complex_type *src, int nfft) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftwl_plan_dft_1d(nfft, src, dst, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwl_execute_dft(m_plan.get(), src, dst);
}
inline void fwd(complex_type *dst, scalar_type *src, int nfft) {
if (!m_plan)
set_plan(
fftw_make_plan([&] { return fftwl_plan_dft_r2c_1d(nfft, src, dst, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwl_execute_dft_r2c(m_plan.get(), src, dst);
}
inline void inv(scalar_type *dst, complex_type *src, int nfft) {
if (!m_plan)
set_plan(
fftw_make_plan([&] { return fftwl_plan_dft_c2r_1d(nfft, src, dst, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwl_execute_dft_c2r(m_plan.get(), src, dst);
}
inline void fwd2(complex_type *dst, complex_type *src, int n0, int n1) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftwl_plan_dft_2d(n0, n1, src, dst, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwl_execute_dft(m_plan.get(), src, dst);
}
inline void inv2(complex_type *dst, complex_type *src, int n0, int n1) {
if (!m_plan)
set_plan(fftw_make_plan(
[&] { return fftwl_plan_dft_2d(n0, n1, src, dst, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT); }));
fftwl_execute_dft(m_plan.get(), src, dst);
}
};
template <typename Scalar_>
struct fftw_impl {
using Scalar = Scalar_;
using Complex = std::complex<Scalar>;
inline void clear() { m_plans.clear(); }
// complex-to-complex forward FFT
inline void fwd(Complex *dst, const Complex *src, int nfft) {
get_plan(nfft, false, /*real_io=*/false, dst, src).fwd(fftw_cast(dst), fftw_cast(src), nfft);
}
// real-to-complex forward FFT
inline void fwd(Complex *dst, const Scalar *src, int nfft) {
get_plan(nfft, false, /*real_io=*/true, dst, src).fwd(fftw_cast(dst), fftw_cast(src), nfft);
}
// 2-d complex-to-complex
inline void fwd2(Complex *dst, const Complex *src, int n0, int n1) {
get_plan(n0, n1, false, /*real_io=*/false, dst, src).fwd2(fftw_cast(dst), fftw_cast(src), n0, n1);
}
// inverse complex-to-complex
inline void inv(Complex *dst, const Complex *src, int nfft) {
get_plan(nfft, true, /*real_io=*/false, dst, src).inv(fftw_cast(dst), fftw_cast(src), nfft);
}
// half-complex to scalar
inline void inv(Scalar *dst, const Complex *src, int nfft) {
get_plan(nfft, true, /*real_io=*/true, dst, src).inv(fftw_cast(dst), fftw_cast(src), nfft);
}
// 2-d complex-to-complex
inline void inv2(Complex *dst, const Complex *src, int n0, int n1) {
get_plan(n0, n1, true, /*real_io=*/false, dst, src).inv2(fftw_cast(dst), fftw_cast(src), n0, n1);
}
protected:
using PlanData = fftw_plan<Scalar>;
// (n0, n1, plan_flags), with n1 = -1 for a 1-d transform. Transforms that share a key share a plan, which runs
// over the sizes it was created for.
using PlanKey = std::tuple<int, int, int>;
using PlanMap = std::map<PlanKey, PlanData>;
PlanMap m_plans;
// real_io separates r2c/c2r plans from c2c ones. The key holds the src and dst addresses mod 16 separately: a plan
// created without FFTW_UNALIGNED runs on new arrays only if each has the fftw_alignment_of it was created with
// (FFTW manual, "New-array Execute Functions").
static int plan_flags(bool inverse, bool real_io, void *dst, const void *src) {
bool inplace = (dst == src);
int src_alignment = static_cast<int>(reinterpret_cast<size_t>(src) & 15);
int dst_alignment = static_cast<int>(reinterpret_cast<size_t>(dst) & 15);
return (inverse << 10) | (real_io << 9) | (inplace << 8) | (src_alignment << 4) | dst_alignment;
}
inline PlanData &get_plan(int nfft, bool inverse, bool real_io, void *dst, const void *src) {
return m_plans[PlanKey(nfft, -1, plan_flags(inverse, real_io, dst, src))];
}
inline PlanData &get_plan(int n0, int n1, bool inverse, bool real_io, void *dst, const void *src) {
return m_plans[PlanKey(n0, n1, plan_flags(inverse, real_io, dst, src))];
}
};
} // end namespace internal
} // end namespace Eigen
#endif // EIGEN_FFT_FFTW_IMPL_H