2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2003 David van der Spoel, Erik Lindahl, University of Groningen.
5 * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
6 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
48 #include "gromacs/fft/fft.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/fatalerror.h"
53 # define FFTWPREFIX(name) fftw_##name
55 # define FFTWPREFIX(name) fftwf_##name
58 /* none of the fftw3 calls, except execute(), are thread-safe, so
59 we need to serialize them with this mutex. */
60 static std::mutex big_fftw_mutex;
64 big_fftw_mutex.lock(); \
66 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
70 big_fftw_mutex.unlock(); \
72 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
74 /* We assume here that aligned memory starts at multiple of 16 bytes and unaligned memory starts at multiple of 8 bytes. The later is guranteed for all malloc implementation.
76 - It is not allowed to use these FFT plans from memory which doesn't have a starting address as a multiple of 8 bytes.
77 This is OK as long as the memory directly comes from malloc and is not some subarray within alloated memory.
78 - This has to be fixed if any future architecute requires memory to be aligned to multiples of 32 bytes.
82 * Contents of the FFTW3 fft datatype.
84 * Note that this is one of several possible implementations of gmx_fft_t.
95 * Three alternatives (unaligned/aligned, out-of-place/in-place, forward/backward)
96 * results in 8 different FFTW plans. Keep track of them with 3 array indices:
97 * first index: 0=unaligned, 1=aligned
98 * second index: 0=out-of-place, 1=in-place
99 * third index: 0=backward, 1=forward
101 FFTWPREFIX(plan) plan[2][2][2];
102 /** Used to catch user mistakes */
104 /** Number of dimensions in the FFT */
108 int gmx_fft_init_1d(gmx_fft_t* pfft, int nx, gmx_fft_flag flags)
110 return gmx_fft_init_many_1d(pfft, nx, 1, flags);
114 int gmx_fft_init_many_1d(gmx_fft_t* pfft, int nx, int howmany, gmx_fft_flag flags)
117 FFTWPREFIX(complex) * p1, *p2, *up1, *up2;
122 #if GMX_DISABLE_FFTW_MEASURE
123 flags |= GMX_FFT_FLAG_CONSERVATIVE;
126 fftw_flags = (flags & GMX_FFT_FLAG_CONSERVATIVE) ? FFTW_ESTIMATE : FFTW_MEASURE;
130 gmx_fatal(FARGS, "Invalid opaque FFT datatype pointer.");
136 if ((fft = static_cast<gmx_fft_t>(FFTWPREFIX(malloc)(sizeof(struct gmx_fft)))) == nullptr)
142 /* allocate aligned, and extra memory to make it unaligned */
143 p1 = static_cast<FFTWPREFIX(complex)*>(
144 FFTWPREFIX(malloc)(sizeof(FFTWPREFIX(complex)) * (nx + 2) * howmany));
147 FFTWPREFIX(free)(fft);
152 p2 = static_cast<FFTWPREFIX(complex)*>(
153 FFTWPREFIX(malloc)(sizeof(FFTWPREFIX(complex)) * (nx + 2) * howmany));
156 FFTWPREFIX(free)(p1);
157 FFTWPREFIX(free)(fft);
162 /* make unaligned pointers.
163 * In double precision the actual complex datatype will be 16 bytes,
164 * so go to a char pointer and force an offset of 8 bytes instead.
166 pc = reinterpret_cast<char*>(p1);
168 up1 = reinterpret_cast<FFTWPREFIX(complex)*>(pc);
170 pc = reinterpret_cast<char*>(p2);
172 up2 = reinterpret_cast<FFTWPREFIX(complex)*>(pc);
174 /* int rank, const int *n, int howmany,
175 fftw_complex *in, const int *inembed,
176 int istride, int idist,
177 fftw_complex *out, const int *onembed,
178 int ostride, int odist,
179 int sign, unsigned flags */
180 fft->plan[0][0][0] = FFTWPREFIX(plan_many_dft)(
181 1, &nx, howmany, up1, &nx, 1, nx, up2, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
182 fft->plan[0][0][1] = FFTWPREFIX(plan_many_dft)(
183 1, &nx, howmany, up1, &nx, 1, nx, up2, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
184 fft->plan[0][1][0] = FFTWPREFIX(plan_many_dft)(
185 1, &nx, howmany, up1, &nx, 1, nx, up1, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
186 fft->plan[0][1][1] = FFTWPREFIX(plan_many_dft)(
187 1, &nx, howmany, up1, &nx, 1, nx, up1, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
188 fft->plan[1][0][0] = FFTWPREFIX(plan_many_dft)(
189 1, &nx, howmany, p1, &nx, 1, nx, p2, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
190 fft->plan[1][0][1] = FFTWPREFIX(plan_many_dft)(
191 1, &nx, howmany, p1, &nx, 1, nx, p2, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
192 fft->plan[1][1][0] = FFTWPREFIX(plan_many_dft)(
193 1, &nx, howmany, p1, &nx, 1, nx, p1, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
194 fft->plan[1][1][1] = FFTWPREFIX(plan_many_dft)(
195 1, &nx, howmany, p1, &nx, 1, nx, p1, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
197 for (i = 0; i < 2; i++)
199 for (j = 0; j < 2; j++)
201 for (k = 0; k < 2; k++)
203 if (fft->plan[i][j][k] == nullptr)
205 gmx_fatal(FARGS, "Error initializing FFTW3 plan.");
207 gmx_fft_destroy(fft);
209 FFTWPREFIX(free)(p1);
210 FFTWPREFIX(free)(p2);
218 FFTWPREFIX(free)(p1);
219 FFTWPREFIX(free)(p2);
221 fft->real_transform = 0;
229 int gmx_fft_init_1d_real(gmx_fft_t* pfft, int nx, gmx_fft_flag flags)
231 return gmx_fft_init_many_1d_real(pfft, nx, 1, flags);
234 int gmx_fft_init_many_1d_real(gmx_fft_t* pfft, int nx, int howmany, gmx_fft_flag flags)
237 real * p1, *p2, *up1, *up2;
242 #if GMX_DISABLE_FFTW_MEASURE
243 flags |= GMX_FFT_FLAG_CONSERVATIVE;
246 fftw_flags = (flags & GMX_FFT_FLAG_CONSERVATIVE) ? FFTW_ESTIMATE : FFTW_MEASURE;
250 gmx_fatal(FARGS, "Invalid opaque FFT datatype pointer.");
256 if ((fft = static_cast<gmx_fft_t>(FFTWPREFIX(malloc)(sizeof(struct gmx_fft)))) == nullptr)
262 /* allocate aligned, and extra memory to make it unaligned */
263 p1 = static_cast<real*>(FFTWPREFIX(malloc)(sizeof(real) * (nx / 2 + 1) * 2 * howmany + 8));
266 FFTWPREFIX(free)(fft);
271 p2 = static_cast<real*>(FFTWPREFIX(malloc)(sizeof(real) * (nx / 2 + 1) * 2 * howmany + 8));
274 FFTWPREFIX(free)(p1);
275 FFTWPREFIX(free)(fft);
280 /* make unaligned pointers.
281 * In double precision the actual complex datatype will be 16 bytes,
282 * so go to a char pointer and force an offset of 8 bytes instead.
284 pc = reinterpret_cast<char*>(p1);
286 up1 = reinterpret_cast<real*>(pc);
288 pc = reinterpret_cast<char*>(p2);
290 up2 = reinterpret_cast<real*>(pc);
292 /* int rank, const int *n, int howmany,
293 double *in, const int *inembed,
294 int istride, int idist,
295 fftw_complex *out, const int *onembed,
296 int ostride, int odist,
298 fft->plan[0][0][1] = FFTWPREFIX(plan_many_dft_r2c)(1,
305 reinterpret_cast<FFTWPREFIX(complex)*>(up2),
310 fft->plan[0][1][1] = FFTWPREFIX(plan_many_dft_r2c)(1,
317 reinterpret_cast<FFTWPREFIX(complex)*>(up1),
322 fft->plan[1][0][1] = FFTWPREFIX(plan_many_dft_r2c)(1,
329 reinterpret_cast<FFTWPREFIX(complex)*>(p2),
334 fft->plan[1][1][1] = FFTWPREFIX(plan_many_dft_r2c)(1,
341 reinterpret_cast<FFTWPREFIX(complex)*>(p1),
347 fft->plan[0][0][0] = FFTWPREFIX(plan_many_dft_c2r)(1,
350 reinterpret_cast<FFTWPREFIX(complex)*>(up1),
359 fft->plan[0][1][0] = FFTWPREFIX(plan_many_dft_c2r)(1,
362 reinterpret_cast<FFTWPREFIX(complex)*>(up1),
371 fft->plan[1][0][0] = FFTWPREFIX(plan_many_dft_c2r)(1,
374 reinterpret_cast<FFTWPREFIX(complex)*>(p1),
383 fft->plan[1][1][0] = FFTWPREFIX(plan_many_dft_c2r)(1,
386 reinterpret_cast<FFTWPREFIX(complex)*>(p1),
396 for (i = 0; i < 2; i++)
398 for (j = 0; j < 2; j++)
400 for (k = 0; k < 2; k++)
402 if (fft->plan[i][j][k] == nullptr)
404 gmx_fatal(FARGS, "Error initializing FFTW3 plan.");
406 gmx_fft_destroy(fft);
408 FFTWPREFIX(free)(p1);
409 FFTWPREFIX(free)(p2);
417 FFTWPREFIX(free)(p1);
418 FFTWPREFIX(free)(p2);
420 fft->real_transform = 1;
429 int gmx_fft_init_2d_real(gmx_fft_t* pfft, int nx, int ny, gmx_fft_flag flags)
432 real * p1, *p2, *up1, *up2;
437 #if GMX_DISABLE_FFTW_MEASURE
438 flags |= GMX_FFT_FLAG_CONSERVATIVE;
441 fftw_flags = (flags & GMX_FFT_FLAG_CONSERVATIVE) ? FFTW_ESTIMATE : FFTW_MEASURE;
445 gmx_fatal(FARGS, "Invalid opaque FFT datatype pointer.");
451 if ((fft = static_cast<gmx_fft_t>(FFTWPREFIX(malloc)(sizeof(struct gmx_fft)))) == nullptr)
457 /* allocate aligned, and extra memory to make it unaligned */
458 p1 = static_cast<real*>(FFTWPREFIX(malloc)(sizeof(real) * (nx * (ny / 2 + 1) * 2 + 2)));
461 FFTWPREFIX(free)(fft);
466 p2 = static_cast<real*>(FFTWPREFIX(malloc)(sizeof(real) * (nx * (ny / 2 + 1) * 2 + 2)));
469 FFTWPREFIX(free)(p1);
470 FFTWPREFIX(free)(fft);
475 /* make unaligned pointers.
476 * In double precision the actual complex datatype will be 16 bytes,
477 * so go to a char pointer and force an offset of 8 bytes instead.
479 pc = reinterpret_cast<char*>(p1);
481 up1 = reinterpret_cast<real*>(pc);
483 pc = reinterpret_cast<char*>(p2);
485 up2 = reinterpret_cast<real*>(pc);
488 fft->plan[0][0][0] = FFTWPREFIX(plan_dft_c2r_2d)(
489 nx, ny, reinterpret_cast<FFTWPREFIX(complex)*>(up1), up2, fftw_flags);
490 fft->plan[0][0][1] = FFTWPREFIX(plan_dft_r2c_2d)(
491 nx, ny, up1, reinterpret_cast<FFTWPREFIX(complex)*>(up2), fftw_flags);
492 fft->plan[0][1][0] = FFTWPREFIX(plan_dft_c2r_2d)(
493 nx, ny, reinterpret_cast<FFTWPREFIX(complex)*>(up1), up1, fftw_flags);
494 fft->plan[0][1][1] = FFTWPREFIX(plan_dft_r2c_2d)(
495 nx, ny, up1, reinterpret_cast<FFTWPREFIX(complex)*>(up1), fftw_flags);
497 fft->plan[1][0][0] = FFTWPREFIX(plan_dft_c2r_2d)(
498 nx, ny, reinterpret_cast<FFTWPREFIX(complex)*>(p1), p2, fftw_flags);
499 fft->plan[1][0][1] = FFTWPREFIX(plan_dft_r2c_2d)(
500 nx, ny, p1, reinterpret_cast<FFTWPREFIX(complex)*>(p2), fftw_flags);
501 fft->plan[1][1][0] = FFTWPREFIX(plan_dft_c2r_2d)(
502 nx, ny, reinterpret_cast<FFTWPREFIX(complex)*>(p1), p1, fftw_flags);
503 fft->plan[1][1][1] = FFTWPREFIX(plan_dft_r2c_2d)(
504 nx, ny, p1, reinterpret_cast<FFTWPREFIX(complex)*>(p1), fftw_flags);
507 for (i = 0; i < 2; i++)
509 for (j = 0; j < 2; j++)
511 for (k = 0; k < 2; k++)
513 if (fft->plan[i][j][k] == nullptr)
515 gmx_fatal(FARGS, "Error initializing FFTW3 plan.");
517 gmx_fft_destroy(fft);
519 FFTWPREFIX(free)(p1);
520 FFTWPREFIX(free)(p2);
528 FFTWPREFIX(free)(p1);
529 FFTWPREFIX(free)(p2);
531 fft->real_transform = 1;
539 int gmx_fft_1d(gmx_fft_t fft, enum gmx_fft_direction dir, void* in_data, void* out_data)
541 bool aligned = (((size_t(in_data) | size_t(out_data)) & 0xf) == 0);
542 bool inplace = (in_data == out_data);
543 bool isforward = (dir == GMX_FFT_FORWARD);
546 if ((fft->real_transform == 1) || (fft->ndim != 1)
547 || ((dir != GMX_FFT_FORWARD) && (dir != GMX_FFT_BACKWARD)))
549 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
553 FFTWPREFIX(execute_dft)
554 (fft->plan[aligned][inplace][isforward],
555 static_cast<FFTWPREFIX(complex)*>(in_data),
556 static_cast<FFTWPREFIX(complex)*>(out_data));
561 int gmx_fft_many_1d(gmx_fft_t fft, enum gmx_fft_direction dir, void* in_data, void* out_data)
563 return gmx_fft_1d(fft, dir, in_data, out_data);
566 int gmx_fft_1d_real(gmx_fft_t fft, enum gmx_fft_direction dir, void* in_data, void* out_data)
568 bool aligned = (((size_t(in_data) | size_t(out_data)) & 0xf) == 0);
569 bool inplace = (in_data == out_data);
570 bool isforward = (dir == GMX_FFT_REAL_TO_COMPLEX);
573 if ((fft->real_transform != 1) || (fft->ndim != 1)
574 || ((dir != GMX_FFT_REAL_TO_COMPLEX) && (dir != GMX_FFT_COMPLEX_TO_REAL)))
576 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
582 FFTWPREFIX(execute_dft_r2c)
583 (fft->plan[aligned][inplace][isforward],
584 static_cast<real*>(in_data),
585 static_cast<FFTWPREFIX(complex)*>(out_data));
589 FFTWPREFIX(execute_dft_c2r)
590 (fft->plan[aligned][inplace][isforward],
591 static_cast<FFTWPREFIX(complex)*>(in_data),
592 static_cast<real*>(out_data));
598 int gmx_fft_many_1d_real(gmx_fft_t fft, enum gmx_fft_direction dir, void* in_data, void* out_data)
600 return gmx_fft_1d_real(fft, dir, in_data, out_data);
603 int gmx_fft_2d_real(gmx_fft_t fft, enum gmx_fft_direction dir, void* in_data, void* out_data)
605 bool aligned = (((size_t(in_data) | size_t(out_data)) & 0xf) == 0);
606 bool inplace = (in_data == out_data);
607 bool isforward = (dir == GMX_FFT_REAL_TO_COMPLEX);
610 if ((fft->real_transform != 1) || (fft->ndim != 2)
611 || ((dir != GMX_FFT_REAL_TO_COMPLEX) && (dir != GMX_FFT_COMPLEX_TO_REAL)))
613 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
619 FFTWPREFIX(execute_dft_r2c)
620 (fft->plan[aligned][inplace][isforward],
621 static_cast<real*>(in_data),
622 static_cast<FFTWPREFIX(complex)*>(out_data));
626 FFTWPREFIX(execute_dft_c2r)
627 (fft->plan[aligned][inplace][isforward],
628 static_cast<FFTWPREFIX(complex)*>(in_data),
629 static_cast<real*>(out_data));
636 void gmx_fft_destroy(gmx_fft_t fft)
642 for (i = 0; i < 2; i++)
644 for (j = 0; j < 2; j++)
646 for (k = 0; k < 2; k++)
648 if (fft->plan[i][j][k] != nullptr)
651 FFTWPREFIX(destroy_plan)(fft->plan[i][j][k]);
653 fft->plan[i][j][k] = nullptr;
659 FFTWPREFIX(free)(fft);
664 void gmx_many_fft_destroy(gmx_fft_t fft)
666 gmx_fft_destroy(fft);
669 void gmx_fft_cleanup()
671 FFTWPREFIX(cleanup)();