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, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
43 #include "gromacs/fft/fft.h"
44 #include "gromacs/utility/fatalerror.h"
47 #define FFTWPREFIX(name) fftw_ ## name
49 #define FFTWPREFIX(name) fftwf_ ## name
52 #include "thread_mpi/mutex.h"
53 #include "gromacs/utility/exceptions.h"
55 /* none of the fftw3 calls, except execute(), are thread-safe, so
56 we need to serialize them with this mutex. */
57 static tMPI::mutex big_fftw_mutex;
58 #define FFTW_LOCK try { big_fftw_mutex.lock(); } GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
59 #define FFTW_UNLOCK try { big_fftw_mutex.unlock(); } GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
61 /* 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.
63 - It is not allowed to use these FFT plans from memory which doesn't have a starting address as a multiple of 8 bytes.
64 This is OK as long as the memory directly comes from malloc and is not some subarray within alloated memory.
65 - This has to be fixed if any future architecute requires memory to be aligned to multiples of 32 bytes.
69 * Contents of the FFTW3 fft datatype.
71 * Note that this is one of several possible implementations of gmx_fft_t.
82 * Three alternatives (unaligned/aligned, out-of-place/in-place, forward/backward)
83 * results in 8 different FFTW plans. Keep track of them with 3 array indices:
84 * first index: 0=unaligned, 1=aligned
85 * second index: 0=out-of-place, 1=in-place
86 * third index: 0=backward, 1=forward
88 FFTWPREFIX(plan) plan[2][2][2];
89 /** Used to catch user mistakes */
91 /** Number of dimensions in the FFT */
96 gmx_fft_init_1d(gmx_fft_t * pfft,
100 return gmx_fft_init_many_1d(pfft, nx, 1, flags);
105 gmx_fft_init_many_1d(gmx_fft_t * pfft,
111 FFTWPREFIX(complex) *p1, *p2, *up1, *up2;
116 #ifdef GMX_DISABLE_FFTW_MEASURE
117 flags |= GMX_FFT_FLAG_CONSERVATIVE;
120 fftw_flags = (flags & GMX_FFT_FLAG_CONSERVATIVE) ? FFTW_ESTIMATE : FFTW_MEASURE;
124 gmx_fatal(FARGS, "Invalid opaque FFT datatype pointer.");
130 if ( (fft = (gmx_fft_t)FFTWPREFIX(malloc)(sizeof(struct gmx_fft))) == NULL)
136 /* allocate aligned, and extra memory to make it unaligned */
137 p1 = (FFTWPREFIX(complex) *) FFTWPREFIX(malloc)(sizeof(FFTWPREFIX(complex))*(nx+2)*howmany);
140 FFTWPREFIX(free)(fft);
145 p2 = (FFTWPREFIX(complex) *) FFTWPREFIX(malloc)(sizeof(FFTWPREFIX(complex))*(nx+2)*howmany);
148 FFTWPREFIX(free)(p1);
149 FFTWPREFIX(free)(fft);
154 /* make unaligned pointers.
155 * In double precision the actual complex datatype will be 16 bytes,
156 * so go to a char pointer and force an offset of 8 bytes instead.
160 up1 = (FFTWPREFIX(complex) *)pc;
164 up2 = (FFTWPREFIX(complex) *)pc;
166 /* int rank, const int *n, int howmany,
167 fftw_complex *in, const int *inembed,
168 int istride, int idist,
169 fftw_complex *out, const int *onembed,
170 int ostride, int odist,
171 int sign, unsigned flags */
172 fft->plan[0][0][0] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, up1, &nx, 1, nx, up2, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
173 fft->plan[0][0][1] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, up1, &nx, 1, nx, up2, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
174 fft->plan[0][1][0] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, up1, &nx, 1, nx, up1, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
175 fft->plan[0][1][1] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, up1, &nx, 1, nx, up1, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
176 fft->plan[1][0][0] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, p1, &nx, 1, nx, p2, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
177 fft->plan[1][0][1] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, p1, &nx, 1, nx, p2, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
178 fft->plan[1][1][0] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, p1, &nx, 1, nx, p1, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
179 fft->plan[1][1][1] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, p1, &nx, 1, nx, p1, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
181 for (i = 0; i < 2; i++)
183 for (j = 0; j < 2; j++)
185 for (k = 0; k < 2; k++)
187 if (fft->plan[i][j][k] == NULL)
189 gmx_fatal(FARGS, "Error initializing FFTW3 plan.");
191 gmx_fft_destroy(fft);
193 FFTWPREFIX(free)(p1);
194 FFTWPREFIX(free)(p2);
202 FFTWPREFIX(free)(p1);
203 FFTWPREFIX(free)(p2);
205 fft->real_transform = 0;
214 gmx_fft_init_1d_real(gmx_fft_t * pfft,
218 return gmx_fft_init_many_1d_real(pfft, nx, 1, flags);
222 gmx_fft_init_many_1d_real(gmx_fft_t * pfft,
228 real *p1, *p2, *up1, *up2;
233 #ifdef GMX_DISABLE_FFTW_MEASURE
234 flags |= GMX_FFT_FLAG_CONSERVATIVE;
237 fftw_flags = (flags & GMX_FFT_FLAG_CONSERVATIVE) ? FFTW_ESTIMATE : FFTW_MEASURE;
241 gmx_fatal(FARGS, "Invalid opaque FFT datatype pointer.");
247 if ( (fft = (gmx_fft_t) FFTWPREFIX(malloc)(sizeof(struct gmx_fft))) == NULL)
253 /* allocate aligned, and extra memory to make it unaligned */
254 p1 = (real *) FFTWPREFIX(malloc)(sizeof(real)*(nx/2+1)*2*howmany + 8);
257 FFTWPREFIX(free)(fft);
262 p2 = (real *) FFTWPREFIX(malloc)(sizeof(real)*(nx/2+1)*2*howmany + 8);
265 FFTWPREFIX(free)(p1);
266 FFTWPREFIX(free)(fft);
271 /* make unaligned pointers.
272 * In double precision the actual complex datatype will be 16 bytes,
273 * so go to a char pointer and force an offset of 8 bytes instead.
283 /* int rank, const int *n, int howmany,
284 double *in, const int *inembed,
285 int istride, int idist,
286 fftw_complex *out, const int *onembed,
287 int ostride, int odist,
289 fft->plan[0][0][1] = FFTWPREFIX(plan_many_dft_r2c)(1, &nx, howmany, up1, 0, 1, (nx/2+1) *2, (FFTWPREFIX(complex) *) up2, 0, 1, (nx/2+1), fftw_flags);
290 fft->plan[0][1][1] = FFTWPREFIX(plan_many_dft_r2c)(1, &nx, howmany, up1, 0, 1, (nx/2+1) *2, (FFTWPREFIX(complex) *) up1, 0, 1, (nx/2+1), fftw_flags);
291 fft->plan[1][0][1] = FFTWPREFIX(plan_many_dft_r2c)(1, &nx, howmany, p1, 0, 1, (nx/2+1) *2, (FFTWPREFIX(complex) *) p2, 0, 1, (nx/2+1), fftw_flags);
292 fft->plan[1][1][1] = FFTWPREFIX(plan_many_dft_r2c)(1, &nx, howmany, p1, 0, 1, (nx/2+1) *2, (FFTWPREFIX(complex) *) p1, 0, 1, (nx/2+1), fftw_flags);
294 fft->plan[0][0][0] = FFTWPREFIX(plan_many_dft_c2r)(1, &nx, howmany, (FFTWPREFIX(complex) *) up1, 0, 1, (nx/2+1), up2, 0, 1, (nx/2+1) *2, fftw_flags);
295 fft->plan[0][1][0] = FFTWPREFIX(plan_many_dft_c2r)(1, &nx, howmany, (FFTWPREFIX(complex) *) up1, 0, 1, (nx/2+1), up1, 0, 1, (nx/2+1) *2, fftw_flags);
296 fft->plan[1][0][0] = FFTWPREFIX(plan_many_dft_c2r)(1, &nx, howmany, (FFTWPREFIX(complex) *) p1, 0, 1, (nx/2+1), p2, 0, 1, (nx/2+1) *2, fftw_flags);
297 fft->plan[1][1][0] = FFTWPREFIX(plan_many_dft_c2r)(1, &nx, howmany, (FFTWPREFIX(complex) *) p1, 0, 1, (nx/2+1), p1, 0, 1, (nx/2+1) *2, fftw_flags);
299 for (i = 0; i < 2; i++)
301 for (j = 0; j < 2; j++)
303 for (k = 0; k < 2; k++)
305 if (fft->plan[i][j][k] == NULL)
307 gmx_fatal(FARGS, "Error initializing FFTW3 plan.");
309 gmx_fft_destroy(fft);
311 FFTWPREFIX(free)(p1);
312 FFTWPREFIX(free)(p2);
320 FFTWPREFIX(free)(p1);
321 FFTWPREFIX(free)(p2);
323 fft->real_transform = 1;
333 gmx_fft_init_2d_real(gmx_fft_t * pfft,
339 real *p1, *p2, *up1, *up2;
344 #ifdef GMX_DISABLE_FFTW_MEASURE
345 flags |= GMX_FFT_FLAG_CONSERVATIVE;
348 fftw_flags = (flags & GMX_FFT_FLAG_CONSERVATIVE) ? FFTW_ESTIMATE : FFTW_MEASURE;
352 gmx_fatal(FARGS, "Invalid opaque FFT datatype pointer.");
358 if ( (fft = (gmx_fft_t) FFTWPREFIX(malloc)(sizeof(struct gmx_fft))) == NULL)
364 /* allocate aligned, and extra memory to make it unaligned */
365 p1 = (real *) FFTWPREFIX(malloc)(sizeof(real) *( nx*(ny/2+1)*2 + 2) );
368 FFTWPREFIX(free)(fft);
373 p2 = (real *) FFTWPREFIX(malloc)(sizeof(real) *( nx*(ny/2+1)*2 + 2) );
376 FFTWPREFIX(free)(p1);
377 FFTWPREFIX(free)(fft);
382 /* make unaligned pointers.
383 * In double precision the actual complex datatype will be 16 bytes,
384 * so go to a char pointer and force an offset of 8 bytes instead.
395 fft->plan[0][0][0] = FFTWPREFIX(plan_dft_c2r_2d)(nx, ny, (FFTWPREFIX(complex) *) up1, up2, fftw_flags);
396 fft->plan[0][0][1] = FFTWPREFIX(plan_dft_r2c_2d)(nx, ny, up1, (FFTWPREFIX(complex) *) up2, fftw_flags);
397 fft->plan[0][1][0] = FFTWPREFIX(plan_dft_c2r_2d)(nx, ny, (FFTWPREFIX(complex) *) up1, up1, fftw_flags);
398 fft->plan[0][1][1] = FFTWPREFIX(plan_dft_r2c_2d)(nx, ny, up1, (FFTWPREFIX(complex) *) up1, fftw_flags);
400 fft->plan[1][0][0] = FFTWPREFIX(plan_dft_c2r_2d)(nx, ny, (FFTWPREFIX(complex) *) p1, p2, fftw_flags);
401 fft->plan[1][0][1] = FFTWPREFIX(plan_dft_r2c_2d)(nx, ny, p1, (FFTWPREFIX(complex) *) p2, fftw_flags);
402 fft->plan[1][1][0] = FFTWPREFIX(plan_dft_c2r_2d)(nx, ny, (FFTWPREFIX(complex) *) p1, p1, fftw_flags);
403 fft->plan[1][1][1] = FFTWPREFIX(plan_dft_r2c_2d)(nx, ny, p1, (FFTWPREFIX(complex) *) p1, fftw_flags);
406 for (i = 0; i < 2; i++)
408 for (j = 0; j < 2; j++)
410 for (k = 0; k < 2; k++)
412 if (fft->plan[i][j][k] == NULL)
414 gmx_fatal(FARGS, "Error initializing FFTW3 plan.");
416 gmx_fft_destroy(fft);
418 FFTWPREFIX(free)(p1);
419 FFTWPREFIX(free)(p2);
427 FFTWPREFIX(free)(p1);
428 FFTWPREFIX(free)(p2);
430 fft->real_transform = 1;
439 gmx_fft_1d (gmx_fft_t fft,
440 enum gmx_fft_direction dir,
444 int aligned = ((((size_t)in_data | (size_t)out_data) & 0xf) == 0);
445 int inplace = (in_data == out_data);
446 int isforward = (dir == GMX_FFT_FORWARD);
449 if ( (fft->real_transform == 1) || (fft->ndim != 1) ||
450 ((dir != GMX_FFT_FORWARD) && (dir != GMX_FFT_BACKWARD)) )
452 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
456 FFTWPREFIX(execute_dft)(fft->plan[aligned][inplace][isforward],
457 (FFTWPREFIX(complex) *) in_data,
458 (FFTWPREFIX(complex) *) out_data);
464 gmx_fft_many_1d (gmx_fft_t fft,
465 enum gmx_fft_direction dir,
469 return gmx_fft_1d(fft, dir, in_data, out_data);
473 gmx_fft_1d_real (gmx_fft_t fft,
474 enum gmx_fft_direction dir,
478 int aligned = ((((size_t)in_data | (size_t)out_data) & 0xf) == 0);
479 int inplace = (in_data == out_data);
480 int isforward = (dir == GMX_FFT_REAL_TO_COMPLEX);
483 if ( (fft->real_transform != 1) || (fft->ndim != 1) ||
484 ((dir != GMX_FFT_REAL_TO_COMPLEX) && (dir != GMX_FFT_COMPLEX_TO_REAL)) )
486 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
492 FFTWPREFIX(execute_dft_r2c)(fft->plan[aligned][inplace][isforward],
493 (real *)in_data, (FFTWPREFIX(complex) *) out_data);
497 FFTWPREFIX(execute_dft_c2r)(fft->plan[aligned][inplace][isforward],
498 (FFTWPREFIX(complex) *) in_data, (real *)out_data);
505 gmx_fft_many_1d_real (gmx_fft_t fft,
506 enum gmx_fft_direction dir,
510 return gmx_fft_1d_real(fft, dir, in_data, out_data);
514 gmx_fft_2d_real (gmx_fft_t fft,
515 enum gmx_fft_direction dir,
519 int aligned = ((((size_t)in_data | (size_t)out_data) & 0xf) == 0);
520 int inplace = (in_data == out_data);
521 int isforward = (dir == GMX_FFT_REAL_TO_COMPLEX);
524 if ( (fft->real_transform != 1) || (fft->ndim != 2) ||
525 ((dir != GMX_FFT_REAL_TO_COMPLEX) && (dir != GMX_FFT_COMPLEX_TO_REAL)) )
527 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
533 FFTWPREFIX(execute_dft_r2c)(fft->plan[aligned][inplace][isforward],
535 (FFTWPREFIX(complex) *) out_data);
539 FFTWPREFIX(execute_dft_c2r)(fft->plan[aligned][inplace][isforward],
540 (FFTWPREFIX(complex) *) in_data,
549 gmx_fft_destroy(gmx_fft_t fft)
555 for (i = 0; i < 2; i++)
557 for (j = 0; j < 2; j++)
559 for (k = 0; k < 2; k++)
561 if (fft->plan[i][j][k] != NULL)
564 FFTWPREFIX(destroy_plan)(fft->plan[i][j][k]);
566 fft->plan[i][j][k] = NULL;
572 FFTWPREFIX(free)(fft);
579 gmx_many_fft_destroy(gmx_fft_t fft)
581 gmx_fft_destroy(fft);
584 void gmx_fft_cleanup()
586 FFTWPREFIX(cleanup)();
589 const char *gmx_fft_get_version_info()
591 #ifdef GMX_NATIVE_WINDOWS
594 return FFTWPREFIX(version);