(git:e70887f)
Loading...
Searching...
No Matches
fft_lib.c
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* CP2K: A general program to perform molecular dynamics simulations */
3/* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: BSD-3-Clause */
6/*----------------------------------------------------------------------------*/
7
8#include "fft_lib.h"
10#include "fft_lib_fftw.h"
11#include "fft_timer.h"
12#include "fft_utils.h"
13#include "gpu/fft_gpu.h"
14
15#include "../base/cp_assert.h"
16#include <math.h>
17#include <stdarg.h>
18#include <stdbool.h>
19#include <stddef.h>
20#include <stdio.h>
21#include <string.h>
22
23#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
25#elif defined(__FFTW3)
27#else
28#error "The FFT backend needs at least the FFTW3 backend."
29const int fft_lib_default = -1; // No backend available
30#endif
33
34/*******************************************************************************
35 * \brief Start an FFT timer from a printf-style label.
36 ******************************************************************************/
37static int fft_timer_start(const char *format, ...) {
38 char routine_name[FFT_MAX_STRING_LENGTH + 1];
39 va_list arguments;
40 va_start(arguments, format);
41 vsnprintf(routine_name, sizeof(routine_name), format, arguments);
42 va_end(arguments);
43 return fft_start_timer(routine_name);
44}
45
46/*******************************************************************************
47 * \brief Forward a void FFT call to the selected backend.
48 ******************************************************************************/
49#define FFT_DISPATCH_VOID(gpu_call, fftw_call) \
50 do { \
51 switch (fft_lib_choice) { \
52 case FFT_LIB_GPU: \
53 gpu_call; \
54 break; \
55 case FFT_LIB_FFTW: \
56 fftw_call; \
57 break; \
58 default: \
59 cp_assert(0 && "Unknown FFT library."); \
60 } \
61 } while (0)
62
63/*******************************************************************************
64 * \brief Initialize the accelerated FFT library.
65 * \author Frederick Stein
66 ******************************************************************************/
68#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
70 return 0;
71#else
72 return 1;
73#endif
74}
75
76/*******************************************************************************
77 * \brief Initialize the FFT library (if not done externally).
78 * \param lib FFT backend to use.
79 * \param fftw_planning_flag FFTW planning mode.
80 * \param use_guru_interface Whether the FFTW Guru interface may be used.
81 * \param wisdom_file Path to the FFTW wisdom file.
82 * \author Frederick Stein
83 ******************************************************************************/
84int fft_init_lib(const fft_lib lib, const int fftw_planning_flag,
85 const bool use_guru_interface, const char *wisdom_file) {
87 return 0;
88 }
90 if (lib != FFT_LIB_DEFAULT)
91 fft_lib_choice = lib;
92 int status = 1;
93 switch (fft_lib_choice) {
94 case FFT_LIB_FFTW:
95 status =
96 fft_fftw_init_lib(fftw_planning_flag, use_guru_interface, wisdom_file);
97#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
98 // Distributed FFTs always use the GPU backend, so it must be initialized
99 // even when FFTW3 is selected for the local FFTs.
100 if (status == 0) {
101 status = fft_init_acc_lib();
102 }
103#endif
104 break;
105 case FFT_LIB_GPU:
106 status = fft_init_acc_lib();
107 break;
108 default:
109 cp_assert(0 && "Unknown FFT library.");
110 }
111 if (status != 0) {
112 fft_finalize_lib(NULL);
113 return status;
114 }
116 return status;
117}
118
119/*******************************************************************************
120 * \brief Finalize the accelerated FFT library.
121 * \author Frederick Stein
122 ******************************************************************************/
124#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
126#endif
127}
128
129/*******************************************************************************
130 * \brief Finalize the FFT library (if not done externally).
131 * \param wisdom_file Path to the FFTW wisdom file.
132 * \author Frederick Stein
133 ******************************************************************************/
134void fft_finalize_lib(const char *wisdom_file) {
135 fft_fftw_finalize_lib(wisdom_file);
137 fft_lib_initialized = false;
138}
139
140/*******************************************************************************
141 * \brief Get the default library (GPU if offloading was enabled, else FFTW3).
142 * \author Frederick Stein
143 ******************************************************************************/
145
146/*******************************************************************************
147 * \brief Whether a compound MPI implementation is available.
148 * \author Frederick Stein
149 ******************************************************************************/
151
152/*******************************************************************************
153 * \brief Whether a compound MPI implementation is available.
154 * \author Frederick Stein
155 ******************************************************************************/
157 switch (fft_lib_choice) {
158 case FFT_LIB_FFTW:
160 case FFT_LIB_GPU:
161 return false;
162 default:
163 cp_assert(0 && "Unknown FFT library.");
164 return false;
165 }
166}
167
168/*******************************************************************************
169 * \brief Allocate buffer of type double.
170 * \param length Number of elements.
171 * \param buffer Buffer pointer.
172 * \author Frederick Stein
173 ******************************************************************************/
174void fft_allocate_double(const int length, double **buffer) {
176 (fft_fftw_allocate_double(length, buffer)));
177}
178
179/*******************************************************************************
180 * \brief Allocate buffer of type double complex.
181 * \param length Number of elements.
182 * \param buffer Buffer pointer.
183 * \author Frederick Stein
184 ******************************************************************************/
185void fft_allocate_complex(const int length, double complex **buffer) {
186 FFT_DISPATCH_VOID((fft_gpu_allocate_complex(imax(1, length), buffer)),
187 (fft_fftw_allocate_complex(imax(1, length), buffer)));
188}
189
190/*******************************************************************************
191 * \brief Allocate buffer of type double.
192 * \param buffer Buffer pointer.
193 * \author Frederick Stein
194 ******************************************************************************/
195void fft_free_double(double *buffer) {
197 fft_fftw_free_double(buffer);
198 } else if (fft_lib_choice == FFT_LIB_GPU) {
199 fft_gpu_free_double(buffer);
200 } else {
201 cp_assert(0 && "Unknown FFT library.");
202 }
203}
204
205/*******************************************************************************
206 * \brief Allocate buffer of type double complex.
207 * \param buffer Buffer pointer.
208 * \author Frederick Stein
209 ******************************************************************************/
210void fft_free_complex(double complex *buffer) {
212 fft_fftw_free_complex(buffer);
213 } else if (fft_lib_choice == FFT_LIB_GPU) {
214 fft_gpu_free_complex(buffer);
215 } else {
216 cp_assert(0 && "Unknown FFT library.");
217 }
218}
219
220/*******************************************************************************
221 * \brief Register a local 1D C2C FFT.
222 * \param dir FFT direction (forward or backward).
223 * \param fft_size Size of the transform in each dimension.
224 * \param number_of_ffts Number of independent (batched) transforms.
225 * \param transpose_in Whether the input array uses the transposed layout.
226 * \param transpose_out Whether the output array uses the transposed layout.
227 * \param leading_dimension_in Leading dimension of the input array.
228 * \param leading_dimension_out Leading dimension of the output array.
229 * \param grid_in Input grid buffer.
230 * \param grid_out Output grid buffer.
231 * \author Frederick Stein
232 ******************************************************************************/
233void fft_register_1d_local(const bool dir, const int fft_size,
234 const int number_of_ffts, const bool transpose_in,
235 const bool transpose_out,
236 const int leading_dimension_in,
237 const int leading_dimension_out,
238 double complex *grid_in, double complex *grid_out) {
239 const int handle = fft_timer_start("register_1d_local");
240 const int handle2 = fft_timer_start(
241 "register_1d_l_%s_%s_%i_%i", dir ? "fw" : "bw",
242 grid_in == grid_out ? "i" : "o", fft_size, number_of_ffts);
244 (fft_gpu_register_1d_local(dir, fft_size, number_of_ffts, transpose_in,
245 transpose_out, leading_dimension_in,
246 leading_dimension_out, grid_in == grid_out)),
247 (fft_fftw_register_1d_local(dir, fft_size, number_of_ffts, transpose_in,
248 transpose_out, leading_dimension_in,
249 leading_dimension_out, grid_in, grid_out)));
250 fft_stop_timer(handle2);
251 fft_stop_timer(handle);
252}
253
254/*******************************************************************************
255 * \brief Register a local 1D R2C FFT.
256 * \param fft_size Size of the transform in each dimension.
257 * \param number_of_ffts Number of independent (batched) transforms.
258 * \param transpose_in Whether the input array uses the transposed layout.
259 * \param transpose_out Whether the output array uses the transposed layout.
260 * \param leading_dimension_in Leading dimension of the input array.
261 * \param leading_dimension_out Leading dimension of the output array.
262 * \param grid_in Input grid buffer.
263 * \param grid_out Output grid buffer.
264 * \author Frederick Stein
265 ******************************************************************************/
266void fft_register_1d_r2c_local(const int fft_size, const int number_of_ffts,
267 const bool transpose_in,
268 const bool transpose_out,
269 const int leading_dimension_in,
270 const int leading_dimension_out, double *grid_in,
271 double complex *grid_out) {
272 const int handle = fft_timer_start("register_1d_r2c_local");
273 const int handle2 = fft_timer_start("register_1d_r2c_l_%s_%i_%i",
274 grid_in == (double *)grid_out ? "i" : "o",
275 fft_size, number_of_ffts);
278 true, fft_size, number_of_ffts, transpose_in, transpose_out,
279 leading_dimension_in, leading_dimension_out,
280 grid_in == (double *)grid_out)),
282 fft_size, number_of_ffts, transpose_in, transpose_out,
283 leading_dimension_in, leading_dimension_out, grid_in, grid_out)));
284 fft_stop_timer(handle2);
285 fft_stop_timer(handle);
286}
287
288/*******************************************************************************
289 * \brief Register a local 1D C2R FFT.
290 * \param fft_size Size of the transform in each dimension.
291 * \param number_of_ffts Number of independent (batched) transforms.
292 * \param transpose_in Whether the input array uses the transposed layout.
293 * \param transpose_out Whether the output array uses the transposed layout.
294 * \param leading_dimension_in Leading dimension of the input array.
295 * \param leading_dimension_out Leading dimension of the output array.
296 * \param grid_in Input grid buffer.
297 * \param grid_out Output grid buffer.
298 * \author Frederick Stein
299 ******************************************************************************/
300void fft_register_1d_c2r_local(const int fft_size, const int number_of_ffts,
301 const bool transpose_in,
302 const bool transpose_out,
303 const int leading_dimension_in,
304 const int leading_dimension_out,
305 double complex *grid_in, double *grid_out) {
306 const int handle = fft_timer_start("register_1d_c2r_local");
307 const int handle2 = fft_timer_start("register_1d_c2r_l_%s_%i_%i",
308 (double *)grid_in == grid_out ? "i" : "o",
309 fft_size, number_of_ffts);
312 false, fft_size, number_of_ffts, transpose_in, transpose_out,
313 leading_dimension_in, leading_dimension_out,
314 grid_in == (double complex *)grid_out)),
316 fft_size, number_of_ffts, transpose_in, transpose_out,
317 leading_dimension_in, leading_dimension_out, grid_in, grid_out)));
318 fft_stop_timer(handle2);
319 fft_stop_timer(handle);
320}
321
322/*******************************************************************************
323 * \brief Register a local 2D FFT.
324 * \param dir FFT direction (forward or backward).
325 * \param fft_size Size of the transform in each dimension.
326 * \param number_of_ffts Number of independent (batched) transforms.
327 * \param transpose_in Whether the input array uses the transposed layout.
328 * \param transpose_out Whether the output array uses the transposed layout.
329 * \param grid_in Input grid buffer.
330 * \param grid_out Output grid buffer.
331 * \author Frederick Stein
332 ******************************************************************************/
333void fft_register_2d_local(const bool dir, const int fft_size[2],
334 const int number_of_ffts, const bool transpose_in,
335 const bool transpose_out, double complex *grid_in,
336 double complex *grid_out) {
337 const int handle = fft_timer_start("register_2d_local");
338 const int handle2 =
339 fft_timer_start("register_2d_l_%s_%s_%i_%i_%i", dir ? "fw" : "bw",
340 grid_in == grid_out ? "i" : "o", fft_size[0], fft_size[1],
341 number_of_ffts);
343 (fft_gpu_register_2d_local(dir, fft_size, number_of_ffts, transpose_in,
344 transpose_out, grid_in == grid_out)),
345 (fft_fftw_register_2d_local(dir, fft_size, number_of_ffts, transpose_in,
346 transpose_out, grid_in, grid_out)));
347 fft_stop_timer(handle2);
348 fft_stop_timer(handle);
349}
350
351/*******************************************************************************
352 * \brief Register a local 2D R2C FFT.
353 * \param fft_size Size of the transform in each dimension.
354 * \param number_of_ffts Number of independent (batched) transforms.
355 * \param transpose_in Whether the input array uses the transposed layout.
356 * \param transpose_out Whether the output array uses the transposed layout.
357 * \param grid_in Input grid buffer.
358 * \param grid_out Output grid buffer.
359 * \author Frederick Stein
360 ******************************************************************************/
361void fft_register_2d_r2c_local(const int fft_size[2], const int number_of_ffts,
362 const bool transpose_in,
363 const bool transpose_out, double *grid_in,
364 double complex *grid_out) {
365 const int handle = fft_timer_start("register_2d_r2c_local");
366 const int handle2 = fft_timer_start("register_2d_r2c_l_%s_%i_%i_%i",
367 grid_in == (double *)grid_out ? "i" : "o",
368 fft_size[0], fft_size[1], number_of_ffts);
370 (fft_gpu_register_2d_r2c_local(true, fft_size, number_of_ffts,
371 transpose_in, transpose_out,
372 grid_in == (double *)grid_out)),
373 (fft_fftw_register_2d_r2c_local(fft_size, number_of_ffts, transpose_in,
374 transpose_out, grid_in, grid_out)));
375 fft_stop_timer(handle2);
376 fft_stop_timer(handle);
377}
378
379/*******************************************************************************
380 * \brief Register a local 2D C2R FFT.
381 * \param fft_size Size of the transform in each dimension.
382 * \param number_of_ffts Number of independent (batched) transforms.
383 * \param transpose_in Whether the input array uses the transposed layout.
384 * \param transpose_out Whether the output array uses the transposed layout.
385 * \param grid_in Input grid buffer.
386 * \param grid_out Output grid buffer.
387 * \author Frederick Stein
388 ******************************************************************************/
389void fft_register_2d_c2r_local(const int fft_size[2], const int number_of_ffts,
390 const bool transpose_in,
391 const bool transpose_out,
392 double complex *grid_in, double *grid_out) {
393 const int handle = fft_timer_start("register_2d_c2r_local");
394 const int handle2 = fft_timer_start("register_2d_c2r_l_%s_%i_%i_%i",
395 (double *)grid_in == grid_out ? "i" : "o",
396 fft_size[0], fft_size[1], number_of_ffts);
398 (fft_gpu_register_2d_r2c_local(false, fft_size, number_of_ffts,
399 transpose_in, transpose_out,
400 grid_in == (double complex *)grid_out)),
401 (fft_fftw_register_2d_c2r_local(fft_size, number_of_ffts, transpose_in,
402 transpose_out, grid_in, grid_out)));
403 fft_stop_timer(handle2);
404 fft_stop_timer(handle);
405}
406
407/*******************************************************************************
408 * \brief Register a local 3D FFT.
409 * \note fft_3d_bw_local(grid_gs, grid_rs, n) is the reverse to
410 * fft_3d_rw_local(grid_rs, grid_gs, n) (ignoring normalization).
411 * \param dir FFT direction (forward or backward).
412 * \param fft_size Size of the transform in each dimension.
413 * \param number_of_ffts Number of independent (batched) transforms.
414 * \param transpose_in Whether the input array uses the transposed layout.
415 * \param transpose_out Whether the output array uses the transposed layout.
416 * \param grid_in Input grid buffer.
417 * \param grid_out Output grid buffer.
418 * \author Frederick Stein
419 ******************************************************************************/
420void fft_register_3d_local(const bool dir, const int fft_size[3],
421 const int number_of_ffts, const bool transpose_in,
422 const bool transpose_out, double complex *grid_in,
423 double complex *grid_out) {
424 const int handle = fft_timer_start("register_3d_local");
425 const int handle2 = fft_timer_start(
426 "register_3d_l_%s_%s_%i_%i_%i", dir ? "fw" : "bw",
427 grid_in == grid_out ? "i" : "o", fft_size[0], fft_size[1], fft_size[2]);
429 (fft_gpu_register_3d_local(dir, fft_size, grid_in == grid_out,
430 number_of_ffts, transpose_in, transpose_out)),
431 (fft_fftw_register_3d_local(dir, fft_size, number_of_ffts, transpose_in,
432 transpose_out, grid_in, grid_out)));
433 fft_stop_timer(handle2);
434 fft_stop_timer(handle);
435}
436
437/*******************************************************************************
438 * \brief Register a local 3D R2C FFT.
439 * \param fft_size Size of the transform in each dimension.
440 * \param number_of_ffts Number of independent (batched) transforms.
441 * \param transpose_in Whether the input array uses the transposed layout.
442 * \param transpose_out Whether the output array uses the transposed layout.
443 * \param grid_in Input grid buffer.
444 * \param grid_out Output grid buffer.
445 * \author Frederick Stein
446 ******************************************************************************/
447void fft_register_3d_r2c_local(const int fft_size[3], const int number_of_ffts,
448 const bool transpose_in,
449 const bool transpose_out, double *grid_in,
450 double complex *grid_out) {
451 const int handle = fft_timer_start("register_3d_r2c_local");
452 const int handle2 = fft_timer_start("register_3d_r2c_l_%s_%i_%i_%i",
453 grid_in == (double *)grid_out ? "i" : "o",
454 fft_size[0], fft_size[1], fft_size[2]);
457 true, fft_size, grid_in == (double *)grid_out, number_of_ffts,
458 transpose_in, transpose_out)),
459 (fft_fftw_register_3d_r2c_local(fft_size, number_of_ffts, transpose_in,
460 transpose_out, grid_in, grid_out)));
461 fft_stop_timer(handle2);
462 fft_stop_timer(handle);
463}
464
465/*******************************************************************************
466 * \brief Register a local 3D C2R FFT.
467 * \param fft_size Size of the transform in each dimension.
468 * \param number_of_ffts Number of independent (batched) transforms.
469 * \param transpose_in Whether the input array uses the transposed layout.
470 * \param transpose_out Whether the output array uses the transposed layout.
471 * \param grid_in Input grid buffer.
472 * \param grid_out Output grid buffer.
473 * \author Frederick Stein
474 ******************************************************************************/
475void fft_register_3d_c2r_local(const int fft_size[3], const int number_of_ffts,
476 const bool transpose_in,
477 const bool transpose_out,
478 double complex *grid_in, double *grid_out) {
479 const int handle = fft_timer_start("register_3d_c2r_local");
480 const int handle2 = fft_timer_start("register_3d_c2r_l_%s_%i_%i_%i",
481 (double *)grid_in == grid_out ? "i" : "o",
482 fft_size[0], fft_size[1], fft_size[2]);
485 false, fft_size, grid_in == (double complex *)grid_out,
486 number_of_ffts, transpose_in, transpose_out)),
487 (fft_fftw_register_3d_c2r_local(fft_size, number_of_ffts, transpose_in,
488 transpose_out, grid_in, grid_out)));
489 fft_stop_timer(handle2);
490 fft_stop_timer(handle);
491}
492
493/*******************************************************************************
494 * \brief Perform a local 1D FFT.
495 * \param dir FFT direction (forward or backward).
496 * \param fft_size Size of the transform in each dimension.
497 * \param number_of_ffts Number of independent (batched) transforms.
498 * \param transpose_in Whether the input array uses the transposed layout.
499 * \param transpose_out Whether the output array uses the transposed layout.
500 * \param leading_dimension_in Leading dimension of the input array.
501 * \param leading_dimension_out Leading dimension of the output array.
502 * \param grid_in Input grid buffer.
503 * \param grid_out Output grid buffer.
504 * \author Frederick Stein
505 ******************************************************************************/
506void fft_1d_local(const bool dir, const int fft_size, const int number_of_ffts,
507 const bool transpose_in, const bool transpose_out,
508 const int leading_dimension_in,
509 const int leading_dimension_out, double complex *grid_in,
510 double complex *grid_out) {
511 const int handle = fft_timer_start("1d_local");
512 const int handle2 = fft_timer_start("1d_l_%s_%s_%i_%i", dir ? "fw" : "bw",
513 grid_in == grid_out ? "i" : "o", fft_size,
514 number_of_ffts);
516 (fft_gpu_1d_local((const double *)grid_in, (double *)grid_out, dir,
517 fft_size, number_of_ffts, transpose_in, transpose_out,
518 leading_dimension_in, leading_dimension_out)),
519 (fft_fftw_1d_local(dir, fft_size, number_of_ffts, transpose_in,
520 transpose_out, leading_dimension_in,
521 leading_dimension_out, grid_in, grid_out)));
522 fft_stop_timer(handle2);
523 fft_stop_timer(handle);
524}
525
526/*******************************************************************************
527 * \brief Perform a local 1D R2C FFT.
528 * \param fft_size Size of the transform in each dimension.
529 * \param number_of_ffts Number of independent (batched) transforms.
530 * \param transpose_in Whether the input array uses the transposed layout.
531 * \param transpose_out Whether the output array uses the transposed layout.
532 * \param leading_dimension_in Leading dimension of the input array.
533 * \param leading_dimension_out Leading dimension of the output array.
534 * \param grid_in Input grid buffer.
535 * \param grid_out Output grid buffer.
536 * \author Frederick Stein
537 ******************************************************************************/
538void fft_1d_r2c_local(const int fft_size, const int number_of_ffts,
539 const bool transpose_in, const bool transpose_out,
540 const int leading_dimension_in,
541 const int leading_dimension_out, double *grid_in,
542 double complex *grid_out) {
543 const int handle = fft_timer_start("1d_r2c_local");
544 const int handle2 = fft_timer_start("1d_r2c_l_%s_%i_%i",
545 grid_in == (double *)grid_out ? "i" : "o",
546 fft_size, number_of_ffts);
548 (fft_gpu_1d_r2c_local((const double *)grid_in, (double *)grid_out, true,
549 fft_size, number_of_ffts, transpose_in,
550 transpose_out, leading_dimension_in,
551 leading_dimension_out)),
552 (fft_fftw_1d_r2c_local(fft_size, number_of_ffts, transpose_in,
553 transpose_out, leading_dimension_in,
554 leading_dimension_out, grid_in, grid_out)));
555 fft_stop_timer(handle2);
556 fft_stop_timer(handle);
557}
558
559/*******************************************************************************
560 * \brief Perform a local 1D C2R FFT.
561 * \param fft_size Size of the transform in each dimension.
562 * \param number_of_ffts Number of independent (batched) transforms.
563 * \param transpose_in Whether the input array uses the transposed layout.
564 * \param transpose_out Whether the output array uses the transposed layout.
565 * \param leading_dimension_in Leading dimension of the input array.
566 * \param leading_dimension_out Leading dimension of the output array.
567 * \param grid_in Input grid buffer.
568 * \param grid_out Output grid buffer.
569 * \author Frederick Stein
570 ******************************************************************************/
571void fft_1d_c2r_local(const int fft_size, const int number_of_ffts,
572 const bool transpose_in, const bool transpose_out,
573 const int leading_dimension_in,
574 const int leading_dimension_out, double complex *grid_in,
575 double *grid_out) {
576 const int handle = fft_timer_start("1d_c2r_local");
577 const int handle2 = fft_timer_start("1d_c2r_l_%s_%i_%i",
578 (double *)grid_in == grid_out ? "i" : "o",
579 fft_size, number_of_ffts);
581 (fft_gpu_1d_r2c_local((const double *)grid_in, (double *)grid_out, false,
582 fft_size, number_of_ffts, transpose_in,
583 transpose_out, leading_dimension_in,
584 leading_dimension_out)),
585 (fft_fftw_1d_c2r_local(fft_size, number_of_ffts, transpose_in,
586 transpose_out, leading_dimension_in,
587 leading_dimension_out, grid_in, grid_out)));
588 fft_stop_timer(handle2);
589 fft_stop_timer(handle);
590}
591
592/*******************************************************************************
593 * \brief Perform a local 2D FFT.
594 * \param dir FFT direction (forward or backward).
595 * \param fft_size Size of the transform in each dimension.
596 * \param number_of_ffts Number of independent (batched) transforms.
597 * \param transpose_in Whether the input array uses the transposed layout.
598 * \param transpose_out Whether the output array uses the transposed layout.
599 * \param grid_in Input grid buffer.
600 * \param grid_out Output grid buffer.
601 * \author Frederick Stein
602 ******************************************************************************/
603void fft_2d_local(const bool dir, const int fft_size[2],
604 const int number_of_ffts, const bool transpose_in,
605 const bool transpose_out, double complex *grid_in,
606 double complex *grid_out) {
607 const int handle = fft_timer_start("2d_local");
608 const int handle2 = fft_timer_start("2d_l_%s_%s_%i_%i_%i", dir ? "fw" : "bw",
609 grid_in == grid_out ? "i" : "o",
610 fft_size[0], fft_size[1], number_of_ffts);
612 (fft_gpu_2d_local((const double *)grid_in, (double *)grid_out, dir,
613 fft_size, number_of_ffts, transpose_in, transpose_out)),
614 (fft_fftw_2d_local(dir, fft_size, number_of_ffts, transpose_in,
615 transpose_out, grid_in, grid_out)));
616 fft_stop_timer(handle2);
617 fft_stop_timer(handle);
618}
619
620/*******************************************************************************
621 * \brief Perform a local 2D R2C FFT.
622 * \param fft_size Size of the transform in each dimension.
623 * \param number_of_ffts Number of independent (batched) transforms.
624 * \param transpose_in Whether the input array uses the transposed layout.
625 * \param transpose_out Whether the output array uses the transposed layout.
626 * \param grid_in Input grid buffer.
627 * \param grid_out Output grid buffer.
628 * \author Frederick Stein
629 ******************************************************************************/
630void fft_2d_r2c_local(const int fft_size[2], const int number_of_ffts,
631 const bool transpose_in, const bool transpose_out,
632 double *grid_in, double complex *grid_out) {
633 const int handle = fft_timer_start("2d_r2c_local");
634 const int handle2 = fft_timer_start("2d_r2c_l_%s_%i_%i_%i",
635 grid_in == (double *)grid_out ? "i" : "o",
636 fft_size[0], fft_size[1], number_of_ffts);
638 (fft_gpu_2d_r2c_local((const double *)grid_in, (double *)grid_out, true,
639 fft_size, number_of_ffts, transpose_in,
640 transpose_out)),
641 (fft_fftw_2d_r2c_local(fft_size, number_of_ffts, transpose_in,
642 transpose_out, grid_in, grid_out)));
643 fft_stop_timer(handle2);
644 fft_stop_timer(handle);
645}
646
647/*******************************************************************************
648 * \brief Perform a local 2D C2R FFT.
649 * \param fft_size Size of the transform in each dimension.
650 * \param number_of_ffts Number of independent (batched) transforms.
651 * \param transpose_in Whether the input array uses the transposed layout.
652 * \param transpose_out Whether the output array uses the transposed layout.
653 * \param grid_in Input grid buffer.
654 * \param grid_out Output grid buffer.
655 * \author Frederick Stein
656 ******************************************************************************/
657void fft_2d_c2r_local(const int fft_size[2], const int number_of_ffts,
658 const bool transpose_in, const bool transpose_out,
659 double complex *grid_in, double *grid_out) {
660 const int handle = fft_timer_start("2d_c2r_local");
661 const int handle2 = fft_timer_start("2d_c2r_l_%s_%i_%i_%i",
662 (double *)grid_in == grid_out ? "i" : "o",
663 fft_size[0], fft_size[1], number_of_ffts);
665 (fft_gpu_2d_r2c_local((const double *)grid_in, (double *)grid_out, false,
666 fft_size, number_of_ffts, transpose_in,
667 transpose_out)),
668 (fft_fftw_2d_c2r_local(fft_size, number_of_ffts, transpose_in,
669 transpose_out, grid_in, grid_out)));
670 fft_stop_timer(handle2);
671 fft_stop_timer(handle);
672}
673
674/*******************************************************************************
675 * \brief Perform a local 3D FFT.
676 * \param dir FFT direction (forward or backward).
677 * \param fft_size Size of the transform in each dimension.
678 * \param number_of_ffts Number of independent (batched) transforms.
679 * \param transpose_in Whether the input array uses the transposed layout.
680 * \param transpose_out Whether the output array uses the transposed layout.
681 * \param grid_in Input grid buffer.
682 * \param grid_out Output grid buffer.
683 * \author Frederick Stein
684 ******************************************************************************/
685void fft_3d_local(const bool dir, const int fft_size[3],
686 const int number_of_ffts, const bool transpose_in,
687 const bool transpose_out, double complex *grid_in,
688 double complex *grid_out) {
689 const int handle = fft_timer_start("3d_local");
690 const int handle2 = fft_timer_start("3d_l_%s_%s_%i_%i_%i", dir ? "fw" : "bw",
691 grid_in == grid_out ? "i" : "o",
692 fft_size[0], fft_size[1], fft_size[2]);
694 (fft_gpu_3d_local((const double *)grid_in, (double *)grid_out, dir,
695 fft_size, number_of_ffts, transpose_in, transpose_out)),
696 (fft_fftw_3d_local(dir, fft_size, number_of_ffts, transpose_in,
697 transpose_out, grid_in, grid_out)));
698 fft_stop_timer(handle2);
699 fft_stop_timer(handle);
700}
701
702/*******************************************************************************
703 * \brief Perform a local 3D R2C FFT.
704 * \param fft_size Size of the transform in each dimension.
705 * \param number_of_ffts Number of independent (batched) transforms.
706 * \param transpose_in Whether the input array uses the transposed layout.
707 * \param transpose_out Whether the output array uses the transposed layout.
708 * \param grid_in Input grid buffer.
709 * \param grid_out Output grid buffer.
710 * \author Frederick Stein
711 ******************************************************************************/
712void fft_3d_r2c_local(const int fft_size[3], const int number_of_ffts,
713 const bool transpose_in, const bool transpose_out,
714 double *grid_in, double complex *grid_out) {
715 const int handle = fft_timer_start("3d_r2c_local");
716 const int handle2 = fft_timer_start("3d_r2c_l_%s_%i_%i_%i",
717 grid_in == (double *)grid_out ? "i" : "o",
718 fft_size[0], fft_size[1], fft_size[2]);
720 (fft_gpu_3d_r2c_local((const double *)grid_in, (double *)grid_out, true,
721 fft_size, number_of_ffts, transpose_in,
722 transpose_out)),
723 (fft_fftw_3d_r2c_local(fft_size, number_of_ffts, transpose_in,
724 transpose_out, grid_in, grid_out)));
725 fft_stop_timer(handle2);
726 fft_stop_timer(handle);
727}
728
729/*******************************************************************************
730 * \brief Perform a local 3D C2R FFT.
731 * \param fft_size Size of the transform in each dimension.
732 * \param number_of_ffts Number of independent (batched) transforms.
733 * \param transpose_in Whether the input array uses the transposed layout.
734 * \param transpose_out Whether the output array uses the transposed layout.
735 * \param grid_in Input grid buffer.
736 * \param grid_out Output grid buffer.
737 * \author Frederick Stein
738 ******************************************************************************/
739void fft_3d_c2r_local(const int fft_size[3], const int number_of_ffts,
740 const bool transpose_in, const bool transpose_out,
741 double complex *grid_in, double *grid_out) {
742 const int handle = fft_timer_start("3d_c2r_local");
743 const int handle2 = fft_timer_start("3d_c2r_l_%s_%i_%i_%i",
744 (double *)grid_in == grid_out ? "i" : "o",
745 fft_size[0], fft_size[1], fft_size[2]);
747 (fft_gpu_3d_r2c_local((const double *)grid_in, (double *)grid_out, false,
748 fft_size, number_of_ffts, transpose_in,
749 transpose_out)),
750 (fft_fftw_3d_c2r_local(fft_size, number_of_ffts, transpose_in,
751 transpose_out, grid_in, grid_out)));
752 fft_stop_timer(handle2);
753 fft_stop_timer(handle);
754}
755
756// EOF
#define cp_assert(condition)
Definition cp_assert.h:22
static int imax(int x, int y)
Returns the larger of two given integers (missing from the C standard).
void fft_gpu_3d_r2c_local(const double *zin, double *zout, const bool dir, const int *npts, const int number_of_ffts, const bool transpose_in, const bool transpose_out)
Performs a 3D-R2C/C2R-FFT, on the GPU.
Definition fft_gpu.c:1239
void fft_gpu_free_double(double *buffer)
Allocate buffer of type double.
Definition fft_gpu.c:218
void fft_gpu_register_3d_local(const bool dir, const int *npts, const bool inplace, const int number_of_ffts, const bool transpose_in, const bool transpose_out)
Performs a (double precision complex) 3D-FFT on the GPU.
Definition fft_gpu.c:864
void fft_gpu_allocate_double(const int length, double **buffer)
Allocate buffer of type double.
Definition fft_gpu.c:185
void fft_gpu_allocate_complex(const int length, double complex **buffer)
Allocate buffer of type double complex.
Definition fft_gpu.c:202
void fft_gpu_register_2d_local(const bool dir, const int n[2], const int m, const bool transpose_in, const bool transpose_out, const bool inplace)
Performs a (double precision complex) 2D-FFT on the GPU.
Definition fft_gpu.c:1012
void fft_gpu_2d_local(const double *zin, double *zout, const bool dir, const int n[2], const int m, const bool transpose_in, const bool transpose_out)
Performs a (double precision complex) 2D-FFT on the GPU.
Definition fft_gpu.c:1582
void fft_gpu_init(void)
Initializes the fft_gpu library.
Definition fft_gpu.c:90
void fft_gpu_2d_r2c_local(const double *zin, double *zout, const bool dir, const int n[2], const int m, const bool transpose_in, const bool transpose_out)
Performs a (double precision complex) R2C/C2R2D-FFT on the GPU.
Definition fft_gpu.c:1630
void fft_gpu_register_3d_r2c_local(const bool dir, const int *npts, const bool inplace, const int number_of_ffts, const bool transpose_in, const bool transpose_out)
Performs a 3D-R2C/C2R-FFT, on the GPU.
Definition fft_gpu.c:897
void fft_gpu_3d_local(const double *zin, double *zout, const bool dir, const int *npts, const int number_of_ffts, const bool transpose_in, const bool transpose_out)
Performs a (double precision complex) 3D-FFT on the GPU.
Definition fft_gpu.c:1190
void fft_gpu_1d_r2c_local(const double *zin, double *zout, const bool dir, const int n, const int m, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out)
Performs a (double precision complex) 1D-FFT on the GPU.
Definition fft_gpu.c:1522
void fft_gpu_finalize(void)
Releases resources held by the fft_gpu library.
Definition fft_gpu.c:119
void fft_gpu_register_2d_r2c_local(const bool dir, const int n[2], const int m, const bool transpose_in, const bool transpose_out, const bool inplace)
Performs a (double precision complex) R2C/C2R2D-FFT on the GPU.
Definition fft_gpu.c:1045
void fft_gpu_register_1d_r2c_local(const bool dir, const int n, const int m, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, const bool inplace)
Performs a (double precision complex) 1D-FFT on the GPU.
Definition fft_gpu.c:973
void fft_gpu_free_complex(double complex *buffer)
Allocate buffer of type double complex.
Definition fft_gpu.c:233
void fft_gpu_1d_local(const double *zin, double *zout, const bool dir, const int n, const int m, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out)
Performs a (double precision complex) 1D-FFT on the GPU.
Definition fft_gpu.c:1457
void fft_gpu_register_1d_local(const bool dir, const int n, const int m, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, const bool inplace)
Performs a (double precision complex) 1D-FFT on the GPU.
Definition fft_gpu.c:933
int fft_lib_default_library()
Get the default library (GPU if offloading was enabled, else FFTW3).
Definition fft_lib.c:144
#define FFT_DISPATCH_VOID(gpu_call, fftw_call)
Forward a void FFT call to the selected backend.
Definition fft_lib.c:49
void fft_3d_r2c_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Perform a local 3D R2C FFT.
Definition fft_lib.c:712
void fft_2d_r2c_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Perform a local 2D R2C FFT.
Definition fft_lib.c:630
void fft_1d_local(const bool dir, const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double complex *grid_out)
Perform a local 1D FFT.
Definition fft_lib.c:506
void fft_finalize_acc_lib()
Finalize the accelerated FFT library.
Definition fft_lib.c:123
static int fft_timer_start(const char *format,...)
Start an FFT timer from a printf-style label.
Definition fft_lib.c:37
int fft_init_acc_lib()
Initialize the accelerated FFT library.
Definition fft_lib.c:67
void fft_1d_r2c_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double *grid_in, double complex *grid_out)
Perform a local 1D R2C FFT.
Definition fft_lib.c:538
void fft_register_1d_r2c_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double *grid_in, double complex *grid_out)
Register a local 1D R2C FFT.
Definition fft_lib.c:266
fft_lib fft_lib_choice
Definition fft_lib.c:31
void fft_2d_local(const bool dir, const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Perform a local 2D FFT.
Definition fft_lib.c:603
void fft_finalize_lib(const char *wisdom_file)
Finalize the FFT library (if not done externally).
Definition fft_lib.c:134
void fft_register_2d_r2c_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Register a local 2D R2C FFT.
Definition fft_lib.c:361
void fft_register_2d_c2r_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Register a local 2D C2R FFT.
Definition fft_lib.c:389
void fft_free_complex(double complex *buffer)
Allocate buffer of type double complex.
Definition fft_lib.c:210
bool fft_lib_has_guru_interface()
Whether a compound MPI implementation is available.
Definition fft_lib.c:156
int fft_init_lib(const fft_lib lib, const int fftw_planning_flag, const bool use_guru_interface, const char *wisdom_file)
Initialize the FFT library (if not done externally).
Definition fft_lib.c:84
void fft_register_1d_local(const bool dir, const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double complex *grid_out)
Register a local 1D C2C FFT.
Definition fft_lib.c:233
int fft_lib_backend_in_use()
Whether a compound MPI implementation is available.
Definition fft_lib.c:150
void fft_2d_c2r_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Perform a local 2D C2R FFT.
Definition fft_lib.c:657
void fft_free_double(double *buffer)
Allocate buffer of type double.
Definition fft_lib.c:195
void fft_3d_c2r_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Perform a local 3D C2R FFT.
Definition fft_lib.c:739
void fft_allocate_double(const int length, double **buffer)
Allocate buffer of type double.
Definition fft_lib.c:174
void fft_register_3d_local(const bool dir, const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Register a local 3D FFT.
Definition fft_lib.c:420
void fft_3d_local(const bool dir, const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Perform a local 3D FFT.
Definition fft_lib.c:685
void fft_register_2d_local(const bool dir, const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Register a local 2D FFT.
Definition fft_lib.c:333
const int fft_lib_default
Definition fft_lib.c:29
void fft_1d_c2r_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double *grid_out)
Perform a local 1D C2R FFT.
Definition fft_lib.c:571
void fft_register_1d_c2r_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double *grid_out)
Register a local 1D C2R FFT.
Definition fft_lib.c:300
void fft_register_3d_c2r_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Register a local 3D C2R FFT.
Definition fft_lib.c:475
void fft_register_3d_r2c_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Register a local 3D R2C FFT.
Definition fft_lib.c:447
bool fft_lib_initialized
Definition fft_lib.c:32
void fft_allocate_complex(const int length, double complex **buffer)
Allocate buffer of type double complex.
Definition fft_lib.c:185
fft_lib
Definition fft_lib.h:16
@ FFT_LIB_FFTW
Definition fft_lib.h:16
@ FFT_LIB_GPU
Definition fft_lib.h:16
@ FFT_LIB_DEFAULT
Definition fft_lib.h:16
bool fft_fftw_lib_has_guru_interface()
Whether a distributed FFT implementation is available.
void fft_fftw_3d_r2c_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C 3D FFT.
void fft_fftw_3d_local(const bool direction, const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 3D FFT.
void fft_fftw_1d_c2r_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double *grid_out)
Performs a local backwards C2R 1D FFT.
void fft_fftw_1d_local(const bool direction, const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double complex *grid_out)
Performs a local forward C2C 1D FFT.
void fft_fftw_register_2d_c2r_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Performs a local backwards C2R 2D FFT.
void fft_fftw_register_3d_c2r_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Performs a local backwards R2C 3D FFT.
void fft_fftw_free_double(double *buffer)
Free buffer of type double.
void fft_fftw_2d_r2c_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C 2D FFT.
void fft_fftw_register_2d_r2c_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C 2D FFT.
int fft_fftw_init_lib(const fftw_plan_type fftw_planning_flag, const bool use_guru_interface, const char *wisdom_file)
Initialize the FFT library (if not done externally).
void fft_fftw_register_3d_r2c_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C 3D FFT.
void fft_fftw_register_2d_local(const bool direction, const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 2D FFT.
void fft_fftw_allocate_complex(const int length, double complex **buffer)
Allocate buffer of type double complex.
void fft_fftw_2d_c2r_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Performs a local backwards C2R 2D FFT.
void fft_fftw_free_complex(double complex *buffer)
Free buffer of type double complex.
void fft_fftw_register_1d_local(const bool direction, const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 1D FFT.
void fft_fftw_finalize_lib(const char *wisdom_file)
Finalize the FFT library (if not done externally).
void fft_fftw_3d_c2r_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Performs a local backwards R2C 3D FFT.
void fft_fftw_1d_r2c_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C FFT.
void fft_fftw_register_1d_c2r_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double *grid_out)
Performs a local backwards C2R 1D FFT.
void fft_fftw_register_1d_r2c_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C FFT.
void fft_fftw_register_3d_local(const bool direction, const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 3D FFT.
void fft_fftw_2d_local(const bool direction, const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 2D FFT.
void fft_fftw_allocate_double(const int length, double **buffer)
Allocate buffer of type double.
void fft_stop_timer(const int handle)
Stop a timing.
Definition fft_timer.c:402
void fft_init_timer()
Initializes the internal timer.
Definition fft_timer.c:199
int fft_start_timer(const char *routine_name)
Start a timing section.
Definition fft_timer.c:386
#define FFT_MAX_STRING_LENGTH
Definition fft_timer.h:12