13#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
26#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
33 offload_fftHandle *plan;
38#define FFT_GPU_CACHE_SIZE 64
39static cache_entry cache[FFT_GPU_CACHE_SIZE];
40static int cache_oldest_entry = 0;
41static omp_lock_t cache_lock;
44 double *dev_buffer_1, *dev_buffer_2;
46 size_t buffer_size, map_size;
47 offloadStream_t dev_stream;
51static int number_of_gpu_threads = 1;
52static gpu_thread_data *gpu_data = NULL;
53static bool is_initialized =
false;
55#define GPU_THREAD_ID omp_get_thread_num()
56#define buffer_dev_1 (gpu_data[GPU_THREAD_ID].dev_buffer_1)
57#define buffer_dev_2 (gpu_data[GPU_THREAD_ID].dev_buffer_2)
58#define ghatmap_dev (gpu_data[GPU_THREAD_ID].dev_map)
59#define allocated_buffer_size (gpu_data[GPU_THREAD_ID].buffer_size)
60#define allocated_map_size (gpu_data[GPU_THREAD_ID].map_size)
61#define stream (gpu_data[GPU_THREAD_ID].dev_stream)
67static void init_thread_data(gpu_thread_data *data) {
69 data->buffer_size = 1;
71 offloadMalloc((
void **)&data->dev_buffer_1, data->buffer_size);
72 offloadMalloc((
void **)&data->dev_buffer_2, data->buffer_size);
73 offloadMalloc((
void **)&data->dev_map, data->map_size);
74 offloadStreamCreate(&data->dev_stream);
75 data->initialized =
true;
78static gpu_thread_data *ensure_thread_data(
void) {
80 gpu_thread_data *data = &gpu_data[GPU_THREAD_ID];
91#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
95 number_of_gpu_threads = omp_get_max_threads();
96 if (number_of_gpu_threads < 1)
97 number_of_gpu_threads = 1;
98 memset(cache, 0,
sizeof(cache));
99 cache_oldest_entry = 0;
100 for (
int i = 0;
i < FFT_GPU_CACHE_SIZE;
i++) {
102 cache[
i].creator = -1;
104 omp_init_lock(&cache_lock);
105 gpu_data = calloc(number_of_gpu_threads,
sizeof(gpu_thread_data));
106 for (
int thread_id = 0; thread_id < number_of_gpu_threads; thread_id++) {
107 init_thread_data(&gpu_data[thread_id]);
109 is_initialized =
true;
120#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
121 if (!is_initialized) {
124 for (
int i = 0;
i < FFT_GPU_CACHE_SIZE;
i++) {
125 cache_entry *entry = &cache[
i];
126 if (entry->plan != NULL) {
127 offload_fftDestroy(*entry->plan);
131 for (
int thread_id = 0; thread_id < number_of_gpu_threads; thread_id++) {
132 gpu_thread_data *data = &gpu_data[thread_id];
133 if (data->initialized) {
135 offloadFree(data->dev_buffer_1);
136 offloadFree(data->dev_buffer_2);
137 offloadFree(data->dev_map);
138 offloadStreamDestroy(data->dev_stream);
143 omp_destroy_lock(&cache_lock);
144 is_initialized =
false;
157 const size_t requested_map_size) {
158#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
160 ensure_thread_data();
161 if (requested_buffer_size > allocated_buffer_size) {
162 offloadFree(buffer_dev_1);
163 offloadFree(buffer_dev_2);
164 offloadMalloc((
void **)&buffer_dev_1, requested_buffer_size);
165 offloadMalloc((
void **)&buffer_dev_2, requested_buffer_size);
166 allocated_buffer_size = requested_buffer_size;
168 if (requested_map_size > allocated_map_size) {
169 offloadFree(ghatmap_dev);
170 offloadMalloc((
void **)&ghatmap_dev, requested_map_size);
171 allocated_map_size = requested_map_size;
174 (void)requested_buffer_size;
175 (void)requested_map_size;
186#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
190 cp_assert(0 &&
"FFT backend was compiled without GPU support!");
203#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
207 cp_assert(0 &&
"FFT backend was compiled without GPU support!");
219#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
223 cp_assert(0 &&
"FFT backend was compiled without GPU support!");
234#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
238 cp_assert(0 &&
"FFT backend was compiled without GPU support!");
243#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
249static offload_fftHandle *lookup_plan_from_cache(
const fft_key_t key) {
251 ensure_thread_data();
252 const int thread_id = GPU_THREAD_ID;
253 omp_set_lock(&cache_lock);
254 offload_fftHandle *plan = NULL;
255 for (
int i = 0;
i < FFT_GPU_CACHE_SIZE;
i++) {
256 const int *x = cache[
i].key;
257 if (memcmp(key, x,
sizeof(fft_key_t)) == 0 && cache[
i].owner == -1 &&
258 cache[
i].creator == thread_id) {
259 cache[
i].owner = thread_id;
260 plan = cache[
i].plan;
264 omp_unset_lock(&cache_lock);
274static void add_plan_to_cache(
const fft_key_t key, offload_fftHandle *plan) {
275 ensure_thread_data();
276 const int thread_id = GPU_THREAD_ID;
277 omp_set_lock(&cache_lock);
278 int i = cache_oldest_entry;
279 for (
int attempt = 0; attempt < FFT_GPU_CACHE_SIZE && cache[
i].owner != -1;
281 i = (
i + 1) % FFT_GPU_CACHE_SIZE;
283 if (cache[
i].owner != -1) {
284 omp_unset_lock(&cache_lock);
287 cache_oldest_entry = (
i + 1) % FFT_GPU_CACHE_SIZE;
288 if (cache[
i].plan != NULL) {
289 offload_fftDestroy(*cache[
i].plan);
292 memcpy(cache[
i].key, key,
sizeof(fft_key_t));
293 cache[
i].plan = plan;
294 cache[
i].owner = thread_id;
295 cache[
i].creator = thread_id;
296 omp_unset_lock(&cache_lock);
304static void return_plan_to_cache(offload_fftHandle *plan) {
305 const int thread_id = GPU_THREAD_ID;
306 omp_set_lock(&cache_lock);
307 for (
int i = 0;
i < FFT_GPU_CACHE_SIZE;
i++) {
308 if (cache[
i].plan == plan && cache[
i].owner == thread_id) {
310 omp_unset_lock(&cache_lock);
314 omp_unset_lock(&cache_lock);
315 offload_fftDestroy(*plan);
333static void fft_gpu_register_1d(
const int direction,
const int n,
const int m,
334 const bool transpose_in,
335 const bool transpose_out,
336 const int leading_dimension_in,
337 const int leading_dimension_out,
338 const bool inplace) {
340 get_key_1d(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
341 transpose_out, leading_dimension_in, leading_dimension_out,
344 offload_fftHandle *plan = lookup_plan_from_cache(key);
346 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
348 int fft_size, inembed, onembed;
349 fetch_data_from_key_nd(key, &rank, &fft_size, &number_of_ffts,
350 &number_of_threads, &dir, &inpl, &inembed, &onembed,
351 &idist, &odist, &istride, &ostride);
352 plan = malloc(
sizeof(offload_fftHandle));
353 offload_fftPlanMany(plan, 1, &fft_size, &inembed, istride, idist, &onembed,
354 ostride, odist, OFFLOAD_FFT_Z2Z, number_of_ffts);
355 offload_fftSetStream(*plan, stream);
356 add_plan_to_cache(key, plan);
358 return_plan_to_cache(plan);
375static void fft_gpu_register_r2c_1d(
const int direction,
const int n,
376 const int m,
const bool transpose_in,
377 const bool transpose_out,
378 const int leading_dimension_in,
379 const int leading_dimension_out,
380 const bool inplace) {
382 get_key_1d_r2c(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
383 transpose_out, leading_dimension_in, leading_dimension_out,
386 offload_fftHandle *plan = lookup_plan_from_cache(key);
388 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
390 int fft_size, inembed, onembed;
391 fetch_data_from_key_nd(key, &rank, &fft_size, &number_of_ffts,
392 &number_of_threads, &dir, &inpl, &inembed, &onembed,
393 &idist, &odist, &istride, &ostride);
394 plan = malloc(
sizeof(offload_fftHandle));
395 if (direction == OFFLOAD_FFT_FORWARD) {
396 offload_fftPlanMany(plan, 1, &fft_size, &inembed, istride, idist,
397 &onembed, ostride, odist, OFFLOAD_FFT_D2Z,
400 offload_fftPlanMany(plan, 1, &fft_size, &inembed, istride, idist,
401 &onembed, ostride, odist, OFFLOAD_FFT_Z2D,
404 offload_fftSetStream(*plan, stream);
405 add_plan_to_cache(key, plan);
407 return_plan_to_cache(plan);
422static void fft_gpu_register_2d(
const int direction,
const int n[2],
423 const int m,
const bool transpose_in,
424 const bool transpose_out,
const bool inplace) {
426 get_key_2d(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
427 transpose_out, transpose_in ? m : n[0] * n[1],
428 transpose_out ? m : n[0] * n[1],
fft_team_size(), inplace, key);
430 offload_fftHandle *plan = lookup_plan_from_cache(key);
432 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
434 int fft_size[2], inembed[2], onembed[2];
435 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
436 &number_of_threads, &dir, &inpl, inembed, onembed,
437 &idist, &odist, &istride, &ostride);
438 plan = malloc(
sizeof(offload_fftHandle));
439 offload_fftPlanMany(plan, 2, fft_size, inembed, istride, idist, onembed,
440 ostride, odist, OFFLOAD_FFT_Z2Z, number_of_ffts);
441 offload_fftSetStream(*plan, stream);
442 add_plan_to_cache(key, plan);
444 return_plan_to_cache(plan);
458static void fft_gpu_register_r2c_2d(
const int direction,
const int n[2],
459 const int m,
const bool transpose_in,
460 const bool transpose_out,
461 const bool inplace) {
464 direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in, transpose_out,
466 : n[0] * (direction == OFFLOAD_FFT_FORWARD
467 ? (inplace ? 2 * (n[1] / 2 + 1) : n[1])
470 : n[0] * (direction == OFFLOAD_FFT_FORWARD
472 : (inplace ? 2 * (n[1] / 2 + 1) : n[1])),
475 offload_fftHandle *plan = lookup_plan_from_cache(key);
477 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
479 int fft_size[2], inembed[2], onembed[2];
480 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
481 &number_of_threads, &dir, &inpl, inembed, onembed,
482 &idist, &odist, &istride, &ostride);
483 plan = malloc(
sizeof(offload_fftHandle));
484 offload_fftPlanMany(plan, 2, fft_size, inembed, istride, idist, onembed,
485 ostride, odist, dir ? OFFLOAD_FFT_D2Z : OFFLOAD_FFT_Z2D,
487 offload_fftSetStream(*plan, stream);
488 add_plan_to_cache(key, plan);
490 return_plan_to_cache(plan);
506static void fft_gpu_register_3d(
const int direction,
const int nx,
const int ny,
507 const int nz,
const int number_of_ffts,
508 const bool transpose_in,
509 const bool transpose_out,
const bool inplace) {
511 get_key_3d(direction == OFFLOAD_FFT_FORWARD, (
const int[]){nx, ny, nz},
512 number_of_ffts, transpose_in, transpose_out,
fft_team_size(),
515 offload_fftHandle *plan = lookup_plan_from_cache(key);
517 int rank, threads, nffts, istride, ostride, idist, odist;
519 int fft_size[3], inembed[3], onembed[3];
520 fetch_data_from_key_nd(key, &rank, fft_size, &nffts, &threads, &dir, &inpl,
521 inembed, onembed, &idist, &odist, &istride,
523 plan = malloc(
sizeof(offload_fftHandle));
524 offload_fftPlanMany(plan, rank, fft_size, inembed, istride, idist, onembed,
525 ostride, odist, OFFLOAD_FFT_Z2Z, nffts);
526 offload_fftSetStream(*plan, stream);
527 add_plan_to_cache(key, plan);
529 return_plan_to_cache(plan);
545static void fft_gpu_register_r2c_3d(
const int direction,
const int nx,
546 const int ny,
const int nz,
547 const int number_of_ffts,
548 const bool transpose_in,
549 const bool transpose_out,
550 const bool inplace) {
552 get_key_3d_r2c(direction == OFFLOAD_FFT_FORWARD, (
const int[]){nx, ny, nz},
553 number_of_ffts, transpose_in, transpose_out,
fft_team_size(),
556 offload_fftHandle *plan = lookup_plan_from_cache(key);
558 int rank, threads, nffts, istride, ostride, idist, odist;
560 int fft_size[3], inembed[3], onembed[3];
561 fetch_data_from_key_nd(key, &rank, fft_size, &nffts, &threads, &dir, &inpl,
562 inembed, onembed, &idist, &odist, &istride,
564 plan = malloc(
sizeof(offload_fftHandle));
566 plan, rank, fft_size, inembed, istride, idist, onembed, ostride, odist,
567 direction == OFFLOAD_FFT_FORWARD ? OFFLOAD_FFT_D2Z : OFFLOAD_FFT_Z2D,
569 offload_fftSetStream(*plan, stream);
570 add_plan_to_cache(key, plan);
572 return_plan_to_cache(plan);
590static void fft_gpu_1d(
const int direction,
const int n,
const int m,
591 const bool transpose_in,
const bool transpose_out,
592 const int leading_dimension_in,
593 const int leading_dimension_out,
const double *data_in,
596 get_key_1d(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
597 transpose_out, leading_dimension_in, leading_dimension_out,
599 offload_fftHandle *plan = lookup_plan_from_cache(key);
602 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
604 int fft_size, inembed, onembed;
605 fetch_data_from_key_nd(key, &rank, &fft_size, &number_of_ffts,
606 &number_of_threads, &dir, &inplace, &inembed,
607 &onembed, &idist, &odist, &istride, &ostride);
608 plan = malloc(
sizeof(offload_fftHandle));
609 offload_fftPlanMany(plan, 1, &fft_size, &inembed, istride, idist, &onembed,
610 ostride, odist, OFFLOAD_FFT_Z2Z, number_of_ffts);
611 offload_fftSetStream(*plan, stream);
612 add_plan_to_cache(key, plan);
615 offload_fftExecZ2Z(*plan, data_in, data_out, direction);
616 return_plan_to_cache(plan);
634static void fft_gpu_r2c_1d(
const int direction,
const int n,
const int m,
635 const bool transpose_in,
const bool transpose_out,
636 const int leading_dimension_in,
637 const int leading_dimension_out,
638 const double *data_in,
double *data_out) {
640 get_key_1d_r2c(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
641 transpose_out, leading_dimension_in, leading_dimension_out,
643 offload_fftHandle *plan = lookup_plan_from_cache(key);
646 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
648 int fft_size, inembed, onembed;
649 fetch_data_from_key_nd(key, &rank, &fft_size, &number_of_ffts,
650 &number_of_threads, &dir, &inplace, &inembed,
651 &onembed, &idist, &odist, &istride, &ostride);
652 plan = malloc(
sizeof(offload_fftHandle));
653 offload_fftPlanMany(plan, 1, &fft_size, &inembed, istride, idist, &onembed,
654 ostride, odist, dir ? OFFLOAD_FFT_D2Z : OFFLOAD_FFT_Z2D,
656 offload_fftSetStream(*plan, stream);
657 add_plan_to_cache(key, plan);
660 if (direction == OFFLOAD_FFT_FORWARD) {
661 offload_fftExecD2Z(*plan, data_in, data_out);
663 offload_fftExecZ2D(*plan, data_in, data_out);
665 return_plan_to_cache(plan);
681static void fft_gpu_2d(
const int direction,
const int n[2],
const int m,
682 const bool transpose_in,
const bool transpose_out,
683 const double *data_in,
double *data_out) {
685 get_key_2d(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
686 transpose_out, transpose_in ? m : n[0] * n[1],
688 data_in == data_out, key);
689 offload_fftHandle *plan = lookup_plan_from_cache(key);
692 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
694 int fft_size[2], inembed[2], onembed[2];
695 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
696 &number_of_threads, &dir, &inplace, inembed, onembed,
697 &idist, &odist, &istride, &ostride);
698 plan = malloc(
sizeof(offload_fftHandle));
699 offload_fftPlanMany(plan, 2, fft_size, inembed, istride, idist, onembed,
700 ostride, odist, OFFLOAD_FFT_Z2Z, number_of_ffts);
701 offload_fftSetStream(*plan, stream);
702 add_plan_to_cache(key, plan);
705 offload_fftExecZ2Z(*plan, data_in, data_out, direction);
706 return_plan_to_cache(plan);
721static void fft_gpu_r2c_2d(
const int direction,
const int n[2],
const int m,
722 const bool transpose_in,
const bool transpose_out,
723 const double *data_in,
double *data_out) {
726 direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in, transpose_out,
729 : n[0] * (direction == OFFLOAD_FFT_FORWARD
730 ? (data_in == data_out ? 2 * (n[1] / 2 + 1) : n[1])
734 : n[0] * (direction == OFFLOAD_FFT_FORWARD
736 : (data_in == data_out ? 2 * (n[1] / 2 + 1) : n[1])),
738 offload_fftHandle *plan = lookup_plan_from_cache(key);
741 int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
743 int fft_size[2], inembed[2], onembed[2];
744 fetch_data_from_key_nd(key, &rank, fft_size, &number_of_ffts,
745 &number_of_threads, &dir, &inplace, inembed, onembed,
746 &idist, &odist, &istride, &ostride);
747 plan = malloc(
sizeof(offload_fftHandle));
748 offload_fftPlanMany(plan, 2, fft_size, inembed, istride, idist, onembed,
749 ostride, odist, dir ? OFFLOAD_FFT_D2Z : OFFLOAD_FFT_Z2D,
751 offload_fftSetStream(*plan, stream);
752 add_plan_to_cache(key, plan);
755 if (direction == OFFLOAD_FFT_FORWARD) {
756 offload_fftExecD2Z(*plan, data_in, data_out);
758 offload_fftExecZ2D(*plan, data_in, data_out);
760 return_plan_to_cache(plan);
777static void fft_gpu_3d(
const int direction,
const int nx,
const int ny,
778 const int nz,
const int number_of_ffts,
779 const bool transpose_in,
const bool transpose_out,
780 double *data_in,
double *data_out) {
782 get_key_3d(direction == OFFLOAD_FFT_FORWARD, (
const int[]){nx, ny, nz},
783 number_of_ffts, transpose_in, transpose_out,
fft_team_size(),
784 data_in == data_out, key);
785 offload_fftHandle *plan = lookup_plan_from_cache(key);
788 int rank, threads, nffts, istride, ostride, idist, odist;
790 int fft_size[3], inembed[3], onembed[3];
791 fetch_data_from_key_nd(key, &rank, fft_size, &nffts, &threads, &dir, &inpl,
792 inembed, onembed, &idist, &odist, &istride,
794 plan = malloc(
sizeof(offload_fftHandle));
795 offload_fftPlanMany(plan, rank, fft_size, inembed, istride, idist, onembed,
796 ostride, odist, OFFLOAD_FFT_Z2Z, nffts);
797 offload_fftSetStream(*plan, stream);
798 add_plan_to_cache(key, plan);
801 offload_fftExecZ2Z(*plan, data_in, data_out, direction);
802 return_plan_to_cache(plan);
819static void fft_gpu_r2c_3d(
const int direction,
const int nx,
const int ny,
820 const int nz,
const int number_of_ffts,
821 const bool transpose_in,
const bool transpose_out,
822 const double *data_in,
double *data_out) {
824 get_key_3d_r2c(direction == OFFLOAD_FFT_FORWARD, (
const int[]){nx, ny, nz},
825 number_of_ffts, transpose_in, transpose_out,
fft_team_size(),
826 data_in == data_out, key);
827 offload_fftHandle *plan = lookup_plan_from_cache(key);
830 int rank, threads, nffts, istride, ostride, idist, odist;
832 int fft_size[3], inembed[3], onembed[3];
833 fetch_data_from_key_nd(key, &rank, fft_size, &nffts, &threads, &dir, &inpl,
834 inembed, onembed, &idist, &odist, &istride,
836 plan = malloc(
sizeof(offload_fftHandle));
838 plan, rank, fft_size, inembed, istride, idist, onembed, ostride, odist,
839 direction == OFFLOAD_FFT_FORWARD ? OFFLOAD_FFT_D2Z : OFFLOAD_FFT_Z2D,
841 offload_fftSetStream(*plan, stream);
842 add_plan_to_cache(key, plan);
845 if (direction == OFFLOAD_FFT_FORWARD) {
846 offload_fftExecD2Z(*plan, data_in, data_out);
848 offload_fftExecZ2D(*plan, data_in, data_out);
850 return_plan_to_cache(plan);
865 const bool inplace,
const int number_of_ffts,
866 const bool transpose_in,
867 const bool transpose_out) {
868#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
870 if (npts[0] == 0 || npts[1] == 0 || npts[2] == 0 || number_of_ffts == 0) {
874 fft_gpu_register_3d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, npts[0],
875 npts[1], npts[2], number_of_ffts, transpose_in,
876 transpose_out, inplace);
881 (void)number_of_ffts;
898 const bool inplace,
const int number_of_ffts,
899 const bool transpose_in,
900 const bool transpose_out) {
901#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
903 if (npts[0] == 0 || npts[1] == 0 || npts[2] == 0 || number_of_ffts == 0) {
908 fft_gpu_register_r2c_3d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE,
909 npts[0], npts[1], npts[2], number_of_ffts,
910 transpose_in, transpose_out, inplace);
915 (void)number_of_ffts;
934 const bool transpose_in,
935 const bool transpose_out,
936 const int leading_dimension_in,
937 const int leading_dimension_out,
938 const bool inplace) {
939#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
941 if (n == 0 || m == 0) {
946 fft_gpu_register_1d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
947 transpose_in, transpose_out, leading_dimension_in,
948 leading_dimension_out, inplace);
955 (void)leading_dimension_in;
956 (void)leading_dimension_out;
974 const bool transpose_in,
975 const bool transpose_out,
976 const int leading_dimension_in,
977 const int leading_dimension_out,
978 const bool inplace) {
979#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
981 const int nrpts = n * m;
987 fft_gpu_register_r2c_1d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
988 transpose_in, transpose_out, leading_dimension_in,
989 leading_dimension_out, inplace);
996 (void)leading_dimension_in;
997 (void)leading_dimension_out;
1013 const bool transpose_in,
1014 const bool transpose_out,
const bool inplace) {
1015#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1017 const int nrpts = n[0] * n[1] * m;
1023 fft_gpu_register_2d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
1024 transpose_in, transpose_out, inplace);
1030 (void)transpose_out;
1046 const bool transpose_in,
1047 const bool transpose_out,
1048 const bool inplace) {
1049#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1051 if (n[0] == 0 || n[1] == 0 || m == 0) {
1056 fft_gpu_register_r2c_2d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
1057 transpose_in, transpose_out, inplace);
1063 (void)transpose_out;
1080 const int *npts,
const int ngpts,
const double scale) {
1082#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1083 const int nrpts = npts[0] * npts[1] * npts[2];
1085 if (nrpts == 0 || ngpts == 0) {
1091 const size_t buffer_size = 2 *
sizeof(double) * nrpts;
1092 const size_t map_size =
sizeof(int) * ngpts;
1096 offloadMemcpyAsyncHtoD(buffer_dev_1, din, buffer_size / 2, stream);
1097 fft_gpu_launch_real_to_complex(buffer_dev_1, buffer_dev_2, nrpts, stream);
1100 fft_gpu_3d(OFFLOAD_FFT_FORWARD, npts[2], npts[1], npts[0], 1,
false,
false,
1101 buffer_dev_2, buffer_dev_2);
1104 offloadMemcpyAsyncHtoD(ghatmap_dev, ghatmap, map_size, stream);
1105 fft_gpu_launch_gather(buffer_dev_1, buffer_dev_2, scale, ngpts, ghatmap_dev,
1109 offloadMemcpyAsyncDtoH(zout, buffer_dev_1, 2 *
sizeof(
double) * ngpts,
1111 offloadStreamSynchronize(stream);
1135 const int *npts,
const int ngpts,
const int nmaps,
1136 const double scale) {
1137#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1139 const int nrpts = npts[0] * npts[1] * npts[2];
1141 if (nrpts == 0 || ngpts == 0) {
1147 const size_t buffer_size = 2 *
sizeof(double) * nrpts;
1148 const size_t map_size =
sizeof(int) * nmaps * ngpts;
1152 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, 2 *
sizeof(
double) * ngpts, stream);
1155 offloadMemcpyAsyncHtoD(ghatmap_dev, ghatmap, map_size, stream);
1156 offloadMemsetAsync(buffer_dev_2, 0, buffer_size, stream);
1157 fft_gpu_launch_scatter(buffer_dev_2, buffer_dev_1, scale, ngpts, nmaps,
1158 ghatmap_dev, stream);
1161 fft_gpu_3d(OFFLOAD_FFT_INVERSE, npts[2], npts[1], npts[0], 1,
false,
false,
1162 buffer_dev_2, buffer_dev_2);
1165 fft_gpu_launch_complex_to_real(buffer_dev_2, buffer_dev_1, nrpts, stream);
1166 offloadMemcpyAsyncDtoH(dout, buffer_dev_1, buffer_size / 2, stream);
1167 offloadStreamSynchronize(stream);
1191 const int *npts,
const int number_of_ffts,
1192 const bool transpose_in,
const bool transpose_out) {
1193#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1195 if (npts[0] == 0 || npts[1] == 0 || npts[2] == 0 || number_of_ffts == 0) {
1201 const size_t volume = (size_t)npts[0] * npts[1] * npts[2];
1202 const size_t buffer_size = 2 *
sizeof(double) * volume * number_of_ffts;
1206 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1209 fft_gpu_3d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, npts[0], npts[1],
1210 npts[2], number_of_ffts, transpose_in, transpose_out, buffer_dev_1,
1211 zin != zout ? buffer_dev_2 : buffer_dev_1);
1214 offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1215 buffer_size, stream);
1216 offloadStreamSynchronize(stream);
1222 (void)number_of_ffts;
1224 (void)transpose_out;
1240 const int *npts,
const int number_of_ffts,
1241 const bool transpose_in,
const bool transpose_out) {
1242#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1244 if (npts[0] == 0 || npts[1] == 0 || npts[2] == 0 || number_of_ffts == 0) {
1251 const size_t real_size =
sizeof(double) * npts[0] * npts[1] *
1252 (zin != zout ? npts[2] : 2 * (npts[2] / 2 + 1)) *
1254 const size_t complex_size =
sizeof(double) * 2 * npts[0] * npts[1] *
1255 (npts[2] / 2 + 1) * number_of_ffts;
1259 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, dir ? real_size : complex_size,
1263 fft_gpu_r2c_3d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, npts[0],
1264 npts[1], npts[2], number_of_ffts, transpose_in, transpose_out,
1265 buffer_dev_1, zin != zout ? buffer_dev_2 : buffer_dev_1);
1268 offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1269 dir ? complex_size : real_size, stream);
1270 offloadStreamSynchronize(stream);
1276 (void)number_of_ffts;
1278 (void)transpose_out;
1291#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1293 const int nrpts = npts[0] * npts[1] * npts[2];
1300 const size_t buffer_size = 2 *
sizeof(double) * nrpts;
1304 offloadMemcpyAsyncHtoD(buffer_dev_1, din, buffer_size / 2, stream);
1305 fft_gpu_launch_real_to_complex(buffer_dev_1, buffer_dev_2, nrpts, stream);
1309 fft_gpu_1d(OFFLOAD_FFT_FORWARD, npts[2], npts[0] * npts[1],
true,
false,
1310 npts[0] * npts[1], npts[2], buffer_dev_2, buffer_dev_1);
1311 fft_gpu_1d(OFFLOAD_FFT_FORWARD, npts[1], npts[0] * npts[2],
true,
false,
1312 npts[0] * npts[2], npts[1], buffer_dev_1, buffer_dev_2);
1315 offloadMemcpyAsyncDtoH(zout, buffer_dev_2, buffer_size, stream);
1316 offloadStreamSynchronize(stream);
1333#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1335 const int nrpts = npts[0] * npts[1] * npts[2];
1342 const size_t buffer_size = 2 *
sizeof(double) * nrpts;
1346 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1350 fft_gpu_1d(OFFLOAD_FFT_INVERSE, npts[1], npts[0] * npts[2],
false,
true,
1351 npts[1], npts[0] * npts[2], buffer_dev_1, buffer_dev_2);
1352 fft_gpu_1d(OFFLOAD_FFT_INVERSE, npts[2], npts[0] * npts[1],
false,
true,
1353 npts[2], npts[0] * npts[1], buffer_dev_2, buffer_dev_1);
1354 fft_gpu_launch_complex_to_real(buffer_dev_1, buffer_dev_2, nrpts, stream);
1357 offloadMemcpyAsyncDtoH(dout, buffer_dev_2, buffer_size / 2, stream);
1358 offloadStreamSynchronize(stream);
1374void fft_gpu_cf(
const double *din,
double *zout,
const int *npts) {
1375#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1377 const int nrpts = npts[0] * npts[1] * npts[2];
1384 const size_t buffer_size = 2 *
sizeof(double) * nrpts;
1388 offloadMemcpyAsyncHtoD(buffer_dev_1, din, buffer_size / 2, stream);
1389 fft_gpu_launch_real_to_complex(buffer_dev_1, buffer_dev_2, nrpts, stream);
1392 fft_gpu_1d(OFFLOAD_FFT_FORWARD, npts[2], npts[0] * npts[1],
true,
false,
1393 npts[0] * npts[1], npts[2], buffer_dev_2, buffer_dev_1);
1396 offloadMemcpyAsyncDtoH(zout, buffer_dev_1, buffer_size, stream);
1397 offloadStreamSynchronize(stream);
1413void fft_gpu_fc(
const double *zin,
double *dout,
const int *npts) {
1414#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1416 const int nrpts = npts[0] * npts[1] * npts[2];
1423 const size_t buffer_size = 2 *
sizeof(double) * nrpts;
1427 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1430 fft_gpu_1d(OFFLOAD_FFT_INVERSE, npts[2], npts[0] * npts[1],
false,
true,
1431 npts[2], npts[0] * npts[1], buffer_dev_1, buffer_dev_2);
1434 fft_gpu_launch_complex_to_real(buffer_dev_2, buffer_dev_1, nrpts, stream);
1435 offloadMemcpyAsyncDtoH(dout, buffer_dev_1, buffer_size / 2, stream);
1436 offloadStreamSynchronize(stream);
1458 const int n,
const int m,
const bool transpose_in,
1459 const bool transpose_out,
const int leading_dimension_in,
1460 const int leading_dimension_out) {
1461#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1463 if (n == 0 || m == 0) {
1469 const size_t input_elements =
1470 transpose_in ? (size_t)(n - 1) * leading_dimension_in + m
1471 : (size_t)(m - 1) * leading_dimension_in + n;
1472 const size_t output_elements =
1473 transpose_out ? (size_t)(n - 1) * leading_dimension_out + m
1474 : (size_t)(m - 1) * leading_dimension_out + n;
1475 const size_t input_size = 2 *
sizeof(double) * input_elements;
1476 const size_t output_size = 2 *
sizeof(double) * output_elements;
1480 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, input_size, stream);
1483 double *output_dev = zin != zout ? buffer_dev_2 : buffer_dev_1;
1484 fft_gpu_1d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
1485 transpose_in, transpose_out, leading_dimension_in,
1486 leading_dimension_out, buffer_dev_1, output_dev);
1490 const size_t element_size = 2 *
sizeof(double);
1491 const size_t width = (size_t)(transpose_out ? m : n) * element_size;
1492 const size_t height = (size_t)(transpose_out ? n : m);
1493 const size_t pitch = (size_t)leading_dimension_out * element_size;
1494 offloadMemcpy2DAsync(zout, pitch, output_dev, pitch, width, height, stream);
1495 offloadStreamSynchronize(stream);
1503 (void)transpose_out;
1504 (void)leading_dimension_in;
1505 (void)leading_dimension_out;
1523 const int n,
const int m,
const bool transpose_in,
1524 const bool transpose_out,
1525 const int leading_dimension_in,
1526 const int leading_dimension_out) {
1527#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1529 if (n == 0 || m == 0) {
1535 const size_t input_size =
1536 sizeof(double) * leading_dimension_in *
1537 (transpose_in ? (zin != zout && dir ? n : 2 * (n / 2 + 1))
1538 : (dir ? m : 2 * m));
1539 const size_t output_size =
1540 sizeof(double) * leading_dimension_out *
1541 (transpose_out ? (zin != zout && !dir ? n : 2 * (n / 2 + 1))
1542 : (!dir ? m : 2 * m));
1546 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, input_size, stream);
1549 fft_gpu_r2c_1d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
1550 transpose_in, transpose_out, leading_dimension_in,
1551 leading_dimension_out, buffer_dev_1,
1552 zin != zout ? buffer_dev_2 : buffer_dev_1);
1555 offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1556 output_size, stream);
1557 offloadStreamSynchronize(stream);
1565 (void)transpose_out;
1566 (void)leading_dimension_in;
1567 (void)leading_dimension_out;
1583 const int n[2],
const int m,
const bool transpose_in,
1584 const bool transpose_out) {
1585#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1587 if (n[0] == 0 || n[1] == 0 || m == 0) {
1593 const size_t buffer_size = 2 *
sizeof(double) * n[0] * n[1] * m;
1597 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1600 fft_gpu_2d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
1601 transpose_in, transpose_out, buffer_dev_1,
1602 zin != zout ? buffer_dev_2 : buffer_dev_1);
1605 offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1606 buffer_size, stream);
1607 offloadStreamSynchronize(stream);
1615 (void)transpose_out;
1631 const int n[2],
const int m,
const bool transpose_in,
1632 const bool transpose_out) {
1633#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1635 if (n[0] == 0 || n[1] == 0 || m == 0) {
1641 const int input_size =
sizeof(double) * n[0] *
1642 (zin != zout && dir ? n[1] : 2 * (n[1] / 2 + 1)) * m;
1643 const int output_size =
sizeof(double) * n[0] *
1644 (zin != zout && !dir ? n[1] : 2 * (n[1] / 2 + 1)) * m;
1648 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, input_size, stream);
1651 fft_gpu_r2c_2d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
1652 transpose_in, transpose_out, buffer_dev_1,
1653 zin != zout ? buffer_dev_2 : buffer_dev_1);
1656 offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1657 output_size, stream);
1658 offloadStreamSynchronize(stream);
1666 (void)transpose_out;
1682void fft_gpu_fg(
const double *zin,
double *zout,
const int *ghatmap,
1683 const int *npts,
const int mmax,
const int ngpts,
1684 const double scale) {
1685#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1687 const int nrpts = npts[0] * mmax;
1689 if (nrpts == 0 || ngpts == 0) {
1695 const size_t buffer_size = 2 *
sizeof(double) * nrpts;
1696 const size_t map_size =
sizeof(int) * ngpts;
1700 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1703 fft_gpu_1d(OFFLOAD_FFT_FORWARD, npts[0], mmax,
true,
false, mmax, npts[0],
1704 buffer_dev_1, buffer_dev_2);
1707 offloadMemcpyAsyncHtoD(ghatmap_dev, ghatmap, map_size, stream);
1708 fft_gpu_launch_gather(buffer_dev_1, buffer_dev_2, scale, ngpts, ghatmap_dev,
1712 offloadMemcpyAsyncDtoH(zout, buffer_dev_1, 2 *
sizeof(
double) * ngpts,
1714 offloadStreamSynchronize(stream);
1739void fft_gpu_sf(
const double *zin,
double *zout,
const int *ghatmap,
1740 const int *npts,
const int mmax,
const int ngpts,
1741 const int nmaps,
const double scale) {
1742#if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1744 const int nrpts = npts[0] * mmax;
1746 if (nrpts == 0 || ngpts == 0) {
1752 const size_t buffer_size = 2 *
sizeof(double) * nrpts;
1753 const size_t map_size =
sizeof(int) * nmaps * ngpts;
1757 offloadMemcpyAsyncHtoD(buffer_dev_1, zin, 2 *
sizeof(
double) * ngpts, stream);
1760 offloadMemcpyAsyncHtoD(ghatmap_dev, ghatmap, map_size, stream);
1761 offloadMemsetAsync(buffer_dev_2, 0, buffer_size, stream);
1762 fft_gpu_launch_scatter(buffer_dev_2, buffer_dev_1, scale, ngpts, nmaps,
1763 ghatmap_dev, stream);
1766 fft_gpu_1d(OFFLOAD_FFT_INVERSE, npts[0], mmax,
false,
true, npts[0], mmax,
1767 buffer_dev_2, buffer_dev_1);
1770 offloadMemcpyAsyncDtoH(zout, buffer_dev_1, buffer_size, stream);
1771 offloadStreamSynchronize(stream);
#define cp_assert(condition)
static int imax(int x, int y)
Returns the larger of two given integers (missing from the C standard).
void fft_gpu_3d_r2c_local(const double *zin, double *zout, const bool dir, const int *npts, const int number_of_ffts, const bool transpose_in, const bool transpose_out)
Performs a 3D-R2C/C2R-FFT, on the GPU.
void fft_gpu_ffc(const double *zin, double *dout, const int *npts)
Performs a (double precision complex) 2D-FFT and a (double complex to double) shrink-down on the GPU.
void fft_gpu_free_double(double *buffer)
Allocate buffer of type double.
void fft_gpu_register_3d_local(const bool dir, const int *npts, const bool inplace, const int number_of_ffts, const bool transpose_in, const bool transpose_out)
Performs a (double precision complex) 3D-FFT on the GPU.
void fft_gpu_fc(const double *zin, double *dout, const int *npts)
Performs a (double precision complex) 1D-FFT and a (double complex to double) shrink-down on the GPU.
void fft_gpu_allocate_double(const int length, double **buffer)
Allocate buffer of type double.
void fft_gpu_cff(const double *din, double *zout, const int *npts)
Performs a (double to complex double) blow-up and a (double precision complex) 2D-FFT on the GPU.
void fft_gpu_allocate_complex(const int length, double complex **buffer)
Allocate buffer of type double complex.
void fft_gpu_register_2d_local(const bool dir, const int n[2], const int m, const bool transpose_in, const bool transpose_out, const bool inplace)
Performs a (double precision complex) 2D-FFT on the GPU.
void fft_gpu_2d_local(const double *zin, double *zout, const bool dir, const int n[2], const int m, const bool transpose_in, const bool transpose_out)
Performs a (double precision complex) 2D-FFT on the GPU.
void fft_gpu_sfffc(const double *zin, double *dout, const int *ghatmap, const int *npts, const int ngpts, const int nmaps, const double scale)
Performs a (double precision complex) scatter, followed by a (double precision complex) FFT,...
void fft_gpu_cf(const double *din, double *zout, const int *npts)
Performs a (double to complex double) blow-up and a (double precision complex) 1D-FFT on the GPU.
void fft_gpu_cfffg(const double *din, double *zout, const int *ghatmap, const int *npts, const int ngpts, const double scale)
Performs a (double precision complex) FFT, followed by a (double precision complex) gather,...
void fft_gpu_init(void)
Initializes the fft_gpu library.
void fft_gpu_2d_r2c_local(const double *zin, double *zout, const bool dir, const int n[2], const int m, const bool transpose_in, const bool transpose_out)
Performs a (double precision complex) R2C/C2R2D-FFT on the GPU.
void fft_gpu_fg(const double *zin, double *zout, const int *ghatmap, const int *npts, const int mmax, const int ngpts, const double scale)
Performs a (double precision complex) 1D-FFT, followed by a (double precision complex) gather,...
void fft_gpu_register_3d_r2c_local(const bool dir, const int *npts, const bool inplace, const int number_of_ffts, const bool transpose_in, const bool transpose_out)
Performs a 3D-R2C/C2R-FFT, on the GPU.
void fft_gpu_3d_local(const double *zin, double *zout, const bool dir, const int *npts, const int number_of_ffts, const bool transpose_in, const bool transpose_out)
Performs a (double precision complex) 3D-FFT on the GPU.
void fft_gpu_1d_r2c_local(const double *zin, double *zout, const bool dir, const int n, const int m, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out)
Performs a (double precision complex) 1D-FFT on the GPU.
void fft_gpu_finalize(void)
Releases resources held by the fft_gpu library.
void fft_gpu_register_2d_r2c_local(const bool dir, const int n[2], const int m, const bool transpose_in, const bool transpose_out, const bool inplace)
Performs a (double precision complex) R2C/C2R2D-FFT on the GPU.
void ensure_memory_sizes(const size_t requested_buffer_size, const size_t requested_map_size)
Checks size of device buffers and re-allocates them if necessary.
void fft_gpu_sf(const double *zin, double *zout, const int *ghatmap, const int *npts, const int mmax, const int ngpts, const int nmaps, const double scale)
Performs a (double precision complex) scatter, followed by a (double precision complex) 1D-FFT,...
void fft_gpu_register_1d_r2c_local(const bool dir, const int n, const int m, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, const bool inplace)
Performs a (double precision complex) 1D-FFT on the GPU.
void fft_gpu_free_complex(double complex *buffer)
Allocate buffer of type double complex.
void fft_gpu_1d_local(const double *zin, double *zout, const bool dir, const int n, const int m, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out)
Performs a (double precision complex) 1D-FFT on the GPU.
void fft_gpu_register_1d_local(const bool dir, const int n, const int m, const bool transpose_in, const bool transpose_out, const int leading_dimension_in, const int leading_dimension_out, const bool inplace)
Performs a (double precision complex) 1D-FFT on the GPU.
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
void offload_activate_chosen_device(void)
Activates the device selected via offload_set_chosen_device().
int offload_host_free(void *ptr__)
free pinned memory (or simple free when there is no gpu)
int offload_host_malloc(void **ptr__, const size_t size__)
Allocate pinned memory (or simple malloc when there is no gpu).