fft_wrapper.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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_INCLUDE(common.hpp)
  14. #include SCTL_INCLUDE(mem_mgr.hpp)
  15. #include SCTL_INCLUDE(matrix.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_Generic {
  133. typedef Complex<ValueType> ComplexType;
  134. struct FFTPlan {
  135. std::vector<Matrix<ValueType>> M;
  136. };
  137. public:
  138. FFT_Generic() {
  139. dim[0] = 0;
  140. dim[1] = 0;
  141. }
  142. FFT_Generic(const FFT_Generic&) {
  143. dim[0]=0;
  144. dim[1]=0;
  145. }
  146. FFT_Generic& operator=(const FFT_Generic&) {
  147. dim[0]=0;
  148. dim[1]=0;
  149. return *this;
  150. };
  151. Long Dim(Integer i) const {
  152. return dim[i];
  153. }
  154. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec) {
  155. Long rank = dim_vec.Dim();
  156. fft_type = fft_type_;
  157. howmany = howmany_;
  158. plan.M.resize(0);
  159. if (fft_type == FFT_Type::R2C) {
  160. plan.M.push_back(fft_r2c(dim_vec[rank - 1]));
  161. for (Long i = rank - 2; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]));
  162. } else if (fft_type == FFT_Type::C2C) {
  163. for (Long i = rank - 1; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]));
  164. } else if (fft_type == FFT_Type::C2C_INV) {
  165. for (Long i = rank - 1; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]).Transpose());
  166. } else if (fft_type == FFT_Type::C2R) {
  167. for (Long i = rank - 2; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]).Transpose());
  168. plan.M.push_back(fft_c2r(dim_vec[rank - 1]));
  169. }
  170. Long N0 = howmany * 2;
  171. Long N1 = howmany * 2;
  172. for (const auto M : plan.M) {
  173. N0 = N0 * M.Dim(0) / 2;
  174. N1 = N1 * M.Dim(1) / 2;
  175. }
  176. dim[0] = N0;
  177. dim[1] = N1;
  178. }
  179. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  180. Long N0 = Dim(0);
  181. Long N1 = Dim(1);
  182. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  183. if (out.Dim() != N1) out.ReInit(N1);
  184. check_align(in, out);
  185. Vector<ValueType> buff0(N0 + N1);
  186. Vector<ValueType> buff1(N0 + N1);
  187. Long rank = plan.M.size();
  188. if (rank <= 0) return;
  189. Long N = N0;
  190. if (fft_type == FFT_Type::C2R) {
  191. const Matrix<ValueType>& M = plan.M[rank - 1];
  192. transpose<ComplexType>(buff0.begin(), in.begin(), N / M.Dim(0), M.Dim(0) / 2);
  193. for (Long i = 0; i < rank - 1; i++) {
  194. const Matrix<ValueType>& M = plan.M[i];
  195. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff0.begin(), false);
  196. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), buff1.begin(), false);
  197. Matrix<ValueType>::GEMM(vo, vi, M);
  198. N = N * M.Dim(1) / M.Dim(0);
  199. transpose<ComplexType>(buff0.begin(), buff1.begin(), N / M.Dim(1), M.Dim(1) / 2);
  200. }
  201. transpose<ComplexType>(buff1.begin(), buff0.begin(), N / howmany / 2, howmany);
  202. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff1.begin(), false);
  203. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), out.begin(), false);
  204. Matrix<ValueType>::GEMM(vo, vi, M);
  205. } else {
  206. memcopy(buff0.begin(), in.begin(), in.Dim());
  207. for (Long i = 0; i < rank; i++) {
  208. const Matrix<ValueType>& M = plan.M[i];
  209. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff0.begin(), false);
  210. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), buff1.begin(), false);
  211. Matrix<ValueType>::GEMM(vo, vi, M);
  212. N = N * M.Dim(1) / M.Dim(0);
  213. transpose<ComplexType>(buff0.begin(), buff1.begin(), N / M.Dim(1), M.Dim(1) / 2);
  214. }
  215. transpose<ComplexType>(out.begin(), buff0.begin(), N / howmany / 2, howmany);
  216. }
  217. }
  218. static void test() {
  219. Vector<Long> fft_dim;
  220. fft_dim.PushBack(2);
  221. fft_dim.PushBack(5);
  222. fft_dim.PushBack(3);
  223. Long howmany = 3;
  224. if (1){ // R2C, C2R
  225. FFT_Derived myfft0, myfft1;
  226. myfft0.Setup(FFT_Type::R2C, howmany, fft_dim);
  227. myfft1.Setup(FFT_Type::C2R, howmany, fft_dim);
  228. Vector<ValueType> v0(myfft0.Dim(0)), v1, v2;
  229. for (int i = 0; i < v0.Dim(); i++) v0[i] = 1 + i;
  230. myfft0.Execute(v0, v1);
  231. myfft1.Execute(v1, v2);
  232. { // Print error
  233. ValueType err = 0;
  234. SCTL_ASSERT(v0.Dim() == v2.Dim());
  235. for (Long i = 0; i < v0.Dim(); i++) err = std::max(err, fabs(v0[i] - v2[i]));
  236. std::cout<<"Error : "<<err<<'\n';
  237. }
  238. }
  239. std::cout<<'\n';
  240. { // C2C, C2C_INV
  241. FFT_Derived myfft0, myfft1;
  242. myfft0.Setup(FFT_Type::C2C, howmany, fft_dim);
  243. myfft1.Setup(FFT_Type::C2C_INV, howmany, fft_dim);
  244. Vector<ValueType> v0(myfft0.Dim(0)), v1, v2;
  245. for (int i = 0; i < v0.Dim(); i++) v0[i] = 1 + i;
  246. myfft0.Execute(v0, v1);
  247. myfft1.Execute(v1, v2);
  248. { // Print error
  249. ValueType err = 0;
  250. SCTL_ASSERT(v0.Dim() == v2.Dim());
  251. for (Long i = 0; i < v0.Dim(); i++) err = std::max(err, fabs(v0[i] - v2[i]));
  252. std::cout<<"Error : "<<err<<'\n';
  253. }
  254. }
  255. std::cout<<'\n';
  256. }
  257. protected:
  258. static Matrix<ValueType> fft_r2c(Long N0) {
  259. ValueType s = 1 / sqrt<ValueType>(N0);
  260. Long N1 = (N0 / 2 + 1);
  261. Matrix<ValueType> M(N0, 2 * N1);
  262. for (Long j = 0; j < N0; j++)
  263. for (Long i = 0; i < N1; i++) {
  264. M[j][2 * i + 0] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  265. M[j][2 * i + 1] = -sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  266. }
  267. return M;
  268. }
  269. static Matrix<ValueType> fft_c2c(Long N0) {
  270. ValueType s = 1 / sqrt<ValueType>(N0);
  271. Matrix<ValueType> M(2 * N0, 2 * N0);
  272. for (Long i = 0; i < N0; i++)
  273. for (Long j = 0; j < N0; j++) {
  274. M[2 * i + 0][2 * j + 0] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  275. M[2 * i + 1][2 * j + 0] = sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  276. M[2 * i + 0][2 * j + 1] = -sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  277. M[2 * i + 1][2 * j + 1] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  278. }
  279. return M;
  280. }
  281. static Matrix<ValueType> fft_c2r(Long N0) {
  282. ValueType s = 1 / sqrt<ValueType>(N0);
  283. Long N1 = (N0 / 2 + 1);
  284. Matrix<ValueType> M(2 * N1, N0);
  285. for (Long i = 0; i < N1; i++) {
  286. for (Long j = 0; j < N0; j++) {
  287. M[2 * i + 0][j] = 2 * cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  288. M[2 * i + 1][j] = -2 * sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  289. }
  290. }
  291. if (N1 > 0) {
  292. for (Long j = 0; j < N0; j++) {
  293. M[0][j] = M[0][j] * (ValueType)0.5;
  294. M[1][j] = M[1][j] * (ValueType)0.5;
  295. }
  296. }
  297. if (N0 % 2 == 0) {
  298. for (Long j = 0; j < N0; j++) {
  299. M[2 * N1 - 2][j] = M[2 * N1 - 2][j] * (ValueType)0.5;
  300. M[2 * N1 - 1][j] = M[2 * N1 - 1][j] * (ValueType)0.5;
  301. }
  302. }
  303. return M;
  304. }
  305. template <class T> static void transpose(Iterator<ValueType> out, ConstIterator<ValueType> in, Long N0, Long N1) {
  306. Matrix<T> M0(N0, N1, (Iterator<T>)in, false);
  307. Matrix<T> M1(N1, N0, (Iterator<T>)out, false);
  308. M1 = M0.Transpose();
  309. }
  310. static void check_align(const Vector<ValueType>& in, const Vector<ValueType>& out) {
  311. //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!");
  312. //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!");
  313. // TODO: copy to auxiliary array if unaligned
  314. }
  315. StaticArray<Long,2> dim;
  316. FFT_Type fft_type;
  317. Long howmany;
  318. FFTPlan plan;
  319. };
  320. template <class ValueType> class FFT : public FFT_Generic<ValueType, FFT<ValueType>> {};
  321. #ifdef SCTL_HAVE_FFTW
  322. template <> class FFT<double> : public FFT_Generic<double, FFT<double>> {
  323. typedef double ValueType;
  324. public:
  325. ~FFT() { if (this->Dim(0) && this->Dim(1)) fftw_destroy_plan(plan); }
  326. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec) {
  327. if (Dim(0) && Dim(1)) fftw_destroy_plan(plan);
  328. this->fft_type = fft_type_;
  329. this->howmany = howmany_;
  330. copy_input = false;
  331. plan = NULL;
  332. Long rank = dim_vec.Dim();
  333. Vector<int> dim_vec_(rank);
  334. for (Integer i = 0; i < rank; i++) {
  335. dim_vec_[i] = dim_vec[i];
  336. }
  337. Long N0 = 0, N1 = 0;
  338. { // Set N0, N1
  339. Long N = howmany;
  340. for (auto ni : dim_vec) N *= ni;
  341. if (fft_type == FFT_Type::R2C) {
  342. N0 = N;
  343. N1 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  344. } else if (fft_type == FFT_Type::C2C) {
  345. N0 = N * 2;
  346. N1 = N * 2;
  347. } else if (fft_type == FFT_Type::C2C_INV) {
  348. N0 = N * 2;
  349. N1 = N * 2;
  350. } else if (fft_type == FFT_Type::C2R) {
  351. N0 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  352. N1 = N;
  353. } else {
  354. N0 = 0;
  355. N1 = 0;
  356. }
  357. this->dim[0] = N0;
  358. this->dim[1] = N1;
  359. }
  360. if (!N0 || !N1) return;
  361. Vector<ValueType> in(N0), out(N1);
  362. if (fft_type == FFT_Type::R2C) {
  363. plan = fftw_plan_many_dft_r2c(rank, &dim_vec_[0], howmany_, &in[0], NULL, 1, N0 / howmany, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  364. } else if (fft_type == FFT_Type::C2C) {
  365. plan = fftw_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  366. } else if (fft_type == FFT_Type::C2C_INV) {
  367. plan = fftw_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  368. } else if (fft_type == FFT_Type::C2R) {
  369. plan = fftw_plan_many_dft_c2r(rank, &dim_vec_[0], howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / howmany, &out[0], NULL, 1, N1 / howmany, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  370. }
  371. if (!plan) { // Build plan without FFTW_PRESERVE_INPUT
  372. if (fft_type == FFT_Type::R2C) {
  373. plan = fftw_plan_many_dft_r2c(rank, &dim_vec_[0], howmany_, &in[0], NULL, 1, N0 / howmany, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_ESTIMATE);
  374. } else if (fft_type == FFT_Type::C2C) {
  375. plan = fftw_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_FORWARD, FFTW_ESTIMATE);
  376. } else if (fft_type == FFT_Type::C2C_INV) {
  377. plan = fftw_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_BACKWARD, FFTW_ESTIMATE);
  378. } else if (fft_type == FFT_Type::C2R) {
  379. plan = fftw_plan_many_dft_c2r(rank, &dim_vec_[0], howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / howmany, &out[0], NULL, 1, N1 / howmany, FFTW_ESTIMATE);
  380. }
  381. copy_input = true;
  382. }
  383. SCTL_ASSERT(plan);
  384. }
  385. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  386. Long N0 = this->Dim(0);
  387. Long N1 = this->Dim(1);
  388. if (!N0 || !N1) return;
  389. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  390. if (out.Dim() != N1) out.ReInit(N1);
  391. check_align(in, out);
  392. ValueType s = 0;
  393. Vector<ValueType> tmp;
  394. auto in_ptr = in.begin();
  395. if (copy_input) { // Save input
  396. tmp.ReInit(N0);
  397. in_ptr = tmp.begin();
  398. tmp = in;
  399. }
  400. if (fft_type == FFT_Type::R2C) {
  401. s = 1 / sqrt<ValueType>(N0 / howmany);
  402. fftw_execute_dft_r2c(plan, (double*)&in_ptr[0], (fftw_complex*)&out[0]);
  403. } else if (fft_type == FFT_Type::C2C) {
  404. s = 1 / sqrt<ValueType>(N0 / howmany * (ValueType)0.5);
  405. fftw_execute_dft(plan, (fftw_complex*)&in_ptr[0], (fftw_complex*)&out[0]);
  406. } else if (fft_type == FFT_Type::C2C_INV) {
  407. s = 1 / sqrt<ValueType>(N1 / howmany * (ValueType)0.5);
  408. fftw_execute_dft(plan, (fftw_complex*)&in_ptr[0], (fftw_complex*)&out[0]);
  409. } else if (fft_type == FFT_Type::C2R) {
  410. s = 1 / sqrt<ValueType>(N1 / howmany);
  411. fftw_execute_dft_c2r(plan, (fftw_complex*)&in_ptr[0], (double*)&out[0]);
  412. }
  413. for (auto& x : out) x *= s;
  414. }
  415. private:
  416. bool copy_input;
  417. fftw_plan plan;
  418. };
  419. #endif
  420. #ifdef SCTL_HAVE_FFTWF
  421. template <> class FFT<float> : public FFT_Generic<float, FFT<float>> {
  422. typedef float ValueType;
  423. public:
  424. ~FFT() { if (this->Dim(0) && this->Dim(1)) fftwf_destroy_plan(plan); }
  425. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec) {
  426. if (Dim(0) && Dim(1)) fftwf_destroy_plan(plan);
  427. this->fft_type = fft_type_;
  428. this->howmany = howmany_;
  429. copy_input = false;
  430. plan = NULL;
  431. Long rank = dim_vec.Dim();
  432. Vector<int> dim_vec_(rank);
  433. for (Integer i = 0; i < rank; i++) {
  434. dim_vec_[i] = dim_vec[i];
  435. }
  436. Long N0, N1;
  437. { // Set N0, N1
  438. Long N = howmany;
  439. for (auto ni : dim_vec) N *= ni;
  440. if (fft_type == FFT_Type::R2C) {
  441. N0 = N;
  442. N1 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  443. } else if (fft_type == FFT_Type::C2C) {
  444. N0 = N * 2;
  445. N1 = N * 2;
  446. } else if (fft_type == FFT_Type::C2C_INV) {
  447. N0 = N * 2;
  448. N1 = N * 2;
  449. } else if (fft_type == FFT_Type::C2R) {
  450. N0 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  451. N1 = N;
  452. } else {
  453. N0 = 0;
  454. N1 = 0;
  455. }
  456. this->dim[0] = N0;
  457. this->dim[1] = N1;
  458. }
  459. if (!N0 || !N1) return;
  460. Vector<ValueType> in (N0), out(N1);
  461. if (fft_type == FFT_Type::R2C) {
  462. plan = fftwf_plan_many_dft_r2c(rank, &dim_vec_[0], howmany_, &in[0], NULL, 1, N0 / howmany, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  463. } else if (fft_type == FFT_Type::C2C) {
  464. plan = fftwf_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  465. } else if (fft_type == FFT_Type::C2C_INV) {
  466. plan = fftwf_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  467. } else if (fft_type == FFT_Type::C2R) {
  468. plan = fftwf_plan_many_dft_c2r(rank, &dim_vec_[0], howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / howmany, &out[0], NULL, 1, N1 / howmany, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  469. }
  470. if (!plan) { // Build plan without FFTW_PRESERVE_INPUT
  471. if (fft_type == FFT_Type::R2C) {
  472. plan = fftwf_plan_many_dft_r2c(rank, &dim_vec_[0], howmany_, &in[0], NULL, 1, N0 / howmany, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_ESTIMATE);
  473. } else if (fft_type == FFT_Type::C2C) {
  474. plan = fftwf_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_FORWARD, FFTW_ESTIMATE);
  475. } else if (fft_type == FFT_Type::C2C_INV) {
  476. plan = fftwf_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_BACKWARD, FFTW_ESTIMATE);
  477. } else if (fft_type == FFT_Type::C2R) {
  478. plan = fftwf_plan_many_dft_c2r(rank, &dim_vec_[0], howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / howmany, &out[0], NULL, 1, N1 / howmany, FFTW_ESTIMATE);
  479. }
  480. copy_input = true;
  481. }
  482. SCTL_ASSERT(plan);
  483. }
  484. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  485. Long N0 = this->Dim(0);
  486. Long N1 = this->Dim(1);
  487. if (!N0 || !N1) return;
  488. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  489. if (out.Dim() != N1) out.ReInit(N1);
  490. check_align(in, out);
  491. ValueType s = 0;
  492. Vector<ValueType> tmp;
  493. auto in_ptr = in.begin();
  494. if (copy_input) { // Save input
  495. tmp.ReInit(N0);
  496. in_ptr = tmp.begin();
  497. tmp = in;
  498. }
  499. if (fft_type == FFT_Type::R2C) {
  500. s = 1 / sqrt<ValueType>(N0 / howmany);
  501. fftwf_execute_dft_r2c(plan, (float*)&in_ptr[0], (fftwf_complex*)&out[0]);
  502. } else if (fft_type == FFT_Type::C2C) {
  503. s = 1 / sqrt<ValueType>(N0 / howmany * (ValueType)0.5);
  504. fftwf_execute_dft(plan, (fftwf_complex*)&in_ptr[0], (fftwf_complex*)&out[0]);
  505. } else if (fft_type == FFT_Type::C2C_INV) {
  506. s = 1 / sqrt<ValueType>(N1 / howmany * (ValueType)0.5);
  507. fftwf_execute_dft(plan, (fftwf_complex*)&in_ptr[0], (fftwf_complex*)&out[0]);
  508. } else if (fft_type == FFT_Type::C2R) {
  509. s = 1 / sqrt<ValueType>(N1 / howmany);
  510. fftwf_execute_dft_c2r(plan, (fftwf_complex*)&in_ptr[0], (float*)&out[0]);
  511. }
  512. for (auto& x : out) x *= s;
  513. }
  514. private:
  515. bool copy_input;
  516. fftwf_plan plan;
  517. };
  518. #endif
  519. #ifdef SCTL_HAVE_FFTWL
  520. template <> class FFT<long double> : public FFT_Generic<long double, FFT<long double>> {
  521. typedef long double ValueType;
  522. public:
  523. ~FFT() { if (this->Dim(0) && this->Dim(1)) fftwl_destroy_plan(plan); }
  524. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec) {
  525. if (Dim(0) && Dim(1)) fftwl_destroy_plan(plan);
  526. this->fft_type = fft_type_;
  527. this->howmany = howmany_;
  528. copy_input = false;
  529. plan = NULL;
  530. Long rank = dim_vec.Dim();
  531. Vector<int> dim_vec_(rank);
  532. for (Integer i = 0; i < rank; i++) dim_vec_[i] = dim_vec[i];
  533. Long N0, N1;
  534. { // Set N0, N1
  535. Long N = howmany;
  536. for (auto ni : dim_vec) N *= ni;
  537. if (fft_type == FFT_Type::R2C) {
  538. N0 = N;
  539. N1 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  540. } else if (fft_type == FFT_Type::C2C) {
  541. N0 = N * 2;
  542. N1 = N * 2;
  543. } else if (fft_type == FFT_Type::C2C_INV) {
  544. N0 = N * 2;
  545. N1 = N * 2;
  546. } else if (fft_type == FFT_Type::C2R) {
  547. N0 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  548. N1 = N;
  549. } else {
  550. N0 = 0;
  551. N1 = 0;
  552. }
  553. this->dim[0] = N0;
  554. this->dim[1] = N1;
  555. }
  556. if (!N0 || !N1) return;
  557. Vector<ValueType> in (N0), out(N1);
  558. if (fft_type == FFT_Type::R2C) {
  559. plan = fftwl_plan_many_dft_r2c(rank, &dim_vec_[0], howmany_, &in[0], NULL, 1, N0 / howmany, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  560. } else if (fft_type == FFT_Type::C2C) {
  561. plan = fftwl_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_FORWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  562. } else if (fft_type == FFT_Type::C2C_INV) {
  563. plan = fftwl_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_BACKWARD, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  564. } else if (fft_type == FFT_Type::C2R) {
  565. plan = fftwl_plan_many_dft_c2r(rank, &dim_vec_[0], howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / howmany, &out[0], NULL, 1, N1 / howmany, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT);
  566. }
  567. if (!plan) { // Build plan without FFTW_PRESERVE_INPUT
  568. if (fft_type == FFT_Type::R2C) {
  569. plan = fftwl_plan_many_dft_r2c(rank, &dim_vec_[0], howmany_, &in[0], NULL, 1, N0 / howmany, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_ESTIMATE);
  570. } else if (fft_type == FFT_Type::C2C) {
  571. plan = fftwl_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_FORWARD, FFTW_ESTIMATE);
  572. } else if (fft_type == FFT_Type::C2C_INV) {
  573. plan = fftwl_plan_many_dft(rank, &dim_vec_[0], howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_BACKWARD, FFTW_ESTIMATE);
  574. } else if (fft_type == FFT_Type::C2R) {
  575. plan = fftwl_plan_many_dft_c2r(rank, &dim_vec_[0], howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / howmany, &out[0], NULL, 1, N1 / howmany, FFTW_ESTIMATE);
  576. }
  577. copy_input = true;
  578. }
  579. SCTL_ASSERT(plan);
  580. }
  581. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  582. Long N0 = this->Dim(0);
  583. Long N1 = this->Dim(1);
  584. if (!N0 || !N1) return;
  585. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  586. if (out.Dim() != N1) out.ReInit(N1);
  587. check_align(in, out);
  588. ValueType s = 0;
  589. Vector<ValueType> tmp;
  590. auto in_ptr = in.begin();
  591. if (copy_input) { // Save input
  592. tmp.ReInit(N0);
  593. in_ptr = tmp.begin();
  594. tmp = in;
  595. }
  596. if (fft_type == FFT_Type::R2C) {
  597. s = 1 / sqrt<ValueType>(N0 / howmany);
  598. fftwl_execute_dft_r2c(plan, (long double*)&in_ptr[0], (fftwl_complex*)&out[0]);
  599. } else if (fft_type == FFT_Type::C2C) {
  600. s = 1 / sqrt<ValueType>(N0 / howmany * (ValueType)0.5);
  601. fftwl_execute_dft(plan, (fftwl_complex*)&in_ptr[0], (fftwl_complex*)&out[0]);
  602. } else if (fft_type == FFT_Type::C2C_INV) {
  603. s = 1 / sqrt<ValueType>(N1 / howmany * (ValueType)0.5);
  604. fftwl_execute_dft(plan, (fftwl_complex*)&in_ptr[0], (fftwl_complex*)&out[0]);
  605. } else if (fft_type == FFT_Type::C2R) {
  606. s = 1 / sqrt<ValueType>(N1 / howmany);
  607. fftwl_execute_dft_c2r(plan, (fftwl_complex*)&in_ptr[0], (long double*)&out[0]);
  608. }
  609. for (auto& x : out) x *= s;
  610. }
  611. private:
  612. bool copy_input;
  613. fftwl_plan plan;
  614. };
  615. #endif
  616. } // end namespace
  617. #endif //_SCTL_FFT_WRAPPER_