fft_wrapper.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. #ifndef _SCTL_FFT_WRAPPER_
  2. #define _SCTL_FFT_WRAPPER_
  3. #include <cmath>
  4. #include <cassert>
  5. #include <cstdlib>
  6. #include <vector>
  7. #if defined(SCTL_HAVE_FFTW) || defined(SCTL_HAVE_FFTWF)
  8. #include <fftw3.h>
  9. #ifdef SCTL_FFTW3_MKL
  10. #include <fftw3_mkl.h>
  11. #endif
  12. #endif
  13. #include <sctl/common.hpp>
  14. #include SCTL_INCLUDE(matrix.hpp)
  15. #include SCTL_INCLUDE(math_utils.hpp)
  16. namespace SCTL_NAMESPACE {
  17. template <class ValueType> class Complex {
  18. public:
  19. Complex<ValueType>(ValueType r=0, ValueType i=0) : real(r), imag(i) {}
  20. Complex<ValueType> operator-() const {
  21. Complex<ValueType> z;
  22. z.real = -real;
  23. z.imag = -imag;
  24. return z;
  25. }
  26. Complex<ValueType> conj() const {
  27. Complex<ValueType> z;
  28. z.real = real;
  29. z.imag = -imag;
  30. return z;
  31. }
  32. bool operator==(const Complex<ValueType>& x) const {
  33. return real == x.real && imag == x.imag;
  34. }
  35. bool operator!=(const Complex<ValueType>& x) const {
  36. return !((*this) == x);;
  37. }
  38. template <class ScalarType> void operator+=(const Complex<ScalarType>& x) {
  39. (*this) = (*this) + x;
  40. }
  41. template <class ScalarType> void operator-=(const Complex<ScalarType>& x) {
  42. (*this) = (*this) - x;
  43. }
  44. template <class ScalarType> void operator*=(const Complex<ScalarType>& x) {
  45. (*this) = (*this) * x;
  46. }
  47. template <class ScalarType> void operator/=(const Complex<ScalarType>& x) {
  48. (*this) = (*this) / x;
  49. }
  50. template <class ScalarType> Complex<ValueType> operator+(const ScalarType& x) const {
  51. Complex<ValueType> z;
  52. z.real = real + x;
  53. z.imag = imag;
  54. return z;
  55. }
  56. template <class ScalarType> Complex<ValueType> operator-(const ScalarType& x) const {
  57. Complex<ValueType> z;
  58. z.real = real - x;
  59. z.imag = imag;
  60. return z;
  61. }
  62. template <class ScalarType> Complex<ValueType> operator*(const ScalarType& x) const {
  63. Complex<ValueType> z;
  64. z.real = real * x;
  65. z.imag = imag * x;
  66. return z;
  67. }
  68. template <class ScalarType> Complex<ValueType> operator/(const ScalarType& y) const {
  69. Complex<ValueType> z;
  70. z.real = real / y;
  71. z.imag = imag / y;
  72. return z;
  73. }
  74. Complex<ValueType> operator+(const Complex<ValueType>& x) const {
  75. Complex<ValueType> z;
  76. z.real = real + x.real;
  77. z.imag = imag + x.imag;
  78. return z;
  79. }
  80. Complex<ValueType> operator-(const Complex<ValueType>& x) const {
  81. Complex<ValueType> z;
  82. z.real = real - x.real;
  83. z.imag = imag - x.imag;
  84. return z;
  85. }
  86. Complex<ValueType> operator*(const Complex<ValueType>& x) const {
  87. Complex<ValueType> z;
  88. z.real = real * x.real - imag * x.imag;
  89. z.imag = imag * x.real + real * x.imag;
  90. return z;
  91. }
  92. Complex<ValueType> operator/(const Complex<ValueType>& y) const {
  93. Complex<ValueType> z;
  94. ValueType y_inv = 1 / (y.real * y.real + y.imag * y.imag);
  95. z.real = (y.real * real + y.imag * imag) * y_inv;
  96. z.imag = (y.real * imag - y.imag * real) * y_inv;
  97. return z;
  98. }
  99. ValueType real;
  100. ValueType imag;
  101. };
  102. template <class ScalarType, class ValueType> Complex<ValueType> operator*(const ScalarType& x, const Complex<ValueType>& y) {
  103. Complex<ValueType> z;
  104. z.real = y.real * x;
  105. z.imag = y.imag * x;
  106. return z;
  107. }
  108. template <class ScalarType, class ValueType> Complex<ValueType> operator+(const ScalarType& x, const Complex<ValueType>& y) {
  109. Complex<ValueType> z;
  110. z.real = y.real + x;
  111. z.imag = y.imag;
  112. return z;
  113. }
  114. template <class ScalarType, class ValueType> Complex<ValueType> operator-(const ScalarType& x, const Complex<ValueType>& y) {
  115. Complex<ValueType> z;
  116. z.real = y.real - x;
  117. z.imag = y.imag;
  118. return z;
  119. }
  120. template <class ScalarType, class ValueType> Complex<ValueType> operator/(const ScalarType& x, const Complex<ValueType>& y) {
  121. Complex<ValueType> z;
  122. ValueType y_inv = 1 / (y.real * y.real + y.imag * y.imag);
  123. z.real = (y.real * x) * y_inv;
  124. z.imag = -(y.imag * x) * y_inv;
  125. return z;
  126. }
  127. template <class ValueType> std::ostream& operator<<(std::ostream& output, const Complex<ValueType>& V) {
  128. output << "(" << V.real <<"," << V.imag << ")";
  129. return output;
  130. }
  131. enum class FFT_Type {R2C, C2C, C2C_INV, C2R};
  132. template <class ValueType, class FFT_Derived> class FFT_Base {
  133. public:
  134. FFT_Base() : dim{0,0}, fft_type(FFT_Type::R2C), howmany_(0) {}
  135. Long Dim(Integer i) const {
  136. return dim[i];
  137. }
  138. static void test() {
  139. static constexpr ValueType eps = machine_eps<ValueType>() * 64;
  140. Vector<Long> fft_dim;
  141. fft_dim.PushBack(2);
  142. fft_dim.PushBack(5);
  143. fft_dim.PushBack(3);
  144. Long howmany = 3;
  145. if (1){ // R2C, C2R
  146. FFT_Derived myfft0, myfft1;
  147. myfft0.Setup(FFT_Type::R2C, howmany, fft_dim);
  148. myfft1.Setup(FFT_Type::C2R, howmany, fft_dim);
  149. Vector<ValueType> v0(myfft0.Dim(0)), v1, v2;
  150. for (int i = 0; i < v0.Dim(); i++) v0[i] = (1 + i) / (ValueType)v0.Dim();
  151. myfft0.Execute(v0, v1);
  152. myfft1.Execute(v1, v2);
  153. { // Print error
  154. ValueType err = 0;
  155. SCTL_ASSERT(v0.Dim() == v2.Dim());
  156. for (Long i = 0; i < v0.Dim(); i++) err = std::max(err, fabs(v0[i] - v2[i]));
  157. std::cout<<"Error : "<<err<<'\n';
  158. SCTL_ASSERT(err < eps);
  159. }
  160. }
  161. std::cout<<'\n';
  162. { // C2C, C2C_INV
  163. FFT_Derived myfft0, myfft1;
  164. myfft0.Setup(FFT_Type::C2C, howmany, fft_dim);
  165. myfft1.Setup(FFT_Type::C2C_INV, howmany, fft_dim);
  166. Vector<ValueType> v0(myfft0.Dim(0)), v1, v2;
  167. for (int i = 0; i < v0.Dim(); i++) v0[i] = (1 + i) / (ValueType)v0.Dim();
  168. myfft0.Execute(v0, v1);
  169. myfft1.Execute(v1, v2);
  170. { // Print error
  171. ValueType err = 0;
  172. SCTL_ASSERT(v0.Dim() == v2.Dim());
  173. for (Long i = 0; i < v0.Dim(); i++) err = std::max(err, fabs(v0[i] - v2[i]));
  174. std::cout<<"Error : "<<err<<'\n';
  175. SCTL_ASSERT(err < eps);
  176. }
  177. }
  178. std::cout<<'\n';
  179. }
  180. protected:
  181. static void check_align(const Vector<ValueType>& in, const Vector<ValueType>& out) {
  182. //SCTL_ASSERT_MSG((((uintptr_t)& in[0]) & ((uintptr_t)(SCTL_MEM_ALIGN - 1))) == 0, "sctl::FFT: Input vector not aligned to " <<SCTL_MEM_ALIGN<<" bytes!");
  183. //SCTL_ASSERT_MSG((((uintptr_t)&out[0]) & ((uintptr_t)(SCTL_MEM_ALIGN - 1))) == 0, "sctl::FFT: Output vector not aligned to "<<SCTL_MEM_ALIGN<<" bytes!");
  184. // TODO: copy to auxiliary array if unaligned
  185. }
  186. StaticArray<Long,2> dim;
  187. FFT_Type fft_type;
  188. Long howmany_;
  189. };
  190. template <class ValueType> class FFT : public FFT_Base<ValueType, FFT<ValueType>> {
  191. typedef Complex<ValueType> ComplexType;
  192. struct FFTPlan {
  193. std::vector<Matrix<ValueType>> M;
  194. };
  195. public:
  196. FFT() = default;
  197. FFT (const FFT&) = delete;
  198. FFT& operator= (const FFT&) = delete;
  199. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec, Integer Nthreads = 1) {
  200. Long rank = dim_vec.Dim();
  201. this->fft_type = fft_type_;
  202. this->howmany_ = howmany_;
  203. plan.M.resize(0);
  204. if (this->fft_type == FFT_Type::R2C) {
  205. plan.M.push_back(fft_r2c(dim_vec[rank - 1]));
  206. for (Long i = rank - 2; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]));
  207. } else if (this->fft_type == FFT_Type::C2C) {
  208. for (Long i = rank - 1; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]));
  209. } else if (this->fft_type == FFT_Type::C2C_INV) {
  210. for (Long i = rank - 1; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]).Transpose());
  211. } else if (this->fft_type == FFT_Type::C2R) {
  212. for (Long i = rank - 2; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]).Transpose());
  213. plan.M.push_back(fft_c2r(dim_vec[rank - 1]));
  214. }
  215. Long N0 = this->howmany_ * 2;
  216. Long N1 = this->howmany_ * 2;
  217. for (const auto& M : plan.M) {
  218. N0 = N0 * M.Dim(0) / 2;
  219. N1 = N1 * M.Dim(1) / 2;
  220. }
  221. this->dim[0] = N0;
  222. this->dim[1] = N1;
  223. }
  224. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  225. Long N0 = this->Dim(0);
  226. Long N1 = this->Dim(1);
  227. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  228. if (out.Dim() != N1) out.ReInit(N1);
  229. this->check_align(in, out);
  230. Vector<ValueType> buff0(N0 + N1);
  231. Vector<ValueType> buff1(N0 + N1);
  232. Long rank = plan.M.size();
  233. if (rank <= 0) return;
  234. Long N = N0;
  235. if (this->fft_type == FFT_Type::C2R) {
  236. const Matrix<ValueType>& M = plan.M[rank - 1];
  237. transpose<ComplexType>(buff0.begin(), in.begin(), N / M.Dim(0), M.Dim(0) / 2);
  238. for (Long i = 0; i < rank - 1; i++) {
  239. const Matrix<ValueType>& M = plan.M[i];
  240. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff0.begin(), false);
  241. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), buff1.begin(), false);
  242. Matrix<ValueType>::GEMM(vo, vi, M);
  243. N = N * M.Dim(1) / M.Dim(0);
  244. transpose<ComplexType>(buff0.begin(), buff1.begin(), N / M.Dim(1), M.Dim(1) / 2);
  245. }
  246. transpose<ComplexType>(buff1.begin(), buff0.begin(), N / this->howmany_ / 2, this->howmany_);
  247. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff1.begin(), false);
  248. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), out.begin(), false);
  249. Matrix<ValueType>::GEMM(vo, vi, M);
  250. } else {
  251. memcopy(buff0.begin(), in.begin(), in.Dim());
  252. for (Long i = 0; i < rank; i++) {
  253. const Matrix<ValueType>& M = plan.M[i];
  254. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff0.begin(), false);
  255. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), buff1.begin(), false);
  256. Matrix<ValueType>::GEMM(vo, vi, M);
  257. N = N * M.Dim(1) / M.Dim(0);
  258. transpose<ComplexType>(buff0.begin(), buff1.begin(), N / M.Dim(1), M.Dim(1) / 2);
  259. }
  260. transpose<ComplexType>(out.begin(), buff0.begin(), N / this->howmany_ / 2, this->howmany_);
  261. }
  262. }
  263. private:
  264. static Matrix<ValueType> fft_r2c(Long N0) {
  265. ValueType s = 1 / sqrt<ValueType>(N0);
  266. Long N1 = (N0 / 2 + 1);
  267. Matrix<ValueType> M(N0, 2 * N1);
  268. for (Long j = 0; j < N0; j++)
  269. for (Long i = 0; i < N1; i++) {
  270. M[j][2 * i + 0] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  271. M[j][2 * i + 1] = -sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  272. }
  273. return M;
  274. }
  275. static Matrix<ValueType> fft_c2c(Long N0) {
  276. ValueType s = 1 / sqrt<ValueType>(N0);
  277. Matrix<ValueType> M(2 * N0, 2 * N0);
  278. for (Long i = 0; i < N0; i++)
  279. for (Long j = 0; j < N0; j++) {
  280. M[2 * i + 0][2 * j + 0] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  281. M[2 * i + 1][2 * j + 0] = sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  282. M[2 * i + 0][2 * j + 1] = -sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  283. M[2 * i + 1][2 * j + 1] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  284. }
  285. return M;
  286. }
  287. static Matrix<ValueType> fft_c2r(Long N0) {
  288. ValueType s = 1 / sqrt<ValueType>(N0);
  289. Long N1 = (N0 / 2 + 1);
  290. Matrix<ValueType> M(2 * N1, N0);
  291. for (Long i = 0; i < N1; i++) {
  292. for (Long j = 0; j < N0; j++) {
  293. M[2 * i + 0][j] = 2 * cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  294. M[2 * i + 1][j] = -2 * sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  295. }
  296. }
  297. if (N1 > 0) {
  298. for (Long j = 0; j < N0; j++) {
  299. M[0][j] = M[0][j] * (ValueType)0.5;
  300. M[1][j] = M[1][j] * (ValueType)0.5;
  301. }
  302. }
  303. if (N0 % 2 == 0) {
  304. for (Long j = 0; j < N0; j++) {
  305. M[2 * N1 - 2][j] = M[2 * N1 - 2][j] * (ValueType)0.5;
  306. M[2 * N1 - 1][j] = M[2 * N1 - 1][j] * (ValueType)0.5;
  307. }
  308. }
  309. return M;
  310. }
  311. template <class T> static void transpose(Iterator<ValueType> out, ConstIterator<ValueType> in, Long N0, Long N1) {
  312. const Matrix<T> M0(N0, N1, (Iterator<T>)in, false);
  313. Matrix<T> M1(N1, N0, (Iterator<T>)out, false);
  314. M1 = M0.Transpose();
  315. }
  316. FFTPlan plan;
  317. };
  318. static inline void FFTWInitThreads(Integer Nthreads) {
  319. #ifdef SCTL_FFTW_THREADS
  320. static bool first_time = true;
  321. #pragma omp critical(SCTL_FFTW_INIT_THREADS)
  322. if (first_time) {
  323. fftw_init_threads();
  324. first_time = false;
  325. }
  326. fftw_plan_with_nthreads(Nthreads);
  327. #endif
  328. }
  329. #ifdef SCTL_HAVE_FFTW
  330. template <> class FFT<double> : public FFT_Base<double, FFT<double>> {
  331. typedef double ValueType;
  332. public:
  333. FFT() = default;
  334. FFT(const FFT&) = delete;
  335. FFT& operator=(const FFT&) = delete;
  336. ~FFT() { if (Dim(0) && Dim(1)) fftw_destroy_plan(plan); }
  337. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec, Integer Nthreads = 1) {
  338. FFTWInitThreads(Nthreads);
  339. if (Dim(0) && Dim(1)) fftw_destroy_plan(plan);
  340. fft_type = fft_type_;
  341. this->howmany_ = howmany_;
  342. copy_input = false;
  343. plan = NULL;
  344. Long rank = dim_vec.Dim();
  345. Vector<int> dim_vec_(rank);
  346. for (Integer i = 0; i < rank; i++) {
  347. dim_vec_[i] = dim_vec[i];
  348. }
  349. Long N0 = 0, N1 = 0;
  350. { // Set N0, N1
  351. Long N = this->howmany_;
  352. for (auto ni : dim_vec) N *= ni;
  353. if (fft_type == FFT_Type::R2C) {
  354. N0 = N;
  355. N1 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  356. } else if (fft_type == FFT_Type::C2C) {
  357. N0 = N * 2;
  358. N1 = N * 2;
  359. } else if (fft_type == FFT_Type::C2C_INV) {
  360. N0 = N * 2;
  361. N1 = N * 2;
  362. } else if (fft_type == FFT_Type::C2R) {
  363. N0 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  364. N1 = N;
  365. } else {
  366. N0 = 0;
  367. N1 = 0;
  368. }
  369. dim[0] = N0;
  370. dim[1] = N1;
  371. }
  372. if (!N0 || !N1) return;
  373. Vector<ValueType> in(N0), out(N1);
  374. if (fft_type == FFT_Type::R2C) {
  375. plan = fftw_plan_many_dft_r2c(rank, &dim_vec_[0], this->howmany_, &in[0], NULL, 1, N0 / this->howmany_, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  376. } else if (fft_type == FFT_Type::C2C) {
  377. plan = fftw_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  378. } else if (fft_type == FFT_Type::C2C_INV) {
  379. plan = fftw_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  380. } else if (fft_type == FFT_Type::C2R) {
  381. plan = fftw_plan_many_dft_c2r(rank, &dim_vec_[0], this->howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, &out[0], NULL, 1, N1 / this->howmany_, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  382. }
  383. if (!plan) { // Build plan without FFTW_PRESERVE_INPUT
  384. if (fft_type == FFT_Type::R2C) {
  385. plan = fftw_plan_many_dft_r2c(rank, &dim_vec_[0], this->howmany_, &in[0], NULL, 1, N0 / this->howmany_, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_ESTIMATE);
  386. } else if (fft_type == FFT_Type::C2C) {
  387. plan = fftw_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_FORWARD, FFTW_ESTIMATE);
  388. } else if (fft_type == FFT_Type::C2C_INV) {
  389. plan = fftw_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_BACKWARD, FFTW_ESTIMATE);
  390. } else if (fft_type == FFT_Type::C2R) {
  391. plan = fftw_plan_many_dft_c2r(rank, &dim_vec_[0], this->howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, &out[0], NULL, 1, N1 / this->howmany_, FFTW_ESTIMATE);
  392. }
  393. copy_input = true;
  394. }
  395. SCTL_ASSERT(plan);
  396. }
  397. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  398. Long N0 = Dim(0);
  399. Long N1 = Dim(1);
  400. if (!N0 || !N1) return;
  401. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  402. if (out.Dim() != N1) out.ReInit(N1);
  403. check_align(in, out);
  404. ValueType s = 0;
  405. Vector<ValueType> tmp;
  406. auto in_ptr = in.begin();
  407. if (copy_input) { // Save input
  408. tmp.ReInit(N0);
  409. in_ptr = tmp.begin();
  410. tmp = in;
  411. }
  412. if (fft_type == FFT_Type::R2C) {
  413. s = 1 / sqrt<ValueType>(N0 / this->howmany_);
  414. fftw_execute_dft_r2c(plan, (double*)&in_ptr[0], (fftw_complex*)&out[0]);
  415. } else if (fft_type == FFT_Type::C2C) {
  416. s = 1 / sqrt<ValueType>(N0 / this->howmany_ * (ValueType)0.5);
  417. fftw_execute_dft(plan, (fftw_complex*)&in_ptr[0], (fftw_complex*)&out[0]);
  418. } else if (fft_type == FFT_Type::C2C_INV) {
  419. s = 1 / sqrt<ValueType>(N1 / this->howmany_ * (ValueType)0.5);
  420. fftw_execute_dft(plan, (fftw_complex*)&in_ptr[0], (fftw_complex*)&out[0]);
  421. } else if (fft_type == FFT_Type::C2R) {
  422. s = 1 / sqrt<ValueType>(N1 / this->howmany_);
  423. fftw_execute_dft_c2r(plan, (fftw_complex*)&in_ptr[0], (double*)&out[0]);
  424. }
  425. for (auto& x : out) x *= s;
  426. }
  427. private:
  428. bool copy_input;
  429. fftw_plan plan;
  430. };
  431. #endif
  432. #ifdef SCTL_HAVE_FFTWF
  433. template <> class FFT<float> : public FFT_Base<float, FFT<float>> {
  434. typedef float ValueType;
  435. public:
  436. FFT() = default;
  437. FFT(const FFT&) = delete;
  438. FFT& operator=(const FFT&) = delete;
  439. ~FFT() { if (Dim(0) && Dim(1)) fftwf_destroy_plan(plan); }
  440. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec, Integer Nthreads = 1) {
  441. FFTWInitThreads(Nthreads);
  442. if (Dim(0) && Dim(1)) fftwf_destroy_plan(plan);
  443. fft_type = fft_type_;
  444. this->howmany_ = howmany_;
  445. copy_input = false;
  446. plan = NULL;
  447. Long rank = dim_vec.Dim();
  448. Vector<int> dim_vec_(rank);
  449. for (Integer i = 0; i < rank; i++) {
  450. dim_vec_[i] = dim_vec[i];
  451. }
  452. Long N0, N1;
  453. { // Set N0, N1
  454. Long N = this->howmany_;
  455. for (auto ni : dim_vec) N *= ni;
  456. if (fft_type == FFT_Type::R2C) {
  457. N0 = N;
  458. N1 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  459. } else if (fft_type == FFT_Type::C2C) {
  460. N0 = N * 2;
  461. N1 = N * 2;
  462. } else if (fft_type == FFT_Type::C2C_INV) {
  463. N0 = N * 2;
  464. N1 = N * 2;
  465. } else if (fft_type == FFT_Type::C2R) {
  466. N0 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  467. N1 = N;
  468. } else {
  469. N0 = 0;
  470. N1 = 0;
  471. }
  472. dim[0] = N0;
  473. dim[1] = N1;
  474. }
  475. if (!N0 || !N1) return;
  476. Vector<ValueType> in (N0), out(N1);
  477. if (fft_type == FFT_Type::R2C) {
  478. plan = fftwf_plan_many_dft_r2c(rank, &dim_vec_[0], this->howmany_, &in[0], NULL, 1, N0 / this->howmany_, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  479. } else if (fft_type == FFT_Type::C2C) {
  480. plan = fftwf_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  481. } else if (fft_type == FFT_Type::C2C_INV) {
  482. plan = fftwf_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  483. } else if (fft_type == FFT_Type::C2R) {
  484. plan = fftwf_plan_many_dft_c2r(rank, &dim_vec_[0], this->howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, &out[0], NULL, 1, N1 / this->howmany_, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  485. }
  486. if (!plan) { // Build plan without FFTW_PRESERVE_INPUT
  487. if (fft_type == FFT_Type::R2C) {
  488. plan = fftwf_plan_many_dft_r2c(rank, &dim_vec_[0], this->howmany_, &in[0], NULL, 1, N0 / this->howmany_, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_ESTIMATE);
  489. } else if (fft_type == FFT_Type::C2C) {
  490. plan = fftwf_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_FORWARD, FFTW_ESTIMATE);
  491. } else if (fft_type == FFT_Type::C2C_INV) {
  492. plan = fftwf_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_BACKWARD, FFTW_ESTIMATE);
  493. } else if (fft_type == FFT_Type::C2R) {
  494. plan = fftwf_plan_many_dft_c2r(rank, &dim_vec_[0], this->howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, &out[0], NULL, 1, N1 / this->howmany_, FFTW_ESTIMATE);
  495. }
  496. copy_input = true;
  497. }
  498. SCTL_ASSERT(plan);
  499. }
  500. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  501. Long N0 = Dim(0);
  502. Long N1 = Dim(1);
  503. if (!N0 || !N1) return;
  504. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  505. if (out.Dim() != N1) out.ReInit(N1);
  506. check_align(in, out);
  507. ValueType s = 0;
  508. Vector<ValueType> tmp;
  509. auto in_ptr = in.begin();
  510. if (copy_input) { // Save input
  511. tmp.ReInit(N0);
  512. in_ptr = tmp.begin();
  513. tmp = in;
  514. }
  515. if (fft_type == FFT_Type::R2C) {
  516. s = 1 / sqrt<ValueType>(N0 / this->howmany_);
  517. fftwf_execute_dft_r2c(plan, (float*)&in_ptr[0], (fftwf_complex*)&out[0]);
  518. } else if (fft_type == FFT_Type::C2C) {
  519. s = 1 / sqrt<ValueType>(N0 / this->howmany_ * (ValueType)0.5);
  520. fftwf_execute_dft(plan, (fftwf_complex*)&in_ptr[0], (fftwf_complex*)&out[0]);
  521. } else if (fft_type == FFT_Type::C2C_INV) {
  522. s = 1 / sqrt<ValueType>(N1 / this->howmany_ * (ValueType)0.5);
  523. fftwf_execute_dft(plan, (fftwf_complex*)&in_ptr[0], (fftwf_complex*)&out[0]);
  524. } else if (fft_type == FFT_Type::C2R) {
  525. s = 1 / sqrt<ValueType>(N1 / this->howmany_);
  526. fftwf_execute_dft_c2r(plan, (fftwf_complex*)&in_ptr[0], (float*)&out[0]);
  527. }
  528. for (auto& x : out) x *= s;
  529. }
  530. private:
  531. bool copy_input;
  532. fftwf_plan plan;
  533. };
  534. #endif
  535. #ifdef SCTL_HAVE_FFTWL
  536. template <> class FFT<long double> : public FFT_Base<long double, FFT<long double>> {
  537. typedef long double ValueType;
  538. public:
  539. FFT() = default;
  540. FFT(const FFT&) = delete;
  541. FFT& operator=(const FFT&) = delete;
  542. ~FFT() { if (Dim(0) && Dim(1)) fftwl_destroy_plan(plan); }
  543. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec, Integer Nthreads = 1) {
  544. FFTWInitThreads(Nthreads);
  545. if (Dim(0) && Dim(1)) fftwl_destroy_plan(plan);
  546. fft_type = fft_type_;
  547. this->howmany_ = howmany_;
  548. copy_input = false;
  549. plan = NULL;
  550. Long rank = dim_vec.Dim();
  551. Vector<int> dim_vec_(rank);
  552. for (Integer i = 0; i < rank; i++) dim_vec_[i] = dim_vec[i];
  553. Long N0, N1;
  554. { // Set N0, N1
  555. Long N = this->howmany_;
  556. for (auto ni : dim_vec) N *= ni;
  557. if (fft_type == FFT_Type::R2C) {
  558. N0 = N;
  559. N1 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  560. } else if (fft_type == FFT_Type::C2C) {
  561. N0 = N * 2;
  562. N1 = N * 2;
  563. } else if (fft_type == FFT_Type::C2C_INV) {
  564. N0 = N * 2;
  565. N1 = N * 2;
  566. } else if (fft_type == FFT_Type::C2R) {
  567. N0 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  568. N1 = N;
  569. } else {
  570. N0 = 0;
  571. N1 = 0;
  572. }
  573. dim[0] = N0;
  574. dim[1] = N1;
  575. }
  576. if (!N0 || !N1) return;
  577. Vector<ValueType> in (N0), out(N1);
  578. if (fft_type == FFT_Type::R2C) {
  579. plan = fftwl_plan_many_dft_r2c(rank, &dim_vec_[0], this->howmany_, &in[0], NULL, 1, N0 / this->howmany_, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  580. } else if (fft_type == FFT_Type::C2C) {
  581. plan = fftwl_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  582. } else if (fft_type == FFT_Type::C2C_INV) {
  583. plan = fftwl_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  584. } else if (fft_type == FFT_Type::C2R) {
  585. plan = fftwl_plan_many_dft_c2r(rank, &dim_vec_[0], this->howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, &out[0], NULL, 1, N1 / this->howmany_, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  586. }
  587. if (!plan) { // Build plan without FFTW_PRESERVE_INPUT
  588. if (fft_type == FFT_Type::R2C) {
  589. plan = fftwl_plan_many_dft_r2c(rank, &dim_vec_[0], this->howmany_, &in[0], NULL, 1, N0 / this->howmany_, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_ESTIMATE);
  590. } else if (fft_type == FFT_Type::C2C) {
  591. plan = fftwl_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_FORWARD, FFTW_ESTIMATE);
  592. } else if (fft_type == FFT_Type::C2C_INV) {
  593. plan = fftwl_plan_many_dft(rank, &dim_vec_[0], this->howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / this->howmany_, FFTW_BACKWARD, FFTW_ESTIMATE);
  594. } else if (fft_type == FFT_Type::C2R) {
  595. plan = fftwl_plan_many_dft_c2r(rank, &dim_vec_[0], this->howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / this->howmany_, &out[0], NULL, 1, N1 / this->howmany_, FFTW_ESTIMATE);
  596. }
  597. copy_input = true;
  598. }
  599. SCTL_ASSERT(plan);
  600. }
  601. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  602. Long N0 = Dim(0);
  603. Long N1 = Dim(1);
  604. if (!N0 || !N1) return;
  605. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  606. if (out.Dim() != N1) out.ReInit(N1);
  607. check_align(in, out);
  608. ValueType s = 0;
  609. Vector<ValueType> tmp;
  610. auto in_ptr = in.begin();
  611. if (copy_input) { // Save input
  612. tmp.ReInit(N0);
  613. in_ptr = tmp.begin();
  614. tmp = in;
  615. }
  616. if (fft_type == FFT_Type::R2C) {
  617. s = 1 / sqrt<ValueType>(N0 / this->howmany_);
  618. fftwl_execute_dft_r2c(plan, (long double*)&in_ptr[0], (fftwl_complex*)&out[0]);
  619. } else if (fft_type == FFT_Type::C2C) {
  620. s = 1 / sqrt<ValueType>(N0 / this->howmany_ * (ValueType)0.5);
  621. fftwl_execute_dft(plan, (fftwl_complex*)&in_ptr[0], (fftwl_complex*)&out[0]);
  622. } else if (fft_type == FFT_Type::C2C_INV) {
  623. s = 1 / sqrt<ValueType>(N1 / this->howmany_ * (ValueType)0.5);
  624. fftwl_execute_dft(plan, (fftwl_complex*)&in_ptr[0], (fftwl_complex*)&out[0]);
  625. } else if (fft_type == FFT_Type::C2R) {
  626. s = 1 / sqrt<ValueType>(N1 / this->howmany_);
  627. fftwl_execute_dft_c2r(plan, (fftwl_complex*)&in_ptr[0], (long double*)&out[0]);
  628. }
  629. for (auto& x : out) x *= s;
  630. }
  631. private:
  632. bool copy_input;
  633. fftwl_plan plan;
  634. };
  635. #endif
  636. } // end namespace
  637. #endif //_SCTL_FFT_WRAPPER_