(git:e70887f)
Loading...
Searching...
No Matches
fft_lib_fftw.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_fftw.h"
9#include "fft_key.h"
10#include "fft_timer.h"
11#include "fft_utils.h"
12
13#include "../base/cp_assert.h"
14#include <math.h>
15#include <omp.h>
16#include <stdbool.h>
17#include <stddef.h>
18#include <stdlib.h>
19#include <string.h>
20
21#if defined(__FFTW3)
22#include <fftw3.h>
23
24/*******************************************************************************
25 * \brief Static variables for retaining objects that are expensive to create.
26 * \author Ole Schuett, Frederick Stein
27 ******************************************************************************/
28typedef struct {
29 // The key contains
30 // 0: rank (see below), R2C, direction
31 // 1: associated Fortran communicator handle (to store it as an integer)
32 // 2: Number of associated OpenMP threads
33 // 3, 4, 5: FFT sizes (or FFT sizes and number of FFTs)
34 // 6, 7, 8: input stride
35 // 0, 10, 11: output strides
36 fft_key_t key;
37 fftw_plan *plan;
38} cache_entry;
39
40// We need to reserve more space because of the different combinations
41// (local/distributed, C2C/R2C) and potential overlap from OpenMP. This works to
42// run all tests.
43#define FFTW_CACHE_SIZE 256
44
45// A single FFTW plan cannot be executed concurrently by several OpenMP threads.
46// Keep one independent plan cache per thread so that every worker reuses only
47// its own plans. The caches are allocated once at initialization.
48static cache_entry *caches = NULL;
49static int *cache_oldest_entries = NULL;
50static int number_of_caches = 0;
51
52static bool is_initialized = false;
53
54static omp_nest_lock_t cache_lock;
55
56static int fftw_planning_mode = -1;
57
58/*******************************************************************************
59 * \brief Index of the cache that belongs to the calling thread.
60 * \return cache index, clamped to a valid range
61 * \author Frederick Stein
62 ******************************************************************************/
63static inline int current_cache_id(void) {
64 int thread_id = omp_get_thread_num();
65 if (thread_id < 0 || thread_id >= number_of_caches)
66 thread_id = 0;
67 return thread_id;
68}
69
70/*******************************************************************************
71 * \brief Cache of the calling thread.
72 * \return pointer to the thread-local cache entries
73 * \author Frederick Stein
74 ******************************************************************************/
75static inline cache_entry *current_cache(void) {
76 return &caches[current_cache_id() * FFTW_CACHE_SIZE];
77}
78
79/*******************************************************************************
80 * \brief FIFO position of the calling thread's cache.
81 * \return pointer to the thread-local FIFO index
82 * \author Frederick Stein
83 ******************************************************************************/
84static inline int *current_cache_oldest_entry(void) {
85 return &cache_oldest_entries[current_cache_id()];
86}
87
88/*******************************************************************************
89 * \brief Fetches an fft plan from the cache. Returns NULL if not found.
90 * \param key Encoded FFT key.
91 * \author Ole Schuett, Frederick Stein
92 ******************************************************************************/
93static fftw_plan *lookup_plan_from_cache(const fft_key_t key) {
94 cp_assert(is_initialized);
95 omp_set_nest_lock(&cache_lock);
96 cache_entry *cache = current_cache();
97 fftw_plan *plan = NULL;
98 for (int i = 0; i < FFTW_CACHE_SIZE; i++) {
99 const int *x = cache[i].key;
100 if (memcmp(key, x, sizeof(fft_key_t)) == 0) {
101 plan = cache[i].plan;
102 break;
103 }
104 }
105 omp_unset_nest_lock(&cache_lock);
106 return plan;
107}
108
109/*******************************************************************************
110 * \brief Adds an fft plan to the cache. Assumes ownership of plan's memory.
111 * \param key Encoded FFT key.
112 * \param plan FFT plan handle.
113 * \author Ole Schuett, Frederick Stein
114 ******************************************************************************/
115static void add_plan_to_cache(const fft_key_t key, fftw_plan *plan) {
116 omp_set_nest_lock(&cache_lock);
117 cache_entry *cache = current_cache();
118 int *cache_oldest_entry = current_cache_oldest_entry();
119 const int i = *cache_oldest_entry;
120 *cache_oldest_entry = (*cache_oldest_entry + 1) % FFTW_CACHE_SIZE;
121 if (cache[i].plan != NULL) {
122 fftw_destroy_plan(*cache[i].plan);
123 free(cache[i].plan);
124 }
125 memcpy(cache[i].key, key, sizeof(fft_key_t));
126 cache[i].plan = plan;
127 omp_unset_nest_lock(&cache_lock);
128}
129#endif
130
131static bool has_guru_interface = false;
132
133#if defined(__FFTW3)
134/*******************************************************************************
135 * \brief Check whether the FFTW guru interface is available.
136 * \return True when a minimal guru plan can be created.
137 * \author Frederick Stein
138 ******************************************************************************/
139bool is_guru_interface_available() {
140 fftw_iodim dims[1];
141 dims[0].n = 1;
142 dims[0].is = 1;
143 dims[0].os = 1;
144 fftw_iodim howmany_dims[2];
145 howmany_dims[0].n = 1;
146 howmany_dims[0].is = 1;
147 howmany_dims[0].os = 1;
148 howmany_dims[1].n = 1;
149 howmany_dims[1].is = 1;
150 howmany_dims[1].os = 1;
151 double complex buffer1;
152 fftw_plan plan =
153 fftw_plan_guru_dft(1, dims, 2, howmany_dims, &buffer1, &buffer1,
154 FFTW_FORWARD, fftw_planning_mode);
155
156 if (plan != NULL) {
157 fftw_destroy_plan(plan);
158 return true;
159 } else {
160 return false;
161 }
162}
163#endif
164
165#if defined(__FFTW3)
166/*******************************************************************************
167 * \brief Decide whether a 3D FFT should be split into threaded 1D batches.
168 * \param fft_size Size of the 3D transform.
169 * \param transpose_in Whether the input layout is transposed.
170 * \param transpose_out Whether the output layout is transposed.
171 * \param in_place Whether the transform is in-place.
172 * \return True when the threaded batched implementation should be used.
173 * \author Frederick Stein
174 ******************************************************************************/
175static bool use_threaded_3d(const int fft_size[3], const bool transpose_in,
176 const bool transpose_out, const bool in_place) {
177 return !transpose_in && !transpose_out && has_guru_interface &&
178 ((((fft_size[0] >= 256 || fft_size[1] >= 256 || fft_size[2] >= 256) &&
179 !in_place) ||
180 ((fft_size[0] >= 128 || fft_size[1] >= 128 || fft_size[2] >= 128) &&
181 in_place)) &&
182 fft_team_size() > 1) &&
183 (fftw_planning_mode == FFTW_ESTIMATE);
184}
185#endif
186
187#if defined(__FFTW3)
188fftw_plan *fft_fftw_create_3d_plan(const fft_key_t key, double complex *grid_in,
189 double complex *grid_out);
190fftw_plan *fft_fftw_create_3d_plan_r2c(const fft_key_t key, double *grid_in,
191 double *grid_out);
192fftw_plan *fft_fftw_create_guru_plan(const fft_key_t key,
193 double complex *grid_in,
194 double complex *grid_out);
195fftw_plan *fft_fftw_create_guru_plan_r2c(const fft_key_t key, double *grid_in,
196 double *grid_out);
197
198/*******************************************************************************
199 * \brief Register FFTW plans for a threaded batched complex 3D FFT.
200 * \param direction FFT direction.
201 * \param fft_size Size of the 3D transform.
202 * \param number_of_ffts Number of batched transforms.
203 * \param in_place Whether the transform is in-place.
204 * \param grid_in Input complex grid buffer.
205 * \param grid_out Output complex grid buffer.
206 * \author Frederick Stein
207 ******************************************************************************/
208static void
209register_threaded_3d_c2c(const bool direction, const int fft_size[3],
210 const int number_of_ffts, const bool in_place,
211 double complex *grid_in, double complex *grid_out) {
212 const int number_of_threads = fft_team_size();
213 const int block_size =
214 (number_of_ffts + number_of_threads - 1) / number_of_threads;
215 const int number_of_full_blocks = number_of_ffts / block_size;
216 fft_key_t key;
217 get_key_3d(direction, fft_size, block_size, false, false, 1, in_place, key);
218 if (lookup_plan_from_cache(key) == NULL)
219 fft_fftw_create_3d_plan(key, grid_in, grid_out);
220 if (block_size * number_of_full_blocks != number_of_ffts) {
221 const int block_size_last_thread = number_of_ffts % block_size;
222 fft_key_t key_last;
223 get_key_3d(direction, fft_size, block_size_last_thread, false, false, 1,
224 in_place, key_last);
225 if (lookup_plan_from_cache(key_last) == NULL)
226 fft_fftw_create_3d_plan(key_last, grid_in, grid_out);
227 }
228}
229
230/*******************************************************************************
231 * \brief Register FFTW plans for a threaded batched real-to-complex 3D FFT.
232 * \param direction FFT direction.
233 * \param fft_size Size of the 3D transform.
234 * \param number_of_ffts Number of batched transforms.
235 * \param in_place Whether the transform is in-place.
236 * \param grid_in Input real grid buffer.
237 * \param grid_out Output complex grid buffer.
238 * \author Frederick Stein
239 ******************************************************************************/
240static void register_threaded_3d_r2c(const bool direction,
241 const int fft_size[3],
242 const int number_of_ffts,
243 const bool in_place, double *grid_in,
244 double *grid_out) {
245 const int number_of_threads = fft_team_size();
246 const int block_size =
247 (number_of_ffts + number_of_threads - 1) / number_of_threads;
248 const int number_of_full_blocks = number_of_ffts / block_size;
249 fft_key_t key;
250 get_key_3d_r2c(direction, fft_size, block_size, false, false, 1, in_place,
251 key);
252 if (lookup_plan_from_cache(key) == NULL)
253 fft_fftw_create_3d_plan_r2c(key, grid_in, grid_out);
254 if (block_size * number_of_full_blocks != number_of_ffts) {
255 const int block_size_last_thread = number_of_ffts % block_size;
256 fft_key_t key_last;
257 get_key_3d_r2c(direction, fft_size, block_size_last_thread, false, false, 1,
258 in_place, key_last);
259 if (lookup_plan_from_cache(key_last) == NULL)
260 fft_fftw_create_3d_plan_r2c(key_last, grid_in, grid_out);
261 }
262}
263
264/*******************************************************************************
265 * \brief Execute a threaded batched complex-to-complex 3D FFT.
266 * \param direction FFT direction.
267 * \param fft_size Size of the 3D transform.
268 * \param number_of_ffts Number of batched transforms.
269 * \param in_place Whether the transform is in-place.
270 * \param grid_in Input complex grid buffer.
271 * \param grid_out Output complex grid buffer.
272 * \author Frederick Stein
273 ******************************************************************************/
274static void execute_threaded_3d_c2c(const bool direction, const int fft_size[3],
275 const int number_of_ffts,
276 const bool in_place,
277 double complex *grid_in,
278 double complex *grid_out) {
279 const int number_of_threads = fft_team_size();
280 const int block_size =
281 (number_of_ffts + number_of_threads - 1) / number_of_threads;
282 const int number_of_full_blocks = number_of_ffts / block_size;
283 const int total_number_of_blocks =
284 number_of_full_blocks + (number_of_ffts % block_size != 0);
285 const int volume = fft_size[0] * fft_size[1] * fft_size[2];
286#pragma omp parallel default(none) num_threads(number_of_threads) shared( \
287 block_size, number_of_full_blocks, total_number_of_blocks, direction, \
288 fft_size, grid_in, grid_out, in_place, number_of_ffts, volume)
289 {
290 const int thread_id = omp_get_thread_num();
291 fft_key_t key;
292 if (thread_id < number_of_full_blocks) {
293 get_key_3d(direction, fft_size, block_size, false, false, 1, in_place,
294 key);
295 fftw_plan *plan = lookup_plan_from_cache(key);
296 if (plan == NULL)
297 plan = fft_fftw_create_3d_plan(key, grid_in, grid_out);
298 const int offset = thread_id * block_size * volume;
299 fftw_execute_dft(*plan, grid_in + offset, grid_out + offset);
300 } else if (thread_id + 1 == total_number_of_blocks) {
301 const int block_size_last_thread = number_of_ffts % block_size;
302 get_key_3d(direction, fft_size, block_size_last_thread, false, false, 1,
303 in_place, key);
304 fftw_plan *plan = lookup_plan_from_cache(key);
305 if (plan == NULL)
306 plan = fft_fftw_create_3d_plan(key, grid_in, grid_out);
307 const int offset = number_of_full_blocks * block_size * volume;
308 fftw_execute_dft(*plan, grid_in + offset, grid_out + offset);
309 }
310 }
311}
312
313/*******************************************************************************
314 * \brief Execute a threaded batched real-to-complex 3D FFT.
315 * \param direction FFT direction.
316 * \param fft_size Size of the 3D transform.
317 * \param number_of_ffts Number of batched transforms.
318 * \param in_place Whether the transform is in-place.
319 * \param grid_in Input real grid buffer.
320 * \param grid_out Output complex grid buffer.
321 * \author Frederick Stein
322 ******************************************************************************/
323static void execute_threaded_3d_r2c(const bool direction, const int fft_size[3],
324 const int number_of_ffts,
325 const bool in_place, double *grid_in,
326 double *grid_out) {
327 const int number_of_threads = fft_team_size();
328 const int block_size =
329 (number_of_ffts + number_of_threads - 1) / number_of_threads;
330 const int number_of_full_blocks = number_of_ffts / block_size;
331 const int total_number_of_blocks =
332 number_of_full_blocks + (number_of_ffts % block_size != 0);
333 const int real_volume = fft_size[0] * fft_size[1] * fft_size[2];
334 const int complex_volume = fft_size[0] * fft_size[1] * (fft_size[2] / 2 + 1);
335 const int real_offset = in_place ? 2 * complex_volume : real_volume;
336#pragma omp parallel default(none) num_threads(number_of_threads) \
337 shared(block_size, number_of_full_blocks, total_number_of_blocks, \
338 direction, fft_size, grid_in, grid_out, in_place, \
339 number_of_ffts, complex_volume, real_offset)
340 {
341 const int thread_id = omp_get_thread_num();
342 fft_key_t key;
343 if (thread_id < number_of_full_blocks) {
344 get_key_3d_r2c(direction, fft_size, block_size, false, false, 1, in_place,
345 key);
346 fftw_plan *plan = lookup_plan_from_cache(key);
347 if (plan == NULL)
348 plan = fft_fftw_create_3d_plan_r2c(key, grid_in, grid_out);
349 const int start = thread_id * block_size;
350 if (direction) {
351 fftw_execute_dft_r2c(*plan, grid_in + start * real_offset,
352 (double complex *)grid_out +
353 start * complex_volume);
354 } else {
355 fftw_execute_dft_c2r(*plan,
356 (double complex *)grid_in + start * complex_volume,
357 grid_out + start * real_offset);
358 }
359 } else if (thread_id + 1 == total_number_of_blocks) {
360 const int block_size_last_thread = number_of_ffts % block_size;
361 get_key_3d_r2c(direction, fft_size, block_size_last_thread, false, false,
362 1, in_place, key);
363 fftw_plan *plan = lookup_plan_from_cache(key);
364 if (plan == NULL)
365 plan = fft_fftw_create_3d_plan_r2c(key, grid_in, grid_out);
366 const int start = number_of_full_blocks * block_size;
367 if (direction) {
368 fftw_execute_dft_r2c(*plan, grid_in + start * real_offset,
369 (double complex *)grid_out +
370 start * complex_volume);
371 } else {
372 fftw_execute_dft_c2r(*plan,
373 (double complex *)grid_in + start * complex_volume,
374 grid_out + start * real_offset);
375 }
376 }
377 }
378}
379
380/*******************************************************************************
381 * \brief Register the thread-split 1D chain for a single real/complex 3D FFT.
382 ******************************************************************************/
383static void register_threaded_3d_r2c_1d(const bool direction,
384 const int fft_size[3],
385 const bool in_place, double *grid_real,
386 double complex *grid_complex) {
387 double complex *buffer = in_place
388 ? grid_complex
389 : fftw_alloc_complex(fft_size[0] * fft_size[1] *
390 (fft_size[2] / 2 + 1));
391 double complex *input_c = direction ? buffer : grid_complex;
392 double complex *output_c = direction ? grid_complex : buffer;
393 const int number_of_threads = fft_team_size();
394 int block_sizes[3];
395 fftw_plan *plans[3], *plans_last_thread[3];
396 for (int stage = 0; stage < 3; stage++) {
397 plans[stage] = NULL;
398 plans_last_thread[stage] = NULL;
399 const int extent = stage == 0 ? fft_size[1] * (fft_size[2] / 2 + 1)
400 : stage == 1 ? fft_size[0]
401 : fft_size[0] * fft_size[1];
402 block_sizes[stage] = (extent + number_of_threads - 1) / number_of_threads;
403 const bool needs_tail = block_sizes[stage] * number_of_threads != extent;
404 const int tail = needs_tail ? extent % block_sizes[stage] : 0;
405 if (stage == 0) {
406 const int n = fft_size[1] * (fft_size[2] / 2 + 1);
407 fft_iodim dim = {.n = fft_size[0], .is = n, .os = n};
408 fft_iodim howmany = {.n = block_sizes[0], .is = 1, .os = 1};
409 fft_key_t key;
410 get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
411 plans[0] = lookup_plan_from_cache(key);
412 if (plans[0] == NULL)
413 plans[0] = fft_fftw_create_guru_plan(key, input_c, output_c);
414 if (needs_tail) {
415 howmany.n = tail;
416 get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
417 plans_last_thread[0] = lookup_plan_from_cache(key);
418 if (plans_last_thread[0] == NULL)
419 plans_last_thread[0] =
420 fft_fftw_create_guru_plan(key, input_c, output_c);
421 }
422 } else if (stage == 1) {
423 const int half = fft_size[2] / 2 + 1;
424 fft_iodim dim = {.n = fft_size[1], .is = half, .os = half};
425 fft_iodim howmany[2] = {{.n = block_sizes[1],
426 .is = fft_size[1] * half,
427 .os = fft_size[1] * half},
428 {.n = half, .is = 1, .os = 1}};
429 fft_key_t key;
430 get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
431 plans[1] = lookup_plan_from_cache(key);
432 if (plans[1] == NULL)
433 plans[1] = fft_fftw_create_guru_plan(key, input_c, output_c);
434 if (needs_tail) {
435 howmany[0].n = tail;
436 get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
437 plans_last_thread[1] = lookup_plan_from_cache(key);
438 if (plans_last_thread[1] == NULL)
439 plans_last_thread[1] =
440 fft_fftw_create_guru_plan(key, input_c, output_c);
441 }
442 } else {
443 const int half = fft_size[2] / 2 + 1;
444 fft_iodim dim = {.n = fft_size[2], .is = 1, .os = 1};
445 fft_iodim howmany = {
446 .n = block_sizes[2],
447 .is = direction ? (in_place ? 2 * half : fft_size[2]) : half,
448 .os = direction ? half : (in_place ? 2 * half : fft_size[2])};
449 fft_key_t key;
450 get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
451 plans[2] = lookup_plan_from_cache(key);
452 if (plans[2] == NULL)
453 plans[2] = fft_fftw_create_guru_plan_r2c(key, grid_real,
454 (double *)grid_complex);
455 if (needs_tail) {
456 howmany.n = tail;
457 get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
458 plans_last_thread[2] = lookup_plan_from_cache(key);
459 if (plans_last_thread[2] == NULL)
460 plans_last_thread[2] = fft_fftw_create_guru_plan_r2c(
461 key, grid_real, (double *)grid_complex);
462 }
463 }
464 }
465 if (!in_place)
466 fftw_free(buffer);
467}
468
469/*******************************************************************************
470 * \brief Execute the thread-split 1D chain for a single real/complex 3D FFT.
471 ******************************************************************************/
472static void execute_threaded_3d_r2c_1d(const bool direction,
473 const int fft_size[3],
474 const bool in_place, double *grid_real,
475 double complex *grid_complex) {
476 const int half = fft_size[2] / 2 + 1;
477 double complex *buffer =
478 direction && in_place
479 ? grid_complex
480 : fftw_alloc_complex(fft_size[0] * fft_size[1] * half);
481 double complex *input_c = direction ? buffer : grid_complex;
482 double complex *output_c = direction ? grid_complex : buffer;
483 const int number_of_threads = fft_team_size();
484 int block_sizes[3];
485 int number_of_full_blocks[3] = {0, 0, 0};
486 int total_number_of_blocks[3] = {0, 0, 0};
487 fftw_plan *plans[3], *plans_last_thread[3];
488 for (int stage = 0; stage < 3; stage++) {
489 plans[stage] = NULL;
490 plans_last_thread[stage] = NULL;
491 const int extent = stage == 0 ? fft_size[1] * half
492 : stage == 1 ? fft_size[0]
493 : fft_size[0] * fft_size[1];
494 block_sizes[stage] = (extent + number_of_threads - 1) / number_of_threads;
495 number_of_full_blocks[stage] = extent / block_sizes[stage];
496 total_number_of_blocks[stage] =
497 (extent + block_sizes[stage] - 1) / block_sizes[stage];
498 const bool needs_tail = block_sizes[stage] * number_of_threads != extent;
499 const int tail = needs_tail ? extent % block_sizes[stage] : 0;
500 if (stage == 0) {
501 const int n = fft_size[1] * half;
502 fft_iodim dim = {.n = fft_size[0], .is = n, .os = n};
503 fft_iodim howmany = {.n = block_sizes[0], .is = 1, .os = 1};
504 fft_key_t key;
505 get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
506 plans[0] = lookup_plan_from_cache(key);
507 if (plans[0] == NULL) {
508 double complex *tmp = fftw_alloc_complex(get_buffer_size_from_key(key));
509 plans[0] = fft_fftw_create_guru_plan(key, tmp, output_c);
510 fftw_free(tmp);
511 }
512 if (needs_tail) {
513 howmany.n = tail;
514 get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
515 plans_last_thread[0] = lookup_plan_from_cache(key);
516 if (plans_last_thread[0] == NULL) {
517 double complex *tmp =
518 fftw_alloc_complex(get_buffer_size_from_key(key));
519 plans_last_thread[0] = fft_fftw_create_guru_plan(key, tmp, output_c);
520 fftw_free(tmp);
521 }
522 }
523 } else if (stage == 1) {
524 fft_iodim dim = {.n = fft_size[1], .is = half, .os = half};
525 fft_iodim howmany[2] = {{.n = block_sizes[1],
526 .is = fft_size[1] * half,
527 .os = fft_size[1] * half},
528 {.n = half, .is = 1, .os = 1}};
529 fft_key_t key;
530 get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
531 plans[1] = lookup_plan_from_cache(key);
532 if (plans[1] == NULL) {
533 double complex *tmp = fftw_alloc_complex(get_buffer_size_from_key(key));
534 plans[1] = fft_fftw_create_guru_plan(key, tmp, output_c);
535 fftw_free(tmp);
536 }
537 if (needs_tail) {
538 howmany[0].n = tail;
539 get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
540 plans_last_thread[1] = lookup_plan_from_cache(key);
541 if (plans_last_thread[1] == NULL) {
542 double complex *tmp =
543 fftw_alloc_complex(get_buffer_size_from_key(key));
544 plans_last_thread[1] = fft_fftw_create_guru_plan(key, tmp, output_c);
545 fftw_free(tmp);
546 }
547 }
548 } else {
549 fft_iodim dim = {.n = fft_size[2], .is = 1, .os = 1};
550 fft_iodim howmany = {
551 .n = block_sizes[2],
552 .is = direction ? (in_place ? 2 * half : fft_size[2]) : half,
553 .os = direction ? half : (in_place ? 2 * half : fft_size[2])};
554 fft_key_t key;
555 get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
556 plans[2] = lookup_plan_from_cache(key);
557 if (plans[2] == NULL) {
558 double *tmp = fftw_alloc_real(2 * get_buffer_size_from_key(key));
559 plans[2] = fft_fftw_create_guru_plan_r2c(key, tmp, (double *)output_c);
560 fftw_free(tmp);
561 }
562 if (needs_tail) {
563 howmany.n = tail;
564 get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
565 plans_last_thread[2] = lookup_plan_from_cache(key);
566 if (plans_last_thread[2] == NULL) {
567 double *tmp = fftw_alloc_real(2 * get_buffer_size_from_key(key));
568 plans_last_thread[2] =
569 fft_fftw_create_guru_plan_r2c(key, tmp, (double *)output_c);
570 fftw_free(tmp);
571 }
572 }
573 }
574 }
575 const int real_stride =
576 direction ? (in_place ? 2 * half : fft_size[2]) : half;
577 const int complex_stride =
578 direction ? half : (in_place ? 2 * half : fft_size[2]);
579#pragma omp parallel default(none) \
580 shared(number_of_threads, number_of_full_blocks, total_number_of_blocks, \
581 plans, plans_last_thread, block_sizes, fft_size, grid_real, \
582 grid_complex, in_place, direction, input_c, output_c, \
583 real_stride, complex_stride)
584 {
585 const int thread_id = omp_get_thread_num();
586 if (thread_id < number_of_full_blocks[0]) {
587 fftw_execute_dft(*plans[0], input_c + block_sizes[0] * thread_id,
588 output_c + block_sizes[0] * thread_id);
589 } else if (thread_id == total_number_of_blocks[0] - 1) {
590 fftw_execute_dft(*plans_last_thread[0],
591 input_c + block_sizes[0] * thread_id,
592 output_c + block_sizes[0] * thread_id);
593 }
594#pragma omp barrier
595 if (thread_id < number_of_full_blocks[1]) {
596 const int offset =
597 block_sizes[1] * fft_size[1] * (fft_size[2] / 2 + 1) * thread_id;
598 fftw_execute_dft(*plans[1], output_c + offset, input_c + offset);
599 } else if (thread_id == total_number_of_blocks[1] - 1) {
600 const int offset =
601 block_sizes[1] * fft_size[1] * (fft_size[2] / 2 + 1) * thread_id;
602 fftw_execute_dft(*plans_last_thread[1], output_c + offset,
603 input_c + offset);
604 }
605#pragma omp barrier
606 if (thread_id < number_of_full_blocks[2]) {
607 const int start = block_sizes[2] * thread_id;
608 if (direction) {
609 fftw_execute_dft_r2c(*plans[2], grid_real + start * real_stride,
610 grid_complex + start * complex_stride);
611 } else {
612 fftw_execute_dft_c2r(*plans[2], grid_complex + start * complex_stride,
613 grid_real + start * real_stride);
614 }
615 } else if (thread_id == total_number_of_blocks[2] - 1) {
616 const int start = block_sizes[2] * thread_id;
617 if (direction) {
618 fftw_execute_dft_r2c(*plans_last_thread[2],
619 grid_real + start * real_stride,
620 grid_complex + start * complex_stride);
621 } else {
622 fftw_execute_dft_c2r(*plans_last_thread[2],
623 grid_complex + start * complex_stride,
624 grid_real + start * real_stride);
625 }
626 }
627 }
628 if (!(direction && in_place))
629 fftw_free(buffer);
630}
631
632/*******************************************************************************
633 * \brief Acquire the plans for a thread-split single C2C 3D FFT.
634 ******************************************************************************/
635static void acquire_threaded_3d_c2c_1d(
636 const bool direction, const int fft_size[3], const bool in_place,
637 const bool for_execution, double complex *grid_in, double complex *grid_out,
638 int block_sizes[3], fftw_plan *plans[3], fftw_plan *plans_last_thread[3],
639 int number_of_full_blocks[3], int total_number_of_blocks[3]) {
640 const int number_of_threads = fft_team_size();
641 for (int stage = 0; stage < 3; stage++) {
642 plans[stage] = NULL;
643 plans_last_thread[stage] = NULL;
644 const int extent = stage == 0 ? fft_size[1] * fft_size[2]
645 : stage == 1 ? fft_size[0]
646 : fft_size[0] * fft_size[1];
647 block_sizes[stage] = (extent + number_of_threads - 1) / number_of_threads;
648 number_of_full_blocks[stage] = extent / block_sizes[stage];
649 total_number_of_blocks[stage] =
650 (extent + block_sizes[stage] - 1) / block_sizes[stage];
651 const bool needs_tail = block_sizes[stage] * number_of_threads != extent;
652 const int tail = needs_tail ? extent % block_sizes[stage] : 0;
653
654 fft_iodim dim[1];
655 fft_iodim howmany[2];
656 int rank = 1;
657 int howmany_rank = 1;
658 if (stage == 0) {
659 const int n = fft_size[1] * fft_size[2];
660 dim[0] = (fft_iodim){.n = fft_size[0], .is = n, .os = n};
661 howmany[0] = (fft_iodim){.n = block_sizes[0], .is = 1, .os = 1};
662 } else if (stage == 1) {
663 dim[0] =
664 (fft_iodim){.n = fft_size[1], .is = fft_size[2], .os = fft_size[2]};
665 howmany[0] = (fft_iodim){.n = block_sizes[1],
666 .is = fft_size[1] * fft_size[2],
667 .os = fft_size[1] * fft_size[2]};
668 howmany[1] = (fft_iodim){.n = fft_size[2], .is = 1, .os = 1};
669 howmany_rank = 2;
670 } else {
671 dim[0] = (fft_iodim){.n = fft_size[2], .is = 1, .os = 1};
672 howmany[0] = (fft_iodim){
673 .n = block_sizes[2], .is = fft_size[2], .os = fft_size[2]};
674 }
675
676 fft_key_t key;
677 get_key_guru(direction, rank, dim, howmany_rank, howmany, 1, in_place, key);
678 plans[stage] = lookup_plan_from_cache(key);
679 if (plans[stage] == NULL) {
680 if (for_execution) {
681 double complex *buffer =
682 fftw_alloc_complex(get_buffer_size_from_key(key));
683 plans[stage] = fft_fftw_create_guru_plan(key, buffer,
684 in_place ? buffer : grid_out);
685 fftw_free(buffer);
686 } else {
687 plans[stage] = fft_fftw_create_guru_plan(key, grid_in, grid_out);
688 }
689 }
690 if (needs_tail) {
691 howmany[0].n = tail;
692 get_key_guru(direction, rank, dim, howmany_rank, howmany, 1, in_place,
693 key);
694 plans_last_thread[stage] = lookup_plan_from_cache(key);
695 if (plans_last_thread[stage] == NULL) {
696 if (for_execution) {
697 double complex *buffer =
698 fftw_alloc_complex(get_buffer_size_from_key(key));
699 plans_last_thread[stage] = fft_fftw_create_guru_plan(
700 key, buffer, in_place ? buffer : grid_out);
701 fftw_free(buffer);
702 } else {
703 plans_last_thread[stage] =
704 fft_fftw_create_guru_plan(key, grid_in, grid_out);
705 }
706 }
707 }
708 }
709}
710
711/*******************************************************************************
712 * \brief Register the thread-split 1D chain for a single C2C 3D FFT.
713 ******************************************************************************/
714static void register_threaded_3d_c2c_1d(const bool direction,
715 const int fft_size[3],
716 const bool in_place,
717 double complex *grid_in,
718 double complex *grid_out) {
719 int block_sizes[3];
720 int number_of_full_blocks[3];
721 int total_number_of_blocks[3];
722 fftw_plan *plans[3], *plans_last_thread[3];
723 acquire_threaded_3d_c2c_1d(direction, fft_size, in_place, false, grid_in,
724 grid_out, block_sizes, plans, plans_last_thread,
725 number_of_full_blocks, total_number_of_blocks);
726}
727
728/*******************************************************************************
729 * \brief Execute the thread-split 1D chain for a single C2C 3D FFT.
730 ******************************************************************************/
731static void execute_threaded_3d_c2c_1d(const bool direction,
732 const int fft_size[3],
733 const bool in_place,
734 double complex *grid_in,
735 double complex *grid_out) {
736 int block_sizes[3];
737 int number_of_full_blocks[3];
738 int total_number_of_blocks[3];
739 fftw_plan *plans[3], *plans_last_thread[3];
740 acquire_threaded_3d_c2c_1d(direction, fft_size, in_place, true, grid_in,
741 grid_out, block_sizes, plans, plans_last_thread,
742 number_of_full_blocks, total_number_of_blocks);
743#pragma omp parallel default(none) \
744 shared(number_of_full_blocks, total_number_of_blocks, plans, \
745 plans_last_thread, block_sizes, fft_size, grid_in, grid_out)
746 {
747 const int thread_id = omp_get_thread_num();
748 if (thread_id < number_of_full_blocks[2]) {
749 fftw_execute_dft(*plans[2],
750 grid_in + block_sizes[2] * fft_size[2] * thread_id,
751 grid_out + block_sizes[2] * fft_size[2] * thread_id);
752 } else if (thread_id == total_number_of_blocks[2] - 1) {
753 fftw_execute_dft(*plans_last_thread[2],
754 grid_in + block_sizes[2] * fft_size[2] * thread_id,
755 grid_out + block_sizes[2] * fft_size[2] * thread_id);
756 }
757#pragma omp barrier
758 if (thread_id < number_of_full_blocks[1]) {
759 const int offset = block_sizes[1] * fft_size[1] * fft_size[2] * thread_id;
760 fftw_execute_dft(*plans[1], grid_out + offset, grid_in + offset);
761 } else if (thread_id == total_number_of_blocks[1] - 1) {
762 const int offset = block_sizes[1] * fft_size[1] * fft_size[2] * thread_id;
763 fftw_execute_dft(*plans_last_thread[1], grid_out + offset,
764 grid_in + offset);
765 }
766#pragma omp barrier
767 if (thread_id < number_of_full_blocks[0]) {
768 fftw_execute_dft(*plans[0], grid_in + block_sizes[0] * thread_id,
769 grid_out + block_sizes[0] * thread_id);
770 } else if (thread_id == total_number_of_blocks[0] - 1) {
771 fftw_execute_dft(*plans_last_thread[0],
772 grid_in + block_sizes[0] * thread_id,
773 grid_out + block_sizes[0] * thread_id);
774 }
775 }
776}
777
778#endif
779
780/*******************************************************************************
781 * \\brief Initialize the FFT library (if not done externally).
782 * \param fftw_planning_flag FFTW planning mode.
783 * \param use_guru_interface Whether the FFTW Guru interface may be used.
784 * \param wisdom_file Path to the FFTW wisdom file.
785 * \author Frederick Stein
786 ******************************************************************************/
787int fft_fftw_init_lib(const fftw_plan_type fftw_planning_flag,
788 const bool use_guru_interface, const char *wisdom_file) {
789#if defined(__FFTW3)
790 cp_assert(omp_get_num_threads() == 1);
791 if (is_initialized) {
792 return 0;
793 }
794 number_of_caches =
795 imax(1, omp_get_max_threads()); // NOLINT: per-thread caches
796 caches =
797 calloc((size_t)number_of_caches * FFTW_CACHE_SIZE, sizeof(cache_entry));
798 cache_oldest_entries = calloc((size_t)number_of_caches, sizeof(int));
799 cp_assert(caches != NULL && cache_oldest_entries != NULL);
800 omp_init_nest_lock(&cache_lock);
801
802 fftw_planning_mode = fftw_planning_flag;
803 switch (fftw_planning_flag) {
804 case FFT_ESTIMATE:
805 fftw_planning_mode = FFTW_ESTIMATE;
806 break;
807 case FFT_MEASURE:
808 fftw_planning_mode = FFTW_MEASURE;
809 break;
810 case FFT_PATIENT:
811 fftw_planning_mode = FFTW_PATIENT;
812 break;
813 case FFT_EXHAUSTIVE:
814 fftw_planning_mode = FFTW_EXHAUSTIVE;
815 break;
816 default:
817 cp_assert(0 && "Unknown FFTW planning flag.");
818 }
819
820 // We need a threaded library!
821 const int status = fftw_init_threads();
822 if (status == 0)
823 return 1;
824
825 has_guru_interface = use_guru_interface;
826 if (has_guru_interface) {
827 has_guru_interface = is_guru_interface_available();
828 }
829
830 // Export wisdom after intializing the library to ensure correct threading
831 // etc.
832 if (wisdom_file != NULL) {
833 const bool is_print_rank = cp_mpi_comm_rank(cp_mpi_get_comm_world()) == 0;
834 const int error = fftw_import_wisdom_from_filename(wisdom_file);
835 if (false && error != 0 && is_print_rank)
836 fprintf(stderr,
837 "Importing wisdom failed! Maybe the file does not exist.");
838 }
839
840 is_initialized = true;
841 return 0;
842#else
843 cp_assert(false && "The grid library was not compiled with FFTW support.");
844 (void)fftw_planning_flag;
845 (void)use_guru_interface;
846 (void)wisdom_file;
847 return 0;
848#endif
849}
850
851/*******************************************************************************
852 * \brief Finalize the FFT library (if not done externally).
853 * \param wisdom_file Path to the FFTW wisdom file.
854 * \author Frederick Stein
855 ******************************************************************************/
856void fft_fftw_finalize_lib(const char *wisdom_file) {
857#if defined(__FFTW3)
858 cp_assert(omp_get_num_threads() == 1);
859 if (!is_initialized) {
860 return;
861 }
862 for (int t = 0; t < number_of_caches; t++) {
863 cache_entry *cache = &caches[t * FFTW_CACHE_SIZE];
864 for (int i = 0; i < FFTW_CACHE_SIZE; i++) {
865 if (cache[i].plan != NULL) {
866 fftw_destroy_plan(*cache[i].plan);
867 free(cache[i].plan);
868 }
869 }
870 }
871 free(caches);
872 caches = NULL;
873 free(cache_oldest_entries);
874 cache_oldest_entries = NULL;
875 number_of_caches = 0;
876 omp_destroy_nest_lock(&cache_lock);
877 // Export wisdom before finalizing the library to ensure storing the correct
878 // threading etc.
879 if (wisdom_file != NULL) {
880 const int error = fftw_export_wisdom_to_filename(wisdom_file);
881 if (false && error != 0 && cp_mpi_comm_rank(cp_mpi_get_comm_world()))
882 fprintf(stderr,
883 "Exporting wisdom failed! Maybe writing access is missing.");
884 }
885 is_initialized = false;
886 fftw_planning_mode = -1;
887 fftw_cleanup_threads();
888#else
889 cp_assert(false && "The grid library was not compiled with FFTW support.");
890 (void)wisdom_file;
891#endif
892}
893
894/*******************************************************************************
895 * \brief Whether a distributed FFT implementation is available.
896 * \author Frederick Stein
897 ******************************************************************************/
899
900/*******************************************************************************
901 * \brief Allocate buffer of type double.
902 * \param length Number of elements.
903 * \param buffer Buffer pointer.
904 * \author Frederick Stein
905 ******************************************************************************/
906void fft_fftw_allocate_double(const int length, double **buffer) {
907#if defined(__FFTW3)
908 cp_assert(is_initialized);
909 cp_assert(buffer != NULL);
910 cp_assert(*buffer == NULL);
911 *buffer = fftw_alloc_real(imax(1, length));
912#else
913 (void)length;
914 (void)buffer;
915 cp_assert(0 && "The grid library was not compiled with FFTW support.");
916#endif
917}
918
919/*******************************************************************************
920 * \brief Allocate buffer of type double complex.
921 * \param length Number of elements.
922 * \param buffer Buffer pointer.
923 * \author Frederick Stein
924 ******************************************************************************/
925void fft_fftw_allocate_complex(const int length, double complex **buffer) {
926#if defined(__FFTW3)
927 cp_assert(is_initialized);
928 cp_assert(buffer != NULL);
929 cp_assert(*buffer == NULL);
930 *buffer = fftw_alloc_complex(imax(1, length));
931#else
932 (void)length;
933 (void)buffer;
934 cp_assert(0 && "The grid library was not compiled with FFTW support.");
935#endif
936}
937
938/*******************************************************************************
939 * \brief Free buffer of type double.
940 * \param buffer Buffer pointer.
941 * \author Frederick Stein
942 ******************************************************************************/
943void fft_fftw_free_double(double *buffer) {
944#if defined(__FFTW3)
945 cp_assert(is_initialized);
946 fftw_free(buffer);
947#else
948 (void)buffer;
949 cp_assert(0 && "The grid library was not compiled with FFTW support.");
950#endif
951}
952
953/*******************************************************************************
954 * \brief Free buffer of type double complex.
955 * \param buffer Buffer pointer.
956 * \author Frederick Stein
957 ******************************************************************************/
958void fft_fftw_free_complex(double complex *buffer) {
959#if defined(__FFTW3)
960 cp_assert(is_initialized);
961 fftw_free(buffer);
962#else
963 (void)buffer;
964 cp_assert(0 && "The grid library was not compiled with FFTW support.");
965#endif
966}
967
968#if defined(__FFTW3)
969
970/*******************************************************************************
971 * \brief Create plan of a local C2C 1D FFT.
972 * \param key Encoded FFT key.
973 * \param grid_in Input grid buffer.
974 * \param grid_out Output grid buffer.
975 * \author Frederick Stein
976 ******************************************************************************/
977fftw_plan *fft_fftw_create_1d_plan(const fft_key_t key, double complex *grid_in,
978 double complex *grid_out) {
979 omp_set_nest_lock(&cache_lock);
980 fftw_plan *cached_plan = lookup_plan_from_cache(key);
981 if (cached_plan != NULL) {
982 omp_unset_nest_lock(&cache_lock);
983 return cached_plan;
984 }
985 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
986 bool direction, inplace;
987 int fft_size[3], inembed[3], onembed[3];
988 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
989 &number_of_threads, &direction, &inplace, inembed,
990 onembed, &idist, &odist, &istride, &ostride);
991 cp_assert(rank == 1);
992 fftw_plan_with_nthreads(number_of_threads);
993 fftw_plan *plan = malloc(sizeof(fftw_plan));
994 if (grid_in == grid_out) {
995 cp_assert(inplace);
996 cp_assert(istride == ostride);
997 cp_assert(idist == odist);
998 } else {
999 cp_assert(!inplace);
1000 }
1001 *plan = fftw_plan_many_dft(rank, fft_size, number_of_ffts, grid_in, inembed,
1002 istride, idist, grid_out, onembed, ostride, odist,
1003 direction ? FFTW_FORWARD : FFTW_BACKWARD,
1004 fftw_planning_mode);
1005 cp_assert(plan != NULL);
1006 add_plan_to_cache(key, plan);
1007 omp_unset_nest_lock(&cache_lock);
1008 return plan;
1009}
1010/*******************************************************************************
1011 * \brief Create plan of a local R2C/C2R 1D FFT.
1012 * \param key Encoded FFT key.
1013 * \param grid_in Input grid buffer.
1014 * \param grid_out Output grid buffer.
1015 * \author Frederick Stein
1016 ******************************************************************************/
1017fftw_plan *fft_fftw_create_1d_plan_r2c(const fft_key_t key, double *grid_in,
1018 double *grid_out) {
1019 omp_set_nest_lock(&cache_lock);
1020 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1021 if (cached_plan != NULL) {
1022 omp_unset_nest_lock(&cache_lock);
1023 return cached_plan;
1024 }
1025 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1026 bool direction, inplace;
1027 int fft_size[3], inembed[3], onembed[3];
1028 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1029 &number_of_threads, &direction, &inplace, inembed,
1030 onembed, &idist, &odist, &istride, &ostride);
1031 cp_assert(rank == 1);
1032 fftw_plan_with_nthreads(number_of_threads);
1033 fftw_plan *plan = malloc(sizeof(fftw_plan));
1034 if (grid_in == grid_out) {
1035 if (istride == 1) {
1036 // Check whether the arrays are correctly padded
1037 if (direction) {
1038 cp_assert(idist == 2 * odist);
1039 } else {
1040 cp_assert(odist == 2 * idist);
1041 }
1042 }
1043 cp_assert(istride == ostride);
1044 }
1045 if (direction) {
1046 *plan =
1047 fftw_plan_many_dft_r2c(rank, fft_size, number_of_ffts, grid_in, inembed,
1048 istride, idist, (double complex *)grid_out,
1049 onembed, ostride, odist, fftw_planning_mode);
1050 } else {
1051 // We use the buffers the other way around to prevent
1052 // out-of-bounds-accesses of the planner if the output array has only the
1053 // minimum size
1054 *plan = fftw_plan_many_dft_c2r(
1055 rank, fft_size, number_of_ffts, (double complex *)grid_in, inembed,
1056 istride, idist, grid_out, onembed, ostride, odist, fftw_planning_mode);
1057 }
1058 cp_assert(plan != NULL);
1059 add_plan_to_cache(key, plan);
1060 omp_unset_nest_lock(&cache_lock);
1061 return plan;
1062}
1063
1064/*******************************************************************************
1065 * \brief Create plan of a local C2C 2D FFT.
1066 * \param key Encoded FFT key.
1067 * \param grid_in Input grid buffer.
1068 * \param grid_out Output grid buffer.
1069 * \author Frederick Stein
1070 ******************************************************************************/
1071fftw_plan *fft_fftw_create_2d_plan(const fft_key_t key, double complex *grid_in,
1072 double complex *grid_out) {
1073 omp_set_nest_lock(&cache_lock);
1074 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1075 if (cached_plan != NULL) {
1076 omp_unset_nest_lock(&cache_lock);
1077 return cached_plan;
1078 }
1079 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1080 bool direction, inplace;
1081 int fft_size[3], inembed[3], onembed[3];
1082 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1083 &number_of_threads, &direction, &inplace, inembed,
1084 onembed, &idist, &odist, &istride, &ostride);
1085 cp_assert(rank == 2);
1086 fftw_plan_with_nthreads(number_of_threads);
1087 fftw_plan *plan = malloc(sizeof(fftw_plan));
1088 if (grid_in == grid_out) {
1089 // Array strides need to match for in-place FFTs
1090 cp_assert(istride == ostride);
1091 cp_assert(idist == odist);
1092 }
1093 *plan = fftw_plan_many_dft(rank, fft_size, number_of_ffts, grid_in, inembed,
1094 istride, idist, grid_out, onembed, ostride, odist,
1095 direction ? FFTW_FORWARD : FFTW_BACKWARD,
1096 fftw_planning_mode);
1097 cp_assert(plan != NULL);
1098 add_plan_to_cache(key, plan);
1099 omp_unset_nest_lock(&cache_lock);
1100 return plan;
1101}
1102
1103/*******************************************************************************
1104 * \brief Create plan of a local R2C/C2R 2D FFT.
1105 * \param key Encoded FFT key.
1106 * \param grid_in Input grid buffer.
1107 * \param grid_out Output grid buffer.
1108 * \author Frederick Stein
1109 ******************************************************************************/
1110fftw_plan *fft_fftw_create_2d_plan_r2c(const fft_key_t key, double *grid_in,
1111 double *grid_out) {
1112 omp_set_nest_lock(&cache_lock);
1113 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1114 if (cached_plan != NULL) {
1115 omp_unset_nest_lock(&cache_lock);
1116 return cached_plan;
1117 }
1118 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1119 bool direction, inplace;
1120 int fft_size[3], inembed[3], onembed[3];
1121 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1122 &number_of_threads, &direction, &inplace, inembed,
1123 onembed, &idist, &odist, &istride, &ostride);
1124 cp_assert(rank == 2);
1125 fftw_plan_with_nthreads(number_of_threads);
1126 // We need the guru interface here because cuts the last dimension in half
1127 // whereas we want the first dimension
1128 fftw_plan *plan = malloc(sizeof(fftw_plan));
1129 if (grid_in == grid_out) {
1130 if (istride == 1) {
1131 // Check whether the arrays are correctly padded
1132 if (direction) {
1133 cp_assert(idist == 2 * odist);
1134 } else {
1135 cp_assert(odist == 2 * idist);
1136 }
1137 }
1138 cp_assert(istride == ostride);
1139 }
1140 if (direction) {
1141 *plan =
1142 fftw_plan_many_dft_r2c(rank, fft_size, number_of_ffts, grid_in, inembed,
1143 istride, idist, (double complex *)grid_out,
1144 onembed, ostride, odist, fftw_planning_mode);
1145 } else {
1146 // We use the buffers the other way around to prevent
1147 // out-of-bounds-accesses of the planner if the output array has only the
1148 // minimum size
1149 *plan = fftw_plan_many_dft_c2r(
1150 rank, fft_size, number_of_ffts, (double complex *)grid_in, inembed,
1151 istride, idist, grid_out, onembed, ostride, odist, fftw_planning_mode);
1152 }
1153 cp_assert(plan != NULL);
1154 add_plan_to_cache(key, plan);
1155 omp_unset_nest_lock(&cache_lock);
1156 return plan;
1157}
1158
1159/*******************************************************************************
1160 * \brief Create plan of a local C2C 3D FFT.
1161 * \param key Encoded FFT key.
1162 * \param grid_in Input grid buffer.
1163 * \param grid_out Output grid buffer.
1164 * \author Frederick Stein
1165 ******************************************************************************/
1166fftw_plan *fft_fftw_create_3d_plan(const fft_key_t key, double complex *grid_in,
1167 double complex *grid_out) {
1168 omp_set_nest_lock(&cache_lock);
1169 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1170 if (cached_plan != NULL) {
1171 omp_unset_nest_lock(&cache_lock);
1172 return cached_plan;
1173 }
1174 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1175 bool direction, inplace;
1176 int fft_size[3], inembed[3], onembed[3];
1177 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1178 &number_of_threads, &direction, &inplace, inembed,
1179 onembed, &idist, &odist, &istride, &ostride);
1180 cp_assert(rank == 3);
1181 fftw_plan_with_nthreads(number_of_threads);
1182 fftw_plan *plan = malloc(sizeof(fftw_plan));
1183 *plan = fftw_plan_many_dft(rank, fft_size, number_of_ffts, grid_in, inembed,
1184 istride, idist, grid_out, onembed, ostride, odist,
1185 direction ? FFTW_FORWARD : FFTW_BACKWARD,
1186 fftw_planning_mode);
1187 add_plan_to_cache(key, plan);
1188 cp_assert(plan != NULL);
1189 omp_unset_nest_lock(&cache_lock);
1190 return plan;
1191}
1192
1193/*******************************************************************************
1194 * \brief Create plan of a local R2C/C2R 3D FFT.
1195 * \param key Encoded FFT key.
1196 * \param grid_in Input grid buffer.
1197 * \param grid_out Output grid buffer.
1198 * \author Frederick Stein
1199 ******************************************************************************/
1200fftw_plan *fft_fftw_create_3d_plan_r2c(const fft_key_t key, double *grid_in,
1201 double *grid_out) {
1202 omp_set_nest_lock(&cache_lock);
1203 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1204 if (cached_plan != NULL) {
1205 omp_unset_nest_lock(&cache_lock);
1206 return cached_plan;
1207 }
1208 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1209 bool direction, inplace;
1210 int fft_size[3], inembed[3], onembed[3];
1211 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1212 &number_of_threads, &direction, &inplace, inembed,
1213 onembed, &idist, &odist, &istride, &ostride);
1214 cp_assert(rank == 3);
1215 fftw_plan_with_nthreads(number_of_threads);
1216 fftw_plan *plan = malloc(sizeof(fftw_plan));
1217 if (direction) {
1218 *plan =
1219 fftw_plan_many_dft_r2c(rank, fft_size, number_of_ffts, grid_in, inembed,
1220 istride, idist, (double complex *)grid_out,
1221 onembed, ostride, odist, fftw_planning_mode);
1222 } else {
1223 // We use the buffers the other way around to prevent
1224 // out-of-bounds-accesses of the planner if the output array has only the
1225 // minimum size
1226 *plan = fftw_plan_many_dft_c2r(
1227 rank, fft_size, number_of_ffts, (double complex *)grid_in, inembed,
1228 istride, idist, grid_out, onembed, ostride, odist, fftw_planning_mode);
1229 }
1230 add_plan_to_cache(key, plan);
1231 cp_assert(plan != NULL);
1232 omp_unset_nest_lock(&cache_lock);
1233 return plan;
1234}
1235
1236/*******************************************************************************
1237 * \brief Create plan of a local C2C 3D FFT.
1238 * \param key Encoded FFT key.
1239 * \param grid_in Input grid buffer.
1240 * \param grid_out Output grid buffer.
1241 * \author Frederick Stein
1242 ******************************************************************************/
1243fftw_plan *fft_fftw_create_guru_plan(const fft_key_t key,
1244 double complex *grid_in,
1245 double complex *grid_out) {
1247 omp_set_nest_lock(&cache_lock);
1248 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1249 if (cached_plan != NULL) {
1250 omp_unset_nest_lock(&cache_lock);
1251 return cached_plan;
1252 }
1253 bool direction, inplace;
1254 int rank, howmany_rank, number_of_threads;
1255 fft_iodim dims[3], howmany_dims[3];
1256 fetch_data_from_key_guru(key, &direction, &rank, dims, &howmany_rank,
1257 howmany_dims, &number_of_threads, &inplace);
1258
1259 fftw_plan_with_nthreads(number_of_threads);
1260 fftw_plan *plan = malloc(sizeof(fftw_plan));
1261 if ((double complex *)grid_in == grid_out) {
1262 cp_assert(inplace);
1263 for (int r = 0; r < rank; r++)
1264 cp_assert(dims[r].is == dims[r].os);
1265 for (int r = 0; r < howmany_rank; r++)
1266 cp_assert(howmany_dims[r].is == howmany_dims[r].os);
1267 } else {
1268 cp_assert(!inplace);
1269 }
1270 *plan = fftw_plan_guru_dft(rank, dims, howmany_rank, howmany_dims, grid_in,
1271 grid_out, direction ? FFTW_FORWARD : FFTW_BACKWARD,
1272 fftw_planning_mode);
1273 add_plan_to_cache(key, plan);
1274 cp_assert(plan != NULL);
1275 omp_unset_nest_lock(&cache_lock);
1276 return plan;
1277}
1278
1279/*******************************************************************************
1280 * \brief Create plan of a local R2C/C2R 3D FFT.
1281 * \param key Encoded FFT key.
1282 * \param grid_in Input grid buffer.
1283 * \param grid_out Output grid buffer.
1284 * \author Frederick Stein
1285 ******************************************************************************/
1286fftw_plan *fft_fftw_create_guru_plan_r2c(const fft_key_t key, double *grid_in,
1287 double *grid_out) {
1289 omp_set_nest_lock(&cache_lock);
1290 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1291 if (cached_plan != NULL) {
1292 omp_unset_nest_lock(&cache_lock);
1293 return cached_plan;
1294 }
1295 bool direction, inplace;
1296 int rank, howmany_rank, number_of_threads;
1297 fft_iodim dims[3], howmany_dims[3];
1298 fetch_data_from_key_guru(key, &direction, &rank, dims, &howmany_rank,
1299 howmany_dims, &number_of_threads, &inplace);
1300
1301 fftw_plan_with_nthreads(number_of_threads);
1302 fftw_plan *plan = malloc(sizeof(fftw_plan));
1303 if (direction) {
1304 if (grid_in == grid_out) {
1305 cp_assert(inplace);
1306 for (int r = 0; r < rank; r++)
1307 cp_assert(dims[r].is == 2 * dims[r].os ||
1308 (dims[r].is == 1 && dims[r].os == 1));
1309 for (int r = 0; r < howmany_rank; r++)
1310 cp_assert(howmany_dims[r].is == 2 * howmany_dims[r].os);
1311 } else {
1312 cp_assert(!inplace);
1313 }
1314 *plan =
1315 fftw_plan_guru_dft_r2c(rank, dims, howmany_rank, howmany_dims, grid_in,
1316 (double complex *)grid_out, fftw_planning_mode);
1317 } else {
1318 if (grid_in == grid_out) {
1319 cp_assert(inplace);
1320 for (int r = 0; r < rank; r++)
1321 cp_assert(2 * dims[r].is == dims[r].os ||
1322 (dims[r].is == 1 && dims[r].os == 1));
1323 for (int r = 0; r < howmany_rank; r++)
1324 cp_assert(2 * howmany_dims[r].is == howmany_dims[r].os);
1325 } else {
1326 cp_assert(!inplace);
1327 }
1328 // We use the buffers the other way around to prevent
1329 // out-of-bounds-accesses of the planner if the output array has only the
1330 // minimum size
1331 *plan = fftw_plan_guru_dft_c2r(rank, dims, howmany_rank, howmany_dims,
1332 (double complex *)grid_in, grid_out,
1333 fftw_planning_mode);
1334 }
1335 add_plan_to_cache(key, plan);
1336 cp_assert(plan != NULL);
1337 omp_unset_nest_lock(&cache_lock);
1338 return plan;
1339}
1340#endif
1341
1342/*******************************************************************************
1343 * \brief Performs a local C2C 1D FFT.
1344 * \param direction FFT direction (forward or backward).
1345 * \param fft_size Size of the transform in each dimension.
1346 * \param number_of_ffts Number of independent (batched) transforms.
1347 * \param transpose_in Whether the input array uses the transposed layout.
1348 * \param transpose_out Whether the output array uses the transposed layout.
1349 * \param leading_dimension_in Leading dimension of the input array.
1350 * \param leading_dimension_out Leading dimension of the output array.
1351 * \param grid_in Input grid buffer.
1352 * \param grid_out Output grid buffer.
1353 * \author Frederick Stein
1354 ******************************************************************************/
1356 const bool direction, const int fft_size, const int number_of_ffts,
1357 const bool transpose_in, const bool transpose_out,
1358 const int leading_dimension_in, const int leading_dimension_out,
1359 double complex *grid_in, double complex *grid_out) {
1360#if defined(__FFTW3)
1361 cp_assert(is_initialized);
1362 if (fft_size == 0 || number_of_ffts == 0)
1363 return;
1364 const bool in_place = grid_in == grid_out;
1365 const int number_of_threads = fft_team_size();
1366 if (fftw_planning_mode == FFTW_ESTIMATE) {
1367 fftw_plan *plan = NULL;
1368 const int block_size =
1369 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1370 const int number_of_full_blocks = number_of_ffts / block_size;
1371 fft_key_t key;
1372 get_key_1d(direction, fft_size, block_size, transpose_in, transpose_out,
1373 leading_dimension_in, leading_dimension_out, 1, in_place, key);
1374 plan = lookup_plan_from_cache(key);
1375 if (plan == NULL)
1376 plan = fft_fftw_create_1d_plan(key, grid_in, grid_out);
1377 if (block_size * number_of_full_blocks != number_of_ffts) {
1378 const int block_size_last_thread = number_of_ffts % block_size;
1379 fft_key_t key;
1380 get_key_1d(direction, fft_size, block_size_last_thread, transpose_in,
1381 transpose_out, leading_dimension_in, leading_dimension_out, 1,
1382 in_place, key);
1383 plan = lookup_plan_from_cache(key);
1384 if (plan == NULL)
1385 plan = fft_fftw_create_1d_plan(key, grid_in, grid_out);
1386 }
1387 } else {
1388 fft_key_t key;
1389 get_key_1d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
1390 leading_dimension_in, leading_dimension_out, fft_team_size(),
1391 in_place, key);
1392 fftw_plan *plan = lookup_plan_from_cache(key);
1393 if (plan == NULL)
1394 plan = fft_fftw_create_1d_plan(key, grid_in, grid_out);
1395 }
1396#else
1397 (void)direction;
1398 (void)fft_size;
1399 (void)number_of_ffts;
1400 (void)grid_in;
1401 (void)grid_out;
1402 (void)transpose_in;
1403 (void)transpose_out;
1404 (void)leading_dimension_in;
1405 (void)leading_dimension_out;
1406 cp_assert(0 && "The grid library was not compiled with FFTW support.");
1407#endif
1408}
1409
1410/*******************************************************************************
1411 * \brief Performs a local forward R2C FFT.
1412 * \param fft_size Size of the transform in each dimension.
1413 * \param number_of_ffts Number of independent (batched) transforms.
1414 * \param transpose_in Whether the input array uses the transposed layout.
1415 * \param transpose_out Whether the output array uses the transposed layout.
1416 * \param leading_dimension_in Leading dimension of the input array.
1417 * \param leading_dimension_out Leading dimension of the output array.
1418 * \param grid_in Input grid buffer.
1419 * \param grid_out Output grid buffer.
1420 * \author Frederick Stein
1421 ******************************************************************************/
1422void fft_fftw_register_1d_r2c_local(const int fft_size,
1423 const int number_of_ffts,
1424 const bool transpose_in,
1425 const bool transpose_out,
1426 const int leading_dimension_in,
1427 const int leading_dimension_out,
1428 double *grid_in, double complex *grid_out) {
1429#if defined(__FFTW3)
1430 cp_assert(is_initialized);
1431 if (fft_size == 0 || number_of_ffts == 0)
1432 return;
1433 const bool in_place = grid_in == (double *)grid_out;
1434 const int number_of_threads = fft_team_size();
1435 // The self-written plans do not work with in-place transforms using the
1436 // transposed format
1437 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1438 fftw_plan *plan = NULL;
1439 const int block_size =
1440 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1441 const int number_of_full_blocks = number_of_ffts / block_size;
1442 fft_key_t key;
1443 get_key_1d_r2c(true, fft_size, block_size, transpose_in, transpose_out,
1444 leading_dimension_in, leading_dimension_out, 1, in_place,
1445 key);
1446 plan = lookup_plan_from_cache(key);
1447 if (plan == NULL) {
1448 plan = fft_fftw_create_1d_plan_r2c(key, grid_in, (double *)grid_out);
1449 }
1450 if (block_size * number_of_full_blocks != number_of_ffts) {
1451 const int block_size_last_thread = number_of_ffts % block_size;
1452 fft_key_t key;
1453 get_key_1d_r2c(true, fft_size, block_size_last_thread, transpose_in,
1454 transpose_out, leading_dimension_in, leading_dimension_out,
1455 1, in_place, key);
1456 plan = lookup_plan_from_cache(key);
1457 if (plan == NULL) {
1458 plan = fft_fftw_create_1d_plan_r2c(key, grid_in, (double *)grid_out);
1459 }
1460 }
1461 } else {
1462 fft_key_t key;
1463 get_key_1d_r2c(true, fft_size, number_of_ffts, transpose_in, transpose_out,
1464 leading_dimension_in, leading_dimension_out, fft_team_size(),
1465 in_place, key);
1466 fftw_plan *plan = lookup_plan_from_cache(key);
1467 if (plan == NULL) {
1468 plan = fft_fftw_create_1d_plan_r2c(key, grid_in, (double *)grid_out);
1469 }
1470 }
1471#else
1472 (void)fft_size;
1473 (void)number_of_ffts;
1474 (void)grid_in;
1475 (void)grid_out;
1476 (void)transpose_in;
1477 (void)transpose_out;
1478 (void)leading_dimension_in;
1479 (void)leading_dimension_out;
1480 cp_assert(0 && "The grid library was not compiled with FFTW support.");
1481#endif
1482}
1483
1484/*******************************************************************************
1485 * \brief Performs a local backwards C2R 1D FFT
1486 * \param fft_size Size of the transform in each dimension.
1487 * \param number_of_ffts Number of independent (batched) transforms.
1488 * \param transpose_in Whether the input array uses the transposed layout.
1489 * \param transpose_out Whether the output array uses the transposed layout.
1490 * \param leading_dimension_in Leading dimension of the input array.
1491 * \param leading_dimension_out Leading dimension of the output array.
1492 * \param grid_in Input grid buffer.
1493 * \param grid_out Output grid buffer.
1494 * \author Frederick Stein
1495 ******************************************************************************/
1496void fft_fftw_register_1d_c2r_local(const int fft_size,
1497 const int number_of_ffts,
1498 const bool transpose_in,
1499 const bool transpose_out,
1500 const int leading_dimension_in,
1501 const int leading_dimension_out,
1502 double complex *grid_in, double *grid_out) {
1503#if defined(__FFTW3)
1504 cp_assert(is_initialized);
1505 if (fft_size == 0 || number_of_ffts == 0)
1506 return;
1507 const bool in_place = (double *)grid_in == grid_out;
1508 const int number_of_threads = fft_team_size();
1509 // The self-written plans do not work with in-place transforms using the
1510 // transposed format
1511 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1512 fftw_plan *plan = NULL;
1513 const int block_size =
1514 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1515 const int number_of_full_blocks = number_of_ffts / block_size;
1516 fft_key_t key;
1517 get_key_1d_r2c(false, fft_size, block_size, transpose_in, transpose_out,
1518 leading_dimension_in, leading_dimension_out, 1, in_place,
1519 key);
1520 plan = lookup_plan_from_cache(key);
1521 if (plan == NULL) {
1522 plan = fft_fftw_create_1d_plan_r2c(key, (double *)grid_in, grid_out);
1523 }
1524 if (block_size * number_of_full_blocks != number_of_ffts) {
1525 const int block_size_last_thread = number_of_ffts % block_size;
1526 fft_key_t key;
1527 get_key_1d_r2c(false, fft_size, block_size_last_thread, transpose_in,
1528 transpose_out, leading_dimension_in, leading_dimension_out,
1529 1, in_place, key);
1530 plan = lookup_plan_from_cache(key);
1531 if (plan == NULL) {
1532 plan = fft_fftw_create_1d_plan_r2c(key, (double *)grid_in, grid_out);
1533 }
1534 }
1535 } else {
1536 fft_key_t key;
1537 get_key_1d_r2c(false, fft_size, number_of_ffts, transpose_in, transpose_out,
1538 leading_dimension_in, leading_dimension_out, fft_team_size(),
1539 in_place, key);
1540 fftw_plan *plan = lookup_plan_from_cache(key);
1541 if (plan == NULL) {
1542 plan = fft_fftw_create_1d_plan_r2c(key, (double *)grid_in, grid_out);
1543 }
1544 }
1545#else
1546 (void)fft_size;
1547 (void)number_of_ffts;
1548 (void)grid_in;
1549 (void)grid_out;
1550 (void)transpose_in;
1551 (void)transpose_out;
1552 (void)leading_dimension_in;
1553 (void)leading_dimension_out;
1554 cp_assert(0 && "The grid library was not compiled with FFTW support.");
1555#endif
1556}
1557
1558/*******************************************************************************
1559 * \brief Performs a local C2C 2D FFT
1560 * \param direction FFT direction (forward or backward).
1561 * \param fft_size Size of the transform in each dimension.
1562 * \param number_of_ffts Number of independent (batched) transforms.
1563 * \param transpose_in Whether the input array uses the transposed layout.
1564 * \param transpose_out Whether the output array uses the transposed layout.
1565 * \param grid_in Input grid buffer.
1566 * \param grid_out Output grid buffer.
1567 * \author Frederick Stein
1568 ******************************************************************************/
1569void fft_fftw_register_2d_local(const bool direction, const int fft_size[2],
1570 const int number_of_ffts,
1571 const bool transpose_in,
1572 const bool transpose_out,
1573 double complex *grid_in,
1574 double complex *grid_out) {
1575#if defined(__FFTW3)
1576 cp_assert(is_initialized);
1577 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
1578 return;
1579 const bool in_place = grid_in == grid_out;
1580 const int number_of_threads = fft_team_size();
1581 if (fftw_planning_mode == FFTW_ESTIMATE) {
1582 fftw_plan *plan = NULL, *plan_last_thread = NULL;
1583 const int block_size =
1584 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1585 const int number_of_full_blocks = number_of_ffts / block_size;
1586 fft_key_t key;
1587 get_key_2d(direction, fft_size, block_size, transpose_in, transpose_out,
1588 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
1589 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
1590 in_place, key);
1591 plan = lookup_plan_from_cache(key);
1592 if (plan == NULL) {
1593 plan = fft_fftw_create_2d_plan(key, grid_in, grid_out);
1594 }
1595 if (block_size * number_of_full_blocks != number_of_ffts) {
1596 const int block_size_last_thread = number_of_ffts % block_size;
1597 fft_key_t key;
1598 get_key_2d(direction, fft_size, block_size_last_thread, transpose_in,
1599 transpose_out,
1600 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
1601 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
1602 in_place, key);
1603 plan_last_thread = lookup_plan_from_cache(key);
1604 if (plan_last_thread == NULL) {
1605 plan_last_thread = fft_fftw_create_2d_plan(key, grid_in, grid_out);
1606 }
1607 }
1608 } else {
1609 fft_key_t key;
1610 get_key_2d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
1611 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
1612 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1],
1613 fft_team_size(), grid_in == grid_out, key);
1614 fftw_plan *plan = lookup_plan_from_cache(key);
1615 if (plan == NULL) {
1616 plan = fft_fftw_create_2d_plan(key, grid_in, grid_out);
1617 }
1618 }
1619#else
1620 (void)direction;
1621 (void)fft_size;
1622 (void)number_of_ffts;
1623 (void)grid_in;
1624 (void)grid_out;
1625 (void)transpose_in;
1626 (void)transpose_out;
1627 cp_assert(0 && "The grid library was not compiled with FFTW support.");
1628#endif
1629}
1630
1631/*******************************************************************************
1632 * \brief Performs a local forward R2C 2D FFT.
1633 * \param fft_size Size of the transform in each dimension.
1634 * \param number_of_ffts Number of independent (batched) transforms.
1635 * \param transpose_in Whether the input array uses the transposed layout.
1636 * \param transpose_out Whether the output array uses the transposed layout.
1637 * \param grid_in Input grid buffer.
1638 * \param grid_out Output grid buffer.
1639 * \author Frederick Stein
1640 ******************************************************************************/
1641void fft_fftw_register_2d_r2c_local(const int fft_size[2],
1642 const int number_of_ffts,
1643 const bool transpose_in,
1644 const bool transpose_out, double *grid_in,
1645 double complex *grid_out) {
1646#if defined(__FFTW3)
1647 cp_assert(is_initialized);
1648 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
1649 return;
1650 const bool in_place = grid_in == (double *)grid_out;
1651 const int number_of_threads = fft_team_size();
1652 // The self-written plans do not work with in-place transforms using the
1653 // transposed format
1654 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1655 fftw_plan *plan = NULL, *plan_last_thread = NULL;
1656 const int block_size =
1657 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1658 const int number_of_full_blocks = number_of_ffts / block_size;
1659 fft_key_t key;
1660 get_key_2d_r2c(
1661 true, fft_size, block_size, transpose_in, transpose_out,
1662 transpose_in ? number_of_ffts
1663 : fft_size[0] *
1664 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
1665 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1), 1,
1666 in_place, key);
1667 plan = lookup_plan_from_cache(key);
1668 if (plan == NULL) {
1669 plan = fft_fftw_create_2d_plan_r2c(key, grid_in, (double *)grid_out);
1670 }
1671 if (block_size * number_of_full_blocks != number_of_ffts) {
1672 const int block_size_last_thread = number_of_ffts % block_size;
1673 fft_key_t key;
1674 get_key_2d_r2c(
1675 true, fft_size, block_size_last_thread, transpose_in, transpose_out,
1676 transpose_in ? number_of_ffts
1677 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1678 : fft_size[1]),
1679 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1680 1, in_place, key);
1681 plan_last_thread = lookup_plan_from_cache(key);
1682 if (plan_last_thread == NULL) {
1683 plan_last_thread =
1684 fft_fftw_create_2d_plan_r2c(key, grid_in, (double *)grid_out);
1685 }
1686 }
1687 } else {
1688 fft_key_t key;
1689 get_key_2d_r2c(
1690 true, fft_size, number_of_ffts, transpose_in, transpose_out,
1691 transpose_in ? number_of_ffts
1692 : fft_size[0] *
1693 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
1694 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1695 fft_team_size(), grid_in == (double *)grid_out, key);
1696 fftw_plan *plan = lookup_plan_from_cache(key);
1697 if (plan == NULL) {
1698 plan = fft_fftw_create_2d_plan_r2c(key, grid_in, (double *)grid_out);
1699 }
1700 }
1701#else
1702 (void)fft_size;
1703 (void)number_of_ffts;
1704 (void)grid_in;
1705 (void)grid_out;
1706 (void)transpose_in;
1707 (void)transpose_out;
1708 cp_assert(0 && "The grid library was not compiled with FFTW support.");
1709#endif
1710}
1711
1712/*******************************************************************************
1713 * \brief Performs a local backwards C2R 2D FFT.
1714 * \param fft_size Size of the transform in each dimension.
1715 * \param number_of_ffts Number of independent (batched) transforms.
1716 * \param transpose_in Whether the input array uses the transposed layout.
1717 * \param transpose_out Whether the output array uses the transposed layout.
1718 * \param grid_in Input grid buffer.
1719 * \param grid_out Output grid buffer.
1720 * \author Frederick Stein
1721 ******************************************************************************/
1722void fft_fftw_register_2d_c2r_local(const int fft_size[2],
1723 const int number_of_ffts,
1724 const bool transpose_in,
1725 const bool transpose_out,
1726 double complex *grid_in, double *grid_out) {
1727#if defined(__FFTW3)
1728 cp_assert(is_initialized);
1729 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
1730 return;
1731 const bool in_place = (double *)grid_in == grid_out;
1732 const int number_of_threads = fft_team_size();
1733 // The self-written plans do not work with in-place transforms using the
1734 // transposed format
1735 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1736 fftw_plan *plan = NULL, *plan_last_thread = NULL;
1737 const int block_size =
1738 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1739 const int number_of_full_blocks = number_of_ffts / block_size;
1740 fft_key_t key;
1741 get_key_2d_r2c(
1742 false, fft_size, block_size, transpose_in, transpose_out,
1743 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1744 transpose_out ? number_of_ffts
1745 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1746 : fft_size[1]),
1747 1, in_place, key);
1748 plan = lookup_plan_from_cache(key);
1749 if (plan == NULL) {
1750 plan = fft_fftw_create_2d_plan_r2c(key, (double *)grid_in, grid_out);
1751 }
1752 if (block_size * number_of_full_blocks != number_of_ffts) {
1753 const int block_size_last_thread = number_of_ffts % block_size;
1754 fft_key_t key;
1755 get_key_2d_r2c(
1756 false, fft_size, block_size_last_thread, transpose_in, transpose_out,
1757 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1758 transpose_out ? number_of_ffts
1759 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1760 : fft_size[1]),
1761 1, in_place, key);
1762 plan_last_thread = lookup_plan_from_cache(key);
1763 if (plan_last_thread == NULL) {
1764 plan_last_thread =
1765 fft_fftw_create_2d_plan_r2c(key, (double *)grid_in, grid_out);
1766 }
1767 }
1768 } else {
1769 fft_key_t key;
1770 get_key_2d_r2c(
1771 false, fft_size, number_of_ffts, transpose_in, transpose_out,
1772 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1773 transpose_out ? number_of_ffts
1774 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1775 : fft_size[1]),
1776 fft_team_size(), grid_in == (double complex *)grid_out, key);
1777 fftw_plan *plan = lookup_plan_from_cache(key);
1778 if (plan == NULL) {
1779 plan = fft_fftw_create_2d_plan_r2c(key, (double *)grid_in, grid_out);
1780 }
1781 }
1782#else
1783 (void)fft_size;
1784 (void)number_of_ffts;
1785 (void)grid_in;
1786 (void)grid_out;
1787 (void)transpose_in;
1788 (void)transpose_out;
1789 cp_assert(0 && "The grid library was not compiled with FFTW support.");
1790#endif
1791}
1792
1793/*******************************************************************************
1794 * \brief Performs a local C2C 3D FFT.
1795 * \param direction FFT direction (forward or backward).
1796 * \param fft_size Size of the transform in each dimension.
1797 * \param number_of_ffts Number of independent (batched) transforms.
1798 * \param transpose_in Whether the input array uses the transposed layout.
1799 * \param transpose_out Whether the output array uses the transposed layout.
1800 * \param grid_in Input grid buffer.
1801 * \param grid_out Output grid buffer.
1802 * \author Frederick Stein
1803 ******************************************************************************/
1804void fft_fftw_register_3d_local(const bool direction, const int fft_size[3],
1805 const int number_of_ffts,
1806 const bool transpose_in,
1807 const bool transpose_out,
1808 double complex *grid_in,
1809 double complex *grid_out) {
1810#if defined(__FFTW3)
1811 cp_assert(is_initialized);
1812 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
1813 number_of_ffts == 0)
1814 return;
1815 const bool in_place = grid_in == grid_out;
1816 if (number_of_ffts > 1 &&
1817 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1818 register_threaded_3d_c2c(direction, fft_size, number_of_ffts, in_place,
1819 grid_in, grid_out);
1820 } else if (number_of_ffts == 1 &&
1821 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1822 register_threaded_3d_c2c_1d(direction, fft_size, in_place, grid_in,
1823 grid_out);
1824 } else {
1825 fft_key_t key;
1826 get_key_3d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
1827 fft_team_size(), in_place, key);
1828 fftw_plan *plan = lookup_plan_from_cache(key);
1829 if (plan == NULL) {
1830 plan = fft_fftw_create_3d_plan(key, grid_in, grid_out);
1831 }
1832 }
1833#else
1834 (void)fft_size;
1835 (void)number_of_ffts;
1836 (void)transpose_in;
1837 (void)transpose_out;
1838 (void)grid_in;
1839 (void)grid_out;
1840 cp_assert(0 && "The grid library was not compiled with FFTW support.");
1841#endif
1842}
1843
1844/*******************************************************************************
1845 * \brief Performs a local forward R2C 3D FFT.
1846 * \param fft_size Size of the transform in each dimension.
1847 * \param number_of_ffts Number of independent (batched) transforms.
1848 * \param transpose_in Whether the input array uses the transposed layout.
1849 * \param transpose_out Whether the output array uses the transposed layout.
1850 * \param grid_in Input grid buffer.
1851 * \param grid_out Output grid buffer.
1852 * \author Frederick Stein
1853 ******************************************************************************/
1854void fft_fftw_register_3d_r2c_local(const int fft_size[3],
1855 const int number_of_ffts,
1856 const bool transpose_in,
1857 const bool transpose_out, double *grid_in,
1858 double complex *grid_out) {
1859#if defined(__FFTW3)
1860 cp_assert(is_initialized);
1861 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
1862 number_of_ffts == 0)
1863 return;
1864 const bool in_place = grid_in == (double *)grid_out;
1865 if (number_of_ffts > 1 &&
1866 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1867 register_threaded_3d_r2c(true, fft_size, number_of_ffts, in_place, grid_in,
1868 (double *)grid_out);
1869 } else if (number_of_ffts == 1 &&
1870 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1871 register_threaded_3d_r2c_1d(true, fft_size, in_place, grid_in, grid_out);
1872 } else {
1873 fft_key_t key;
1874 get_key_3d_r2c(true, fft_size, number_of_ffts, transpose_in, transpose_out,
1875 fft_team_size(), in_place, key);
1876 fftw_plan *plan = lookup_plan_from_cache(key);
1877 if (plan == NULL) {
1878 plan = fft_fftw_create_3d_plan_r2c(key, grid_in, (double *)grid_out);
1879 }
1880 }
1881#else
1882 (void)fft_size;
1883 (void)number_of_ffts;
1884 (void)transpose_in;
1885 (void)transpose_out;
1886 (void)grid_in;
1887 (void)grid_out;
1888 cp_assert(0 && "The grid library was not compiled with FFTW support.");
1889#endif
1890}
1891
1892/*******************************************************************************
1893 * \brief Performs a local backwards R2C 3D FFT.
1894 * \param fft_size Size of the transform in each dimension.
1895 * \param number_of_ffts Number of independent (batched) transforms.
1896 * \param transpose_in Whether the input array uses the transposed layout.
1897 * \param transpose_out Whether the output array uses the transposed layout.
1898 * \param grid_in Input grid buffer.
1899 * \param grid_out Output grid buffer.
1900 * \author Frederick Stein
1901 ******************************************************************************/
1902void fft_fftw_register_3d_c2r_local(const int fft_size[3],
1903 const int number_of_ffts,
1904 const bool transpose_in,
1905 const bool transpose_out,
1906 double complex *grid_in, double *grid_out) {
1907#if defined(__FFTW3)
1908 cp_assert(is_initialized);
1909 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
1910 number_of_ffts == 0)
1911 return;
1912 const bool in_place = (double *)grid_in == grid_out;
1913 if (number_of_ffts > 1 &&
1914 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1915 register_threaded_3d_r2c(false, fft_size, number_of_ffts, in_place,
1916 (double *)grid_in, grid_out);
1917 } else if (number_of_ffts == 1 &&
1918 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1919 register_threaded_3d_r2c_1d(false, fft_size, in_place, grid_out, grid_in);
1920 } else {
1921 fft_key_t key;
1922 get_key_3d_r2c(false, fft_size, number_of_ffts, transpose_in, transpose_out,
1923 fft_team_size(), in_place, key);
1924 fftw_plan *plan = lookup_plan_from_cache(key);
1925 if (plan == NULL) {
1926 plan = fft_fftw_create_3d_plan_r2c(key, (double *)grid_in, grid_out);
1927 }
1928 }
1929#else
1930 (void)fft_size;
1931 (void)number_of_ffts;
1932 (void)transpose_in;
1933 (void)transpose_out;
1934 (void)grid_in;
1935 (void)grid_out;
1936 cp_assert(0 && "The grid library was not compiled with FFTW support.");
1937#endif
1938}
1939
1940/*******************************************************************************
1941 * \brief Performs a local forward C2C 1D FFT.
1942 * \param direction FFT direction (forward or backward).
1943 * \param fft_size Size of the transform in each dimension.
1944 * \param number_of_ffts Number of independent (batched) transforms.
1945 * \param transpose_in Whether the input array uses the transposed layout.
1946 * \param transpose_out Whether the output array uses the transposed layout.
1947 * \param leading_dimension_in Leading dimension of the input array.
1948 * \param leading_dimension_out Leading dimension of the output array.
1949 * \param grid_in Input grid buffer.
1950 * \param grid_out Output grid buffer.
1951 * \author Frederick Stein
1952 ******************************************************************************/
1953void fft_fftw_1d_local(const bool direction, const int fft_size,
1954 const int number_of_ffts, const bool transpose_in,
1955 const bool transpose_out, const int leading_dimension_in,
1956 const int leading_dimension_out, double complex *grid_in,
1957 double complex *grid_out) {
1958#if defined(__FFTW3)
1959 cp_assert(is_initialized);
1960 if (fft_size == 0 || number_of_ffts == 0)
1961 return;
1962 const bool in_place = grid_in == grid_out;
1963 const int number_of_threads = fft_team_size();
1964 if (number_of_threads == 1 && fftw_planning_mode == FFTW_ESTIMATE) {
1965 // Called from inside an OpenMP region: use the original batched strategy.
1966 fftw_plan *plan = NULL, *plan_last_thread = NULL;
1967 const int block_size =
1968 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1969 const int number_of_full_blocks = number_of_ffts / block_size;
1970 const int total_number_of_blocks =
1971 number_of_full_blocks + (number_of_ffts % block_size != 0);
1972 fft_key_t key;
1973 get_key_1d(direction, fft_size, block_size, transpose_in, transpose_out,
1974 leading_dimension_in, leading_dimension_out, 1, in_place, key);
1975 plan = lookup_plan_from_cache(key);
1976 if (plan == NULL) {
1977 double complex *buffer =
1978 fftw_alloc_complex(get_buffer_size_from_key(key));
1979 plan = fft_fftw_create_1d_plan(key, buffer, in_place ? buffer : grid_out);
1980 fftw_free(buffer);
1981 }
1982 if (block_size * number_of_full_blocks != number_of_ffts) {
1983 const int block_size_last_thread = number_of_ffts % block_size;
1984 fft_key_t key_last;
1985 get_key_1d(direction, fft_size, block_size_last_thread, transpose_in,
1986 transpose_out, leading_dimension_in, leading_dimension_out, 1,
1987 in_place, key_last);
1988 plan_last_thread = lookup_plan_from_cache(key_last);
1989 if (plan_last_thread == NULL) {
1990 double complex *buffer =
1991 fftw_alloc_complex(get_buffer_size_from_key(key_last));
1992 plan_last_thread = fft_fftw_create_1d_plan(
1993 key_last, buffer, in_place ? buffer : grid_out);
1994 fftw_free(buffer);
1995 }
1996 }
1997 const int offset_in =
1998 transpose_in ? block_size : block_size * leading_dimension_in;
1999 const int offset_out =
2000 transpose_out ? block_size : block_size * leading_dimension_out;
2001#pragma omp parallel num_threads(number_of_threads) default(none) \
2002 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2003 offset_out, plan_last_thread, total_number_of_blocks)
2004 {
2005 const int thread_id = omp_get_thread_num();
2006 if (thread_id < number_of_full_blocks) {
2007 fftw_execute_dft(*plan, grid_in + thread_id * offset_in,
2008 grid_out + thread_id * offset_out);
2009 } else if (thread_id + 1 == total_number_of_blocks) {
2010 fftw_execute_dft(*plan_last_thread, grid_in + thread_id * offset_in,
2011 grid_out + thread_id * offset_out);
2012 }
2013 }
2014 } else {
2015 fft_key_t key;
2016 get_key_1d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
2017 leading_dimension_in, leading_dimension_out, number_of_threads,
2018 in_place, key);
2019 fftw_plan *plan = lookup_plan_from_cache(key);
2020 if (plan == NULL) {
2021 double complex *buffer =
2022 fftw_alloc_complex(get_buffer_size_from_key(key));
2023 plan = fft_fftw_create_1d_plan(key, buffer, in_place ? buffer : grid_out);
2024 fftw_free(buffer);
2025 }
2026 fftw_execute_dft(*plan, grid_in, grid_out);
2027 }
2028#else
2029 (void)direction;
2030 (void)fft_size;
2031 (void)number_of_ffts;
2032 (void)grid_in;
2033 (void)grid_out;
2034 (void)transpose_in;
2035 (void)transpose_out;
2036 (void)leading_dimension_in;
2037 (void)leading_dimension_out;
2038 cp_assert(0 && "The grid library was not compiled with FFTW support.");
2039#endif
2040}
2041
2042/*******************************************************************************
2043 * \brief Performs a local forward R2C FFT.
2044 * \param fft_size Size of the transform in each dimension.
2045 * \param number_of_ffts Number of independent (batched) transforms.
2046 * \param transpose_in Whether the input array uses the transposed layout.
2047 * \param transpose_out Whether the output array uses the transposed layout.
2048 * \param leading_dimension_in Leading dimension of the input array.
2049 * \param leading_dimension_out Leading dimension of the output array.
2050 * \param grid_in Input grid buffer.
2051 * \param grid_out Output grid buffer.
2052 * \author Frederick Stein
2053 ******************************************************************************/
2054void fft_fftw_1d_r2c_local(const int fft_size, const int number_of_ffts,
2055 const bool transpose_in, const bool transpose_out,
2056 const int leading_dimension_in,
2057 const int leading_dimension_out, double *grid_in,
2058 double complex *grid_out) {
2059#if defined(__FFTW3)
2060 cp_assert(is_initialized);
2061 if (fft_size == 0 || number_of_ffts == 0)
2062 return;
2063 const bool in_place = grid_in == (double *)grid_out;
2064 const int number_of_threads = fft_team_size();
2065 // The self-written plans do not work with in-place transforms using the
2066 // transposed format
2067 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2068 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2069 const int block_size =
2070 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2071 const int number_of_full_blocks = number_of_ffts / block_size;
2072 const int total_number_of_blocks =
2073 number_of_full_blocks + (number_of_ffts % block_size != 0);
2074 fft_key_t key;
2075 get_key_1d_r2c(true, fft_size, block_size, transpose_in, transpose_out,
2076 leading_dimension_in, leading_dimension_out, 1, in_place,
2077 key);
2078 plan = lookup_plan_from_cache(key);
2079 if (plan == NULL) {
2080 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2081 plan = fft_fftw_create_1d_plan_r2c(
2082 key, buffer, in_place ? buffer : (double *)grid_out);
2083 fftw_free(buffer);
2084 }
2085 if (block_size * number_of_full_blocks != number_of_ffts) {
2086 const int block_size_last_thread = number_of_ffts % block_size;
2087 fft_key_t key;
2088 get_key_1d_r2c(true, fft_size, block_size_last_thread, transpose_in,
2089 transpose_out, leading_dimension_in, leading_dimension_out,
2090 1, in_place, key);
2091 plan_last_thread = lookup_plan_from_cache(key);
2092 if (plan_last_thread == NULL) {
2093 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2094 plan_last_thread = fft_fftw_create_1d_plan_r2c(
2095 key, buffer, in_place ? buffer : (double *)grid_out);
2096 fftw_free(buffer);
2097 }
2098 }
2099 const int offset_in =
2100 transpose_in ? block_size : block_size * leading_dimension_in;
2101 const int offset_out =
2102 transpose_out ? block_size : block_size * leading_dimension_out;
2103#pragma omp parallel default(none) \
2104 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2105 offset_out, plan_last_thread, total_number_of_blocks)
2106 {
2107 const int thread_id = omp_get_thread_num();
2108 if (thread_id < number_of_full_blocks) {
2109 fftw_execute_dft_r2c(*plan, grid_in + thread_id * offset_in,
2110 grid_out + thread_id * offset_out);
2111 } else if (thread_id + 1 == total_number_of_blocks) {
2112 fftw_execute_dft_r2c(*plan_last_thread, grid_in + thread_id * offset_in,
2113 grid_out + thread_id * offset_out);
2114 }
2115 }
2116 } else {
2117 fft_key_t key;
2118 get_key_1d_r2c(true, fft_size, number_of_ffts, transpose_in, transpose_out,
2119 leading_dimension_in, leading_dimension_out, fft_team_size(),
2120 in_place, key);
2121 fftw_plan *plan = lookup_plan_from_cache(key);
2122 if (plan == NULL) {
2123 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2124 plan = fft_fftw_create_1d_plan_r2c(
2125 key, buffer, in_place ? buffer : (double *)grid_out);
2126 fftw_free(buffer);
2127 }
2128 cp_assert(plan != NULL);
2129 fftw_execute_dft_r2c(*plan, grid_in, grid_out);
2130 }
2131#else
2132 (void)fft_size;
2133 (void)number_of_ffts;
2134 (void)grid_in;
2135 (void)grid_out;
2136 (void)transpose_in;
2137 (void)transpose_out;
2138 (void)leading_dimension_in;
2139 (void)leading_dimension_out;
2140 cp_assert(0 && "The grid library was not compiled with FFTW support.");
2141#endif
2142}
2143
2144/*******************************************************************************
2145 * \brief Performs a local backwards C2R 1D FFT
2146 * \param fft_size Size of the transform in each dimension.
2147 * \param number_of_ffts Number of independent (batched) transforms.
2148 * \param transpose_in Whether the input array uses the transposed layout.
2149 * \param transpose_out Whether the output array uses the transposed layout.
2150 * \param leading_dimension_in Leading dimension of the input array.
2151 * \param leading_dimension_out Leading dimension of the output array.
2152 * \param grid_in Input grid buffer.
2153 * \param grid_out Output grid buffer.
2154 * \author Frederick Stein
2155 ******************************************************************************/
2156void fft_fftw_1d_c2r_local(const int fft_size, const int number_of_ffts,
2157 const bool transpose_in, const bool transpose_out,
2158 const int leading_dimension_in,
2159 const int leading_dimension_out,
2160 double complex *grid_in, double *grid_out) {
2161#if defined(__FFTW3)
2162 cp_assert(is_initialized);
2163 if (fft_size == 0 || number_of_ffts == 0)
2164 return;
2165 const bool in_place = (double *)grid_in == grid_out;
2166 const int number_of_threads = fft_team_size();
2167 // The self-written plans do not work with in-place transforms using the
2168 // transposed format
2169 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2170 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2171 const int block_size =
2172 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2173 const int number_of_full_blocks = number_of_ffts / block_size;
2174 const int total_number_of_blocks =
2175 number_of_full_blocks + (number_of_ffts % block_size != 0);
2176 fft_key_t key;
2177 get_key_1d_r2c(false, fft_size, block_size, transpose_in, transpose_out,
2178 leading_dimension_in, leading_dimension_out, 1, in_place,
2179 key);
2180 plan = lookup_plan_from_cache(key);
2181 if (plan == NULL) {
2182 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2183 plan = fft_fftw_create_1d_plan_r2c(key, buffer,
2184 in_place ? buffer : grid_out);
2185 fftw_free(buffer);
2186 }
2187 if (block_size * number_of_full_blocks != number_of_ffts) {
2188 const int block_size_last_thread = number_of_ffts % block_size;
2189 fft_key_t key;
2190 get_key_1d_r2c(false, fft_size, block_size_last_thread, transpose_in,
2191 transpose_out, leading_dimension_in, leading_dimension_out,
2192 1, in_place, key);
2193 plan_last_thread = lookup_plan_from_cache(key);
2194 if (plan_last_thread == NULL) {
2195 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2196 plan_last_thread = fft_fftw_create_1d_plan_r2c(
2197 key, buffer, in_place ? buffer : grid_out);
2198 fftw_free(buffer);
2199 }
2200 }
2201 const int offset_in =
2202 transpose_in ? block_size : block_size * leading_dimension_in;
2203 const int offset_out =
2204 transpose_out ? block_size : block_size * leading_dimension_out;
2205#pragma omp parallel default(none) \
2206 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2207 offset_out, plan_last_thread, total_number_of_blocks)
2208 {
2209 const int thread_id = omp_get_thread_num();
2210 if (thread_id < number_of_full_blocks) {
2211 fftw_execute_dft_c2r(*plan, grid_in + thread_id * offset_in,
2212 grid_out + thread_id * offset_out);
2213 } else if (thread_id + 1 == total_number_of_blocks) {
2214 fftw_execute_dft_c2r(*plan_last_thread, grid_in + thread_id * offset_in,
2215 grid_out + thread_id * offset_out);
2216 }
2217 }
2218 } else {
2219 fft_key_t key;
2220 get_key_1d_r2c(false, fft_size, number_of_ffts, transpose_in, transpose_out,
2221 leading_dimension_in, leading_dimension_out, fft_team_size(),
2222 grid_in == (double complex *)grid_out, key);
2223 fftw_plan *plan = lookup_plan_from_cache(key);
2224 if (plan == NULL) {
2225 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2226 plan = fft_fftw_create_1d_plan_r2c(key, buffer,
2227 in_place ? buffer : grid_out);
2228 fftw_free(buffer);
2229 }
2230 cp_assert(plan != NULL);
2231 fftw_execute_dft_c2r(*plan, grid_in, grid_out);
2232 }
2233#else
2234 (void)fft_size;
2235 (void)number_of_ffts;
2236 (void)grid_in;
2237 (void)grid_out;
2238 (void)transpose_in;
2239 (void)transpose_out;
2240 (void)leading_dimension_in;
2241 (void)leading_dimension_out;
2242 cp_assert(0 && "The grid library was not compiled with FFTW support.");
2243#endif
2244}
2245
2246/*******************************************************************************
2247 * \brief Performs a local C2C 2D FFT
2248 * \param direction FFT direction (forward or backward).
2249 * \param fft_size Size of the transform in each dimension.
2250 * \param number_of_ffts Number of independent (batched) transforms.
2251 * \param transpose_in Whether the input array uses the transposed layout.
2252 * \param transpose_out Whether the output array uses the transposed layout.
2253 * \param grid_in Input grid buffer.
2254 * \param grid_out Output grid buffer.
2255 * \author Frederick Stein
2256 ******************************************************************************/
2257void fft_fftw_2d_local(const bool direction, const int fft_size[2],
2258 const int number_of_ffts, const bool transpose_in,
2259 const bool transpose_out, double complex *grid_in,
2260 double complex *grid_out) {
2261#if defined(__FFTW3)
2262 cp_assert(is_initialized);
2263 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
2264 return;
2265 const bool in_place = grid_in == grid_out;
2266 const int number_of_threads = fft_team_size();
2267 if (fftw_planning_mode == FFTW_ESTIMATE) {
2268 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2269 const int block_size =
2270 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2271 const int number_of_full_blocks = number_of_ffts / block_size;
2272 const int total_number_of_blocks =
2273 number_of_full_blocks + (number_of_ffts % block_size != 0);
2274 fft_key_t key;
2275 get_key_2d(direction, fft_size, block_size, transpose_in, transpose_out,
2276 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
2277 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
2278 in_place, key);
2279 plan = lookup_plan_from_cache(key);
2280 if (plan == NULL) {
2281 double complex *buffer =
2282 fftw_alloc_complex(get_buffer_size_from_key(key));
2283 plan = fft_fftw_create_2d_plan(key, buffer, in_place ? buffer : grid_out);
2284 fftw_free(buffer);
2285 }
2286 if (block_size * number_of_full_blocks != number_of_ffts) {
2287 const int block_size_last_thread = number_of_ffts % block_size;
2288 fft_key_t key;
2289 get_key_2d(direction, fft_size, block_size_last_thread, transpose_in,
2290 transpose_out,
2291 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
2292 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
2293 in_place, key);
2294 plan_last_thread = lookup_plan_from_cache(key);
2295 if (plan_last_thread == NULL) {
2296 double complex *buffer =
2297 fftw_alloc_complex(get_buffer_size_from_key(key));
2298 plan_last_thread =
2299 fft_fftw_create_2d_plan(key, buffer, in_place ? buffer : grid_out);
2300 fftw_free(buffer);
2301 }
2302 }
2303 const int offset_in =
2304 transpose_in ? block_size : block_size * fft_size[0] * fft_size[1];
2305 const int offset_out =
2306 transpose_out ? block_size : block_size * fft_size[0] * fft_size[1];
2307#pragma omp parallel default(none) \
2308 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2309 offset_out, plan_last_thread, total_number_of_blocks)
2310 {
2311 const int thread_id = omp_get_thread_num();
2312 if (thread_id < number_of_full_blocks) {
2313 fftw_execute_dft(*plan, grid_in + thread_id * offset_in,
2314 grid_out + thread_id * offset_out);
2315 } else if (thread_id + 1 == total_number_of_blocks) {
2316 fftw_execute_dft(*plan_last_thread, grid_in + thread_id * offset_in,
2317 grid_out + thread_id * offset_out);
2318 }
2319 }
2320 } else {
2321 fft_key_t key;
2322 get_key_2d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
2323 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
2324 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1],
2325 fft_team_size(), grid_in == grid_out, key);
2326 fftw_plan *plan = lookup_plan_from_cache(key);
2327 if (plan == NULL) {
2328 double complex *buffer =
2329 fftw_alloc_complex(get_buffer_size_from_key(key));
2330 plan = fft_fftw_create_2d_plan(key, buffer,
2331 grid_in == grid_out ? buffer : grid_out);
2332 fftw_free(buffer);
2333 }
2334 fftw_execute_dft(*plan, grid_in, grid_out);
2335 }
2336#else
2337 (void)direction;
2338 (void)fft_size;
2339 (void)number_of_ffts;
2340 (void)grid_in;
2341 (void)grid_out;
2342 (void)transpose_in;
2343 (void)transpose_out;
2344 cp_assert(0 && "The grid library was not compiled with FFTW support.");
2345#endif
2346}
2347
2348/*******************************************************************************
2349 * \brief Performs a local forward R2C 2D FFT.
2350 * \param fft_size Size of the transform in each dimension.
2351 * \param number_of_ffts Number of independent (batched) transforms.
2352 * \param transpose_in Whether the input array uses the transposed layout.
2353 * \param transpose_out Whether the output array uses the transposed layout.
2354 * \param grid_in Input grid buffer.
2355 * \param grid_out Output grid buffer.
2356 * \author Frederick Stein
2357 ******************************************************************************/
2358void fft_fftw_2d_r2c_local(const int fft_size[2], const int number_of_ffts,
2359 const bool transpose_in, const bool transpose_out,
2360 double *grid_in, double complex *grid_out) {
2361#if defined(__FFTW3)
2362 cp_assert(is_initialized);
2363 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
2364 return;
2365 const bool in_place = grid_in == (double *)grid_out;
2366 const int number_of_threads = fft_team_size();
2367 // The self-written plans do not work with in-place transforms using the
2368 // transposed format
2369 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2370 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2371 const int block_size =
2372 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2373 const int number_of_full_blocks = number_of_ffts / block_size;
2374 const int total_number_of_blocks =
2375 number_of_full_blocks + (number_of_ffts % block_size != 0);
2376 fft_key_t key;
2377 get_key_2d_r2c(
2378 true, fft_size, block_size, transpose_in, transpose_out,
2379 transpose_in ? number_of_ffts
2380 : fft_size[0] *
2381 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
2382 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1), 1,
2383 in_place, key);
2384 plan = lookup_plan_from_cache(key);
2385 if (plan == NULL) {
2386 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2387 plan = fft_fftw_create_2d_plan_r2c(
2388 key, buffer, in_place ? buffer : (double *)grid_out);
2389 fftw_free(buffer);
2390 }
2391 if (block_size * number_of_full_blocks != number_of_ffts) {
2392 const int block_size_last_thread = number_of_ffts % block_size;
2393 fft_key_t key;
2394 get_key_2d_r2c(
2395 true, fft_size, block_size_last_thread, transpose_in, transpose_out,
2396 transpose_in ? number_of_ffts
2397 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2398 : fft_size[1]),
2399 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2400 1, in_place, key);
2401 plan_last_thread = lookup_plan_from_cache(key);
2402 if (plan_last_thread == NULL) {
2403 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2404 plan_last_thread = fft_fftw_create_2d_plan_r2c(
2405 key, buffer, in_place ? buffer : (double *)grid_out);
2406 fftw_free(buffer);
2407 }
2408 }
2409 const int offset_in =
2410 transpose_in ? block_size
2411 : block_size * fft_size[0] *
2412 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]);
2413 const int offset_out =
2414 transpose_out ? block_size
2415 : block_size * fft_size[0] * (fft_size[1] / 2 + 1);
2416#pragma omp parallel default(none) \
2417 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2418 offset_out, plan_last_thread, total_number_of_blocks)
2419 {
2420 const int thread_id = omp_get_thread_num();
2421 if (thread_id < number_of_full_blocks) {
2422 fftw_execute_dft_r2c(*plan, grid_in + thread_id * offset_in,
2423 grid_out + thread_id * offset_out);
2424 } else if (thread_id + 1 == total_number_of_blocks) {
2425 fftw_execute_dft_r2c(*plan_last_thread, grid_in + thread_id * offset_in,
2426 grid_out + thread_id * offset_out);
2427 }
2428 }
2429 } else {
2430 fft_key_t key;
2431 get_key_2d_r2c(
2432 true, fft_size, number_of_ffts, transpose_in, transpose_out,
2433 transpose_in ? number_of_ffts
2434 : fft_size[0] *
2435 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
2436 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2437 fft_team_size(), grid_in == (double *)grid_out, key);
2438 fftw_plan *plan = lookup_plan_from_cache(key);
2439 if (plan == NULL) {
2440 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2441 plan = fft_fftw_create_2d_plan_r2c(
2442 key, buffer,
2443 grid_in == (double *)grid_out ? buffer : (double *)grid_out);
2444 fftw_free(buffer);
2445 }
2446 fftw_execute_dft_r2c(*plan, grid_in, grid_out);
2447 }
2448#else
2449 (void)fft_size;
2450 (void)number_of_ffts;
2451 (void)grid_in;
2452 (void)grid_out;
2453 (void)transpose_in;
2454 (void)transpose_out;
2455 cp_assert(0 && "The grid library was not compiled with FFTW support.");
2456#endif
2457}
2458
2459/*******************************************************************************
2460 * \brief Performs a local backwards C2R 2D FFT.
2461 * \param fft_size Size of the transform in each dimension.
2462 * \param number_of_ffts Number of independent (batched) transforms.
2463 * \param transpose_in Whether the input array uses the transposed layout.
2464 * \param transpose_out Whether the output array uses the transposed layout.
2465 * \param grid_in Input grid buffer.
2466 * \param grid_out Output grid buffer.
2467 * \author Frederick Stein
2468 ******************************************************************************/
2469void fft_fftw_2d_c2r_local(const int fft_size[2], const int number_of_ffts,
2470 const bool transpose_in, const bool transpose_out,
2471 double complex *grid_in, double *grid_out) {
2472#if defined(__FFTW3)
2473 cp_assert(is_initialized);
2474 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
2475 return;
2476 const bool in_place = (double *)grid_in == grid_out;
2477 const int number_of_threads = fft_team_size();
2478 // The self-written plans do not work with in-place transforms using the
2479 // transposed format
2480 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2481 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2482 const int block_size =
2483 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2484 const int number_of_full_blocks = number_of_ffts / block_size;
2485 const int total_number_of_blocks =
2486 number_of_full_blocks + (number_of_ffts % block_size != 0);
2487 fft_key_t key;
2488 get_key_2d_r2c(
2489 false, fft_size, block_size, transpose_in, transpose_out,
2490 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2491 transpose_out ? number_of_ffts
2492 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2493 : fft_size[1]),
2494 1, in_place, key);
2495 plan = lookup_plan_from_cache(key);
2496 if (plan == NULL) {
2497 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2498 plan = fft_fftw_create_2d_plan_r2c(key, buffer,
2499 in_place ? buffer : grid_out);
2500 fftw_free(buffer);
2501 }
2502 if (block_size * number_of_full_blocks != number_of_ffts) {
2503 const int block_size_last_thread = number_of_ffts % block_size;
2504 fft_key_t key;
2505 get_key_2d_r2c(
2506 false, fft_size, block_size_last_thread, transpose_in, transpose_out,
2507 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2508 transpose_out ? number_of_ffts
2509 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2510 : fft_size[1]),
2511 1, in_place, key);
2512 plan_last_thread = lookup_plan_from_cache(key);
2513 if (plan_last_thread == NULL) {
2514 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2515 plan_last_thread = fft_fftw_create_2d_plan_r2c(
2516 key, buffer, in_place ? buffer : grid_out);
2517 fftw_free(buffer);
2518 }
2519 }
2520 const int offset_in =
2521 transpose_in ? block_size
2522 : block_size * fft_size[0] * (fft_size[1] / 2 + 1);
2523 const int offset_out =
2524 transpose_out
2525 ? block_size
2526 : block_size * fft_size[0] *
2527 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]);
2528#pragma omp parallel default(none) \
2529 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2530 offset_out, plan_last_thread, total_number_of_blocks)
2531 {
2532 const int thread_id = omp_get_thread_num();
2533 if (thread_id < number_of_full_blocks) {
2534 fftw_execute_dft_c2r(*plan, grid_in + thread_id * offset_in,
2535 grid_out + thread_id * offset_out);
2536 } else if (thread_id + 1 == total_number_of_blocks) {
2537 fftw_execute_dft_c2r(*plan_last_thread, grid_in + thread_id * offset_in,
2538 grid_out + thread_id * offset_out);
2539 }
2540 }
2541 } else {
2542 fft_key_t key;
2543 get_key_2d_r2c(
2544 false, fft_size, number_of_ffts, transpose_in, transpose_out,
2545 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2546 transpose_out ? number_of_ffts
2547 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2548 : fft_size[1]),
2549 fft_team_size(), grid_in == (double complex *)grid_out, key);
2550 fftw_plan *plan = lookup_plan_from_cache(key);
2551 if (plan == NULL) {
2552 double complex *buffer =
2553 fftw_alloc_complex(get_buffer_size_from_key(key));
2554 plan = fft_fftw_create_2d_plan_r2c(
2555 key, (double *)buffer,
2556 (double *)grid_in == grid_out ? (double *)buffer : grid_out);
2557 fftw_free(buffer);
2558 }
2559 fftw_execute_dft_c2r(*plan, grid_in, grid_out);
2560 }
2561#else
2562 (void)fft_size;
2563 (void)number_of_ffts;
2564 (void)grid_in;
2565 (void)grid_out;
2566 (void)transpose_in;
2567 (void)transpose_out;
2568 cp_assert(0 && "The grid library was not compiled with FFTW support.");
2569#endif
2570}
2571
2572/*******************************************************************************
2573 * \brief Performs a local C2C 3D FFT.
2574 * \param direction FFT direction (forward or backward).
2575 * \param fft_size Size of the transform in each dimension.
2576 * \param number_of_ffts Number of independent (batched) transforms.
2577 * \param transpose_in Whether the input array uses the transposed layout.
2578 * \param transpose_out Whether the output array uses the transposed layout.
2579 * \param grid_in Input grid buffer.
2580 * \param grid_out Output grid buffer.
2581 * \author Frederick Stein
2582 ******************************************************************************/
2583void fft_fftw_3d_local(const bool direction, const int fft_size[3],
2584 const int number_of_ffts, const bool transpose_in,
2585 const bool transpose_out, double complex *grid_in,
2586 double complex *grid_out) {
2587#if defined(__FFTW3)
2588 cp_assert(is_initialized);
2589 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
2590 number_of_ffts == 0)
2591 return;
2592 const bool in_place = grid_in == grid_out;
2593 if (number_of_ffts > 1 &&
2594 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2595 execute_threaded_3d_c2c(direction, fft_size, number_of_ffts, in_place,
2596 grid_in, grid_out);
2597 } else if (number_of_ffts == 1 &&
2598 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2599 execute_threaded_3d_c2c_1d(direction, fft_size, in_place, grid_in,
2600 grid_out);
2601 } else {
2602 fft_key_t key;
2603 get_key_3d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
2604 fft_team_size(), in_place, key);
2605 fftw_plan *plan = lookup_plan_from_cache(key);
2606 if (plan == NULL) {
2607 double complex *buffer =
2608 fftw_alloc_complex(get_buffer_size_from_key(key));
2609 plan = fft_fftw_create_3d_plan(key, buffer, in_place ? buffer : grid_out);
2610 fftw_free(buffer);
2611 }
2612 cp_assert(plan != NULL);
2613 cp_assert(grid_in != NULL);
2614 cp_assert(grid_out != NULL);
2615 fftw_execute_dft(*plan, grid_in, grid_out);
2616 }
2617#else
2618 (void)fft_size;
2619 (void)number_of_ffts;
2620 (void)transpose_in;
2621 (void)transpose_out;
2622 (void)grid_in;
2623 (void)grid_out;
2624 cp_assert(0 && "The grid library was not compiled with FFTW support.");
2625#endif
2626}
2627
2628/*******************************************************************************
2629 * \brief Performs a local forward R2C 3D FFT.
2630 * \param fft_size Size of the transform in each dimension.
2631 * \param number_of_ffts Number of independent (batched) transforms.
2632 * \param transpose_in Whether the input array uses the transposed layout.
2633 * \param transpose_out Whether the output array uses the transposed layout.
2634 * \param grid_in Input grid buffer.
2635 * \param grid_out Output grid buffer.
2636 * \author Frederick Stein
2637 ******************************************************************************/
2638void fft_fftw_3d_r2c_local(const int fft_size[3], const int number_of_ffts,
2639 const bool transpose_in, const bool transpose_out,
2640 double *grid_in, double complex *grid_out) {
2641#if defined(__FFTW3)
2642 cp_assert(is_initialized);
2643 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
2644 number_of_ffts == 0)
2645 return;
2646 const bool in_place = grid_in == (double *)grid_out;
2647 if (number_of_ffts > 1 &&
2648 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2649 execute_threaded_3d_r2c(true, fft_size, number_of_ffts, in_place, grid_in,
2650 (double *)grid_out);
2651 } else if (number_of_ffts == 1 &&
2652 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2653 execute_threaded_3d_r2c_1d(true, fft_size, in_place, grid_in, grid_out);
2654 } else {
2655 fft_key_t key;
2656 get_key_3d_r2c(true, fft_size, number_of_ffts, transpose_in, transpose_out,
2657 fft_team_size(), in_place, key);
2658 fftw_plan *plan = lookup_plan_from_cache(key);
2659 if (plan == NULL) {
2660 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2661 plan = fft_fftw_create_3d_plan_r2c(
2662 key, buffer, in_place ? buffer : (double *)grid_out);
2663 fftw_free(buffer);
2664 }
2665 cp_assert(plan != NULL);
2666 cp_assert(grid_in != NULL);
2667 cp_assert(grid_out != NULL);
2668 fftw_execute_dft_r2c(*plan, grid_in, grid_out);
2669 }
2670#else
2671 (void)fft_size;
2672 (void)number_of_ffts;
2673 (void)transpose_in;
2674 (void)transpose_out;
2675 (void)grid_in;
2676 (void)grid_out;
2677 cp_assert(0 && "The grid library was not compiled with FFTW support.");
2678#endif
2679}
2680
2681/*******************************************************************************
2682 * \brief Performs a local backwards R2C 3D FFT.
2683 * \param fft_size Size of the transform in each dimension.
2684 * \param number_of_ffts Number of independent (batched) transforms.
2685 * \param transpose_in Whether the input array uses the transposed layout.
2686 * \param transpose_out Whether the output array uses the transposed layout.
2687 * \param grid_in Input grid buffer.
2688 * \param grid_out Output grid buffer.
2689 * \author Frederick Stein
2690 ******************************************************************************/
2691void fft_fftw_3d_c2r_local(const int fft_size[3], const int number_of_ffts,
2692 const bool transpose_in, const bool transpose_out,
2693 double complex *grid_in, double *grid_out) {
2694#if defined(__FFTW3)
2695 cp_assert(is_initialized);
2696 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
2697 number_of_ffts == 0)
2698 return;
2699 const bool in_place = (double *)grid_in == grid_out;
2700 if (number_of_ffts > 1 &&
2701 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2702 execute_threaded_3d_r2c(false, fft_size, number_of_ffts, in_place,
2703 (double *)grid_in, grid_out);
2704 } else if (number_of_ffts == 1 &&
2705 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2706 execute_threaded_3d_r2c_1d(false, fft_size, in_place, grid_out, grid_in);
2707 } else {
2708 fft_key_t key;
2709 get_key_3d_r2c(false, fft_size, number_of_ffts, transpose_in, transpose_out,
2710 fft_team_size(), in_place, key);
2711 fftw_plan *plan = lookup_plan_from_cache(key);
2712 if (plan == NULL) {
2713 double complex *buffer =
2714 fftw_alloc_complex(get_buffer_size_from_key(key));
2715 plan = fft_fftw_create_3d_plan_r2c(
2716 key, (double *)buffer, in_place ? (double *)buffer : grid_out);
2717 fftw_free(buffer);
2718 }
2719 cp_assert(plan != NULL);
2720 cp_assert(grid_in != NULL);
2721 cp_assert(grid_out != NULL);
2722 fftw_execute_dft_c2r(*plan, grid_in, grid_out);
2723 }
2724#else
2725 (void)fft_size;
2726 (void)number_of_ffts;
2727 (void)transpose_in;
2728 (void)transpose_out;
2729 (void)grid_in;
2730 (void)grid_out;
2731 cp_assert(0 && "The grid library was not compiled with FFTW support.");
2732#endif
2733}
2734
2735// EOF
#define cp_assert(condition)
Definition cp_assert.h:22
cp_mpi_comm_t cp_mpi_get_comm_world(void)
Returns MPI_COMM_WORLD.
Definition cp_mpi.c:138
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
Definition cp_mpi.c:191
static int imax(int x, int y)
Returns the larger of two given integers (missing from the C standard).
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.
static bool has_guru_interface
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.
fftw_plan_type
@ FFT_PATIENT
@ FFT_MEASURE
@ FFT_EXHAUSTIVE
@ FFT_ESTIMATE
static int fft_team_size(void)
Returns the number of threads used for FFT planning. Outside an OpenMP parallel region the maximum nu...
Definition fft_utils.h:27
static void const int const int i
real(kind=dp), parameter, public half