3 * Gromacs 4.0 Copyright (c) 1991-2003
4 * David van der Spoel, Erik Lindahl, University of Groningen.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * To help us fund GROMACS development, we humbly ask that you cite
12 * the research papers on the package. Check out http://www.gromacs.org
15 * Gnomes, ROck Monsters And Chili Sauce
26 #include "gromacs/fft/fft.h"
27 #include "gmx_fatal.h"
30 #define FFTWPREFIX(name) fftw_ ## name
32 #define FFTWPREFIX(name) fftwf_ ## name
35 #include "thread_mpi/mutex.h"
36 #include "gromacs/utility/exceptions.h"
38 /* none of the fftw3 calls, except execute(), are thread-safe, so
39 we need to serialize them with this mutex. */
40 static tMPI::mutex big_fftw_mutex;
41 #define FFTW_LOCK try { big_fftw_mutex.lock(); } GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
42 #define FFTW_UNLOCK try { big_fftw_mutex.unlock(); } GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
44 /* 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.
46 - It is not allowed to use these FFT plans from memory which doesn't have a starting address as a multiple of 8 bytes.
47 This is OK as long as the memory directly comes from malloc and is not some subarray within alloated memory.
48 - This has to be fixed if any future architecute requires memory to be aligned to multiples of 32 bytes.
51 * Contents of the FFTW3 fft datatype.
53 * Note that this is one of several possible implementations of gmx_fft_t.
64 * Three alternatives (unaligned/aligned, out-of-place/in-place, forward/backward)
65 * results in 8 different FFTW plans. Keep track of them with 3 array indices:
66 * first index: 0=unaligned, 1=aligned
67 * second index: 0=out-of-place, 1=in-place
68 * third index: 0=backward, 1=forward
70 FFTWPREFIX(plan) plan[2][2][2];
71 /** Used to catch user mistakes */
73 /** Number of dimensions in the FFT */
78 gmx_fft_init_1d(gmx_fft_t * pfft,
82 return gmx_fft_init_many_1d(pfft, nx, 1, flags);
87 gmx_fft_init_many_1d(gmx_fft_t * pfft,
93 FFTWPREFIX(complex) *p1, *p2, *up1, *up2;
98 #ifdef GMX_DISABLE_FFTW_MEASURE
99 flags |= GMX_FFT_FLAG_CONSERVATIVE;
102 fftw_flags = (flags & GMX_FFT_FLAG_CONSERVATIVE) ? FFTW_ESTIMATE : FFTW_MEASURE;
106 gmx_fatal(FARGS, "Invalid opaque FFT datatype pointer.");
112 if ( (fft = (gmx_fft_t)FFTWPREFIX(malloc)(sizeof(struct gmx_fft))) == NULL)
118 /* allocate aligned, and extra memory to make it unaligned */
119 p1 = (FFTWPREFIX(complex) *) FFTWPREFIX(malloc)(sizeof(FFTWPREFIX(complex))*(nx+2)*howmany);
122 FFTWPREFIX(free)(fft);
127 p2 = (FFTWPREFIX(complex) *) FFTWPREFIX(malloc)(sizeof(FFTWPREFIX(complex))*(nx+2)*howmany);
130 FFTWPREFIX(free)(p1);
131 FFTWPREFIX(free)(fft);
136 /* make unaligned pointers.
137 * In double precision the actual complex datatype will be 16 bytes,
138 * so go to a char pointer and force an offset of 8 bytes instead.
142 up1 = (FFTWPREFIX(complex) *)pc;
146 up2 = (FFTWPREFIX(complex) *)pc;
148 /* int rank, const int *n, int howmany,
149 fftw_complex *in, const int *inembed,
150 int istride, int idist,
151 fftw_complex *out, const int *onembed,
152 int ostride, int odist,
153 int sign, unsigned flags */
154 fft->plan[0][0][0] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, up1, &nx, 1, nx, up2, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
155 fft->plan[0][0][1] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, up1, &nx, 1, nx, up2, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
156 fft->plan[0][1][0] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, up1, &nx, 1, nx, up1, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
157 fft->plan[0][1][1] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, up1, &nx, 1, nx, up1, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
158 fft->plan[1][0][0] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, p1, &nx, 1, nx, p2, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
159 fft->plan[1][0][1] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, p1, &nx, 1, nx, p2, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
160 fft->plan[1][1][0] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, p1, &nx, 1, nx, p1, &nx, 1, nx, FFTW_BACKWARD, fftw_flags);
161 fft->plan[1][1][1] = FFTWPREFIX(plan_many_dft)(1, &nx, howmany, p1, &nx, 1, nx, p1, &nx, 1, nx, FFTW_FORWARD, fftw_flags);
163 for (i = 0; i < 2; i++)
165 for (j = 0; j < 2; j++)
167 for (k = 0; k < 2; k++)
169 if (fft->plan[i][j][k] == NULL)
171 gmx_fatal(FARGS, "Error initializing FFTW3 plan.");
173 gmx_fft_destroy(fft);
175 FFTWPREFIX(free)(p1);
176 FFTWPREFIX(free)(p2);
184 FFTWPREFIX(free)(p1);
185 FFTWPREFIX(free)(p2);
187 fft->real_transform = 0;
196 gmx_fft_init_1d_real(gmx_fft_t * pfft,
200 return gmx_fft_init_many_1d_real(pfft, nx, 1, flags);
204 gmx_fft_init_many_1d_real(gmx_fft_t * pfft,
210 real *p1, *p2, *up1, *up2;
215 #ifdef GMX_DISABLE_FFTW_MEASURE
216 flags |= GMX_FFT_FLAG_CONSERVATIVE;
219 fftw_flags = (flags & GMX_FFT_FLAG_CONSERVATIVE) ? FFTW_ESTIMATE : FFTW_MEASURE;
223 gmx_fatal(FARGS, "Invalid opaque FFT datatype pointer.");
229 if ( (fft = (gmx_fft_t) FFTWPREFIX(malloc)(sizeof(struct gmx_fft))) == NULL)
235 /* allocate aligned, and extra memory to make it unaligned */
236 p1 = (real *) FFTWPREFIX(malloc)(sizeof(real)*(nx/2+1)*2*howmany + 8);
239 FFTWPREFIX(free)(fft);
244 p2 = (real *) FFTWPREFIX(malloc)(sizeof(real)*(nx/2+1)*2*howmany + 8);
247 FFTWPREFIX(free)(p1);
248 FFTWPREFIX(free)(fft);
253 /* make unaligned pointers.
254 * In double precision the actual complex datatype will be 16 bytes,
255 * so go to a char pointer and force an offset of 8 bytes instead.
265 /* int rank, const int *n, int howmany,
266 double *in, const int *inembed,
267 int istride, int idist,
268 fftw_complex *out, const int *onembed,
269 int ostride, int odist,
271 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);
272 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);
273 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);
274 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);
276 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);
277 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);
278 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);
279 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);
281 for (i = 0; i < 2; i++)
283 for (j = 0; j < 2; j++)
285 for (k = 0; k < 2; k++)
287 if (fft->plan[i][j][k] == NULL)
289 gmx_fatal(FARGS, "Error initializing FFTW3 plan.");
291 gmx_fft_destroy(fft);
293 FFTWPREFIX(free)(p1);
294 FFTWPREFIX(free)(p2);
302 FFTWPREFIX(free)(p1);
303 FFTWPREFIX(free)(p2);
305 fft->real_transform = 1;
315 gmx_fft_init_2d_real(gmx_fft_t * pfft,
321 real *p1, *p2, *up1, *up2;
326 #ifdef GMX_DISABLE_FFTW_MEASURE
327 flags |= GMX_FFT_FLAG_CONSERVATIVE;
330 fftw_flags = (flags & GMX_FFT_FLAG_CONSERVATIVE) ? FFTW_ESTIMATE : FFTW_MEASURE;
334 gmx_fatal(FARGS, "Invalid opaque FFT datatype pointer.");
340 if ( (fft = (gmx_fft_t) FFTWPREFIX(malloc)(sizeof(struct gmx_fft))) == NULL)
346 /* allocate aligned, and extra memory to make it unaligned */
347 p1 = (real *) FFTWPREFIX(malloc)(sizeof(real) *( nx*(ny/2+1)*2 + 2) );
350 FFTWPREFIX(free)(fft);
355 p2 = (real *) FFTWPREFIX(malloc)(sizeof(real) *( nx*(ny/2+1)*2 + 2) );
358 FFTWPREFIX(free)(p1);
359 FFTWPREFIX(free)(fft);
364 /* make unaligned pointers.
365 * In double precision the actual complex datatype will be 16 bytes,
366 * so go to a char pointer and force an offset of 8 bytes instead.
377 fft->plan[0][0][0] = FFTWPREFIX(plan_dft_c2r_2d)(nx, ny, (FFTWPREFIX(complex) *) up1, up2, fftw_flags);
378 fft->plan[0][0][1] = FFTWPREFIX(plan_dft_r2c_2d)(nx, ny, up1, (FFTWPREFIX(complex) *) up2, fftw_flags);
379 fft->plan[0][1][0] = FFTWPREFIX(plan_dft_c2r_2d)(nx, ny, (FFTWPREFIX(complex) *) up1, up1, fftw_flags);
380 fft->plan[0][1][1] = FFTWPREFIX(plan_dft_r2c_2d)(nx, ny, up1, (FFTWPREFIX(complex) *) up1, fftw_flags);
382 fft->plan[1][0][0] = FFTWPREFIX(plan_dft_c2r_2d)(nx, ny, (FFTWPREFIX(complex) *) p1, p2, fftw_flags);
383 fft->plan[1][0][1] = FFTWPREFIX(plan_dft_r2c_2d)(nx, ny, p1, (FFTWPREFIX(complex) *) p2, fftw_flags);
384 fft->plan[1][1][0] = FFTWPREFIX(plan_dft_c2r_2d)(nx, ny, (FFTWPREFIX(complex) *) p1, p1, fftw_flags);
385 fft->plan[1][1][1] = FFTWPREFIX(plan_dft_r2c_2d)(nx, ny, p1, (FFTWPREFIX(complex) *) p1, fftw_flags);
388 for (i = 0; i < 2; i++)
390 for (j = 0; j < 2; j++)
392 for (k = 0; k < 2; k++)
394 if (fft->plan[i][j][k] == NULL)
396 gmx_fatal(FARGS, "Error initializing FFTW3 plan.");
398 gmx_fft_destroy(fft);
400 FFTWPREFIX(free)(p1);
401 FFTWPREFIX(free)(p2);
409 FFTWPREFIX(free)(p1);
410 FFTWPREFIX(free)(p2);
412 fft->real_transform = 1;
421 gmx_fft_1d (gmx_fft_t fft,
422 enum gmx_fft_direction dir,
426 int aligned = ((((size_t)in_data | (size_t)out_data) & 0xf) == 0);
427 int inplace = (in_data == out_data);
428 int isforward = (dir == GMX_FFT_FORWARD);
431 if ( (fft->real_transform == 1) || (fft->ndim != 1) ||
432 ((dir != GMX_FFT_FORWARD) && (dir != GMX_FFT_BACKWARD)) )
434 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
438 FFTWPREFIX(execute_dft)(fft->plan[aligned][inplace][isforward],
439 (FFTWPREFIX(complex) *) in_data,
440 (FFTWPREFIX(complex) *) out_data);
446 gmx_fft_many_1d (gmx_fft_t fft,
447 enum gmx_fft_direction dir,
451 return gmx_fft_1d(fft, dir, in_data, out_data);
455 gmx_fft_1d_real (gmx_fft_t fft,
456 enum gmx_fft_direction dir,
460 int aligned = ((((size_t)in_data | (size_t)out_data) & 0xf) == 0);
461 int inplace = (in_data == out_data);
462 int isforward = (dir == GMX_FFT_REAL_TO_COMPLEX);
465 if ( (fft->real_transform != 1) || (fft->ndim != 1) ||
466 ((dir != GMX_FFT_REAL_TO_COMPLEX) && (dir != GMX_FFT_COMPLEX_TO_REAL)) )
468 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
474 FFTWPREFIX(execute_dft_r2c)(fft->plan[aligned][inplace][isforward],
475 (real *)in_data, (FFTWPREFIX(complex) *) out_data);
479 FFTWPREFIX(execute_dft_c2r)(fft->plan[aligned][inplace][isforward],
480 (FFTWPREFIX(complex) *) in_data, (real *)out_data);
487 gmx_fft_many_1d_real (gmx_fft_t fft,
488 enum gmx_fft_direction dir,
492 return gmx_fft_1d_real(fft, dir, in_data, out_data);
496 gmx_fft_2d_real (gmx_fft_t fft,
497 enum gmx_fft_direction dir,
501 int aligned = ((((size_t)in_data | (size_t)out_data) & 0xf) == 0);
502 int inplace = (in_data == out_data);
503 int isforward = (dir == GMX_FFT_REAL_TO_COMPLEX);
506 if ( (fft->real_transform != 1) || (fft->ndim != 2) ||
507 ((dir != GMX_FFT_REAL_TO_COMPLEX) && (dir != GMX_FFT_COMPLEX_TO_REAL)) )
509 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
515 FFTWPREFIX(execute_dft_r2c)(fft->plan[aligned][inplace][isforward],
517 (FFTWPREFIX(complex) *) out_data);
521 FFTWPREFIX(execute_dft_c2r)(fft->plan[aligned][inplace][isforward],
522 (FFTWPREFIX(complex) *) in_data,
531 gmx_fft_destroy(gmx_fft_t fft)
537 for (i = 0; i < 2; i++)
539 for (j = 0; j < 2; j++)
541 for (k = 0; k < 2; k++)
543 if (fft->plan[i][j][k] != NULL)
546 FFTWPREFIX(destroy_plan)(fft->plan[i][j][k]);
548 fft->plan[i][j][k] = NULL;
554 FFTWPREFIX(free)(fft);
561 gmx_many_fft_destroy(gmx_fft_t fft)
563 gmx_fft_destroy(fft);
566 void gmx_fft_cleanup()
568 FFTWPREFIX(cleanup)();