fft_wrapper.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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> operator*(const Complex<ValueType>& x){
  20. Complex<ValueType> z;
  21. z.real = real * x.real - imag * x.imag;
  22. z.imag = imag * x.real - real * x.imag;
  23. return z;
  24. }
  25. Complex<ValueType> operator*(const ValueType& x){
  26. Complex<ValueType> z;
  27. z.real = real * x;
  28. z.imag = imag * x;
  29. return z;
  30. }
  31. Complex<ValueType> operator+(const Complex<ValueType>& x){
  32. Complex<ValueType> z;
  33. z.real = real + x.real;
  34. z.imag = imag + x.imag;
  35. return z;
  36. }
  37. Complex<ValueType> operator+(const ValueType& x){
  38. Complex<ValueType> z;
  39. z.real = real + x;
  40. z.imag = imag;
  41. return z;
  42. }
  43. Complex<ValueType> operator-(const Complex<ValueType>& x){
  44. Complex<ValueType> z;
  45. z.real = real - x.real;
  46. z.imag = imag - x.imag;
  47. return z;
  48. }
  49. Complex<ValueType> operator-(const ValueType& x){
  50. Complex<ValueType> z;
  51. z.real = real - x;
  52. z.imag = imag;
  53. return z;
  54. }
  55. ValueType real;
  56. ValueType imag;
  57. };
  58. template <class ValueType> Complex<ValueType> operator*(const ValueType& x, const Complex<ValueType>& y){
  59. Complex<ValueType> z;
  60. z.real = y.real * x;
  61. z.imag = y.imag * x;
  62. return z;
  63. }
  64. template <class ValueType> Complex<ValueType> operator+(const ValueType& x, const Complex<ValueType>& y){
  65. Complex<ValueType> z;
  66. z.real = y.real + x;
  67. z.imag = y.imag;
  68. return z;
  69. }
  70. template <class ValueType> Complex<ValueType> operator-(const ValueType& x, const Complex<ValueType>& y){
  71. Complex<ValueType> z;
  72. z.real = y.real - x;
  73. z.imag = y.imag;
  74. return z;
  75. }
  76. enum class FFT_Type {R2C, C2C, C2C_INV, C2R};
  77. template <class ValueType, class FFT_Derived> class FFT_Generic {
  78. typedef Complex<ValueType> ComplexType;
  79. struct FFTPlan {
  80. std::vector<Matrix<ValueType>> M;
  81. };
  82. public:
  83. FFT_Generic() {
  84. dim[0] = 0;
  85. dim[1] = 0;
  86. }
  87. FFT_Generic(const FFT_Generic&) = delete;
  88. FFT_Generic& operator=(const FFT_Generic&) = delete;
  89. Long Dim(Integer i) const {
  90. return dim[i];
  91. }
  92. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec) {
  93. Long rank = dim_vec.Dim();
  94. fft_type = fft_type_;
  95. howmany = howmany_;
  96. plan.M.resize(0);
  97. if (fft_type == FFT_Type::R2C) {
  98. plan.M.push_back(fft_r2c(dim_vec[rank - 1]));
  99. for (Long i = rank - 2; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]));
  100. } else if (fft_type == FFT_Type::C2C) {
  101. for (Long i = rank - 1; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]));
  102. } else if (fft_type == FFT_Type::C2C_INV) {
  103. for (Long i = rank - 1; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]).Transpose());
  104. } else if (fft_type == FFT_Type::C2R) {
  105. for (Long i = rank - 2; i >= 0; i--) plan.M.push_back(fft_c2c(dim_vec[i]).Transpose());
  106. plan.M.push_back(fft_c2r(dim_vec[rank - 1]));
  107. }
  108. Long N0 = howmany * 2;
  109. Long N1 = howmany * 2;
  110. for (const auto M : plan.M) {
  111. N0 = N0 * M.Dim(0) / 2;
  112. N1 = N1 * M.Dim(1) / 2;
  113. }
  114. dim[0] = N0;
  115. dim[1] = N1;
  116. }
  117. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  118. Long N0 = Dim(0);
  119. Long N1 = Dim(1);
  120. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  121. if (out.Dim() != N1) out.ReInit(N1);
  122. Vector<ValueType> buff0(N0 + N1);
  123. Vector<ValueType> buff1(N0 + N1);
  124. Long rank = plan.M.size();
  125. if (rank <= 0) return;
  126. Long N = N0;
  127. if (fft_type == FFT_Type::C2R) {
  128. const Matrix<ValueType>& M = plan.M[rank - 1];
  129. transpose<ComplexType>(buff0.begin(), in.begin(), N / M.Dim(0), M.Dim(0) / 2);
  130. for (Long i = 0; i < rank - 1; i++) {
  131. const Matrix<ValueType>& M = plan.M[i];
  132. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff0.begin(), false);
  133. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), buff1.begin(), false);
  134. Matrix<ValueType>::GEMM(vo, vi, M);
  135. N = N * M.Dim(1) / M.Dim(0);
  136. transpose<ComplexType>(buff0.begin(), buff1.begin(), N / M.Dim(1), M.Dim(1) / 2);
  137. }
  138. transpose<ComplexType>(buff1.begin(), buff0.begin(), N / howmany / 2, howmany);
  139. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff1.begin(), false);
  140. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), out.begin(), false);
  141. Matrix<ValueType>::GEMM(vo, vi, M);
  142. } else {
  143. memcopy(buff0.begin(), in.begin(), in.Dim());
  144. for (Long i = 0; i < rank; i++) {
  145. const Matrix<ValueType>& M = plan.M[i];
  146. Matrix<ValueType> vi(N / M.Dim(0), M.Dim(0), buff0.begin(), false);
  147. Matrix<ValueType> vo(N / M.Dim(0), M.Dim(1), buff1.begin(), false);
  148. Matrix<ValueType>::GEMM(vo, vi, M);
  149. N = N * M.Dim(1) / M.Dim(0);
  150. transpose<ComplexType>(buff0.begin(), buff1.begin(), N / M.Dim(1), M.Dim(1) / 2);
  151. }
  152. transpose<ComplexType>(out.begin(), buff0.begin(), N / howmany / 2, howmany);
  153. }
  154. }
  155. static void test() {
  156. Vector<Long> fft_dim;
  157. fft_dim.PushBack(2);
  158. fft_dim.PushBack(5);
  159. fft_dim.PushBack(3);
  160. Long howmany = 3;
  161. if (1){ // R2C, C2R
  162. FFT_Derived myfft0, myfft1;
  163. myfft0.Setup(FFT_Type::R2C, howmany, fft_dim);
  164. myfft1.Setup(FFT_Type::C2R, howmany, fft_dim);
  165. Vector<ValueType> v0(myfft0.Dim(0)), v1, v2;
  166. for (int i = 0; i < v0.Dim(); i++) v0[i] = 1 + i;
  167. myfft0.Execute(v0, v1);
  168. myfft1.Execute(v1, v2);
  169. { // Print error
  170. ValueType err = 0;
  171. SCTL_ASSERT(v0.Dim() == v2.Dim());
  172. for (Long i = 0; i < v0.Dim(); i++) err = std::max(err, fabs(v0[i] - v2[i]));
  173. std::cout<<"Error : "<<err<<'\n';
  174. }
  175. }
  176. std::cout<<'\n';
  177. { // C2C, C2C_INV
  178. FFT_Derived myfft0, myfft1;
  179. myfft0.Setup(FFT_Type::C2C, howmany, fft_dim);
  180. myfft1.Setup(FFT_Type::C2C_INV, howmany, fft_dim);
  181. Vector<ValueType> v0(myfft0.Dim(0)), v1, v2;
  182. for (int i = 0; i < v0.Dim(); i++) v0[i] = 1 + i;
  183. myfft0.Execute(v0, v1);
  184. myfft1.Execute(v1, v2);
  185. { // Print error
  186. ValueType err = 0;
  187. SCTL_ASSERT(v0.Dim() == v2.Dim());
  188. for (Long i = 0; i < v0.Dim(); i++) err = std::max(err, fabs(v0[i] - v2[i]));
  189. std::cout<<"Error : "<<err<<'\n';
  190. }
  191. }
  192. std::cout<<'\n';
  193. }
  194. protected:
  195. static Matrix<ValueType> fft_r2c(Long N0) {
  196. ValueType s = 1 / sqrt<ValueType>(N0);
  197. Long N1 = (N0 / 2 + 1);
  198. Matrix<ValueType> M(N0, 2 * N1);
  199. for (Long j = 0; j < N0; j++)
  200. for (Long i = 0; i < N1; i++) {
  201. M[j][2 * i + 0] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  202. M[j][2 * i + 1] = -sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  203. }
  204. return M;
  205. }
  206. static Matrix<ValueType> fft_c2c(Long N0) {
  207. ValueType s = 1 / sqrt<ValueType>(N0);
  208. Matrix<ValueType> M(2 * N0, 2 * N0);
  209. for (Long i = 0; i < N0; i++)
  210. for (Long j = 0; j < N0; j++) {
  211. M[2 * i + 0][2 * j + 0] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  212. M[2 * i + 1][2 * j + 0] = sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  213. M[2 * i + 0][2 * j + 1] = -sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  214. M[2 * i + 1][2 * j + 1] = cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  215. }
  216. return M;
  217. }
  218. static Matrix<ValueType> fft_c2r(Long N0) {
  219. ValueType s = 1 / sqrt<ValueType>(N0);
  220. Long N1 = (N0 / 2 + 1);
  221. Matrix<ValueType> M(2 * N1, N0);
  222. for (Long i = 0; i < N1; i++) {
  223. for (Long j = 0; j < N0; j++) {
  224. M[2 * i + 0][j] = 2 * cos<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  225. M[2 * i + 1][j] = -2 * sin<ValueType>(2 * const_pi<ValueType>() * j * i / N0)*s;
  226. }
  227. }
  228. if (N1 > 0) {
  229. for (Long j = 0; j < N0; j++) {
  230. M[0][j] = M[0][j] * (ValueType)0.5;
  231. M[1][j] = M[1][j] * (ValueType)0.5;
  232. }
  233. }
  234. if (N0 % 2 == 0) {
  235. for (Long j = 0; j < N0; j++) {
  236. M[2 * N1 - 2][j] = M[2 * N1 - 2][j] * (ValueType)0.5;
  237. M[2 * N1 - 1][j] = M[2 * N1 - 1][j] * (ValueType)0.5;
  238. }
  239. }
  240. return M;
  241. }
  242. template <class T> static void transpose(Iterator<ValueType> out, ConstIterator<ValueType> in, Long N0, Long N1) {
  243. Matrix<T> M0(N0, N1, (Iterator<T>)in, false);
  244. Matrix<T> M1(N1, N0, (Iterator<T>)out, false);
  245. M1 = M0.Transpose();
  246. }
  247. StaticArray<Long,2> dim;
  248. FFT_Type fft_type;
  249. Long howmany;
  250. FFTPlan plan;
  251. };
  252. template <class ValueType> class FFT : public FFT_Generic<ValueType, FFT<ValueType>> {};
  253. #ifdef SCTL_HAVE_FFTW
  254. template <> class FFT<double> : public FFT_Generic<double, FFT<double>> {
  255. typedef double ValueType;
  256. public:
  257. ~FFT() { if (this->Dim(0) && this->Dim(1)) fftw_destroy_plan(plan); }
  258. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec) {
  259. if (Dim(0) && Dim(1)) fftw_destroy_plan(plan);
  260. Long rank = dim_vec.Dim();
  261. this->fft_type = fft_type_;
  262. this->howmany = howmany_;
  263. Long N0 = 0, N1 = 0;
  264. { // Set N0, N1
  265. Long N = howmany;
  266. for (auto ni : dim_vec) N *= ni;
  267. if (fft_type == FFT_Type::R2C) {
  268. N0 = N;
  269. N1 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  270. } else if (fft_type == FFT_Type::C2C) {
  271. N0 = N * 2;
  272. N1 = N * 2;
  273. } else if (fft_type == FFT_Type::C2C_INV) {
  274. N0 = N * 2;
  275. N1 = N * 2;
  276. } else if (fft_type == FFT_Type::C2R) {
  277. N0 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  278. N1 = N;
  279. }
  280. this->dim[0] = N0;
  281. this->dim[1] = N1;
  282. }
  283. if (!N0 || !N1) return;
  284. in .ReInit(N0);
  285. out.ReInit(N1);
  286. Vector<int> dim_vec_(rank);
  287. for (Integer i = 0; i < rank; i++) dim_vec_[i] = dim_vec[i];
  288. if (fft_type == FFT_Type::R2C) {
  289. plan = fftw_plan_many_dft_r2c(rank, &dim_vec_[0], howmany_, (double*)&in[0], NULL, 1, N0 / howmany, (fftw_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_ESTIMATE);
  290. } else if (fft_type == FFT_Type::C2C) {
  291. 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);
  292. } else if (fft_type == FFT_Type::C2C_INV) {
  293. 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);
  294. } else if (fft_type == FFT_Type::C2R) {
  295. plan = fftw_plan_many_dft_c2r(rank, &dim_vec_[0], howmany_, (fftw_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (double*)&out[0], NULL, 1, N1 / howmany, FFTW_ESTIMATE);
  296. }
  297. }
  298. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  299. Long N0 = this->Dim(0);
  300. Long N1 = this->Dim(1);
  301. if (!N0 || !N1) return;
  302. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  303. if (out.Dim() != N1) out.ReInit(N1);
  304. ValueType s = 0;
  305. if (fft_type == FFT_Type::R2C) {
  306. s = 1 / sqrt<ValueType>(N0 / howmany);
  307. fftw_execute_dft_r2c(plan, (double*)&in[0], (fftw_complex*)&out[0]);
  308. } else if (fft_type == FFT_Type::C2C) {
  309. s = 1 / sqrt<ValueType>(N0 / howmany * (ValueType)0.5);
  310. fftw_execute_dft(plan, (fftw_complex*)&in[0], (fftw_complex*)&out[0]);
  311. } else if (fft_type == FFT_Type::C2C_INV) {
  312. s = 1 / sqrt<ValueType>(N1 / howmany * (ValueType)0.5);
  313. fftw_execute_dft(plan, (fftw_complex*)&in[0], (fftw_complex*)&out[0]);
  314. } else if (fft_type == FFT_Type::C2R) {
  315. s = 1 / sqrt<ValueType>(N1 / howmany);
  316. fftw_execute_dft_c2r(plan, (fftw_complex*)&in[0], (double*)&out[0]);
  317. }
  318. for (auto& x : out) x *= s;
  319. }
  320. private:
  321. Vector<ValueType> in, out;
  322. fftw_plan plan;
  323. };
  324. #endif
  325. #ifdef SCTL_HAVE_FFTWF
  326. template <> class FFT<float> : public FFT_Generic<float, FFT<float>> {
  327. typedef float ValueType;
  328. public:
  329. ~FFT() { if (this->Dim(0) && this->Dim(1)) fftwf_destroy_plan(plan); }
  330. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec) {
  331. if (Dim(0) && Dim(1)) fftwf_destroy_plan(plan);
  332. Long rank = dim_vec.Dim();
  333. this->fft_type = fft_type_;
  334. this->howmany = howmany_;
  335. Long N0, N1;
  336. { // Set N0, N1
  337. Long N = howmany;
  338. for (auto ni : dim_vec) N *= ni;
  339. if (fft_type == FFT_Type::R2C) {
  340. N0 = N;
  341. N1 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  342. } else if (fft_type == FFT_Type::C2C) {
  343. N0 = N * 2;
  344. N1 = N * 2;
  345. } else if (fft_type == FFT_Type::C2C_INV) {
  346. N0 = N * 2;
  347. N1 = N * 2;
  348. } else if (fft_type == FFT_Type::C2R) {
  349. N0 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  350. N1 = N;
  351. }
  352. this->dim[0] = N0;
  353. this->dim[1] = N1;
  354. }
  355. if (!N0 || !N1) return;
  356. in .ReInit(N0);
  357. out.ReInit(N1);
  358. Vector<int> dim_vec_(rank);
  359. for (Integer i = 0; i < rank; i++) dim_vec_[i] = dim_vec[i];
  360. if (fft_type == FFT_Type::R2C) {
  361. plan = fftwf_plan_many_dft_r2c(rank, &dim_vec_[0], howmany_, (float*)&in[0], NULL, 1, N0 / howmany, (fftwf_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_ESTIMATE);
  362. } else if (fft_type == FFT_Type::C2C) {
  363. 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);
  364. } else if (fft_type == FFT_Type::C2C_INV) {
  365. 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);
  366. } else if (fft_type == FFT_Type::C2R) {
  367. plan = fftwf_plan_many_dft_c2r(rank, &dim_vec_[0], howmany_, (fftwf_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (float*)&out[0], NULL, 1, N1 / howmany, FFTW_ESTIMATE);
  368. }
  369. }
  370. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  371. Long N0 = this->Dim(0);
  372. Long N1 = this->Dim(1);
  373. if (!N0 || !N1) return;
  374. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  375. if (out.Dim() != N1) out.ReInit(N1);
  376. ValueType s = 0;
  377. if (fft_type == FFT_Type::R2C) {
  378. s = 1 / sqrt<ValueType>(N0 / howmany);
  379. fftwf_execute_dft_r2c(plan, (float*)&in[0], (fftwf_complex*)&out[0]);
  380. } else if (fft_type == FFT_Type::C2C) {
  381. s = 1 / sqrt<ValueType>(N0 / howmany * (ValueType)0.5);
  382. fftwf_execute_dft(plan, (fftwf_complex*)&in[0], (fftwf_complex*)&out[0]);
  383. } else if (fft_type == FFT_Type::C2C_INV) {
  384. s = 1 / sqrt<ValueType>(N1 / howmany * (ValueType)0.5);
  385. fftwf_execute_dft(plan, (fftwf_complex*)&in[0], (fftwf_complex*)&out[0]);
  386. } else if (fft_type == FFT_Type::C2R) {
  387. s = 1 / sqrt<ValueType>(N1 / howmany);
  388. fftwf_execute_dft_c2r(plan, (fftwf_complex*)&in[0], (float*)&out[0]);
  389. }
  390. for (auto& x : out) x *= s;
  391. }
  392. private:
  393. Vector<ValueType> in, out;
  394. fftwf_plan plan;
  395. };
  396. #endif
  397. #ifdef SCTL_HAVE_FFTWL
  398. template <> class FFT<long double> : public FFT_Generic<long double, FFT<long double>> {
  399. typedef long double ValueType;
  400. public:
  401. ~FFT() { if (this->Dim(0) && this->Dim(1)) fftwl_destroy_plan(plan); }
  402. void Setup(FFT_Type fft_type_, Long howmany_, const Vector<Long>& dim_vec) {
  403. if (Dim(0) && Dim(1)) fftwl_destroy_plan(plan);
  404. Long rank = dim_vec.Dim();
  405. this->fft_type = fft_type_;
  406. this->howmany = howmany_;
  407. Long N0, N1;
  408. { // Set N0, N1
  409. Long N = howmany;
  410. for (auto ni : dim_vec) N *= ni;
  411. if (fft_type == FFT_Type::R2C) {
  412. N0 = N;
  413. N1 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  414. } else if (fft_type == FFT_Type::C2C) {
  415. N0 = N * 2;
  416. N1 = N * 2;
  417. } else if (fft_type == FFT_Type::C2C_INV) {
  418. N0 = N * 2;
  419. N1 = N * 2;
  420. } else if (fft_type == FFT_Type::C2R) {
  421. N0 = (N / dim_vec[rank - 1]) * (dim_vec[rank - 1] / 2 + 1) * 2;
  422. N1 = N;
  423. }
  424. this->dim[0] = N0;
  425. this->dim[1] = N1;
  426. }
  427. if (!N0 || !N1) return;
  428. in .ReInit(N0);
  429. out.ReInit(N1);
  430. Vector<int> dim_vec_(rank);
  431. for (Integer i = 0; i < rank; i++) dim_vec_[i] = dim_vec[i];
  432. if (fft_type == FFT_Type::R2C) {
  433. plan = fftwl_plan_many_dft_r2c(rank, &dim_vec_[0], howmany_, (long double*)&in[0], NULL, 1, N0 / howmany, (fftwl_complex*)&out[0], NULL, 1, N1 / 2 / howmany, FFTW_ESTIMATE);
  434. } else if (fft_type == FFT_Type::C2C) {
  435. 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);
  436. } else if (fft_type == FFT_Type::C2C_INV) {
  437. 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);
  438. } else if (fft_type == FFT_Type::C2R) {
  439. plan = fftwl_plan_many_dft_c2r(rank, &dim_vec_[0], howmany_, (fftwl_complex*)&in[0], NULL, 1, N0 / 2 / howmany, (long double*)&out[0], NULL, 1, N1 / howmany, FFTW_ESTIMATE);
  440. }
  441. }
  442. void Execute(const Vector<ValueType>& in, Vector<ValueType>& out) const {
  443. Long N0 = this->Dim(0);
  444. Long N1 = this->Dim(1);
  445. if (!N0 || !N1) return;
  446. SCTL_ASSERT_MSG(in.Dim() == N0, "FFT: Wrong input size.");
  447. if (out.Dim() != N1) out.ReInit(N1);
  448. ValueType s = 0;
  449. if (fft_type == FFT_Type::R2C) {
  450. s = 1 / sqrt<ValueType>(N0 / howmany);
  451. fftwl_execute_dft_r2c(plan, (long double*)&in[0], (fftwl_complex*)&out[0]);
  452. } else if (fft_type == FFT_Type::C2C) {
  453. s = 1 / sqrt<ValueType>(N0 / howmany * (ValueType)0.5);
  454. fftwl_execute_dft(plan, (fftwl_complex*)&in[0], (fftwl_complex*)&out[0]);
  455. } else if (fft_type == FFT_Type::C2C_INV) {
  456. s = 1 / sqrt<ValueType>(N1 / howmany * (ValueType)0.5);
  457. fftwl_execute_dft(plan, (fftwl_complex*)&in[0], (fftwl_complex*)&out[0]);
  458. } else if (fft_type == FFT_Type::C2R) {
  459. s = 1 / sqrt<ValueType>(N1 / howmany);
  460. fftwl_execute_dft_c2r(plan, (fftwl_complex*)&in[0], (long double*)&out[0]);
  461. }
  462. for (auto& x : out) x *= s;
  463. }
  464. private:
  465. Vector<ValueType> in, out;
  466. fftwl_plan plan;
  467. };
  468. #endif
  469. } // end namespace
  470. #endif //_SCTL_FFT_WRAPPER_