43#define FFTW_CACHE_SIZE 256
48static cache_entry *caches = NULL;
49static int *cache_oldest_entries = NULL;
50static int number_of_caches = 0;
52static bool is_initialized =
false;
54static omp_nest_lock_t cache_lock;
56static int fftw_planning_mode = -1;
63static inline int current_cache_id(
void) {
64 int thread_id = omp_get_thread_num();
65 if (thread_id < 0 || thread_id >= number_of_caches)
75static inline cache_entry *current_cache(
void) {
76 return &caches[current_cache_id() * FFTW_CACHE_SIZE];
84static inline int *current_cache_oldest_entry(
void) {
85 return &cache_oldest_entries[current_cache_id()];
93static fftw_plan *lookup_plan_from_cache(
const fft_key_t key) {
95 omp_set_nest_lock(&cache_lock);
96 cache_entry *cache = current_cache();
97 fftw_plan *plan = NULL;
98 for (
int i = 0;
i < FFTW_CACHE_SIZE;
i++) {
99 const int *x = cache[
i].key;
100 if (memcmp(key, x,
sizeof(fft_key_t)) == 0) {
101 plan = cache[
i].plan;
105 omp_unset_nest_lock(&cache_lock);
115static void add_plan_to_cache(
const fft_key_t key, fftw_plan *plan) {
116 omp_set_nest_lock(&cache_lock);
117 cache_entry *cache = current_cache();
118 int *cache_oldest_entry = current_cache_oldest_entry();
119 const int i = *cache_oldest_entry;
120 *cache_oldest_entry = (*cache_oldest_entry + 1) % FFTW_CACHE_SIZE;
121 if (cache[
i].plan != NULL) {
122 fftw_destroy_plan(*cache[
i].plan);
125 memcpy(cache[
i].key, key,
sizeof(fft_key_t));
126 cache[
i].plan = plan;
127 omp_unset_nest_lock(&cache_lock);
139bool is_guru_interface_available() {
144 fftw_iodim howmany_dims[2];
145 howmany_dims[0].n = 1;
146 howmany_dims[0].is = 1;
147 howmany_dims[0].os = 1;
148 howmany_dims[1].n = 1;
149 howmany_dims[1].is = 1;
150 howmany_dims[1].os = 1;
151 double complex buffer1;
153 fftw_plan_guru_dft(1, dims, 2, howmany_dims, &buffer1, &buffer1,
154 FFTW_FORWARD, fftw_planning_mode);
157 fftw_destroy_plan(plan);
175static bool use_threaded_3d(
const int fft_size[3],
const bool transpose_in,
176 const bool transpose_out,
const bool in_place) {
178 ((((fft_size[0] >= 256 || fft_size[1] >= 256 || fft_size[2] >= 256) &&
180 ((fft_size[0] >= 128 || fft_size[1] >= 128 || fft_size[2] >= 128) &&
183 (fftw_planning_mode == FFTW_ESTIMATE);
188fftw_plan *fft_fftw_create_3d_plan(
const fft_key_t key,
double complex *grid_in,
189 double complex *grid_out);
190fftw_plan *fft_fftw_create_3d_plan_r2c(
const fft_key_t key,
double *grid_in,
192fftw_plan *fft_fftw_create_guru_plan(
const fft_key_t key,
193 double complex *grid_in,
194 double complex *grid_out);
195fftw_plan *fft_fftw_create_guru_plan_r2c(
const fft_key_t key,
double *grid_in,
209register_threaded_3d_c2c(
const bool direction,
const int fft_size[3],
210 const int number_of_ffts,
const bool in_place,
211 double complex *grid_in,
double complex *grid_out) {
213 const int block_size =
214 (number_of_ffts + number_of_threads - 1) / number_of_threads;
215 const int number_of_full_blocks = number_of_ffts / block_size;
217 get_key_3d(direction, fft_size, block_size,
false,
false, 1, in_place, key);
218 if (lookup_plan_from_cache(key) == NULL)
219 fft_fftw_create_3d_plan(key, grid_in, grid_out);
220 if (block_size * number_of_full_blocks != number_of_ffts) {
221 const int block_size_last_thread = number_of_ffts % block_size;
223 get_key_3d(direction, fft_size, block_size_last_thread,
false,
false, 1,
225 if (lookup_plan_from_cache(key_last) == NULL)
226 fft_fftw_create_3d_plan(key_last, grid_in, grid_out);
240static void register_threaded_3d_r2c(
const bool direction,
241 const int fft_size[3],
242 const int number_of_ffts,
243 const bool in_place,
double *grid_in,
246 const int block_size =
247 (number_of_ffts + number_of_threads - 1) / number_of_threads;
248 const int number_of_full_blocks = number_of_ffts / block_size;
250 get_key_3d_r2c(direction, fft_size, block_size,
false,
false, 1, in_place,
252 if (lookup_plan_from_cache(key) == NULL)
253 fft_fftw_create_3d_plan_r2c(key, grid_in, grid_out);
254 if (block_size * number_of_full_blocks != number_of_ffts) {
255 const int block_size_last_thread = number_of_ffts % block_size;
257 get_key_3d_r2c(direction, fft_size, block_size_last_thread,
false,
false, 1,
259 if (lookup_plan_from_cache(key_last) == NULL)
260 fft_fftw_create_3d_plan_r2c(key_last, grid_in, grid_out);
274static void execute_threaded_3d_c2c(
const bool direction,
const int fft_size[3],
275 const int number_of_ffts,
277 double complex *grid_in,
278 double complex *grid_out) {
280 const int block_size =
281 (number_of_ffts + number_of_threads - 1) / number_of_threads;
282 const int number_of_full_blocks = number_of_ffts / block_size;
283 const int total_number_of_blocks =
284 number_of_full_blocks + (number_of_ffts % block_size != 0);
285 const int volume = fft_size[0] * fft_size[1] * fft_size[2];
286#pragma omp parallel default(none) num_threads(number_of_threads) shared( \
287 block_size, number_of_full_blocks, total_number_of_blocks, direction, \
288 fft_size, grid_in, grid_out, in_place, number_of_ffts, volume)
290 const int thread_id = omp_get_thread_num();
292 if (thread_id < number_of_full_blocks) {
293 get_key_3d(direction, fft_size, block_size,
false,
false, 1, in_place,
295 fftw_plan *plan = lookup_plan_from_cache(key);
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,
304 fftw_plan *plan = lookup_plan_from_cache(key);
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);
323static void execute_threaded_3d_r2c(
const bool direction,
const int fft_size[3],
324 const int number_of_ffts,
325 const bool in_place,
double *grid_in,
328 const int block_size =
329 (number_of_ffts + number_of_threads - 1) / number_of_threads;
330 const int number_of_full_blocks = number_of_ffts / block_size;
331 const int total_number_of_blocks =
332 number_of_full_blocks + (number_of_ffts % block_size != 0);
333 const int real_volume = fft_size[0] * fft_size[1] * fft_size[2];
334 const int complex_volume = fft_size[0] * fft_size[1] * (fft_size[2] / 2 + 1);
335 const int real_offset = in_place ? 2 * complex_volume : real_volume;
336#pragma omp parallel default(none) num_threads(number_of_threads) \
337 shared(block_size, number_of_full_blocks, total_number_of_blocks, \
338 direction, fft_size, grid_in, grid_out, in_place, \
339 number_of_ffts, complex_volume, real_offset)
341 const int thread_id = omp_get_thread_num();
343 if (thread_id < number_of_full_blocks) {
344 get_key_3d_r2c(direction, fft_size, block_size,
false,
false, 1, in_place,
346 fftw_plan *plan = lookup_plan_from_cache(key);
348 plan = fft_fftw_create_3d_plan_r2c(key, grid_in, grid_out);
349 const int start = thread_id * block_size;
351 fftw_execute_dft_r2c(*plan, grid_in + start * real_offset,
352 (
double complex *)grid_out +
353 start * complex_volume);
355 fftw_execute_dft_c2r(*plan,
356 (
double complex *)grid_in + start * complex_volume,
357 grid_out + start * real_offset);
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,
363 fftw_plan *plan = lookup_plan_from_cache(key);
365 plan = fft_fftw_create_3d_plan_r2c(key, grid_in, grid_out);
366 const int start = number_of_full_blocks * block_size;
368 fftw_execute_dft_r2c(*plan, grid_in + start * real_offset,
369 (
double complex *)grid_out +
370 start * complex_volume);
372 fftw_execute_dft_c2r(*plan,
373 (
double complex *)grid_in + start * complex_volume,
374 grid_out + start * real_offset);
383static void register_threaded_3d_r2c_1d(
const bool direction,
384 const int fft_size[3],
385 const bool in_place,
double *grid_real,
386 double complex *grid_complex) {
387 double complex *buffer = in_place
389 : fftw_alloc_complex(fft_size[0] * fft_size[1] *
390 (fft_size[2] / 2 + 1));
391 double complex *input_c = direction ? buffer : grid_complex;
392 double complex *output_c = direction ? grid_complex : buffer;
395 fftw_plan *plans[3], *plans_last_thread[3];
396 for (
int stage = 0; stage < 3; stage++) {
398 plans_last_thread[stage] = NULL;
399 const int extent = stage == 0 ? fft_size[1] * (fft_size[2] / 2 + 1)
400 : stage == 1 ? fft_size[0]
401 : fft_size[0] * fft_size[1];
402 block_sizes[stage] = (extent + number_of_threads - 1) / number_of_threads;
403 const bool needs_tail = block_sizes[stage] * number_of_threads != extent;
404 const int tail = needs_tail ? extent % block_sizes[stage] : 0;
406 const int n = fft_size[1] * (fft_size[2] / 2 + 1);
407 fft_iodim dim = {.n = fft_size[0], .is = n, .os = n};
408 fft_iodim howmany = {.n = block_sizes[0], .is = 1, .os = 1};
410 get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
411 plans[0] = lookup_plan_from_cache(key);
412 if (plans[0] == NULL)
413 plans[0] = fft_fftw_create_guru_plan(key, input_c, output_c);
416 get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
417 plans_last_thread[0] = lookup_plan_from_cache(key);
418 if (plans_last_thread[0] == NULL)
419 plans_last_thread[0] =
420 fft_fftw_create_guru_plan(key, input_c, output_c);
422 }
else if (stage == 1) {
423 const int half = fft_size[2] / 2 + 1;
425 fft_iodim howmany[2] = {{.n = block_sizes[1],
426 .is = fft_size[1] *
half,
427 .os = fft_size[1] *
half},
428 {.n =
half, .is = 1, .os = 1}};
430 get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
431 plans[1] = lookup_plan_from_cache(key);
432 if (plans[1] == NULL)
433 plans[1] = fft_fftw_create_guru_plan(key, input_c, output_c);
436 get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
437 plans_last_thread[1] = lookup_plan_from_cache(key);
438 if (plans_last_thread[1] == NULL)
439 plans_last_thread[1] =
440 fft_fftw_create_guru_plan(key, input_c, output_c);
443 const int half = fft_size[2] / 2 + 1;
444 fft_iodim dim = {.n = fft_size[2], .is = 1, .os = 1};
447 .is = direction ? (in_place ? 2 *
half : fft_size[2]) :
half,
448 .os = direction ?
half : (in_place ? 2 *
half : fft_size[2])};
450 get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
451 plans[2] = lookup_plan_from_cache(key);
452 if (plans[2] == NULL)
453 plans[2] = fft_fftw_create_guru_plan_r2c(key, grid_real,
454 (
double *)grid_complex);
457 get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
458 plans_last_thread[2] = lookup_plan_from_cache(key);
459 if (plans_last_thread[2] == NULL)
460 plans_last_thread[2] = fft_fftw_create_guru_plan_r2c(
461 key, grid_real, (
double *)grid_complex);
472static void execute_threaded_3d_r2c_1d(
const bool direction,
473 const int fft_size[3],
474 const bool in_place,
double *grid_real,
475 double complex *grid_complex) {
476 const int half = fft_size[2] / 2 + 1;
477 double complex *buffer =
478 direction && in_place
480 : fftw_alloc_complex(fft_size[0] * fft_size[1] * half);
481 double complex *input_c = direction ? buffer : grid_complex;
482 double complex *output_c = direction ? grid_complex : buffer;
485 int number_of_full_blocks[3] = {0, 0, 0};
486 int total_number_of_blocks[3] = {0, 0, 0};
487 fftw_plan *plans[3], *plans_last_thread[3];
488 for (
int stage = 0; stage < 3; stage++) {
490 plans_last_thread[stage] = NULL;
491 const int extent = stage == 0 ? fft_size[1] *
half
492 : stage == 1 ? fft_size[0]
493 : fft_size[0] * fft_size[1];
494 block_sizes[stage] = (extent + number_of_threads - 1) / number_of_threads;
495 number_of_full_blocks[stage] = extent / block_sizes[stage];
496 total_number_of_blocks[stage] =
497 (extent + block_sizes[stage] - 1) / block_sizes[stage];
498 const bool needs_tail = block_sizes[stage] * number_of_threads != extent;
499 const int tail = needs_tail ? extent % block_sizes[stage] : 0;
501 const int n = fft_size[1] *
half;
502 fft_iodim dim = {.n = fft_size[0], .is = n, .os = n};
503 fft_iodim howmany = {.n = block_sizes[0], .is = 1, .os = 1};
505 get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
506 plans[0] = lookup_plan_from_cache(key);
507 if (plans[0] == NULL) {
508 double complex *tmp = fftw_alloc_complex(get_buffer_size_from_key(key));
509 plans[0] = fft_fftw_create_guru_plan(key, tmp, output_c);
514 get_key_guru(direction, 1, &dim, 1, &howmany, 1, in_place, key);
515 plans_last_thread[0] = lookup_plan_from_cache(key);
516 if (plans_last_thread[0] == NULL) {
517 double complex *tmp =
518 fftw_alloc_complex(get_buffer_size_from_key(key));
519 plans_last_thread[0] = fft_fftw_create_guru_plan(key, tmp, output_c);
523 }
else if (stage == 1) {
525 fft_iodim howmany[2] = {{.n = block_sizes[1],
526 .is = fft_size[1] *
half,
527 .os = fft_size[1] *
half},
528 {.n =
half, .is = 1, .os = 1}};
530 get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
531 plans[1] = lookup_plan_from_cache(key);
532 if (plans[1] == NULL) {
533 double complex *tmp = fftw_alloc_complex(get_buffer_size_from_key(key));
534 plans[1] = fft_fftw_create_guru_plan(key, tmp, output_c);
539 get_key_guru(direction, 1, &dim, 2, howmany, 1, in_place, key);
540 plans_last_thread[1] = lookup_plan_from_cache(key);
541 if (plans_last_thread[1] == NULL) {
542 double complex *tmp =
543 fftw_alloc_complex(get_buffer_size_from_key(key));
544 plans_last_thread[1] = fft_fftw_create_guru_plan(key, tmp, output_c);
549 fft_iodim dim = {.n = fft_size[2], .is = 1, .os = 1};
552 .is = direction ? (in_place ? 2 *
half : fft_size[2]) :
half,
553 .os = direction ?
half : (in_place ? 2 *
half : fft_size[2])};
555 get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
556 plans[2] = lookup_plan_from_cache(key);
557 if (plans[2] == NULL) {
558 double *tmp = fftw_alloc_real(2 * get_buffer_size_from_key(key));
559 plans[2] = fft_fftw_create_guru_plan_r2c(key, tmp, (
double *)output_c);
564 get_key_guru_r2c(direction, 1, &dim, 1, &howmany, 1, in_place, key);
565 plans_last_thread[2] = lookup_plan_from_cache(key);
566 if (plans_last_thread[2] == NULL) {
567 double *tmp = fftw_alloc_real(2 * get_buffer_size_from_key(key));
568 plans_last_thread[2] =
569 fft_fftw_create_guru_plan_r2c(key, tmp, (
double *)output_c);
575 const int real_stride =
576 direction ? (in_place ? 2 *
half : fft_size[2]) :
half;
577 const int complex_stride =
578 direction ?
half : (in_place ? 2 *
half : fft_size[2]);
579#pragma omp parallel default(none) \
580 shared(number_of_threads, number_of_full_blocks, total_number_of_blocks, \
581 plans, plans_last_thread, block_sizes, fft_size, grid_real, \
582 grid_complex, in_place, direction, input_c, output_c, \
583 real_stride, complex_stride)
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);
595 if (thread_id < number_of_full_blocks[1]) {
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) {
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,
606 if (thread_id < number_of_full_blocks[2]) {
607 const int start = block_sizes[2] * thread_id;
609 fftw_execute_dft_r2c(*plans[2], grid_real + start * real_stride,
610 grid_complex + start * complex_stride);
612 fftw_execute_dft_c2r(*plans[2], grid_complex + start * complex_stride,
613 grid_real + start * real_stride);
615 }
else if (thread_id == total_number_of_blocks[2] - 1) {
616 const int start = block_sizes[2] * thread_id;
618 fftw_execute_dft_r2c(*plans_last_thread[2],
619 grid_real + start * real_stride,
620 grid_complex + start * complex_stride);
622 fftw_execute_dft_c2r(*plans_last_thread[2],
623 grid_complex + start * complex_stride,
624 grid_real + start * real_stride);
628 if (!(direction && in_place))
635static void acquire_threaded_3d_c2c_1d(
636 const bool direction,
const int fft_size[3],
const bool in_place,
637 const bool for_execution,
double complex *grid_in,
double complex *grid_out,
638 int block_sizes[3], fftw_plan *plans[3], fftw_plan *plans_last_thread[3],
639 int number_of_full_blocks[3],
int total_number_of_blocks[3]) {
641 for (
int stage = 0; stage < 3; stage++) {
643 plans_last_thread[stage] = NULL;
644 const int extent = stage == 0 ? fft_size[1] * fft_size[2]
645 : stage == 1 ? fft_size[0]
646 : fft_size[0] * fft_size[1];
647 block_sizes[stage] = (extent + number_of_threads - 1) / number_of_threads;
648 number_of_full_blocks[stage] = extent / block_sizes[stage];
649 total_number_of_blocks[stage] =
650 (extent + block_sizes[stage] - 1) / block_sizes[stage];
651 const bool needs_tail = block_sizes[stage] * number_of_threads != extent;
652 const int tail = needs_tail ? extent % block_sizes[stage] : 0;
657 int howmany_rank = 1;
659 const int n = fft_size[1] * fft_size[2];
660 dim[0] = (
fft_iodim){.n = fft_size[0], .is = n, .os = n};
661 howmany[0] = (
fft_iodim){.n = block_sizes[0], .is = 1, .os = 1};
662 }
else if (stage == 1) {
664 (
fft_iodim){.n = fft_size[1], .is = fft_size[2], .os = fft_size[2]};
665 howmany[0] = (
fft_iodim){.n = block_sizes[1],
666 .is = fft_size[1] * fft_size[2],
667 .os = fft_size[1] * fft_size[2]};
668 howmany[1] = (
fft_iodim){.n = fft_size[2], .is = 1, .os = 1};
671 dim[0] = (
fft_iodim){.n = fft_size[2], .is = 1, .os = 1};
673 .n = block_sizes[2], .is = fft_size[2], .os = fft_size[2]};
677 get_key_guru(direction, rank, dim, howmany_rank, howmany, 1, in_place, key);
678 plans[stage] = lookup_plan_from_cache(key);
679 if (plans[stage] == NULL) {
681 double complex *buffer =
682 fftw_alloc_complex(get_buffer_size_from_key(key));
683 plans[stage] = fft_fftw_create_guru_plan(key, buffer,
684 in_place ? buffer : grid_out);
687 plans[stage] = fft_fftw_create_guru_plan(key, grid_in, grid_out);
692 get_key_guru(direction, rank, dim, howmany_rank, howmany, 1, in_place,
694 plans_last_thread[stage] = lookup_plan_from_cache(key);
695 if (plans_last_thread[stage] == NULL) {
697 double complex *buffer =
698 fftw_alloc_complex(get_buffer_size_from_key(key));
699 plans_last_thread[stage] = fft_fftw_create_guru_plan(
700 key, buffer, in_place ? buffer : grid_out);
703 plans_last_thread[stage] =
704 fft_fftw_create_guru_plan(key, grid_in, grid_out);
714static void register_threaded_3d_c2c_1d(
const bool direction,
715 const int fft_size[3],
717 double complex *grid_in,
718 double complex *grid_out) {
720 int number_of_full_blocks[3];
721 int total_number_of_blocks[3];
722 fftw_plan *plans[3], *plans_last_thread[3];
723 acquire_threaded_3d_c2c_1d(direction, fft_size, in_place,
false, grid_in,
724 grid_out, block_sizes, plans, plans_last_thread,
725 number_of_full_blocks, total_number_of_blocks);
731static void execute_threaded_3d_c2c_1d(
const bool direction,
732 const int fft_size[3],
734 double complex *grid_in,
735 double complex *grid_out) {
737 int number_of_full_blocks[3];
738 int total_number_of_blocks[3];
739 fftw_plan *plans[3], *plans_last_thread[3];
740 acquire_threaded_3d_c2c_1d(direction, fft_size, in_place,
true, grid_in,
741 grid_out, block_sizes, plans, plans_last_thread,
742 number_of_full_blocks, total_number_of_blocks);
743#pragma omp parallel default(none) \
744 shared(number_of_full_blocks, total_number_of_blocks, plans, \
745 plans_last_thread, block_sizes, fft_size, grid_in, grid_out)
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);
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,
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);
788 const bool use_guru_interface,
const char *wisdom_file) {
791 if (is_initialized) {
795 imax(1, omp_get_max_threads());
797 calloc((
size_t)number_of_caches * FFTW_CACHE_SIZE,
sizeof(cache_entry));
798 cache_oldest_entries = calloc((
size_t)number_of_caches,
sizeof(
int));
799 cp_assert(caches != NULL && cache_oldest_entries != NULL);
800 omp_init_nest_lock(&cache_lock);
802 fftw_planning_mode = fftw_planning_flag;
803 switch (fftw_planning_flag) {
805 fftw_planning_mode = FFTW_ESTIMATE;
808 fftw_planning_mode = FFTW_MEASURE;
811 fftw_planning_mode = FFTW_PATIENT;
814 fftw_planning_mode = FFTW_EXHAUSTIVE;
817 cp_assert(0 &&
"Unknown FFTW planning flag.");
821 const int status = fftw_init_threads();
832 if (wisdom_file != NULL) {
834 const int error = fftw_import_wisdom_from_filename(wisdom_file);
835 if (
false && error != 0 && is_print_rank)
837 "Importing wisdom failed! Maybe the file does not exist.");
840 is_initialized =
true;
843 cp_assert(
false &&
"The grid library was not compiled with FFTW support.");
844 (void)fftw_planning_flag;
845 (void)use_guru_interface;
859 if (!is_initialized) {
862 for (
int t = 0; t < number_of_caches; t++) {
863 cache_entry *cache = &caches[t * FFTW_CACHE_SIZE];
864 for (
int i = 0;
i < FFTW_CACHE_SIZE;
i++) {
865 if (cache[
i].plan != NULL) {
866 fftw_destroy_plan(*cache[
i].plan);
873 free(cache_oldest_entries);
874 cache_oldest_entries = NULL;
875 number_of_caches = 0;
876 omp_destroy_nest_lock(&cache_lock);
879 if (wisdom_file != NULL) {
880 const int error = fftw_export_wisdom_to_filename(wisdom_file);
883 "Exporting wisdom failed! Maybe writing access is missing.");
885 is_initialized =
false;
886 fftw_planning_mode = -1;
887 fftw_cleanup_threads();
889 cp_assert(
false &&
"The grid library was not compiled with FFTW support.");
911 *buffer = fftw_alloc_real(
imax(1, length));
915 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
930 *buffer = fftw_alloc_complex(
imax(1, length));
934 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
949 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
964 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
977fftw_plan *fft_fftw_create_1d_plan(
const fft_key_t key,
double complex *grid_in,
978 double complex *grid_out) {
979 omp_set_nest_lock(&cache_lock);
980 fftw_plan *cached_plan = lookup_plan_from_cache(key);
981 if (cached_plan != NULL) {
982 omp_unset_nest_lock(&cache_lock);
985 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
986 bool direction, inplace;
987 int fft_size[3], inembed[3], onembed[3];
988 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
989 &number_of_threads, &direction, &inplace, inembed,
990 onembed, &idist, &odist, &istride, &ostride);
992 fftw_plan_with_nthreads(number_of_threads);
993 fftw_plan *plan = malloc(
sizeof(fftw_plan));
994 if (grid_in == grid_out) {
1001 *plan = fftw_plan_many_dft(rank, fft_size, number_of_ffts, grid_in, inembed,
1002 istride, idist, grid_out, onembed, ostride, odist,
1003 direction ? FFTW_FORWARD : FFTW_BACKWARD,
1004 fftw_planning_mode);
1006 add_plan_to_cache(key, plan);
1007 omp_unset_nest_lock(&cache_lock);
1017fftw_plan *fft_fftw_create_1d_plan_r2c(
const fft_key_t key,
double *grid_in,
1019 omp_set_nest_lock(&cache_lock);
1020 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1021 if (cached_plan != NULL) {
1022 omp_unset_nest_lock(&cache_lock);
1025 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1026 bool direction, inplace;
1027 int fft_size[3], inembed[3], onembed[3];
1028 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1029 &number_of_threads, &direction, &inplace, inembed,
1030 onembed, &idist, &odist, &istride, &ostride);
1032 fftw_plan_with_nthreads(number_of_threads);
1033 fftw_plan *plan = malloc(
sizeof(fftw_plan));
1034 if (grid_in == grid_out) {
1047 fftw_plan_many_dft_r2c(rank, fft_size, number_of_ffts, grid_in, inembed,
1048 istride, idist, (
double complex *)grid_out,
1049 onembed, ostride, odist, fftw_planning_mode);
1054 *plan = fftw_plan_many_dft_c2r(
1055 rank, fft_size, number_of_ffts, (
double complex *)grid_in, inembed,
1056 istride, idist, grid_out, onembed, ostride, odist, fftw_planning_mode);
1059 add_plan_to_cache(key, plan);
1060 omp_unset_nest_lock(&cache_lock);
1071fftw_plan *fft_fftw_create_2d_plan(
const fft_key_t key,
double complex *grid_in,
1072 double complex *grid_out) {
1073 omp_set_nest_lock(&cache_lock);
1074 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1075 if (cached_plan != NULL) {
1076 omp_unset_nest_lock(&cache_lock);
1079 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1080 bool direction, inplace;
1081 int fft_size[3], inembed[3], onembed[3];
1082 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1083 &number_of_threads, &direction, &inplace, inembed,
1084 onembed, &idist, &odist, &istride, &ostride);
1086 fftw_plan_with_nthreads(number_of_threads);
1087 fftw_plan *plan = malloc(
sizeof(fftw_plan));
1088 if (grid_in == grid_out) {
1093 *plan = fftw_plan_many_dft(rank, fft_size, number_of_ffts, grid_in, inembed,
1094 istride, idist, grid_out, onembed, ostride, odist,
1095 direction ? FFTW_FORWARD : FFTW_BACKWARD,
1096 fftw_planning_mode);
1098 add_plan_to_cache(key, plan);
1099 omp_unset_nest_lock(&cache_lock);
1110fftw_plan *fft_fftw_create_2d_plan_r2c(
const fft_key_t key,
double *grid_in,
1112 omp_set_nest_lock(&cache_lock);
1113 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1114 if (cached_plan != NULL) {
1115 omp_unset_nest_lock(&cache_lock);
1118 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1119 bool direction, inplace;
1120 int fft_size[3], inembed[3], onembed[3];
1121 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1122 &number_of_threads, &direction, &inplace, inembed,
1123 onembed, &idist, &odist, &istride, &ostride);
1125 fftw_plan_with_nthreads(number_of_threads);
1128 fftw_plan *plan = malloc(
sizeof(fftw_plan));
1129 if (grid_in == grid_out) {
1142 fftw_plan_many_dft_r2c(rank, fft_size, number_of_ffts, grid_in, inembed,
1143 istride, idist, (
double complex *)grid_out,
1144 onembed, ostride, odist, fftw_planning_mode);
1149 *plan = fftw_plan_many_dft_c2r(
1150 rank, fft_size, number_of_ffts, (
double complex *)grid_in, inembed,
1151 istride, idist, grid_out, onembed, ostride, odist, fftw_planning_mode);
1154 add_plan_to_cache(key, plan);
1155 omp_unset_nest_lock(&cache_lock);
1166fftw_plan *fft_fftw_create_3d_plan(
const fft_key_t key,
double complex *grid_in,
1167 double complex *grid_out) {
1168 omp_set_nest_lock(&cache_lock);
1169 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1170 if (cached_plan != NULL) {
1171 omp_unset_nest_lock(&cache_lock);
1174 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1175 bool direction, inplace;
1176 int fft_size[3], inembed[3], onembed[3];
1177 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1178 &number_of_threads, &direction, &inplace, inembed,
1179 onembed, &idist, &odist, &istride, &ostride);
1181 fftw_plan_with_nthreads(number_of_threads);
1182 fftw_plan *plan = malloc(
sizeof(fftw_plan));
1183 *plan = fftw_plan_many_dft(rank, fft_size, number_of_ffts, grid_in, inembed,
1184 istride, idist, grid_out, onembed, ostride, odist,
1185 direction ? FFTW_FORWARD : FFTW_BACKWARD,
1186 fftw_planning_mode);
1187 add_plan_to_cache(key, plan);
1189 omp_unset_nest_lock(&cache_lock);
1200fftw_plan *fft_fftw_create_3d_plan_r2c(
const fft_key_t key,
double *grid_in,
1202 omp_set_nest_lock(&cache_lock);
1203 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1204 if (cached_plan != NULL) {
1205 omp_unset_nest_lock(&cache_lock);
1208 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
1209 bool direction, inplace;
1210 int fft_size[3], inembed[3], onembed[3];
1211 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
1212 &number_of_threads, &direction, &inplace, inembed,
1213 onembed, &idist, &odist, &istride, &ostride);
1215 fftw_plan_with_nthreads(number_of_threads);
1216 fftw_plan *plan = malloc(
sizeof(fftw_plan));
1219 fftw_plan_many_dft_r2c(rank, fft_size, number_of_ffts, grid_in, inembed,
1220 istride, idist, (
double complex *)grid_out,
1221 onembed, ostride, odist, fftw_planning_mode);
1226 *plan = fftw_plan_many_dft_c2r(
1227 rank, fft_size, number_of_ffts, (
double complex *)grid_in, inembed,
1228 istride, idist, grid_out, onembed, ostride, odist, fftw_planning_mode);
1230 add_plan_to_cache(key, plan);
1232 omp_unset_nest_lock(&cache_lock);
1243fftw_plan *fft_fftw_create_guru_plan(
const fft_key_t key,
1244 double complex *grid_in,
1245 double complex *grid_out) {
1247 omp_set_nest_lock(&cache_lock);
1248 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1249 if (cached_plan != NULL) {
1250 omp_unset_nest_lock(&cache_lock);
1253 bool direction, inplace;
1254 int rank, howmany_rank, number_of_threads;
1256 fetch_data_from_key_guru(key, &direction, &rank, dims, &howmany_rank,
1257 howmany_dims, &number_of_threads, &inplace);
1259 fftw_plan_with_nthreads(number_of_threads);
1260 fftw_plan *plan = malloc(
sizeof(fftw_plan));
1261 if ((
double complex *)grid_in == grid_out) {
1263 for (
int r = 0; r < rank; r++)
1265 for (
int r = 0; r < howmany_rank; r++)
1266 cp_assert(howmany_dims[r].is == howmany_dims[r].os);
1270 *plan = fftw_plan_guru_dft(rank, dims, howmany_rank, howmany_dims, grid_in,
1271 grid_out, direction ? FFTW_FORWARD : FFTW_BACKWARD,
1272 fftw_planning_mode);
1273 add_plan_to_cache(key, plan);
1275 omp_unset_nest_lock(&cache_lock);
1286fftw_plan *fft_fftw_create_guru_plan_r2c(
const fft_key_t key,
double *grid_in,
1289 omp_set_nest_lock(&cache_lock);
1290 fftw_plan *cached_plan = lookup_plan_from_cache(key);
1291 if (cached_plan != NULL) {
1292 omp_unset_nest_lock(&cache_lock);
1295 bool direction, inplace;
1296 int rank, howmany_rank, number_of_threads;
1298 fetch_data_from_key_guru(key, &direction, &rank, dims, &howmany_rank,
1299 howmany_dims, &number_of_threads, &inplace);
1301 fftw_plan_with_nthreads(number_of_threads);
1302 fftw_plan *plan = malloc(
sizeof(fftw_plan));
1304 if (grid_in == grid_out) {
1306 for (
int r = 0; r < rank; r++)
1307 cp_assert(dims[r].is == 2 * dims[r].os ||
1308 (dims[r].is == 1 && dims[r].os == 1));
1309 for (
int r = 0; r < howmany_rank; r++)
1310 cp_assert(howmany_dims[r].is == 2 * howmany_dims[r].os);
1315 fftw_plan_guru_dft_r2c(rank, dims, howmany_rank, howmany_dims, grid_in,
1316 (
double complex *)grid_out, fftw_planning_mode);
1318 if (grid_in == grid_out) {
1320 for (
int r = 0; r < rank; r++)
1321 cp_assert(2 * dims[r].is == dims[r].os ||
1322 (dims[r].is == 1 && dims[r].os == 1));
1323 for (
int r = 0; r < howmany_rank; r++)
1324 cp_assert(2 * howmany_dims[r].is == howmany_dims[r].os);
1331 *plan = fftw_plan_guru_dft_c2r(rank, dims, howmany_rank, howmany_dims,
1332 (
double complex *)grid_in, grid_out,
1333 fftw_planning_mode);
1335 add_plan_to_cache(key, plan);
1337 omp_unset_nest_lock(&cache_lock);
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) {
1362 if (fft_size == 0 || number_of_ffts == 0)
1364 const bool in_place = grid_in == grid_out;
1366 if (fftw_planning_mode == FFTW_ESTIMATE) {
1367 fftw_plan *plan = NULL;
1368 const int block_size =
1369 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1370 const int number_of_full_blocks = number_of_ffts / block_size;
1372 get_key_1d(direction, fft_size, block_size, transpose_in, transpose_out,
1373 leading_dimension_in, leading_dimension_out, 1, in_place, key);
1374 plan = lookup_plan_from_cache(key);
1376 plan = fft_fftw_create_1d_plan(key, grid_in, grid_out);
1377 if (block_size * number_of_full_blocks != number_of_ffts) {
1378 const int block_size_last_thread = number_of_ffts % block_size;
1380 get_key_1d(direction, fft_size, block_size_last_thread, transpose_in,
1381 transpose_out, leading_dimension_in, leading_dimension_out, 1,
1383 plan = lookup_plan_from_cache(key);
1385 plan = fft_fftw_create_1d_plan(key, grid_in, grid_out);
1389 get_key_1d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
1390 leading_dimension_in, leading_dimension_out,
fft_team_size(),
1392 fftw_plan *plan = lookup_plan_from_cache(key);
1394 plan = fft_fftw_create_1d_plan(key, grid_in, grid_out);
1399 (void)number_of_ffts;
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.");
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) {
1431 if (fft_size == 0 || number_of_ffts == 0)
1433 const bool in_place = grid_in == (
double *)grid_out;
1437 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1438 fftw_plan *plan = NULL;
1439 const int block_size =
1440 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1441 const int number_of_full_blocks = number_of_ffts / block_size;
1443 get_key_1d_r2c(
true, fft_size, block_size, transpose_in, transpose_out,
1444 leading_dimension_in, leading_dimension_out, 1, in_place,
1446 plan = lookup_plan_from_cache(key);
1448 plan = fft_fftw_create_1d_plan_r2c(key, grid_in, (
double *)grid_out);
1450 if (block_size * number_of_full_blocks != number_of_ffts) {
1451 const int block_size_last_thread = number_of_ffts % block_size;
1453 get_key_1d_r2c(
true, fft_size, block_size_last_thread, transpose_in,
1454 transpose_out, leading_dimension_in, leading_dimension_out,
1456 plan = lookup_plan_from_cache(key);
1458 plan = fft_fftw_create_1d_plan_r2c(key, grid_in, (
double *)grid_out);
1463 get_key_1d_r2c(
true, fft_size, number_of_ffts, transpose_in, transpose_out,
1464 leading_dimension_in, leading_dimension_out,
fft_team_size(),
1466 fftw_plan *plan = lookup_plan_from_cache(key);
1468 plan = fft_fftw_create_1d_plan_r2c(key, grid_in, (
double *)grid_out);
1473 (void)number_of_ffts;
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.");
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) {
1505 if (fft_size == 0 || number_of_ffts == 0)
1507 const bool in_place = (
double *)grid_in == grid_out;
1511 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1512 fftw_plan *plan = NULL;
1513 const int block_size =
1514 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1515 const int number_of_full_blocks = number_of_ffts / block_size;
1517 get_key_1d_r2c(
false, fft_size, block_size, transpose_in, transpose_out,
1518 leading_dimension_in, leading_dimension_out, 1, in_place,
1520 plan = lookup_plan_from_cache(key);
1522 plan = fft_fftw_create_1d_plan_r2c(key, (
double *)grid_in, grid_out);
1524 if (block_size * number_of_full_blocks != number_of_ffts) {
1525 const int block_size_last_thread = number_of_ffts % block_size;
1527 get_key_1d_r2c(
false, fft_size, block_size_last_thread, transpose_in,
1528 transpose_out, leading_dimension_in, leading_dimension_out,
1530 plan = lookup_plan_from_cache(key);
1532 plan = fft_fftw_create_1d_plan_r2c(key, (
double *)grid_in, grid_out);
1537 get_key_1d_r2c(
false, fft_size, number_of_ffts, transpose_in, transpose_out,
1538 leading_dimension_in, leading_dimension_out,
fft_team_size(),
1540 fftw_plan *plan = lookup_plan_from_cache(key);
1542 plan = fft_fftw_create_1d_plan_r2c(key, (
double *)grid_in, grid_out);
1547 (void)number_of_ffts;
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.");
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) {
1577 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
1579 const bool in_place = grid_in == grid_out;
1581 if (fftw_planning_mode == FFTW_ESTIMATE) {
1582 fftw_plan *plan = NULL, *plan_last_thread = NULL;
1583 const int block_size =
1584 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1585 const int number_of_full_blocks = number_of_ffts / block_size;
1587 get_key_2d(direction, fft_size, block_size, transpose_in, transpose_out,
1588 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
1589 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
1591 plan = lookup_plan_from_cache(key);
1593 plan = fft_fftw_create_2d_plan(key, grid_in, grid_out);
1595 if (block_size * number_of_full_blocks != number_of_ffts) {
1596 const int block_size_last_thread = number_of_ffts % block_size;
1598 get_key_2d(direction, fft_size, block_size_last_thread, transpose_in,
1600 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
1601 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
1603 plan_last_thread = lookup_plan_from_cache(key);
1604 if (plan_last_thread == NULL) {
1605 plan_last_thread = fft_fftw_create_2d_plan(key, grid_in, grid_out);
1610 get_key_2d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
1611 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
1612 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1],
1614 fftw_plan *plan = lookup_plan_from_cache(key);
1616 plan = fft_fftw_create_2d_plan(key, grid_in, grid_out);
1622 (void)number_of_ffts;
1626 (void)transpose_out;
1627 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
1642 const int number_of_ffts,
1643 const bool transpose_in,
1644 const bool transpose_out,
double *grid_in,
1645 double complex *grid_out) {
1648 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
1650 const bool in_place = grid_in == (
double *)grid_out;
1654 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1655 fftw_plan *plan = NULL, *plan_last_thread = NULL;
1656 const int block_size =
1657 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1658 const int number_of_full_blocks = number_of_ffts / block_size;
1661 true, fft_size, block_size, transpose_in, transpose_out,
1662 transpose_in ? number_of_ffts
1664 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
1665 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1), 1,
1667 plan = lookup_plan_from_cache(key);
1669 plan = fft_fftw_create_2d_plan_r2c(key, grid_in, (
double *)grid_out);
1671 if (block_size * number_of_full_blocks != number_of_ffts) {
1672 const int block_size_last_thread = number_of_ffts % block_size;
1675 true, fft_size, block_size_last_thread, transpose_in, transpose_out,
1676 transpose_in ? number_of_ffts
1677 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1679 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1681 plan_last_thread = lookup_plan_from_cache(key);
1682 if (plan_last_thread == NULL) {
1684 fft_fftw_create_2d_plan_r2c(key, grid_in, (
double *)grid_out);
1690 true, fft_size, number_of_ffts, transpose_in, transpose_out,
1691 transpose_in ? number_of_ffts
1693 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
1694 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1696 fftw_plan *plan = lookup_plan_from_cache(key);
1698 plan = fft_fftw_create_2d_plan_r2c(key, grid_in, (
double *)grid_out);
1703 (void)number_of_ffts;
1707 (void)transpose_out;
1708 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
1723 const int number_of_ffts,
1724 const bool transpose_in,
1725 const bool transpose_out,
1726 double complex *grid_in,
double *grid_out) {
1729 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
1731 const bool in_place = (
double *)grid_in == grid_out;
1735 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
1736 fftw_plan *plan = NULL, *plan_last_thread = NULL;
1737 const int block_size =
1738 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1739 const int number_of_full_blocks = number_of_ffts / block_size;
1742 false, fft_size, block_size, transpose_in, transpose_out,
1743 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1744 transpose_out ? number_of_ffts
1745 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1748 plan = lookup_plan_from_cache(key);
1750 plan = fft_fftw_create_2d_plan_r2c(key, (
double *)grid_in, grid_out);
1752 if (block_size * number_of_full_blocks != number_of_ffts) {
1753 const int block_size_last_thread = number_of_ffts % block_size;
1756 false, fft_size, block_size_last_thread, transpose_in, transpose_out,
1757 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1758 transpose_out ? number_of_ffts
1759 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1762 plan_last_thread = lookup_plan_from_cache(key);
1763 if (plan_last_thread == NULL) {
1765 fft_fftw_create_2d_plan_r2c(key, (
double *)grid_in, grid_out);
1771 false, fft_size, number_of_ffts, transpose_in, transpose_out,
1772 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
1773 transpose_out ? number_of_ffts
1774 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
1776 fft_team_size(), grid_in == (
double complex *)grid_out, key);
1777 fftw_plan *plan = lookup_plan_from_cache(key);
1779 plan = fft_fftw_create_2d_plan_r2c(key, (
double *)grid_in, grid_out);
1784 (void)number_of_ffts;
1788 (void)transpose_out;
1789 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
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) {
1812 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
1813 number_of_ffts == 0)
1815 const bool in_place = grid_in == grid_out;
1816 if (number_of_ffts > 1 &&
1817 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1818 register_threaded_3d_c2c(direction, fft_size, number_of_ffts, in_place,
1820 }
else if (number_of_ffts == 1 &&
1821 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1822 register_threaded_3d_c2c_1d(direction, fft_size, in_place, grid_in,
1826 get_key_3d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
1828 fftw_plan *plan = lookup_plan_from_cache(key);
1830 plan = fft_fftw_create_3d_plan(key, grid_in, grid_out);
1835 (void)number_of_ffts;
1837 (void)transpose_out;
1840 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
1855 const int number_of_ffts,
1856 const bool transpose_in,
1857 const bool transpose_out,
double *grid_in,
1858 double complex *grid_out) {
1861 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
1862 number_of_ffts == 0)
1864 const bool in_place = grid_in == (
double *)grid_out;
1865 if (number_of_ffts > 1 &&
1866 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1867 register_threaded_3d_r2c(
true, fft_size, number_of_ffts, in_place, grid_in,
1868 (
double *)grid_out);
1869 }
else if (number_of_ffts == 1 &&
1870 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1871 register_threaded_3d_r2c_1d(
true, fft_size, in_place, grid_in, grid_out);
1874 get_key_3d_r2c(
true, fft_size, number_of_ffts, transpose_in, transpose_out,
1876 fftw_plan *plan = lookup_plan_from_cache(key);
1878 plan = fft_fftw_create_3d_plan_r2c(key, grid_in, (
double *)grid_out);
1883 (void)number_of_ffts;
1885 (void)transpose_out;
1888 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
1903 const int number_of_ffts,
1904 const bool transpose_in,
1905 const bool transpose_out,
1906 double complex *grid_in,
double *grid_out) {
1909 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
1910 number_of_ffts == 0)
1912 const bool in_place = (
double *)grid_in == grid_out;
1913 if (number_of_ffts > 1 &&
1914 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1915 register_threaded_3d_r2c(
false, fft_size, number_of_ffts, in_place,
1916 (
double *)grid_in, grid_out);
1917 }
else if (number_of_ffts == 1 &&
1918 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
1919 register_threaded_3d_r2c_1d(
false, fft_size, in_place, grid_out, grid_in);
1922 get_key_3d_r2c(
false, fft_size, number_of_ffts, transpose_in, transpose_out,
1924 fftw_plan *plan = lookup_plan_from_cache(key);
1926 plan = fft_fftw_create_3d_plan_r2c(key, (
double *)grid_in, grid_out);
1931 (void)number_of_ffts;
1933 (void)transpose_out;
1936 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
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) {
1960 if (fft_size == 0 || number_of_ffts == 0)
1962 const bool in_place = grid_in == grid_out;
1964 if (number_of_threads == 1 && fftw_planning_mode == FFTW_ESTIMATE) {
1966 fftw_plan *plan = NULL, *plan_last_thread = NULL;
1967 const int block_size =
1968 (number_of_ffts + number_of_threads - 1) / number_of_threads;
1969 const int number_of_full_blocks = number_of_ffts / block_size;
1970 const int total_number_of_blocks =
1971 number_of_full_blocks + (number_of_ffts % block_size != 0);
1973 get_key_1d(direction, fft_size, block_size, transpose_in, transpose_out,
1974 leading_dimension_in, leading_dimension_out, 1, in_place, key);
1975 plan = lookup_plan_from_cache(key);
1977 double complex *buffer =
1978 fftw_alloc_complex(get_buffer_size_from_key(key));
1979 plan = fft_fftw_create_1d_plan(key, buffer, in_place ? buffer : grid_out);
1982 if (block_size * number_of_full_blocks != number_of_ffts) {
1983 const int block_size_last_thread = number_of_ffts % block_size;
1985 get_key_1d(direction, fft_size, block_size_last_thread, transpose_in,
1986 transpose_out, leading_dimension_in, leading_dimension_out, 1,
1987 in_place, key_last);
1988 plan_last_thread = lookup_plan_from_cache(key_last);
1989 if (plan_last_thread == NULL) {
1990 double complex *buffer =
1991 fftw_alloc_complex(get_buffer_size_from_key(key_last));
1992 plan_last_thread = fft_fftw_create_1d_plan(
1993 key_last, buffer, in_place ? buffer : grid_out);
1997 const int offset_in =
1998 transpose_in ? block_size : block_size * leading_dimension_in;
1999 const int offset_out =
2000 transpose_out ? block_size : block_size * leading_dimension_out;
2001#pragma omp parallel num_threads(number_of_threads) default(none) \
2002 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2003 offset_out, plan_last_thread, total_number_of_blocks)
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);
2016 get_key_1d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
2017 leading_dimension_in, leading_dimension_out, number_of_threads,
2019 fftw_plan *plan = lookup_plan_from_cache(key);
2021 double complex *buffer =
2022 fftw_alloc_complex(get_buffer_size_from_key(key));
2023 plan = fft_fftw_create_1d_plan(key, buffer, in_place ? buffer : grid_out);
2026 fftw_execute_dft(*plan, grid_in, grid_out);
2031 (void)number_of_ffts;
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.");
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) {
2061 if (fft_size == 0 || number_of_ffts == 0)
2063 const bool in_place = grid_in == (
double *)grid_out;
2067 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2068 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2069 const int block_size =
2070 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2071 const int number_of_full_blocks = number_of_ffts / block_size;
2072 const int total_number_of_blocks =
2073 number_of_full_blocks + (number_of_ffts % block_size != 0);
2075 get_key_1d_r2c(
true, fft_size, block_size, transpose_in, transpose_out,
2076 leading_dimension_in, leading_dimension_out, 1, in_place,
2078 plan = lookup_plan_from_cache(key);
2080 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2081 plan = fft_fftw_create_1d_plan_r2c(
2082 key, buffer, in_place ? buffer : (
double *)grid_out);
2085 if (block_size * number_of_full_blocks != number_of_ffts) {
2086 const int block_size_last_thread = number_of_ffts % block_size;
2088 get_key_1d_r2c(
true, fft_size, block_size_last_thread, transpose_in,
2089 transpose_out, leading_dimension_in, leading_dimension_out,
2091 plan_last_thread = lookup_plan_from_cache(key);
2092 if (plan_last_thread == NULL) {
2093 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2094 plan_last_thread = fft_fftw_create_1d_plan_r2c(
2095 key, buffer, in_place ? buffer : (
double *)grid_out);
2099 const int offset_in =
2100 transpose_in ? block_size : block_size * leading_dimension_in;
2101 const int offset_out =
2102 transpose_out ? block_size : block_size * leading_dimension_out;
2103#pragma omp parallel default(none) \
2104 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2105 offset_out, plan_last_thread, total_number_of_blocks)
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);
2118 get_key_1d_r2c(
true, fft_size, number_of_ffts, transpose_in, transpose_out,
2119 leading_dimension_in, leading_dimension_out,
fft_team_size(),
2121 fftw_plan *plan = lookup_plan_from_cache(key);
2123 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2124 plan = fft_fftw_create_1d_plan_r2c(
2125 key, buffer, in_place ? buffer : (
double *)grid_out);
2129 fftw_execute_dft_r2c(*plan, grid_in, grid_out);
2133 (void)number_of_ffts;
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.");
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) {
2163 if (fft_size == 0 || number_of_ffts == 0)
2165 const bool in_place = (
double *)grid_in == grid_out;
2169 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2170 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2171 const int block_size =
2172 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2173 const int number_of_full_blocks = number_of_ffts / block_size;
2174 const int total_number_of_blocks =
2175 number_of_full_blocks + (number_of_ffts % block_size != 0);
2177 get_key_1d_r2c(
false, fft_size, block_size, transpose_in, transpose_out,
2178 leading_dimension_in, leading_dimension_out, 1, in_place,
2180 plan = lookup_plan_from_cache(key);
2182 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2183 plan = fft_fftw_create_1d_plan_r2c(key, buffer,
2184 in_place ? buffer : grid_out);
2187 if (block_size * number_of_full_blocks != number_of_ffts) {
2188 const int block_size_last_thread = number_of_ffts % block_size;
2190 get_key_1d_r2c(
false, fft_size, block_size_last_thread, transpose_in,
2191 transpose_out, leading_dimension_in, leading_dimension_out,
2193 plan_last_thread = lookup_plan_from_cache(key);
2194 if (plan_last_thread == NULL) {
2195 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2196 plan_last_thread = fft_fftw_create_1d_plan_r2c(
2197 key, buffer, in_place ? buffer : grid_out);
2201 const int offset_in =
2202 transpose_in ? block_size : block_size * leading_dimension_in;
2203 const int offset_out =
2204 transpose_out ? block_size : block_size * leading_dimension_out;
2205#pragma omp parallel default(none) \
2206 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2207 offset_out, plan_last_thread, total_number_of_blocks)
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);
2220 get_key_1d_r2c(
false, fft_size, number_of_ffts, transpose_in, transpose_out,
2221 leading_dimension_in, leading_dimension_out,
fft_team_size(),
2222 grid_in == (
double complex *)grid_out, key);
2223 fftw_plan *plan = lookup_plan_from_cache(key);
2225 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2226 plan = fft_fftw_create_1d_plan_r2c(key, buffer,
2227 in_place ? buffer : grid_out);
2231 fftw_execute_dft_c2r(*plan, grid_in, grid_out);
2235 (void)number_of_ffts;
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.");
2258 const int number_of_ffts,
const bool transpose_in,
2259 const bool transpose_out,
double complex *grid_in,
2260 double complex *grid_out) {
2263 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
2265 const bool in_place = grid_in == grid_out;
2267 if (fftw_planning_mode == FFTW_ESTIMATE) {
2268 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2269 const int block_size =
2270 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2271 const int number_of_full_blocks = number_of_ffts / block_size;
2272 const int total_number_of_blocks =
2273 number_of_full_blocks + (number_of_ffts % block_size != 0);
2275 get_key_2d(direction, fft_size, block_size, transpose_in, transpose_out,
2276 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
2277 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
2279 plan = lookup_plan_from_cache(key);
2281 double complex *buffer =
2282 fftw_alloc_complex(get_buffer_size_from_key(key));
2283 plan = fft_fftw_create_2d_plan(key, buffer, in_place ? buffer : grid_out);
2286 if (block_size * number_of_full_blocks != number_of_ffts) {
2287 const int block_size_last_thread = number_of_ffts % block_size;
2289 get_key_2d(direction, fft_size, block_size_last_thread, transpose_in,
2291 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
2292 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1], 1,
2294 plan_last_thread = lookup_plan_from_cache(key);
2295 if (plan_last_thread == NULL) {
2296 double complex *buffer =
2297 fftw_alloc_complex(get_buffer_size_from_key(key));
2299 fft_fftw_create_2d_plan(key, buffer, in_place ? buffer : grid_out);
2303 const int offset_in =
2304 transpose_in ? block_size : block_size * fft_size[0] * fft_size[1];
2305 const int offset_out =
2306 transpose_out ? block_size : block_size * fft_size[0] * fft_size[1];
2307#pragma omp parallel default(none) \
2308 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2309 offset_out, plan_last_thread, total_number_of_blocks)
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);
2322 get_key_2d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
2323 transpose_in ? number_of_ffts : fft_size[0] * fft_size[1],
2324 transpose_out ? number_of_ffts : fft_size[0] * fft_size[1],
2326 fftw_plan *plan = lookup_plan_from_cache(key);
2328 double complex *buffer =
2329 fftw_alloc_complex(get_buffer_size_from_key(key));
2330 plan = fft_fftw_create_2d_plan(key, buffer,
2331 grid_in == grid_out ? buffer : grid_out);
2334 fftw_execute_dft(*plan, grid_in, grid_out);
2339 (void)number_of_ffts;
2343 (void)transpose_out;
2344 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
2359 const bool transpose_in,
const bool transpose_out,
2360 double *grid_in,
double complex *grid_out) {
2363 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
2365 const bool in_place = grid_in == (
double *)grid_out;
2369 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2370 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2371 const int block_size =
2372 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2373 const int number_of_full_blocks = number_of_ffts / block_size;
2374 const int total_number_of_blocks =
2375 number_of_full_blocks + (number_of_ffts % block_size != 0);
2378 true, fft_size, block_size, transpose_in, transpose_out,
2379 transpose_in ? number_of_ffts
2381 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
2382 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1), 1,
2384 plan = lookup_plan_from_cache(key);
2386 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2387 plan = fft_fftw_create_2d_plan_r2c(
2388 key, buffer, in_place ? buffer : (
double *)grid_out);
2391 if (block_size * number_of_full_blocks != number_of_ffts) {
2392 const int block_size_last_thread = number_of_ffts % block_size;
2395 true, fft_size, block_size_last_thread, transpose_in, transpose_out,
2396 transpose_in ? number_of_ffts
2397 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2399 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2401 plan_last_thread = lookup_plan_from_cache(key);
2402 if (plan_last_thread == NULL) {
2403 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2404 plan_last_thread = fft_fftw_create_2d_plan_r2c(
2405 key, buffer, in_place ? buffer : (
double *)grid_out);
2409 const int offset_in =
2410 transpose_in ? block_size
2411 : block_size * fft_size[0] *
2412 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]);
2413 const int offset_out =
2414 transpose_out ? block_size
2415 : block_size * fft_size[0] * (fft_size[1] / 2 + 1);
2416#pragma omp parallel default(none) \
2417 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2418 offset_out, plan_last_thread, total_number_of_blocks)
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);
2432 true, fft_size, number_of_ffts, transpose_in, transpose_out,
2433 transpose_in ? number_of_ffts
2435 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]),
2436 transpose_out ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2438 fftw_plan *plan = lookup_plan_from_cache(key);
2440 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2441 plan = fft_fftw_create_2d_plan_r2c(
2443 grid_in == (
double *)grid_out ? buffer : (
double *)grid_out);
2446 fftw_execute_dft_r2c(*plan, grid_in, grid_out);
2450 (void)number_of_ffts;
2454 (void)transpose_out;
2455 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
2470 const bool transpose_in,
const bool transpose_out,
2471 double complex *grid_in,
double *grid_out) {
2474 if (fft_size[0] == 0 || fft_size[1] == 0 || number_of_ffts == 0)
2476 const bool in_place = (
double *)grid_in == grid_out;
2480 if (fftw_planning_mode == FFTW_ESTIMATE && (!in_place || !transpose_in)) {
2481 fftw_plan *plan = NULL, *plan_last_thread = NULL;
2482 const int block_size =
2483 (number_of_ffts + number_of_threads - 1) / number_of_threads;
2484 const int number_of_full_blocks = number_of_ffts / block_size;
2485 const int total_number_of_blocks =
2486 number_of_full_blocks + (number_of_ffts % block_size != 0);
2489 false, fft_size, block_size, transpose_in, transpose_out,
2490 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2491 transpose_out ? number_of_ffts
2492 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2495 plan = lookup_plan_from_cache(key);
2497 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2498 plan = fft_fftw_create_2d_plan_r2c(key, buffer,
2499 in_place ? buffer : grid_out);
2502 if (block_size * number_of_full_blocks != number_of_ffts) {
2503 const int block_size_last_thread = number_of_ffts % block_size;
2506 false, fft_size, block_size_last_thread, transpose_in, transpose_out,
2507 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2508 transpose_out ? number_of_ffts
2509 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2512 plan_last_thread = lookup_plan_from_cache(key);
2513 if (plan_last_thread == NULL) {
2514 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2515 plan_last_thread = fft_fftw_create_2d_plan_r2c(
2516 key, buffer, in_place ? buffer : grid_out);
2520 const int offset_in =
2521 transpose_in ? block_size
2522 : block_size * fft_size[0] * (fft_size[1] / 2 + 1);
2523 const int offset_out =
2526 : block_size * fft_size[0] *
2527 (in_place ? 2 * (fft_size[1] / 2 + 1) : fft_size[1]);
2528#pragma omp parallel default(none) \
2529 shared(number_of_full_blocks, plan, grid_in, grid_out, offset_in, \
2530 offset_out, plan_last_thread, total_number_of_blocks)
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);
2544 false, fft_size, number_of_ffts, transpose_in, transpose_out,
2545 transpose_in ? number_of_ffts : fft_size[0] * (fft_size[1] / 2 + 1),
2546 transpose_out ? number_of_ffts
2547 : fft_size[0] * (in_place ? 2 * (fft_size[1] / 2 + 1)
2549 fft_team_size(), grid_in == (
double complex *)grid_out, key);
2550 fftw_plan *plan = lookup_plan_from_cache(key);
2552 double complex *buffer =
2553 fftw_alloc_complex(get_buffer_size_from_key(key));
2554 plan = fft_fftw_create_2d_plan_r2c(
2555 key, (
double *)buffer,
2556 (
double *)grid_in == grid_out ? (
double *)buffer : grid_out);
2559 fftw_execute_dft_c2r(*plan, grid_in, grid_out);
2563 (void)number_of_ffts;
2567 (void)transpose_out;
2568 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
2584 const int number_of_ffts,
const bool transpose_in,
2585 const bool transpose_out,
double complex *grid_in,
2586 double complex *grid_out) {
2589 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
2590 number_of_ffts == 0)
2592 const bool in_place = grid_in == grid_out;
2593 if (number_of_ffts > 1 &&
2594 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2595 execute_threaded_3d_c2c(direction, fft_size, number_of_ffts, in_place,
2597 }
else if (number_of_ffts == 1 &&
2598 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2599 execute_threaded_3d_c2c_1d(direction, fft_size, in_place, grid_in,
2603 get_key_3d(direction, fft_size, number_of_ffts, transpose_in, transpose_out,
2605 fftw_plan *plan = lookup_plan_from_cache(key);
2607 double complex *buffer =
2608 fftw_alloc_complex(get_buffer_size_from_key(key));
2609 plan = fft_fftw_create_3d_plan(key, buffer, in_place ? buffer : grid_out);
2615 fftw_execute_dft(*plan, grid_in, grid_out);
2619 (void)number_of_ffts;
2621 (void)transpose_out;
2624 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
2639 const bool transpose_in,
const bool transpose_out,
2640 double *grid_in,
double complex *grid_out) {
2643 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
2644 number_of_ffts == 0)
2646 const bool in_place = grid_in == (
double *)grid_out;
2647 if (number_of_ffts > 1 &&
2648 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2649 execute_threaded_3d_r2c(
true, fft_size, number_of_ffts, in_place, grid_in,
2650 (
double *)grid_out);
2651 }
else if (number_of_ffts == 1 &&
2652 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2653 execute_threaded_3d_r2c_1d(
true, fft_size, in_place, grid_in, grid_out);
2656 get_key_3d_r2c(
true, fft_size, number_of_ffts, transpose_in, transpose_out,
2658 fftw_plan *plan = lookup_plan_from_cache(key);
2660 double *buffer = fftw_alloc_real(2 * get_buffer_size_from_key(key));
2661 plan = fft_fftw_create_3d_plan_r2c(
2662 key, buffer, in_place ? buffer : (
double *)grid_out);
2668 fftw_execute_dft_r2c(*plan, grid_in, grid_out);
2672 (void)number_of_ffts;
2674 (void)transpose_out;
2677 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
2692 const bool transpose_in,
const bool transpose_out,
2693 double complex *grid_in,
double *grid_out) {
2696 if (fft_size[0] == 0 || fft_size[1] == 0 || fft_size[2] == 0 ||
2697 number_of_ffts == 0)
2699 const bool in_place = (
double *)grid_in == grid_out;
2700 if (number_of_ffts > 1 &&
2701 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2702 execute_threaded_3d_r2c(
false, fft_size, number_of_ffts, in_place,
2703 (
double *)grid_in, grid_out);
2704 }
else if (number_of_ffts == 1 &&
2705 use_threaded_3d(fft_size, transpose_in, transpose_out, in_place)) {
2706 execute_threaded_3d_r2c_1d(
false, fft_size, in_place, grid_out, grid_in);
2709 get_key_3d_r2c(
false, fft_size, number_of_ffts, transpose_in, transpose_out,
2711 fftw_plan *plan = lookup_plan_from_cache(key);
2713 double complex *buffer =
2714 fftw_alloc_complex(get_buffer_size_from_key(key));
2715 plan = fft_fftw_create_3d_plan_r2c(
2716 key, (
double *)buffer, in_place ? (
double *)buffer : grid_out);
2722 fftw_execute_dft_c2r(*plan, grid_in, grid_out);
2726 (void)number_of_ffts;
2728 (void)transpose_out;
2731 cp_assert(0 &&
"The grid library was not compiled with FFTW support.");
#define cp_assert(condition)
cp_mpi_comm_t cp_mpi_get_comm_world(void)
Returns MPI_COMM_WORLD.
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
static int imax(int x, int y)
Returns the larger of two given integers (missing from the C standard).
bool fft_fftw_lib_has_guru_interface()
Whether a distributed FFT implementation is available.
void fft_fftw_3d_r2c_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C 3D FFT.
void fft_fftw_3d_local(const bool direction, const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 3D FFT.
void fft_fftw_1d_c2r_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double *grid_out)
Performs a local backwards C2R 1D FFT.
void fft_fftw_1d_local(const bool direction, const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double complex *grid_out)
Performs a local forward C2C 1D FFT.
void fft_fftw_register_2d_c2r_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Performs a local backwards C2R 2D FFT.
void fft_fftw_register_3d_c2r_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Performs a local backwards R2C 3D FFT.
void fft_fftw_free_double(double *buffer)
Free buffer of type double.
void fft_fftw_2d_r2c_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C 2D FFT.
void fft_fftw_register_2d_r2c_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C 2D FFT.
int fft_fftw_init_lib(const fftw_plan_type fftw_planning_flag, const bool use_guru_interface, const char *wisdom_file)
Initialize the FFT library (if not done externally).
void fft_fftw_register_3d_r2c_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C 3D FFT.
void fft_fftw_register_2d_local(const bool direction, const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 2D FFT.
void fft_fftw_allocate_complex(const int length, double complex **buffer)
Allocate buffer of type double complex.
void fft_fftw_2d_c2r_local(const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Performs a local backwards C2R 2D FFT.
void fft_fftw_free_complex(double complex *buffer)
Free buffer of type double complex.
void fft_fftw_register_1d_local(const bool direction, const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 1D FFT.
void fft_fftw_finalize_lib(const char *wisdom_file)
Finalize the FFT library (if not done externally).
void fft_fftw_3d_c2r_local(const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double *grid_out)
Performs a local backwards R2C 3D FFT.
void fft_fftw_1d_r2c_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C FFT.
static bool has_guru_interface
void fft_fftw_register_1d_c2r_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double complex *grid_in, double *grid_out)
Performs a local backwards C2R 1D FFT.
void fft_fftw_register_1d_r2c_local(const int fft_size, const int number_of_ffts, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, double *grid_in, double complex *grid_out)
Performs a local forward R2C FFT.
void fft_fftw_register_3d_local(const bool direction, const int fft_size[3], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 3D FFT.
void fft_fftw_2d_local(const bool direction, const int fft_size[2], const int number_of_ffts, const bool transpose_in, const bool transpose_out, double complex *grid_in, double complex *grid_out)
Performs a local C2C 2D FFT.
void fft_fftw_allocate_double(const int length, double **buffer)
Allocate buffer of type double.
static int fft_team_size(void)
Returns the number of threads used for FFT planning. Outside an OpenMP parallel region the maximum nu...
static void const int const int i
real(kind=dp), parameter, public half