Line data Source code
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 : ******************************************************************************/
28 : typedef 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.
48 : static cache_entry *caches = NULL;
49 : static int *cache_oldest_entries = NULL;
50 : static int number_of_caches = 0;
51 :
52 : static bool is_initialized = false;
53 :
54 : static omp_nest_lock_t cache_lock;
55 :
56 : static 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 : ******************************************************************************/
63 19627745 : static inline int current_cache_id(void) {
64 19627745 : int thread_id = omp_get_thread_num();
65 19627745 : if (thread_id < 0 || thread_id >= number_of_caches)
66 0 : thread_id = 0;
67 19627745 : 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 : ******************************************************************************/
75 19499790 : static inline cache_entry *current_cache(void) {
76 19499790 : 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 : ******************************************************************************/
84 127955 : static inline int *current_cache_oldest_entry(void) {
85 255910 : 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 : ******************************************************************************/
93 19371835 : static fftw_plan *lookup_plan_from_cache(const fft_key_t key) {
94 19371835 : cp_assert(is_initialized);
95 19371835 : omp_set_nest_lock(&cache_lock);
96 19371835 : cache_entry *cache = current_cache();
97 19371835 : fftw_plan *plan = NULL;
98 442936303 : for (int i = 0; i < FFTW_CACHE_SIZE; i++) {
99 442680393 : const int *x = cache[i].key;
100 442680393 : if (memcmp(key, x, sizeof(fft_key_t)) == 0) {
101 19115925 : plan = cache[i].plan;
102 19115925 : break;
103 : }
104 : }
105 19371835 : omp_unset_nest_lock(&cache_lock);
106 19371835 : 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 : ******************************************************************************/
115 127955 : static void add_plan_to_cache(const fft_key_t key, fftw_plan *plan) {
116 127955 : omp_set_nest_lock(&cache_lock);
117 127955 : cache_entry *cache = current_cache();
118 127955 : int *cache_oldest_entry = current_cache_oldest_entry();
119 127955 : const int i = *cache_oldest_entry;
120 127955 : *cache_oldest_entry = (*cache_oldest_entry + 1) % FFTW_CACHE_SIZE;
121 127955 : if (cache[i].plan != NULL) {
122 0 : fftw_destroy_plan(*cache[i].plan);
123 0 : free(cache[i].plan);
124 : }
125 127955 : memcpy(cache[i].key, key, sizeof(fft_key_t));
126 127955 : cache[i].plan = plan;
127 127955 : omp_unset_nest_lock(&cache_lock);
128 127955 : }
129 : #endif
130 :
131 : static 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 : ******************************************************************************/
139 11492 : bool is_guru_interface_available() {
140 11492 : fftw_iodim dims[1];
141 11492 : dims[0].n = 1;
142 11492 : dims[0].is = 1;
143 11492 : dims[0].os = 1;
144 11492 : fftw_iodim howmany_dims[2];
145 11492 : howmany_dims[0].n = 1;
146 11492 : howmany_dims[0].is = 1;
147 11492 : howmany_dims[0].os = 1;
148 11492 : howmany_dims[1].n = 1;
149 11492 : howmany_dims[1].is = 1;
150 11492 : howmany_dims[1].os = 1;
151 11492 : double complex buffer1;
152 11492 : fftw_plan plan =
153 11492 : fftw_plan_guru_dft(1, dims, 2, howmany_dims, &buffer1, &buffer1,
154 : FFTW_FORWARD, fftw_planning_mode);
155 :
156 11492 : if (plan != NULL) {
157 11492 : fftw_destroy_plan(plan);
158 11492 : 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 : ******************************************************************************/
175 678075 : static bool use_threaded_3d(const int fft_size[3], const bool transpose_in,
176 : const bool transpose_out, const bool in_place) {
177 675292 : return !transpose_in && !transpose_out && has_guru_interface &&
178 675196 : ((((fft_size[0] >= 256 || fft_size[1] >= 256 || fft_size[2] >= 256) &&
179 675196 : !in_place) ||
180 675196 : ((fft_size[0] >= 128 || fft_size[1] >= 128 || fft_size[2] >= 128) &&
181 0 : in_place)) &&
182 678075 : fft_team_size() > 1) &&
183 0 : (fftw_planning_mode == FFTW_ESTIMATE);
184 : }
185 : #endif
186 :
187 : #if defined(__FFTW3)
188 : fftw_plan *fft_fftw_create_3d_plan(const fft_key_t key, double complex *grid_in,
189 : double complex *grid_out);
190 : fftw_plan *fft_fftw_create_3d_plan_r2c(const fft_key_t key, double *grid_in,
191 : double *grid_out);
192 : fftw_plan *fft_fftw_create_guru_plan(const fft_key_t key,
193 : double complex *grid_in,
194 : double complex *grid_out);
195 : fftw_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 : ******************************************************************************/
208 : static void
209 0 : register_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 0 : const int number_of_threads = fft_team_size();
213 0 : const int block_size =
214 0 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
215 0 : const int number_of_full_blocks = number_of_ffts / block_size;
216 0 : fft_key_t key;
217 0 : get_key_3d(direction, fft_size, block_size, false, false, 1, in_place, key);
218 0 : if (lookup_plan_from_cache(key) == NULL)
219 0 : fft_fftw_create_3d_plan(key, grid_in, grid_out);
220 0 : if (block_size * number_of_full_blocks != number_of_ffts) {
221 0 : const int block_size_last_thread = number_of_ffts % block_size;
222 0 : fft_key_t key_last;
223 0 : get_key_3d(direction, fft_size, block_size_last_thread, false, false, 1,
224 : in_place, key_last);
225 0 : if (lookup_plan_from_cache(key_last) == NULL)
226 0 : fft_fftw_create_3d_plan(key_last, grid_in, grid_out);
227 : }
228 0 : }
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 : ******************************************************************************/
240 0 : static 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 0 : const int number_of_threads = fft_team_size();
246 0 : const int block_size =
247 0 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
248 0 : const int number_of_full_blocks = number_of_ffts / block_size;
249 0 : fft_key_t key;
250 0 : get_key_3d_r2c(direction, fft_size, block_size, false, false, 1, in_place,
251 : key);
252 0 : if (lookup_plan_from_cache(key) == NULL)
253 0 : fft_fftw_create_3d_plan_r2c(key, grid_in, grid_out);
254 0 : if (block_size * number_of_full_blocks != number_of_ffts) {
255 0 : const int block_size_last_thread = number_of_ffts % block_size;
256 0 : fft_key_t key_last;
257 0 : get_key_3d_r2c(direction, fft_size, block_size_last_thread, false, false, 1,
258 : in_place, key_last);
259 0 : if (lookup_plan_from_cache(key_last) == NULL)
260 0 : fft_fftw_create_3d_plan_r2c(key_last, grid_in, grid_out);
261 : }
262 0 : }
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 : ******************************************************************************/
274 0 : static 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 0 : const int number_of_threads = fft_team_size();
280 0 : const int block_size =
281 0 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
282 0 : const int number_of_full_blocks = number_of_ffts / block_size;
283 0 : const int total_number_of_blocks =
284 0 : number_of_full_blocks + (number_of_ffts % block_size != 0);
285 0 : const int volume = fft_size[0] * fft_size[1] * fft_size[2];
286 0 : #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 0 : }
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 : ******************************************************************************/
323 0 : static 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 0 : const int number_of_threads = fft_team_size();
328 0 : const int block_size =
329 0 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
330 0 : const int number_of_full_blocks = number_of_ffts / block_size;
331 0 : const int total_number_of_blocks =
332 0 : number_of_full_blocks + (number_of_ffts % block_size != 0);
333 0 : const int real_volume = fft_size[0] * fft_size[1] * fft_size[2];
334 0 : const int complex_volume = fft_size[0] * fft_size[1] * (fft_size[2] / 2 + 1);
335 0 : const int real_offset = in_place ? 2 * complex_volume : real_volume;
336 0 : #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 0 : }
379 :
380 : /*******************************************************************************
381 : * \brief Register the thread-split 1D chain for a single real/complex 3D FFT.
382 : ******************************************************************************/
383 0 : static 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 0 : double complex *buffer = in_place
388 : ? grid_complex
389 0 : : fftw_alloc_complex(fft_size[0] * fft_size[1] *
390 0 : (fft_size[2] / 2 + 1));
391 0 : double complex *input_c = direction ? buffer : grid_complex;
392 0 : double complex *output_c = direction ? grid_complex : buffer;
393 0 : const int number_of_threads = fft_team_size();
394 0 : int block_sizes[3];
395 0 : fftw_plan *plans[3], *plans_last_thread[3];
396 0 : for (int stage = 0; stage < 3; stage++) {
397 0 : plans[stage] = NULL;
398 0 : plans_last_thread[stage] = NULL;
399 0 : const int extent = stage == 0 ? fft_size[1] * (fft_size[2] / 2 + 1)
400 0 : : stage == 1 ? fft_size[0]
401 0 : : fft_size[0] * fft_size[1];
402 0 : block_sizes[stage] = (extent + number_of_threads - 1) / number_of_threads;
403 0 : const bool needs_tail = block_sizes[stage] * number_of_threads != extent;
404 0 : const int tail = needs_tail ? extent % block_sizes[stage] : 0;
405 0 : if (stage == 0) {
406 0 : const int n = fft_size[1] * (fft_size[2] / 2 + 1);
407 0 : fft_iodim dim = {.n = fft_size[0], .is = n, .os = n};
408 0 : fft_iodim howmany = {.n = block_sizes[0], .is = 1, .os = 1};
409 0 : fft_key_t key;
410 0 : get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
411 0 : plans[0] = lookup_plan_from_cache(key);
412 0 : if (plans[0] == NULL)
413 0 : plans[0] = fft_fftw_create_guru_plan(key, input_c, output_c);
414 0 : if (needs_tail) {
415 0 : howmany.n = tail;
416 0 : get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
417 0 : plans_last_thread[0] = lookup_plan_from_cache(key);
418 0 : if (plans_last_thread[0] == NULL)
419 0 : plans_last_thread[0] =
420 0 : fft_fftw_create_guru_plan(key, input_c, output_c);
421 : }
422 0 : } else if (stage == 1) {
423 0 : const int half = fft_size[2] / 2 + 1;
424 0 : fft_iodim dim = {.n = fft_size[1], .is = half, .os = half};
425 0 : fft_iodim howmany[2] = {{.n = block_sizes[1],
426 0 : .is = fft_size[1] * half,
427 : .os = fft_size[1] * half},
428 : {.n = half, .is = 1, .os = 1}};
429 0 : fft_key_t key;
430 0 : get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
431 0 : plans[1] = lookup_plan_from_cache(key);
432 0 : if (plans[1] == NULL)
433 0 : plans[1] = fft_fftw_create_guru_plan(key, input_c, output_c);
434 0 : if (needs_tail) {
435 0 : howmany[0].n = tail;
436 0 : get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
437 0 : plans_last_thread[1] = lookup_plan_from_cache(key);
438 0 : if (plans_last_thread[1] == NULL)
439 0 : plans_last_thread[1] =
440 0 : fft_fftw_create_guru_plan(key, input_c, output_c);
441 : }
442 : } else {
443 0 : const int half = fft_size[2] / 2 + 1;
444 0 : fft_iodim dim = {.n = fft_size[2], .is = 1, .os = 1};
445 0 : fft_iodim howmany = {
446 0 : .n = block_sizes[2],
447 0 : .is = direction ? (in_place ? 2 * half : fft_size[2]) : half,
448 0 : .os = direction ? half : (in_place ? 2 * half : fft_size[2])};
449 0 : fft_key_t key;
450 0 : get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
451 0 : plans[2] = lookup_plan_from_cache(key);
452 0 : if (plans[2] == NULL)
453 0 : plans[2] = fft_fftw_create_guru_plan_r2c(key, grid_real,
454 : (double *)grid_complex);
455 0 : if (needs_tail) {
456 0 : howmany.n = tail;
457 0 : get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
458 0 : plans_last_thread[2] = lookup_plan_from_cache(key);
459 0 : if (plans_last_thread[2] == NULL)
460 0 : plans_last_thread[2] = fft_fftw_create_guru_plan_r2c(
461 : key, grid_real, (double *)grid_complex);
462 : }
463 : }
464 : }
465 0 : if (!in_place)
466 0 : fftw_free(buffer);
467 0 : }
468 :
469 : /*******************************************************************************
470 : * \brief Execute the thread-split 1D chain for a single real/complex 3D FFT.
471 : ******************************************************************************/
472 0 : static 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 0 : const int half = fft_size[2] / 2 + 1;
477 0 : double complex *buffer =
478 : direction && in_place
479 : ? grid_complex
480 0 : : fftw_alloc_complex(fft_size[0] * fft_size[1] * half);
481 0 : double complex *input_c = direction ? buffer : grid_complex;
482 : double complex *output_c = direction ? grid_complex : buffer;
483 0 : const int number_of_threads = fft_team_size();
484 0 : int block_sizes[3];
485 0 : int number_of_full_blocks[3] = {0, 0, 0};
486 0 : int total_number_of_blocks[3] = {0, 0, 0};
487 0 : fftw_plan *plans[3], *plans_last_thread[3];
488 0 : for (int stage = 0; stage < 3; stage++) {
489 0 : plans[stage] = NULL;
490 0 : plans_last_thread[stage] = NULL;
491 0 : const int extent = stage == 0 ? fft_size[1] * half
492 0 : : stage == 1 ? fft_size[0]
493 0 : : fft_size[0] * fft_size[1];
494 0 : block_sizes[stage] = (extent + number_of_threads - 1) / number_of_threads;
495 0 : number_of_full_blocks[stage] = extent / block_sizes[stage];
496 0 : total_number_of_blocks[stage] =
497 0 : (extent + block_sizes[stage] - 1) / block_sizes[stage];
498 0 : const bool needs_tail = block_sizes[stage] * number_of_threads != extent;
499 0 : const int tail = needs_tail ? extent % block_sizes[stage] : 0;
500 0 : if (stage == 0) {
501 0 : const int n = fft_size[1] * half;
502 0 : fft_iodim dim = {.n = fft_size[0], .is = n, .os = n};
503 0 : fft_iodim howmany = {.n = block_sizes[0], .is = 1, .os = 1};
504 0 : fft_key_t key;
505 0 : get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
506 0 : plans[0] = lookup_plan_from_cache(key);
507 0 : if (plans[0] == NULL) {
508 0 : double complex *tmp = fftw_alloc_complex(get_buffer_size_from_key(key));
509 0 : plans[0] = fft_fftw_create_guru_plan(key, tmp, output_c);
510 0 : fftw_free(tmp);
511 : }
512 0 : if (needs_tail) {
513 0 : howmany.n = tail;
514 0 : get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
515 0 : plans_last_thread[0] = lookup_plan_from_cache(key);
516 0 : if (plans_last_thread[0] == NULL) {
517 0 : double complex *tmp =
518 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
519 0 : plans_last_thread[0] = fft_fftw_create_guru_plan(key, tmp, output_c);
520 0 : fftw_free(tmp);
521 : }
522 : }
523 0 : } else if (stage == 1) {
524 0 : fft_iodim dim = {.n = fft_size[1], .is = half, .os = half};
525 0 : fft_iodim howmany[2] = {{.n = block_sizes[1],
526 0 : .is = fft_size[1] * half,
527 : .os = fft_size[1] * half},
528 : {.n = half, .is = 1, .os = 1}};
529 0 : fft_key_t key;
530 0 : get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
531 0 : plans[1] = lookup_plan_from_cache(key);
532 0 : if (plans[1] == NULL) {
533 0 : double complex *tmp = fftw_alloc_complex(get_buffer_size_from_key(key));
534 0 : plans[1] = fft_fftw_create_guru_plan(key, tmp, output_c);
535 0 : fftw_free(tmp);
536 : }
537 0 : if (needs_tail) {
538 0 : howmany[0].n = tail;
539 0 : get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
540 0 : plans_last_thread[1] = lookup_plan_from_cache(key);
541 0 : if (plans_last_thread[1] == NULL) {
542 0 : double complex *tmp =
543 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
544 0 : plans_last_thread[1] = fft_fftw_create_guru_plan(key, tmp, output_c);
545 0 : fftw_free(tmp);
546 : }
547 : }
548 : } else {
549 0 : fft_iodim dim = {.n = fft_size[2], .is = 1, .os = 1};
550 0 : fft_iodim howmany = {
551 0 : .n = block_sizes[2],
552 0 : .is = direction ? (in_place ? 2 * half : fft_size[2]) : half,
553 0 : .os = direction ? half : (in_place ? 2 * half : fft_size[2])};
554 0 : fft_key_t key;
555 0 : get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
556 0 : plans[2] = lookup_plan_from_cache(key);
557 0 : if (plans[2] == NULL) {
558 0 : double *tmp = fftw_alloc_real(2 * get_buffer_size_from_key(key));
559 0 : plans[2] = fft_fftw_create_guru_plan_r2c(key, tmp, (double *)output_c);
560 0 : fftw_free(tmp);
561 : }
562 0 : if (needs_tail) {
563 0 : howmany.n = tail;
564 0 : get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
565 0 : plans_last_thread[2] = lookup_plan_from_cache(key);
566 0 : if (plans_last_thread[2] == NULL) {
567 0 : double *tmp = fftw_alloc_real(2 * get_buffer_size_from_key(key));
568 0 : plans_last_thread[2] =
569 0 : fft_fftw_create_guru_plan_r2c(key, tmp, (double *)output_c);
570 0 : fftw_free(tmp);
571 : }
572 : }
573 : }
574 : }
575 0 : const int real_stride =
576 0 : direction ? (in_place ? 2 * half : fft_size[2]) : half;
577 0 : const int complex_stride =
578 0 : direction ? half : (in_place ? 2 * half : fft_size[2]);
579 0 : #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 0 : if (!(direction && in_place))
629 0 : fftw_free(buffer);
630 0 : }
631 :
632 : /*******************************************************************************
633 : * \brief Acquire the plans for a thread-split single C2C 3D FFT.
634 : ******************************************************************************/
635 0 : static 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 0 : const int number_of_threads = fft_team_size();
641 0 : for (int stage = 0; stage < 3; stage++) {
642 0 : plans[stage] = NULL;
643 0 : plans_last_thread[stage] = NULL;
644 0 : const int extent = stage == 0 ? fft_size[1] * fft_size[2]
645 0 : : stage == 1 ? fft_size[0]
646 0 : : fft_size[0] * fft_size[1];
647 0 : block_sizes[stage] = (extent + number_of_threads - 1) / number_of_threads;
648 0 : number_of_full_blocks[stage] = extent / block_sizes[stage];
649 0 : total_number_of_blocks[stage] =
650 0 : (extent + block_sizes[stage] - 1) / block_sizes[stage];
651 0 : const bool needs_tail = block_sizes[stage] * number_of_threads != extent;
652 0 : const int tail = needs_tail ? extent % block_sizes[stage] : 0;
653 :
654 0 : fft_iodim dim[1];
655 0 : fft_iodim howmany[2];
656 0 : int rank = 1;
657 0 : int howmany_rank = 1;
658 0 : if (stage == 0) {
659 0 : const int n = fft_size[1] * fft_size[2];
660 0 : dim[0] = (fft_iodim){.n = fft_size[0], .is = n, .os = n};
661 0 : howmany[0] = (fft_iodim){.n = block_sizes[0], .is = 1, .os = 1};
662 0 : } else if (stage == 1) {
663 0 : dim[0] =
664 0 : (fft_iodim){.n = fft_size[1], .is = fft_size[2], .os = fft_size[2]};
665 0 : howmany[0] = (fft_iodim){.n = block_sizes[1],
666 0 : .is = fft_size[1] * fft_size[2],
667 : .os = fft_size[1] * fft_size[2]};
668 0 : howmany[1] = (fft_iodim){.n = fft_size[2], .is = 1, .os = 1};
669 0 : howmany_rank = 2;
670 : } else {
671 0 : dim[0] = (fft_iodim){.n = fft_size[2], .is = 1, .os = 1};
672 0 : howmany[0] = (fft_iodim){
673 0 : .n = block_sizes[2], .is = fft_size[2], .os = fft_size[2]};
674 : }
675 :
676 0 : fft_key_t key;
677 0 : get_key_guru(direction, rank, dim, howmany_rank, howmany, 1, in_place, key);
678 0 : plans[stage] = lookup_plan_from_cache(key);
679 0 : if (plans[stage] == NULL) {
680 0 : if (for_execution) {
681 0 : double complex *buffer =
682 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
683 0 : plans[stage] = fft_fftw_create_guru_plan(key, buffer,
684 : in_place ? buffer : grid_out);
685 0 : fftw_free(buffer);
686 : } else {
687 0 : plans[stage] = fft_fftw_create_guru_plan(key, grid_in, grid_out);
688 : }
689 : }
690 0 : if (needs_tail) {
691 0 : howmany[0].n = tail;
692 0 : get_key_guru(direction, rank, dim, howmany_rank, howmany, 1, in_place,
693 : key);
694 0 : plans_last_thread[stage] = lookup_plan_from_cache(key);
695 0 : if (plans_last_thread[stage] == NULL) {
696 0 : if (for_execution) {
697 0 : double complex *buffer =
698 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
699 0 : plans_last_thread[stage] = fft_fftw_create_guru_plan(
700 : key, buffer, in_place ? buffer : grid_out);
701 0 : fftw_free(buffer);
702 : } else {
703 0 : plans_last_thread[stage] =
704 0 : fft_fftw_create_guru_plan(key, grid_in, grid_out);
705 : }
706 : }
707 : }
708 : }
709 0 : }
710 :
711 : /*******************************************************************************
712 : * \brief Register the thread-split 1D chain for a single C2C 3D FFT.
713 : ******************************************************************************/
714 0 : static 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 0 : int block_sizes[3];
720 0 : int number_of_full_blocks[3];
721 0 : int total_number_of_blocks[3];
722 0 : fftw_plan *plans[3], *plans_last_thread[3];
723 0 : 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 0 : }
727 :
728 : /*******************************************************************************
729 : * \brief Execute the thread-split 1D chain for a single C2C 3D FFT.
730 : ******************************************************************************/
731 0 : static 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 0 : int block_sizes[3];
737 0 : int number_of_full_blocks[3];
738 0 : int total_number_of_blocks[3];
739 0 : fftw_plan *plans[3], *plans_last_thread[3];
740 0 : 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 0 : #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 0 : }
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 : ******************************************************************************/
787 11496 : int 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 11496 : cp_assert(omp_get_num_threads() == 1);
791 11496 : if (is_initialized) {
792 : return 0;
793 : }
794 22992 : number_of_caches =
795 11496 : imax(1, omp_get_max_threads()); // NOLINT: per-thread caches
796 11496 : caches =
797 11496 : calloc((size_t)number_of_caches * FFTW_CACHE_SIZE, sizeof(cache_entry));
798 11496 : cache_oldest_entries = calloc((size_t)number_of_caches, sizeof(int));
799 11496 : cp_assert(caches != NULL && cache_oldest_entries != NULL);
800 11496 : omp_init_nest_lock(&cache_lock);
801 :
802 11496 : fftw_planning_mode = fftw_planning_flag;
803 11496 : switch (fftw_planning_flag) {
804 11484 : case FFT_ESTIMATE:
805 11484 : fftw_planning_mode = FFTW_ESTIMATE;
806 11484 : break;
807 6 : case FFT_MEASURE:
808 6 : fftw_planning_mode = FFTW_MEASURE;
809 6 : break;
810 4 : case FFT_PATIENT:
811 4 : fftw_planning_mode = FFTW_PATIENT;
812 4 : break;
813 2 : case FFT_EXHAUSTIVE:
814 2 : fftw_planning_mode = FFTW_EXHAUSTIVE;
815 2 : break;
816 : default:
817 0 : cp_assert(0 && "Unknown FFTW planning flag.");
818 : }
819 :
820 : // We need a threaded library!
821 11496 : const int status = fftw_init_threads();
822 11496 : if (status == 0)
823 : return 1;
824 :
825 11496 : has_guru_interface = use_guru_interface;
826 11496 : if (has_guru_interface) {
827 11492 : has_guru_interface = is_guru_interface_available();
828 : }
829 :
830 : // Export wisdom after intializing the library to ensure correct threading
831 : // etc.
832 11496 : if (wisdom_file != NULL) {
833 11488 : const bool is_print_rank = cp_mpi_comm_rank(cp_mpi_get_comm_world()) == 0;
834 11488 : const int error = fftw_import_wisdom_from_filename(wisdom_file);
835 11488 : if (false && error != 0 && is_print_rank)
836 : fprintf(stderr,
837 : "Importing wisdom failed! Maybe the file does not exist.");
838 : }
839 :
840 11496 : is_initialized = true;
841 11496 : 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 : ******************************************************************************/
856 11747 : void fft_fftw_finalize_lib(const char *wisdom_file) {
857 : #if defined(__FFTW3)
858 11747 : cp_assert(omp_get_num_threads() == 1);
859 11747 : if (!is_initialized) {
860 : return;
861 : }
862 22992 : for (int t = 0; t < number_of_caches; t++) {
863 11496 : cache_entry *cache = &caches[t * FFTW_CACHE_SIZE];
864 2954472 : for (int i = 0; i < FFTW_CACHE_SIZE; i++) {
865 2942976 : if (cache[i].plan != NULL) {
866 127955 : fftw_destroy_plan(*cache[i].plan);
867 127955 : free(cache[i].plan);
868 : }
869 : }
870 : }
871 11496 : free(caches);
872 11496 : caches = NULL;
873 11496 : free(cache_oldest_entries);
874 11496 : cache_oldest_entries = NULL;
875 11496 : number_of_caches = 0;
876 11496 : omp_destroy_nest_lock(&cache_lock);
877 : // Export wisdom before finalizing the library to ensure storing the correct
878 : // threading etc.
879 11496 : if (wisdom_file != NULL) {
880 11488 : const int error = fftw_export_wisdom_to_filename(wisdom_file);
881 11488 : 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 11496 : is_initialized = false;
886 11496 : fftw_planning_mode = -1;
887 11496 : 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 : ******************************************************************************/
898 11919 : bool fft_fftw_lib_has_guru_interface() { return has_guru_interface; }
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 : ******************************************************************************/
906 176 : void fft_fftw_allocate_double(const int length, double **buffer) {
907 : #if defined(__FFTW3)
908 176 : cp_assert(is_initialized);
909 176 : cp_assert(buffer != NULL);
910 176 : cp_assert(*buffer == NULL);
911 176 : *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 176 : }
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 : ******************************************************************************/
925 228114 : void fft_fftw_allocate_complex(const int length, double complex **buffer) {
926 : #if defined(__FFTW3)
927 228114 : cp_assert(is_initialized);
928 228114 : cp_assert(buffer != NULL);
929 228114 : cp_assert(*buffer == NULL);
930 228114 : *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 228114 : }
937 :
938 : /*******************************************************************************
939 : * \brief Free buffer of type double.
940 : * \param buffer Buffer pointer.
941 : * \author Frederick Stein
942 : ******************************************************************************/
943 176 : void fft_fftw_free_double(double *buffer) {
944 : #if defined(__FFTW3)
945 176 : cp_assert(is_initialized);
946 176 : fftw_free(buffer);
947 : #else
948 : (void)buffer;
949 : cp_assert(0 && "The grid library was not compiled with FFTW support.");
950 : #endif
951 176 : }
952 :
953 : /*******************************************************************************
954 : * \brief Free buffer of type double complex.
955 : * \param buffer Buffer pointer.
956 : * \author Frederick Stein
957 : ******************************************************************************/
958 228114 : void fft_fftw_free_complex(double complex *buffer) {
959 : #if defined(__FFTW3)
960 228114 : cp_assert(is_initialized);
961 228114 : fftw_free(buffer);
962 : #else
963 : (void)buffer;
964 : cp_assert(0 && "The grid library was not compiled with FFTW support.");
965 : #endif
966 228114 : }
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 : ******************************************************************************/
977 64329 : fftw_plan *fft_fftw_create_1d_plan(const fft_key_t key, double complex *grid_in,
978 : double complex *grid_out) {
979 64329 : omp_set_nest_lock(&cache_lock);
980 64329 : fftw_plan *cached_plan = lookup_plan_from_cache(key);
981 64329 : if (cached_plan != NULL) {
982 0 : omp_unset_nest_lock(&cache_lock);
983 0 : return cached_plan;
984 : }
985 64329 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
986 64329 : bool direction, inplace;
987 64329 : int fft_size[3], inembed[3], onembed[3];
988 64329 : 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 64329 : cp_assert(rank == 1);
992 64329 : fftw_plan_with_nthreads(number_of_threads);
993 64329 : fftw_plan *plan = malloc(sizeof(fftw_plan));
994 64329 : if (grid_in == grid_out) {
995 40 : cp_assert(inplace);
996 40 : cp_assert(istride == ostride);
997 40 : cp_assert(idist == odist);
998 : } else {
999 64289 : cp_assert(!inplace);
1000 : }
1001 64329 : *plan = fftw_plan_many_dft(rank, fft_size, number_of_ffts, grid_in, inembed,
1002 : istride, idist, grid_out, onembed, ostride, odist,
1003 64329 : direction ? FFTW_FORWARD : FFTW_BACKWARD,
1004 : fftw_planning_mode);
1005 64329 : cp_assert(plan != NULL);
1006 64329 : add_plan_to_cache(key, plan);
1007 64329 : omp_unset_nest_lock(&cache_lock);
1008 64329 : 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 : ******************************************************************************/
1017 112 : fftw_plan *fft_fftw_create_1d_plan_r2c(const fft_key_t key, double *grid_in,
1018 : double *grid_out) {
1019 112 : omp_set_nest_lock(&cache_lock);
1020 112 : fftw_plan *cached_plan = lookup_plan_from_cache(key);
1021 112 : if (cached_plan != NULL) {
1022 0 : omp_unset_nest_lock(&cache_lock);
1023 0 : return cached_plan;
1024 : }
1025 112 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1026 112 : bool direction, inplace;
1027 112 : int fft_size[3], inembed[3], onembed[3];
1028 112 : 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 112 : cp_assert(rank == 1);
1032 112 : fftw_plan_with_nthreads(number_of_threads);
1033 112 : fftw_plan *plan = malloc(sizeof(fftw_plan));
1034 112 : if (grid_in == grid_out) {
1035 32 : if (istride == 1) {
1036 : // Check whether the arrays are correctly padded
1037 16 : if (direction) {
1038 8 : cp_assert(idist == 2 * odist);
1039 : } else {
1040 8 : cp_assert(odist == 2 * idist);
1041 : }
1042 : }
1043 32 : cp_assert(istride == ostride);
1044 : }
1045 112 : if (direction) {
1046 112 : *plan =
1047 56 : 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 56 : *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 112 : cp_assert(plan != NULL);
1059 112 : add_plan_to_cache(key, plan);
1060 112 : omp_unset_nest_lock(&cache_lock);
1061 112 : 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 : ******************************************************************************/
1071 57252 : fftw_plan *fft_fftw_create_2d_plan(const fft_key_t key, double complex *grid_in,
1072 : double complex *grid_out) {
1073 57252 : omp_set_nest_lock(&cache_lock);
1074 57252 : fftw_plan *cached_plan = lookup_plan_from_cache(key);
1075 57252 : if (cached_plan != NULL) {
1076 0 : omp_unset_nest_lock(&cache_lock);
1077 0 : return cached_plan;
1078 : }
1079 57252 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1080 57252 : bool direction, inplace;
1081 57252 : int fft_size[3], inembed[3], onembed[3];
1082 57252 : 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 57252 : cp_assert(rank == 2);
1086 57252 : fftw_plan_with_nthreads(number_of_threads);
1087 57252 : fftw_plan *plan = malloc(sizeof(fftw_plan));
1088 57252 : if (grid_in == grid_out) {
1089 : // Array strides need to match for in-place FFTs
1090 40 : cp_assert(istride == ostride);
1091 40 : cp_assert(idist == odist);
1092 : }
1093 57252 : *plan = fftw_plan_many_dft(rank, fft_size, number_of_ffts, grid_in, inembed,
1094 : istride, idist, grid_out, onembed, ostride, odist,
1095 57252 : direction ? FFTW_FORWARD : FFTW_BACKWARD,
1096 : fftw_planning_mode);
1097 57252 : cp_assert(plan != NULL);
1098 57252 : add_plan_to_cache(key, plan);
1099 57252 : omp_unset_nest_lock(&cache_lock);
1100 57252 : 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 : ******************************************************************************/
1110 112 : fftw_plan *fft_fftw_create_2d_plan_r2c(const fft_key_t key, double *grid_in,
1111 : double *grid_out) {
1112 112 : omp_set_nest_lock(&cache_lock);
1113 112 : fftw_plan *cached_plan = lookup_plan_from_cache(key);
1114 112 : if (cached_plan != NULL) {
1115 0 : omp_unset_nest_lock(&cache_lock);
1116 0 : return cached_plan;
1117 : }
1118 112 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1119 112 : bool direction, inplace;
1120 112 : int fft_size[3], inembed[3], onembed[3];
1121 112 : 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 112 : cp_assert(rank == 2);
1125 112 : 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 112 : fftw_plan *plan = malloc(sizeof(fftw_plan));
1129 112 : if (grid_in == grid_out) {
1130 32 : if (istride == 1) {
1131 : // Check whether the arrays are correctly padded
1132 16 : if (direction) {
1133 8 : cp_assert(idist == 2 * odist);
1134 : } else {
1135 8 : cp_assert(odist == 2 * idist);
1136 : }
1137 : }
1138 32 : cp_assert(istride == ostride);
1139 : }
1140 112 : if (direction) {
1141 112 : *plan =
1142 56 : 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 56 : *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 112 : cp_assert(plan != NULL);
1154 112 : add_plan_to_cache(key, plan);
1155 112 : omp_unset_nest_lock(&cache_lock);
1156 112 : 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 : ******************************************************************************/
1166 6070 : fftw_plan *fft_fftw_create_3d_plan(const fft_key_t key, double complex *grid_in,
1167 : double complex *grid_out) {
1168 6070 : omp_set_nest_lock(&cache_lock);
1169 6070 : fftw_plan *cached_plan = lookup_plan_from_cache(key);
1170 6070 : if (cached_plan != NULL) {
1171 0 : omp_unset_nest_lock(&cache_lock);
1172 0 : return cached_plan;
1173 : }
1174 6070 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1175 6070 : bool direction, inplace;
1176 6070 : int fft_size[3], inembed[3], onembed[3];
1177 6070 : 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 6070 : cp_assert(rank == 3);
1181 6070 : fftw_plan_with_nthreads(number_of_threads);
1182 6070 : fftw_plan *plan = malloc(sizeof(fftw_plan));
1183 6070 : *plan = fftw_plan_many_dft(rank, fft_size, number_of_ffts, grid_in, inembed,
1184 : istride, idist, grid_out, onembed, ostride, odist,
1185 6070 : direction ? FFTW_FORWARD : FFTW_BACKWARD,
1186 : fftw_planning_mode);
1187 6070 : add_plan_to_cache(key, plan);
1188 6070 : cp_assert(plan != NULL);
1189 6070 : omp_unset_nest_lock(&cache_lock);
1190 6070 : 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 : ******************************************************************************/
1200 80 : fftw_plan *fft_fftw_create_3d_plan_r2c(const fft_key_t key, double *grid_in,
1201 : double *grid_out) {
1202 80 : omp_set_nest_lock(&cache_lock);
1203 80 : fftw_plan *cached_plan = lookup_plan_from_cache(key);
1204 80 : if (cached_plan != NULL) {
1205 0 : omp_unset_nest_lock(&cache_lock);
1206 0 : return cached_plan;
1207 : }
1208 80 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1209 80 : bool direction, inplace;
1210 80 : int fft_size[3], inembed[3], onembed[3];
1211 80 : 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 80 : cp_assert(rank == 3);
1215 80 : fftw_plan_with_nthreads(number_of_threads);
1216 80 : fftw_plan *plan = malloc(sizeof(fftw_plan));
1217 80 : if (direction) {
1218 40 : *plan =
1219 40 : 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 40 : *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 80 : add_plan_to_cache(key, plan);
1231 80 : cp_assert(plan != NULL);
1232 80 : omp_unset_nest_lock(&cache_lock);
1233 80 : 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 : ******************************************************************************/
1243 0 : fftw_plan *fft_fftw_create_guru_plan(const fft_key_t key,
1244 : double complex *grid_in,
1245 : double complex *grid_out) {
1246 0 : cp_assert(has_guru_interface);
1247 0 : omp_set_nest_lock(&cache_lock);
1248 0 : fftw_plan *cached_plan = lookup_plan_from_cache(key);
1249 0 : if (cached_plan != NULL) {
1250 0 : omp_unset_nest_lock(&cache_lock);
1251 0 : return cached_plan;
1252 : }
1253 0 : bool direction, inplace;
1254 0 : int rank, howmany_rank, number_of_threads;
1255 0 : fft_iodim dims[3], howmany_dims[3];
1256 0 : fetch_data_from_key_guru(key, &direction, &rank, dims, &howmany_rank,
1257 : howmany_dims, &number_of_threads, &inplace);
1258 :
1259 0 : fftw_plan_with_nthreads(number_of_threads);
1260 0 : fftw_plan *plan = malloc(sizeof(fftw_plan));
1261 0 : if ((double complex *)grid_in == grid_out) {
1262 0 : cp_assert(inplace);
1263 0 : for (int r = 0; r < rank; r++)
1264 0 : cp_assert(dims[r].is == dims[r].os);
1265 0 : for (int r = 0; r < howmany_rank; r++)
1266 0 : cp_assert(howmany_dims[r].is == howmany_dims[r].os);
1267 : } else {
1268 0 : cp_assert(!inplace);
1269 : }
1270 0 : *plan = fftw_plan_guru_dft(rank, dims, howmany_rank, howmany_dims, grid_in,
1271 0 : grid_out, direction ? FFTW_FORWARD : FFTW_BACKWARD,
1272 : fftw_planning_mode);
1273 0 : add_plan_to_cache(key, plan);
1274 0 : cp_assert(plan != NULL);
1275 0 : omp_unset_nest_lock(&cache_lock);
1276 0 : 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 : ******************************************************************************/
1286 0 : fftw_plan *fft_fftw_create_guru_plan_r2c(const fft_key_t key, double *grid_in,
1287 : double *grid_out) {
1288 0 : cp_assert(has_guru_interface);
1289 0 : omp_set_nest_lock(&cache_lock);
1290 0 : fftw_plan *cached_plan = lookup_plan_from_cache(key);
1291 0 : if (cached_plan != NULL) {
1292 0 : omp_unset_nest_lock(&cache_lock);
1293 0 : return cached_plan;
1294 : }
1295 0 : bool direction, inplace;
1296 0 : int rank, howmany_rank, number_of_threads;
1297 0 : fft_iodim dims[3], howmany_dims[3];
1298 0 : fetch_data_from_key_guru(key, &direction, &rank, dims, &howmany_rank,
1299 : howmany_dims, &number_of_threads, &inplace);
1300 :
1301 0 : fftw_plan_with_nthreads(number_of_threads);
1302 0 : fftw_plan *plan = malloc(sizeof(fftw_plan));
1303 0 : if (direction) {
1304 0 : if (grid_in == grid_out) {
1305 0 : cp_assert(inplace);
1306 0 : for (int r = 0; r < rank; r++)
1307 0 : cp_assert(dims[r].is == 2 * dims[r].os ||
1308 : (dims[r].is == 1 && dims[r].os == 1));
1309 0 : for (int r = 0; r < howmany_rank; r++)
1310 0 : cp_assert(howmany_dims[r].is == 2 * howmany_dims[r].os);
1311 : } else {
1312 0 : cp_assert(!inplace);
1313 : }
1314 0 : *plan =
1315 0 : fftw_plan_guru_dft_r2c(rank, dims, howmany_rank, howmany_dims, grid_in,
1316 : (double complex *)grid_out, fftw_planning_mode);
1317 : } else {
1318 0 : if (grid_in == grid_out) {
1319 0 : cp_assert(inplace);
1320 0 : for (int r = 0; r < rank; r++)
1321 0 : cp_assert(2 * dims[r].is == dims[r].os ||
1322 : (dims[r].is == 1 && dims[r].os == 1));
1323 0 : for (int r = 0; r < howmany_rank; r++)
1324 0 : cp_assert(2 * howmany_dims[r].is == howmany_dims[r].os);
1325 : } else {
1326 0 : 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 0 : *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 0 : add_plan_to_cache(key, plan);
1336 0 : cp_assert(plan != NULL);
1337 0 : omp_unset_nest_lock(&cache_lock);
1338 0 : 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 : ******************************************************************************/
1355 343704 : void fft_fftw_register_1d_local(
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 343704 : cp_assert(is_initialized);
1362 343704 : if (fft_size == 0 || number_of_ffts == 0)
1363 : return;
1364 343704 : const bool in_place = grid_in == grid_out;
1365 343704 : const int number_of_threads = fft_team_size();
1366 343704 : if (fftw_planning_mode == FFTW_ESTIMATE) {
1367 343596 : fftw_plan *plan = NULL;
1368 343596 : const int block_size =
1369 343596 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
1370 343596 : const int number_of_full_blocks = number_of_ffts / block_size;
1371 343596 : fft_key_t key;
1372 343596 : get_key_1d(direction, fft_size, block_size, transpose_in, transpose_out,
1373 : leading_dimension_in, leading_dimension_out, 1, in_place, key);
1374 343596 : plan = lookup_plan_from_cache(key);
1375 343596 : if (plan == NULL)
1376 64142 : plan = fft_fftw_create_1d_plan(key, grid_in, grid_out);
1377 343596 : if (block_size * number_of_full_blocks != number_of_ffts) {
1378 0 : const int block_size_last_thread = number_of_ffts % block_size;
1379 0 : fft_key_t key;
1380 0 : 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 0 : plan = lookup_plan_from_cache(key);
1384 0 : if (plan == NULL)
1385 0 : plan = fft_fftw_create_1d_plan(key, grid_in, grid_out);
1386 : }
1387 : } else {
1388 108 : fft_key_t key;
1389 108 : get_key_1d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
1390 108 : leading_dimension_in, leading_dimension_out, fft_team_size(),
1391 : in_place, key);
1392 108 : fftw_plan *plan = lookup_plan_from_cache(key);
1393 108 : if (plan == NULL)
1394 108 : 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 : ******************************************************************************/
1422 56 : void 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 56 : cp_assert(is_initialized);
1431 56 : if (fft_size == 0 || number_of_ffts == 0)
1432 : return;
1433 56 : const bool in_place = grid_in == (double *)grid_out;
1434 56 : 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 80 : if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1438 24 : fftw_plan *plan = NULL;
1439 24 : const int block_size =
1440 24 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
1441 24 : const int number_of_full_blocks = number_of_ffts / block_size;
1442 24 : fft_key_t key;
1443 24 : 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 24 : plan = lookup_plan_from_cache(key);
1447 24 : if (plan == NULL) {
1448 24 : plan = fft_fftw_create_1d_plan_r2c(key, grid_in, (double *)grid_out);
1449 : }
1450 24 : if (block_size * number_of_full_blocks != number_of_ffts) {
1451 0 : const int block_size_last_thread = number_of_ffts % block_size;
1452 0 : fft_key_t key;
1453 0 : 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 0 : plan = lookup_plan_from_cache(key);
1457 0 : if (plan == NULL) {
1458 0 : plan = fft_fftw_create_1d_plan_r2c(key, grid_in, (double *)grid_out);
1459 : }
1460 : }
1461 : } else {
1462 32 : fft_key_t key;
1463 32 : get_key_1d_r2c(true, fft_size, number_of_ffts, transpose_in, transpose_out,
1464 32 : leading_dimension_in, leading_dimension_out, fft_team_size(),
1465 : in_place, key);
1466 32 : fftw_plan *plan = lookup_plan_from_cache(key);
1467 32 : if (plan == NULL) {
1468 32 : 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 : ******************************************************************************/
1496 56 : void 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 56 : cp_assert(is_initialized);
1505 56 : if (fft_size == 0 || number_of_ffts == 0)
1506 : return;
1507 56 : const bool in_place = (double *)grid_in == grid_out;
1508 56 : 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 80 : if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1512 24 : fftw_plan *plan = NULL;
1513 24 : const int block_size =
1514 24 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
1515 24 : const int number_of_full_blocks = number_of_ffts / block_size;
1516 24 : fft_key_t key;
1517 24 : 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 24 : plan = lookup_plan_from_cache(key);
1521 24 : if (plan == NULL) {
1522 24 : plan = fft_fftw_create_1d_plan_r2c(key, (double *)grid_in, grid_out);
1523 : }
1524 24 : if (block_size * number_of_full_blocks != number_of_ffts) {
1525 0 : const int block_size_last_thread = number_of_ffts % block_size;
1526 0 : fft_key_t key;
1527 0 : 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 0 : plan = lookup_plan_from_cache(key);
1531 0 : if (plan == NULL) {
1532 0 : plan = fft_fftw_create_1d_plan_r2c(key, (double *)grid_in, grid_out);
1533 : }
1534 : }
1535 : } else {
1536 32 : fft_key_t key;
1537 32 : get_key_1d_r2c(false, fft_size, number_of_ffts, transpose_in, transpose_out,
1538 32 : leading_dimension_in, leading_dimension_out, fft_team_size(),
1539 : in_place, key);
1540 32 : fftw_plan *plan = lookup_plan_from_cache(key);
1541 32 : if (plan == NULL) {
1542 32 : 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 : ******************************************************************************/
1569 72080 : void 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 72080 : cp_assert(is_initialized);
1577 72080 : if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
1578 : return;
1579 72080 : const bool in_place = grid_in == grid_out;
1580 72080 : const int number_of_threads = fft_team_size();
1581 72080 : if (fftw_planning_mode == FFTW_ESTIMATE) {
1582 71972 : fftw_plan *plan = NULL, *plan_last_thread = NULL;
1583 71972 : const int block_size =
1584 71972 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
1585 71972 : const int number_of_full_blocks = number_of_ffts / block_size;
1586 71972 : fft_key_t key;
1587 143952 : get_key_2d(direction, fft_size, block_size, transpose_in, transpose_out,
1588 35990 : transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
1589 35990 : transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
1590 : in_place, key);
1591 71972 : plan = lookup_plan_from_cache(key);
1592 71972 : if (plan == NULL) {
1593 57144 : plan = fft_fftw_create_2d_plan(key, grid_in, grid_out);
1594 : }
1595 71972 : if (block_size * number_of_full_blocks != number_of_ffts) {
1596 0 : const int block_size_last_thread = number_of_ffts % block_size;
1597 0 : fft_key_t key;
1598 0 : get_key_2d(direction, fft_size, block_size_last_thread, transpose_in,
1599 : transpose_out,
1600 0 : transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
1601 0 : transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
1602 : in_place, key);
1603 0 : plan_last_thread = lookup_plan_from_cache(key);
1604 0 : if (plan_last_thread == NULL) {
1605 0 : plan_last_thread = fft_fftw_create_2d_plan(key, grid_in, grid_out);
1606 : }
1607 : }
1608 : } else {
1609 108 : fft_key_t key;
1610 224 : get_key_2d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
1611 58 : transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
1612 58 : transpose_out ? number_of_ffts : fft_size[0] * fft_size[1],
1613 108 : fft_team_size(), grid_in == grid_out, key);
1614 108 : fftw_plan *plan = lookup_plan_from_cache(key);
1615 108 : if (plan == NULL) {
1616 108 : 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 : ******************************************************************************/
1641 56 : void 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 56 : cp_assert(is_initialized);
1648 56 : if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
1649 : return;
1650 56 : const bool in_place = grid_in == (double *)grid_out;
1651 56 : 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 80 : if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1655 24 : fftw_plan *plan = NULL, *plan_last_thread = NULL;
1656 24 : const int block_size =
1657 24 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
1658 24 : const int number_of_full_blocks = number_of_ffts / block_size;
1659 24 : fft_key_t key;
1660 56 : get_key_2d_r2c(
1661 : true, fft_size, block_size, transpose_in, transpose_out,
1662 : transpose_in ? number_of_ffts
1663 16 : : fft_size[0] *
1664 16 : (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
1665 16 : transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1), 1,
1666 : in_place, key);
1667 24 : plan = lookup_plan_from_cache(key);
1668 24 : if (plan == NULL) {
1669 24 : plan = fft_fftw_create_2d_plan_r2c(key, grid_in, (double *)grid_out);
1670 : }
1671 24 : if (block_size * number_of_full_blocks != number_of_ffts) {
1672 0 : const int block_size_last_thread = number_of_ffts % block_size;
1673 0 : fft_key_t key;
1674 0 : get_key_2d_r2c(
1675 : true, fft_size, block_size_last_thread, transpose_in, transpose_out,
1676 : transpose_in ? number_of_ffts
1677 0 : : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1678 0 : : fft_size[1]),
1679 0 : transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1680 : 1, in_place, key);
1681 0 : plan_last_thread = lookup_plan_from_cache(key);
1682 0 : if (plan_last_thread == NULL) {
1683 0 : plan_last_thread =
1684 0 : fft_fftw_create_2d_plan_r2c(key, grid_in, (double *)grid_out);
1685 : }
1686 : }
1687 : } else {
1688 32 : fft_key_t key;
1689 64 : get_key_2d_r2c(
1690 : true, fft_size, number_of_ffts, transpose_in, transpose_out,
1691 : transpose_in ? number_of_ffts
1692 16 : : fft_size[0] *
1693 16 : (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
1694 16 : transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1695 32 : fft_team_size(), grid_in == (double *)grid_out, key);
1696 32 : fftw_plan *plan = lookup_plan_from_cache(key);
1697 32 : if (plan == NULL) {
1698 32 : 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 : ******************************************************************************/
1722 56 : void 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 56 : cp_assert(is_initialized);
1729 56 : if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
1730 : return;
1731 56 : const bool in_place = (double *)grid_in == grid_out;
1732 56 : 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 80 : if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1736 24 : fftw_plan *plan = NULL, *plan_last_thread = NULL;
1737 24 : const int block_size =
1738 24 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
1739 24 : const int number_of_full_blocks = number_of_ffts / block_size;
1740 24 : fft_key_t key;
1741 56 : get_key_2d_r2c(
1742 : false, fft_size, block_size, transpose_in, transpose_out,
1743 16 : transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1744 : transpose_out ? number_of_ffts
1745 20 : : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1746 16 : : fft_size[1]),
1747 : 1, in_place, key);
1748 24 : plan = lookup_plan_from_cache(key);
1749 24 : if (plan == NULL) {
1750 24 : plan = fft_fftw_create_2d_plan_r2c(key, (double *)grid_in, grid_out);
1751 : }
1752 24 : if (block_size * number_of_full_blocks != number_of_ffts) {
1753 0 : const int block_size_last_thread = number_of_ffts % block_size;
1754 0 : fft_key_t key;
1755 0 : get_key_2d_r2c(
1756 : false, fft_size, block_size_last_thread, transpose_in, transpose_out,
1757 0 : transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1758 : transpose_out ? number_of_ffts
1759 0 : : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1760 0 : : fft_size[1]),
1761 : 1, in_place, key);
1762 0 : plan_last_thread = lookup_plan_from_cache(key);
1763 0 : if (plan_last_thread == NULL) {
1764 0 : plan_last_thread =
1765 0 : fft_fftw_create_2d_plan_r2c(key, (double *)grid_in, grid_out);
1766 : }
1767 : }
1768 : } else {
1769 32 : fft_key_t key;
1770 64 : get_key_2d_r2c(
1771 : false, fft_size, number_of_ffts, transpose_in, transpose_out,
1772 16 : transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1773 : transpose_out ? number_of_ffts
1774 20 : : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1775 16 : : fft_size[1]),
1776 32 : fft_team_size(), grid_in == (double complex *)grid_out, key);
1777 32 : fftw_plan *plan = lookup_plan_from_cache(key);
1778 32 : if (plan == NULL) {
1779 32 : 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 : ******************************************************************************/
1804 17116 : void 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 17116 : cp_assert(is_initialized);
1812 17116 : if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
1813 : number_of_ffts == 0)
1814 : return;
1815 17116 : const bool in_place = grid_in == grid_out;
1816 17212 : if (number_of_ffts > 1 &&
1817 96 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1818 0 : register_threaded_3d_c2c(direction, fft_size, number_of_ffts, in_place,
1819 : grid_in, grid_out);
1820 34136 : } else if (number_of_ffts == 1 &&
1821 17020 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1822 0 : register_threaded_3d_c2c_1d(direction, fft_size, in_place, grid_in,
1823 : grid_out);
1824 : } else {
1825 17116 : fft_key_t key;
1826 17116 : get_key_3d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
1827 17116 : fft_team_size(), in_place, key);
1828 17116 : fftw_plan *plan = lookup_plan_from_cache(key);
1829 17116 : if (plan == NULL) {
1830 5900 : 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 : ******************************************************************************/
1854 40 : void 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 40 : cp_assert(is_initialized);
1861 40 : if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
1862 : number_of_ffts == 0)
1863 : return;
1864 40 : const bool in_place = grid_in == (double *)grid_out;
1865 80 : if (number_of_ffts > 1 &&
1866 40 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1867 0 : register_threaded_3d_r2c(true, fft_size, number_of_ffts, in_place, grid_in,
1868 : (double *)grid_out);
1869 40 : } else if (number_of_ffts == 1 &&
1870 0 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1871 0 : register_threaded_3d_r2c_1d(true, fft_size, in_place, grid_in, grid_out);
1872 : } else {
1873 40 : fft_key_t key;
1874 40 : get_key_3d_r2c(true, fft_size, number_of_ffts, transpose_in, transpose_out,
1875 40 : fft_team_size(), in_place, key);
1876 40 : fftw_plan *plan = lookup_plan_from_cache(key);
1877 40 : if (plan == NULL) {
1878 40 : 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 : ******************************************************************************/
1902 40 : void 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 40 : cp_assert(is_initialized);
1909 40 : if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
1910 : number_of_ffts == 0)
1911 : return;
1912 40 : const bool in_place = (double *)grid_in == grid_out;
1913 80 : if (number_of_ffts > 1 &&
1914 40 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1915 0 : register_threaded_3d_r2c(false, fft_size, number_of_ffts, in_place,
1916 : (double *)grid_in, grid_out);
1917 40 : } else if (number_of_ffts == 1 &&
1918 0 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1919 0 : register_threaded_3d_r2c_1d(false, fft_size, in_place, grid_out, grid_in);
1920 : } else {
1921 40 : fft_key_t key;
1922 40 : get_key_3d_r2c(false, fft_size, number_of_ffts, transpose_in, transpose_out,
1923 40 : fft_team_size(), in_place, key);
1924 40 : fftw_plan *plan = lookup_plan_from_cache(key);
1925 40 : if (plan == NULL) {
1926 40 : 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 : ******************************************************************************/
1953 14403159 : void 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 14403159 : cp_assert(is_initialized);
1960 14403159 : if (fft_size == 0 || number_of_ffts == 0)
1961 : return;
1962 14403159 : const bool in_place = grid_in == grid_out;
1963 14403159 : const int number_of_threads = fft_team_size();
1964 14403159 : if (number_of_threads == 1 && fftw_planning_mode == FFTW_ESTIMATE) {
1965 : // Called from inside an OpenMP region: use the original batched strategy.
1966 14402519 : fftw_plan *plan = NULL, *plan_last_thread = NULL;
1967 14402519 : const int block_size =
1968 14402519 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
1969 14402519 : const int number_of_full_blocks = number_of_ffts / block_size;
1970 14402519 : const int total_number_of_blocks =
1971 14402519 : number_of_full_blocks + (number_of_ffts % block_size != 0);
1972 14402519 : fft_key_t key;
1973 14402519 : get_key_1d(direction, fft_size, block_size, transpose_in, transpose_out,
1974 : leading_dimension_in, leading_dimension_out, 1, in_place, key);
1975 14402519 : plan = lookup_plan_from_cache(key);
1976 14402519 : if (plan == NULL) {
1977 79 : double complex *buffer =
1978 79 : fftw_alloc_complex(get_buffer_size_from_key(key));
1979 158 : plan = fft_fftw_create_1d_plan(key, buffer, in_place ? buffer : grid_out);
1980 79 : fftw_free(buffer);
1981 : }
1982 14402519 : if (block_size * number_of_full_blocks != number_of_ffts) {
1983 0 : const int block_size_last_thread = number_of_ffts % block_size;
1984 0 : fft_key_t key_last;
1985 0 : 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 0 : plan_last_thread = lookup_plan_from_cache(key_last);
1989 0 : if (plan_last_thread == NULL) {
1990 0 : double complex *buffer =
1991 0 : fftw_alloc_complex(get_buffer_size_from_key(key_last));
1992 0 : plan_last_thread = fft_fftw_create_1d_plan(
1993 : key_last, buffer, in_place ? buffer : grid_out);
1994 0 : fftw_free(buffer);
1995 : }
1996 : }
1997 1875846 : const int offset_in =
1998 14402519 : transpose_in ? block_size : block_size * leading_dimension_in;
1999 1872048 : const int offset_out =
2000 14402519 : transpose_out ? block_size : block_size * leading_dimension_out;
2001 14402519 : #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 640 : fft_key_t key;
2016 640 : 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 640 : fftw_plan *plan = lookup_plan_from_cache(key);
2020 640 : if (plan == NULL) {
2021 0 : double complex *buffer =
2022 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
2023 0 : plan = fft_fftw_create_1d_plan(key, buffer, in_place ? buffer : grid_out);
2024 0 : fftw_free(buffer);
2025 : }
2026 640 : 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 : ******************************************************************************/
2054 56 : void 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 56 : cp_assert(is_initialized);
2061 56 : if (fft_size == 0 || number_of_ffts == 0)
2062 : return;
2063 56 : const bool in_place = grid_in == (double *)grid_out;
2064 56 : 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 56 : if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2068 24 : fftw_plan *plan = NULL, *plan_last_thread = NULL;
2069 24 : const int block_size =
2070 24 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
2071 24 : const int number_of_full_blocks = number_of_ffts / block_size;
2072 24 : const int total_number_of_blocks =
2073 24 : number_of_full_blocks + (number_of_ffts % block_size != 0);
2074 24 : fft_key_t key;
2075 24 : 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 24 : plan = lookup_plan_from_cache(key);
2079 24 : if (plan == NULL) {
2080 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2081 0 : plan = fft_fftw_create_1d_plan_r2c(
2082 : key, buffer, in_place ? buffer : (double *)grid_out);
2083 0 : fftw_free(buffer);
2084 : }
2085 24 : if (block_size * number_of_full_blocks != number_of_ffts) {
2086 0 : const int block_size_last_thread = number_of_ffts % block_size;
2087 0 : fft_key_t key;
2088 0 : 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 0 : plan_last_thread = lookup_plan_from_cache(key);
2092 0 : if (plan_last_thread == NULL) {
2093 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2094 0 : plan_last_thread = fft_fftw_create_1d_plan_r2c(
2095 : key, buffer, in_place ? buffer : (double *)grid_out);
2096 0 : fftw_free(buffer);
2097 : }
2098 : }
2099 16 : const int offset_in =
2100 24 : transpose_in ? block_size : block_size * leading_dimension_in;
2101 16 : const int offset_out =
2102 24 : transpose_out ? block_size : block_size * leading_dimension_out;
2103 24 : #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 32 : fft_key_t key;
2118 32 : get_key_1d_r2c(true, fft_size, number_of_ffts, transpose_in, transpose_out,
2119 32 : leading_dimension_in, leading_dimension_out, fft_team_size(),
2120 : in_place, key);
2121 32 : fftw_plan *plan = lookup_plan_from_cache(key);
2122 32 : if (plan == NULL) {
2123 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2124 0 : plan = fft_fftw_create_1d_plan_r2c(
2125 : key, buffer, in_place ? buffer : (double *)grid_out);
2126 0 : fftw_free(buffer);
2127 : }
2128 0 : cp_assert(plan != NULL);
2129 32 : 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 : ******************************************************************************/
2156 56 : void 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 56 : cp_assert(is_initialized);
2163 56 : if (fft_size == 0 || number_of_ffts == 0)
2164 : return;
2165 56 : const bool in_place = (double *)grid_in == grid_out;
2166 56 : 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 56 : if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2170 24 : fftw_plan *plan = NULL, *plan_last_thread = NULL;
2171 24 : const int block_size =
2172 24 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
2173 24 : const int number_of_full_blocks = number_of_ffts / block_size;
2174 24 : const int total_number_of_blocks =
2175 24 : number_of_full_blocks + (number_of_ffts % block_size != 0);
2176 24 : fft_key_t key;
2177 24 : 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 24 : plan = lookup_plan_from_cache(key);
2181 24 : if (plan == NULL) {
2182 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2183 0 : plan = fft_fftw_create_1d_plan_r2c(key, buffer,
2184 : in_place ? buffer : grid_out);
2185 0 : fftw_free(buffer);
2186 : }
2187 24 : if (block_size * number_of_full_blocks != number_of_ffts) {
2188 0 : const int block_size_last_thread = number_of_ffts % block_size;
2189 0 : fft_key_t key;
2190 0 : 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 0 : plan_last_thread = lookup_plan_from_cache(key);
2194 0 : if (plan_last_thread == NULL) {
2195 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2196 0 : plan_last_thread = fft_fftw_create_1d_plan_r2c(
2197 : key, buffer, in_place ? buffer : grid_out);
2198 0 : fftw_free(buffer);
2199 : }
2200 : }
2201 16 : const int offset_in =
2202 24 : transpose_in ? block_size : block_size * leading_dimension_in;
2203 16 : const int offset_out =
2204 24 : transpose_out ? block_size : block_size * leading_dimension_out;
2205 24 : #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 32 : fft_key_t key;
2220 32 : get_key_1d_r2c(false, fft_size, number_of_ffts, transpose_in, transpose_out,
2221 32 : leading_dimension_in, leading_dimension_out, fft_team_size(),
2222 : grid_in == (double complex *)grid_out, key);
2223 32 : fftw_plan *plan = lookup_plan_from_cache(key);
2224 32 : if (plan == NULL) {
2225 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2226 0 : plan = fft_fftw_create_1d_plan_r2c(key, buffer,
2227 : in_place ? buffer : grid_out);
2228 0 : fftw_free(buffer);
2229 : }
2230 0 : cp_assert(plan != NULL);
2231 32 : 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 : ******************************************************************************/
2257 3746414 : void 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 3746414 : cp_assert(is_initialized);
2263 3746414 : if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
2264 : return;
2265 3746414 : const bool in_place = grid_in == grid_out;
2266 3746414 : const int number_of_threads = fft_team_size();
2267 3746414 : if (fftw_planning_mode == FFTW_ESTIMATE) {
2268 3745774 : fftw_plan *plan = NULL, *plan_last_thread = NULL;
2269 3745774 : const int block_size =
2270 3745774 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
2271 3745774 : const int number_of_full_blocks = number_of_ffts / block_size;
2272 3745774 : const int total_number_of_blocks =
2273 3745774 : number_of_full_blocks + (number_of_ffts % block_size != 0);
2274 3745774 : fft_key_t key;
2275 7491556 : get_key_2d(direction, fft_size, block_size, transpose_in, transpose_out,
2276 1874790 : transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
2277 1870992 : transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
2278 : in_place, key);
2279 3745774 : plan = lookup_plan_from_cache(key);
2280 3745774 : if (plan == NULL) {
2281 0 : double complex *buffer =
2282 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
2283 0 : plan = fft_fftw_create_2d_plan(key, buffer, in_place ? buffer : grid_out);
2284 0 : fftw_free(buffer);
2285 : }
2286 3745774 : if (block_size * number_of_full_blocks != number_of_ffts) {
2287 0 : const int block_size_last_thread = number_of_ffts % block_size;
2288 0 : fft_key_t key;
2289 0 : get_key_2d(direction, fft_size, block_size_last_thread, transpose_in,
2290 : transpose_out,
2291 0 : transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
2292 0 : transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
2293 : in_place, key);
2294 0 : plan_last_thread = lookup_plan_from_cache(key);
2295 0 : if (plan_last_thread == NULL) {
2296 0 : double complex *buffer =
2297 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
2298 0 : plan_last_thread =
2299 0 : fft_fftw_create_2d_plan(key, buffer, in_place ? buffer : grid_out);
2300 0 : fftw_free(buffer);
2301 : }
2302 : }
2303 5620564 : const int offset_in =
2304 3745774 : transpose_in ? block_size : block_size * fft_size[0] * fft_size[1];
2305 5616766 : const int offset_out =
2306 3745774 : transpose_out ? block_size : block_size * fft_size[0] * fft_size[1];
2307 3745774 : #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 640 : fft_key_t key;
2322 1288 : get_key_2d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
2323 312 : transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
2324 336 : transpose_out ? number_of_ffts : fft_size[0] * fft_size[1],
2325 640 : fft_team_size(), grid_in == grid_out, key);
2326 640 : fftw_plan *plan = lookup_plan_from_cache(key);
2327 640 : if (plan == NULL) {
2328 0 : double complex *buffer =
2329 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
2330 0 : plan = fft_fftw_create_2d_plan(key, buffer,
2331 : grid_in == grid_out ? buffer : grid_out);
2332 0 : fftw_free(buffer);
2333 : }
2334 640 : 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 : ******************************************************************************/
2358 56 : void 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 56 : cp_assert(is_initialized);
2363 56 : if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
2364 : return;
2365 56 : const bool in_place = grid_in == (double *)grid_out;
2366 56 : 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 56 : if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2370 24 : fftw_plan *plan = NULL, *plan_last_thread = NULL;
2371 24 : const int block_size =
2372 24 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
2373 24 : const int number_of_full_blocks = number_of_ffts / block_size;
2374 24 : const int total_number_of_blocks =
2375 24 : number_of_full_blocks + (number_of_ffts % block_size != 0);
2376 24 : fft_key_t key;
2377 56 : get_key_2d_r2c(
2378 : true, fft_size, block_size, transpose_in, transpose_out,
2379 : transpose_in ? number_of_ffts
2380 16 : : fft_size[0] *
2381 16 : (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
2382 16 : transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1), 1,
2383 : in_place, key);
2384 24 : plan = lookup_plan_from_cache(key);
2385 24 : if (plan == NULL) {
2386 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2387 0 : plan = fft_fftw_create_2d_plan_r2c(
2388 : key, buffer, in_place ? buffer : (double *)grid_out);
2389 0 : fftw_free(buffer);
2390 : }
2391 24 : if (block_size * number_of_full_blocks != number_of_ffts) {
2392 0 : const int block_size_last_thread = number_of_ffts % block_size;
2393 0 : fft_key_t key;
2394 0 : get_key_2d_r2c(
2395 : true, fft_size, block_size_last_thread, transpose_in, transpose_out,
2396 : transpose_in ? number_of_ffts
2397 0 : : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2398 0 : : fft_size[1]),
2399 0 : transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2400 : 1, in_place, key);
2401 0 : plan_last_thread = lookup_plan_from_cache(key);
2402 0 : if (plan_last_thread == NULL) {
2403 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2404 0 : plan_last_thread = fft_fftw_create_2d_plan_r2c(
2405 : key, buffer, in_place ? buffer : (double *)grid_out);
2406 0 : fftw_free(buffer);
2407 : }
2408 : }
2409 16 : const int offset_in =
2410 : transpose_in ? block_size
2411 24 : : block_size * fft_size[0] *
2412 16 : (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]);
2413 40 : const int offset_out =
2414 : transpose_out ? block_size
2415 24 : : block_size * fft_size[0] * (fft_size[1] / 2 + 1);
2416 24 : #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 32 : fft_key_t key;
2431 64 : get_key_2d_r2c(
2432 : true, fft_size, number_of_ffts, transpose_in, transpose_out,
2433 : transpose_in ? number_of_ffts
2434 16 : : fft_size[0] *
2435 16 : (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
2436 16 : transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2437 32 : fft_team_size(), grid_in == (double *)grid_out, key);
2438 32 : fftw_plan *plan = lookup_plan_from_cache(key);
2439 32 : if (plan == NULL) {
2440 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2441 0 : plan = fft_fftw_create_2d_plan_r2c(
2442 : key, buffer,
2443 : grid_in == (double *)grid_out ? buffer : (double *)grid_out);
2444 0 : fftw_free(buffer);
2445 : }
2446 32 : 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 : ******************************************************************************/
2469 56 : void 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 56 : cp_assert(is_initialized);
2474 56 : if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
2475 : return;
2476 56 : const bool in_place = (double *)grid_in == grid_out;
2477 56 : 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 56 : if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2481 24 : fftw_plan *plan = NULL, *plan_last_thread = NULL;
2482 24 : const int block_size =
2483 24 : (number_of_ffts + number_of_threads - 1) / number_of_threads;
2484 24 : const int number_of_full_blocks = number_of_ffts / block_size;
2485 24 : const int total_number_of_blocks =
2486 24 : number_of_full_blocks + (number_of_ffts % block_size != 0);
2487 24 : fft_key_t key;
2488 56 : get_key_2d_r2c(
2489 : false, fft_size, block_size, transpose_in, transpose_out,
2490 16 : transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2491 : transpose_out ? number_of_ffts
2492 20 : : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2493 16 : : fft_size[1]),
2494 : 1, in_place, key);
2495 24 : plan = lookup_plan_from_cache(key);
2496 24 : if (plan == NULL) {
2497 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2498 0 : plan = fft_fftw_create_2d_plan_r2c(key, buffer,
2499 : in_place ? buffer : grid_out);
2500 0 : fftw_free(buffer);
2501 : }
2502 24 : if (block_size * number_of_full_blocks != number_of_ffts) {
2503 0 : const int block_size_last_thread = number_of_ffts % block_size;
2504 0 : fft_key_t key;
2505 0 : get_key_2d_r2c(
2506 : false, fft_size, block_size_last_thread, transpose_in, transpose_out,
2507 0 : transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2508 : transpose_out ? number_of_ffts
2509 0 : : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2510 0 : : fft_size[1]),
2511 : 1, in_place, key);
2512 0 : plan_last_thread = lookup_plan_from_cache(key);
2513 0 : if (plan_last_thread == NULL) {
2514 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2515 0 : plan_last_thread = fft_fftw_create_2d_plan_r2c(
2516 : key, buffer, in_place ? buffer : grid_out);
2517 0 : fftw_free(buffer);
2518 : }
2519 : }
2520 40 : const int offset_in =
2521 : transpose_in ? block_size
2522 24 : : block_size * fft_size[0] * (fft_size[1] / 2 + 1);
2523 16 : const int offset_out =
2524 : transpose_out
2525 : ? block_size
2526 24 : : block_size * fft_size[0] *
2527 16 : (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]);
2528 24 : #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 32 : fft_key_t key;
2543 64 : get_key_2d_r2c(
2544 : false, fft_size, number_of_ffts, transpose_in, transpose_out,
2545 16 : transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2546 : transpose_out ? number_of_ffts
2547 20 : : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2548 16 : : fft_size[1]),
2549 32 : fft_team_size(), grid_in == (double complex *)grid_out, key);
2550 32 : fftw_plan *plan = lookup_plan_from_cache(key);
2551 32 : if (plan == NULL) {
2552 0 : double complex *buffer =
2553 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
2554 0 : plan = fft_fftw_create_2d_plan_r2c(
2555 : key, (double *)buffer,
2556 : (double *)grid_in == grid_out ? (double *)buffer : grid_out);
2557 0 : fftw_free(buffer);
2558 : }
2559 32 : 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 : ******************************************************************************/
2583 660799 : void 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 660799 : cp_assert(is_initialized);
2589 660799 : if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
2590 : number_of_ffts == 0)
2591 : return;
2592 660799 : const bool in_place = grid_in == grid_out;
2593 663257 : if (number_of_ffts > 1 &&
2594 2458 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2595 0 : execute_threaded_3d_c2c(direction, fft_size, number_of_ffts, in_place,
2596 : grid_in, grid_out);
2597 1319140 : } else if (number_of_ffts == 1 &&
2598 658341 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2599 0 : execute_threaded_3d_c2c_1d(direction, fft_size, in_place, grid_in,
2600 : grid_out);
2601 : } else {
2602 660799 : fft_key_t key;
2603 660799 : get_key_3d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
2604 660799 : fft_team_size(), in_place, key);
2605 660799 : fftw_plan *plan = lookup_plan_from_cache(key);
2606 660799 : if (plan == NULL) {
2607 170 : double complex *buffer =
2608 170 : fftw_alloc_complex(get_buffer_size_from_key(key));
2609 340 : plan = fft_fftw_create_3d_plan(key, buffer, in_place ? buffer : grid_out);
2610 170 : fftw_free(buffer);
2611 : }
2612 170 : cp_assert(plan != NULL);
2613 660799 : cp_assert(grid_in != NULL);
2614 660799 : cp_assert(grid_out != NULL);
2615 660799 : 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 : ******************************************************************************/
2638 40 : void 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 40 : cp_assert(is_initialized);
2643 40 : if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
2644 : number_of_ffts == 0)
2645 : return;
2646 40 : const bool in_place = grid_in == (double *)grid_out;
2647 80 : if (number_of_ffts > 1 &&
2648 40 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2649 0 : execute_threaded_3d_r2c(true, fft_size, number_of_ffts, in_place, grid_in,
2650 : (double *)grid_out);
2651 40 : } else if (number_of_ffts == 1 &&
2652 0 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2653 0 : execute_threaded_3d_r2c_1d(true, fft_size, in_place, grid_in, grid_out);
2654 : } else {
2655 40 : fft_key_t key;
2656 40 : get_key_3d_r2c(true, fft_size, number_of_ffts, transpose_in, transpose_out,
2657 40 : fft_team_size(), in_place, key);
2658 40 : fftw_plan *plan = lookup_plan_from_cache(key);
2659 40 : if (plan == NULL) {
2660 0 : double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2661 0 : plan = fft_fftw_create_3d_plan_r2c(
2662 : key, buffer, in_place ? buffer : (double *)grid_out);
2663 0 : fftw_free(buffer);
2664 : }
2665 0 : cp_assert(plan != NULL);
2666 40 : cp_assert(grid_in != NULL);
2667 40 : cp_assert(grid_out != NULL);
2668 40 : 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 : ******************************************************************************/
2691 40 : void 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 40 : cp_assert(is_initialized);
2696 40 : if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
2697 : number_of_ffts == 0)
2698 : return;
2699 40 : const bool in_place = (double *)grid_in == grid_out;
2700 80 : if (number_of_ffts > 1 &&
2701 40 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2702 0 : execute_threaded_3d_r2c(false, fft_size, number_of_ffts, in_place,
2703 : (double *)grid_in, grid_out);
2704 40 : } else if (number_of_ffts == 1 &&
2705 0 : use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2706 0 : execute_threaded_3d_r2c_1d(false, fft_size, in_place, grid_out, grid_in);
2707 : } else {
2708 40 : fft_key_t key;
2709 40 : get_key_3d_r2c(false, fft_size, number_of_ffts, transpose_in, transpose_out,
2710 40 : fft_team_size(), in_place, key);
2711 40 : fftw_plan *plan = lookup_plan_from_cache(key);
2712 40 : if (plan == NULL) {
2713 0 : double complex *buffer =
2714 0 : fftw_alloc_complex(get_buffer_size_from_key(key));
2715 0 : plan = fft_fftw_create_3d_plan_r2c(
2716 : key, (double *)buffer, in_place ? (double *)buffer : grid_out);
2717 0 : fftw_free(buffer);
2718 : }
2719 0 : cp_assert(plan != NULL);
2720 40 : cp_assert(grid_in != NULL);
2721 40 : cp_assert(grid_out != NULL);
2722 40 : 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
|