Line data Source code
1 : /*----------------------------------------------------------------------------*/
2 : /* CP2K: A general program to perform molecular dynamics simulations */
3 : /* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4 : /* */
5 : /* SPDX-License-Identifier: BSD-3-Clause */
6 : /*----------------------------------------------------------------------------*/
7 : #include "fft_gpu.h"
8 : #include "../../mpiwrap/cp_mpi.h"
9 : #include "../../offload/offload_runtime.h"
10 : #include "../fft_key.h"
11 : #include "../fft_utils.h"
12 :
13 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
14 : #include "../../offload/offload_fft.h"
15 : #include "../../offload/offload_library.h"
16 : #include "fft_gpu_kernels.h"
17 : #endif
18 :
19 : #include "../../base/cp_assert.h"
20 : #include <omp.h>
21 : #include <stdbool.h>
22 : #include <stddef.h>
23 : #include <stdlib.h>
24 : #include <string.h>
25 :
26 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
27 : /*******************************************************************************
28 : * \brief Static variables for retaining objects that are expensive to create.
29 : * \author Ole Schuett
30 : ******************************************************************************/
31 : typedef struct {
32 : fft_key_t key;
33 : offload_fftHandle *plan;
34 : int owner; // thread id while the plan is checked out, -1 if available
35 : int creator; // thread that created the plan and owns its stream
36 : } cache_entry;
37 :
38 : #define FFT_GPU_CACHE_SIZE 64
39 : static cache_entry cache[FFT_GPU_CACHE_SIZE];
40 : static int cache_oldest_entry = 0; // used for FIFO eviction
41 : static omp_lock_t cache_lock;
42 :
43 : typedef struct {
44 : double *dev_buffer_1, *dev_buffer_2;
45 : int *dev_map;
46 : size_t buffer_size, map_size;
47 : offloadStream_t dev_stream;
48 : bool initialized;
49 : } gpu_thread_data;
50 :
51 : static int number_of_gpu_threads = 1;
52 : static gpu_thread_data *gpu_data = NULL;
53 : static bool is_initialized = false;
54 :
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)
62 :
63 : /*******************************************************************************
64 : * \brief Initialize the per-thread GPU FFT data.
65 : * \param data Per-thread GPU data to initialize.
66 : ******************************************************************************/
67 : static void init_thread_data(gpu_thread_data *data) {
68 : offload_activate_chosen_device();
69 : data->buffer_size = 1;
70 : data->map_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;
76 : }
77 :
78 : static gpu_thread_data *ensure_thread_data(void) {
79 : offload_activate_chosen_device();
80 : gpu_thread_data *data = &gpu_data[GPU_THREAD_ID];
81 : cp_assert(data->initialized);
82 : return data;
83 : }
84 : #endif
85 :
86 : /*******************************************************************************
87 : * \brief Initializes the fft_gpu library.
88 : * \author Ole Schuett
89 : ******************************************************************************/
90 0 : void fft_gpu_init(void) {
91 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
92 : if (is_initialized) {
93 : return;
94 : }
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++) {
101 : cache[i].owner = -1;
102 : cache[i].creator = -1;
103 : }
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]);
108 : }
109 : is_initialized = true;
110 : #else
111 : // Nothing to initialize
112 : #endif
113 0 : }
114 :
115 : /*******************************************************************************
116 : * \brief Releases resources held by the fft_gpu library.
117 : * \author Ole Schuett
118 : ******************************************************************************/
119 0 : void fft_gpu_finalize(void) {
120 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
121 : if (!is_initialized) {
122 : return;
123 : }
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);
128 : free(entry->plan);
129 : }
130 : }
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) {
134 : offload_activate_chosen_device();
135 : offloadFree(data->dev_buffer_1);
136 : offloadFree(data->dev_buffer_2);
137 : offloadFree(data->dev_map);
138 : offloadStreamDestroy(data->dev_stream);
139 : }
140 : }
141 : free(gpu_data);
142 : gpu_data = NULL;
143 : omp_destroy_lock(&cache_lock);
144 : is_initialized = false;
145 : #else
146 : // Nothing to finalize
147 : #endif
148 0 : }
149 :
150 : /*******************************************************************************
151 : * \brief Checks size of device buffers and re-allocates them if necessary.
152 : * \param requested_buffer_size Requested device buffer size in elements.
153 : * \param requested_map_size Requested index-map size in elements.
154 : * \author Ole Schuett
155 : ******************************************************************************/
156 0 : void ensure_memory_sizes(const size_t requested_buffer_size,
157 : const size_t requested_map_size) {
158 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
159 : cp_assert(is_initialized);
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;
167 : }
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;
172 : }
173 : #else
174 0 : (void)requested_buffer_size;
175 0 : (void)requested_map_size;
176 : #endif
177 0 : }
178 :
179 : /*******************************************************************************
180 : * \brief Allocate buffer of type double.
181 : * \param length Number of elements.
182 : * \param buffer Buffer pointer.
183 : * \author Frederick Stein
184 : ******************************************************************************/
185 0 : void fft_gpu_allocate_double(const int length, double **buffer) {
186 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
187 : cp_assert(is_initialized);
188 : offload_host_malloc((void **)buffer, length * sizeof(double));
189 : #else
190 0 : cp_assert(0 && "FFT backend was compiled without GPU support!");
191 : (void)length;
192 : (void)buffer;
193 : #endif
194 : }
195 :
196 : /*******************************************************************************
197 : * \brief Allocate buffer of type double complex.
198 : * \param length Number of elements.
199 : * \param buffer Buffer pointer.
200 : * \author Frederick Stein
201 : ******************************************************************************/
202 0 : void fft_gpu_allocate_complex(const int length, double complex **buffer) {
203 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
204 : cp_assert(is_initialized);
205 : offload_host_malloc((void **)buffer, length * sizeof(double complex));
206 : #else
207 0 : cp_assert(0 && "FFT backend was compiled without GPU support!");
208 : (void)length;
209 : (void)buffer;
210 : #endif
211 : }
212 :
213 : /*******************************************************************************
214 : * \brief Allocate buffer of type double.
215 : * \param buffer Buffer pointer.
216 : * \author Frederick Stein
217 : ******************************************************************************/
218 0 : void fft_gpu_free_double(double *buffer) {
219 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
220 : cp_assert(is_initialized);
221 : offload_host_free(buffer);
222 : #else
223 0 : cp_assert(0 && "FFT backend was compiled without GPU support!");
224 : (void)buffer;
225 : #endif
226 : }
227 :
228 : /*******************************************************************************
229 : * \brief Allocate buffer of type double complex.
230 : * \param buffer Buffer pointer.
231 : * \author Frederick Stein
232 : ******************************************************************************/
233 0 : void fft_gpu_free_complex(double complex *buffer) {
234 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
235 : cp_assert(is_initialized);
236 : offload_host_free(buffer);
237 : #else
238 0 : cp_assert(0 && "FFT backend was compiled without GPU support!");
239 : (void)buffer;
240 : #endif
241 : }
242 :
243 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
244 : /*******************************************************************************
245 : * \brief Fetches an fft plan from the cache. Returns NULL if not found.
246 : * \param key Encoded FFT key.
247 : * \author Ole Schuett
248 : ******************************************************************************/
249 : static offload_fftHandle *lookup_plan_from_cache(const fft_key_t key) {
250 : cp_assert(is_initialized);
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;
261 : break;
262 : }
263 : }
264 : omp_unset_lock(&cache_lock);
265 : return plan;
266 : }
267 :
268 : /*******************************************************************************
269 : * \brief Adds an fft plan to the cache. Assumes ownership of plan's memory.
270 : * \param key Encoded FFT key.
271 : * \param plan FFT plan handle.
272 : * \author Ole Schuett
273 : ******************************************************************************/
274 : static 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;
280 : attempt++) {
281 : i = (i + 1) % FFT_GPU_CACHE_SIZE;
282 : }
283 : if (cache[i].owner != -1) {
284 : omp_unset_lock(&cache_lock);
285 : return; // All entries are checked out; do not cache this plan.
286 : }
287 : cache_oldest_entry = (i + 1) % FFT_GPU_CACHE_SIZE;
288 : if (cache[i].plan != NULL) {
289 : offload_fftDestroy(*cache[i].plan);
290 : free(cache[i].plan);
291 : }
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);
297 : }
298 :
299 : /*******************************************************************************
300 : * \brief Returns an owned plan to the cache, or frees it if it was uncached.
301 : * \param plan FFT plan handle.
302 : * \author Frederick Stein
303 : ******************************************************************************/
304 : static 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) {
309 : cache[i].owner = -1;
310 : omp_unset_lock(&cache_lock);
311 : return;
312 : }
313 : }
314 : omp_unset_lock(&cache_lock);
315 : offload_fftDestroy(*plan);
316 : free(plan);
317 : }
318 :
319 : /*******************************************************************************
320 : * \brief Performs a scaled double precision complex 1D-FFT many times on
321 : * the GPU.
322 : * Input/output are DEVICE pointers (data_in, date_out).
323 : * \param direction FFT direction (forward or backward).
324 : * \param n Transform length.
325 : * \param m Number of transforms.
326 : * \param transpose_in Whether the input array uses the transposed layout.
327 : * \param transpose_out Whether the output array uses the transposed layout.
328 : * \param leading_dimension_in Leading dimension of the input array.
329 : * \param leading_dimension_out Leading dimension of the output array.
330 : * \param inplace Whether the transform is performed in-place.
331 : * \author Andreas Gloess, Ole Schuett
332 : ******************************************************************************/
333 : static 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) {
339 : fft_key_t key;
340 : get_key_1d(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
341 : transpose_out, leading_dimension_in, leading_dimension_out,
342 : fft_team_size(), inplace, key);
343 :
344 : offload_fftHandle *plan = lookup_plan_from_cache(key);
345 : if (plan == NULL) {
346 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
347 : bool dir, inpl;
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);
357 : }
358 : return_plan_to_cache(plan);
359 : }
360 :
361 : /*******************************************************************************
362 : * \brief Performs a scaled double precision complex 1D-FFT many times on
363 : * the GPU.
364 : * Input/output are DEVICE pointers (data_in, date_out).
365 : * \param direction FFT direction (forward or backward).
366 : * \param n Transform length.
367 : * \param m Number of transforms.
368 : * \param transpose_in Whether the input array uses the transposed layout.
369 : * \param transpose_out Whether the output array uses the transposed layout.
370 : * \param leading_dimension_in Leading dimension of the input array.
371 : * \param leading_dimension_out Leading dimension of the output array.
372 : * \param inplace Whether the transform is performed in-place.
373 : * \author Andreas Gloess, Ole Schuett
374 : ******************************************************************************/
375 : static 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) {
381 : fft_key_t key;
382 : get_key_1d_r2c(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
383 : transpose_out, leading_dimension_in, leading_dimension_out,
384 : fft_team_size(), inplace, key);
385 :
386 : offload_fftHandle *plan = lookup_plan_from_cache(key);
387 : if (plan == NULL) {
388 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
389 : bool dir, inpl;
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,
398 : number_of_ffts);
399 : } else {
400 : offload_fftPlanMany(plan, 1, &fft_size, &inembed, istride, idist,
401 : &onembed, ostride, odist, OFFLOAD_FFT_Z2D,
402 : number_of_ffts);
403 : }
404 : offload_fftSetStream(*plan, stream);
405 : add_plan_to_cache(key, plan);
406 : }
407 : return_plan_to_cache(plan);
408 : }
409 :
410 : /*******************************************************************************
411 : * \brief Performs a scaled double precision complex 1D-FFT many times on
412 : * the GPU.
413 : * Input/output are DEVICE pointers (data_in, date_out).
414 : * \param direction FFT direction (forward or backward).
415 : * \param n Transform length.
416 : * \param m Number of transforms.
417 : * \param transpose_in Whether the input array uses the transposed layout.
418 : * \param transpose_out Whether the output array uses the transposed layout.
419 : * \param inplace Whether the transform is performed in-place.
420 : * \author Andreas Gloess, Ole Schuett
421 : ******************************************************************************/
422 : static 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) {
425 : fft_key_t key;
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);
429 :
430 : offload_fftHandle *plan = lookup_plan_from_cache(key);
431 : if (plan == NULL) {
432 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
433 : bool dir, inpl;
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);
443 : }
444 : return_plan_to_cache(plan);
445 : }
446 :
447 : /*******************************************************************************
448 : * \brief Performs a scaled double precision complex R2C/C2R-2D-FFT many times
449 : * on the GPU. Input/output are DEVICE pointers (data_in, date_out).
450 : * \param direction FFT direction (forward or backward).
451 : * \param n Transform length.
452 : * \param m Number of transforms.
453 : * \param transpose_in Whether the input array uses the transposed layout.
454 : * \param transpose_out Whether the output array uses the transposed layout.
455 : * \param inplace Whether the transform is performed in-place.
456 : * \author Andreas Gloess, Ole Schuett
457 : ******************************************************************************/
458 : static 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) {
462 : fft_key_t key;
463 : get_key_2d_r2c(
464 : direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in, transpose_out,
465 : transpose_in ? m
466 : : n[0] * (direction == OFFLOAD_FFT_FORWARD
467 : ? (inplace ? 2 * (n[1] / 2 + 1) : n[1])
468 : : n[1] / 2 + 1),
469 : transpose_out ? m
470 : : n[0] * (direction == OFFLOAD_FFT_FORWARD
471 : ? n[1] / 2 + 1
472 : : (inplace ? 2 * (n[1] / 2 + 1) : n[1])),
473 : fft_team_size(), inplace, key);
474 :
475 : offload_fftHandle *plan = lookup_plan_from_cache(key);
476 : if (plan == NULL) {
477 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
478 : bool dir, inpl;
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,
486 : number_of_ffts);
487 : offload_fftSetStream(*plan, stream);
488 : add_plan_to_cache(key, plan);
489 : }
490 : return_plan_to_cache(plan);
491 : }
492 :
493 : /*******************************************************************************
494 : * \brief Performs a scaled double precision complex 3D-FFT on the GPU.
495 : * Input/output is a DEVICE pointer (data).
496 : * \param direction FFT direction (forward or backward).
497 : * \param nx Transform size along x.
498 : * \param ny Transform size along y.
499 : * \param nz Transform size along z.
500 : * \param number_of_ffts Number of independent (batched) transforms.
501 : * \param transpose_in Whether the input array uses the transposed layout.
502 : * \param transpose_out Whether the output array uses the transposed layout.
503 : * \param inplace Whether the transform is performed in-place.
504 : * \author Andreas Gloess, Ole Schuett
505 : ******************************************************************************/
506 : static 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) {
510 : fft_key_t key;
511 : get_key_3d(direction == OFFLOAD_FFT_FORWARD, (const int[]){nx, ny, nz},
512 : number_of_ffts, transpose_in, transpose_out, fft_team_size(),
513 : inplace, key);
514 :
515 : offload_fftHandle *plan = lookup_plan_from_cache(key);
516 : if (plan == NULL) {
517 : int rank, threads, nffts, istride, ostride, idist, odist;
518 : bool dir, inpl;
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,
522 : &ostride);
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);
528 : }
529 : return_plan_to_cache(plan);
530 : }
531 :
532 : /*******************************************************************************
533 : * \brief Performs a scaled double precision complex 3D-FFT on the GPU.
534 : * Input/output is a DEVICE pointer (data).
535 : * \param direction FFT direction (forward or backward).
536 : * \param nx Transform size along x.
537 : * \param ny Transform size along y.
538 : * \param nz Transform size along z.
539 : * \param number_of_ffts Number of independent (batched) transforms.
540 : * \param transpose_in Whether the input array uses the transposed layout.
541 : * \param transpose_out Whether the output array uses the transposed layout.
542 : * \param inplace Whether the transform is performed in-place.
543 : * \author Andreas Gloess, Ole Schuett
544 : ******************************************************************************/
545 : static 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) {
551 : fft_key_t key;
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(),
554 : inplace, key);
555 :
556 : offload_fftHandle *plan = lookup_plan_from_cache(key);
557 : if (plan == NULL) {
558 : int rank, threads, nffts, istride, ostride, idist, odist;
559 : bool dir, inpl;
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,
563 : &ostride);
564 : plan = malloc(sizeof(offload_fftHandle));
565 : offload_fftPlanMany(
566 : plan, rank, fft_size, inembed, istride, idist, onembed, ostride, odist,
567 : direction == OFFLOAD_FFT_FORWARD ? OFFLOAD_FFT_D2Z : OFFLOAD_FFT_Z2D,
568 : nffts);
569 : offload_fftSetStream(*plan, stream);
570 : add_plan_to_cache(key, plan);
571 : }
572 : return_plan_to_cache(plan);
573 : }
574 :
575 : /*******************************************************************************
576 : * \brief Performs a scaled double precision complex 1D-FFT many times on
577 : * the GPU.
578 : * Input/output are DEVICE pointers (data_in, date_out).
579 : * \param direction FFT direction (forward or backward).
580 : * \param n Transform length.
581 : * \param m Number of transforms.
582 : * \param transpose_in Whether the input array uses the transposed layout.
583 : * \param transpose_out Whether the output array uses the transposed layout.
584 : * \param leading_dimension_in Leading dimension of the input array.
585 : * \param leading_dimension_out Leading dimension of the output array.
586 : * \param data_in Input data buffer.
587 : * \param data_out Output data buffer.
588 : * \author Andreas Gloess, Ole Schuett
589 : ******************************************************************************/
590 : static 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,
594 : double *data_out) {
595 : fft_key_t key;
596 : get_key_1d(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
597 : transpose_out, leading_dimension_in, leading_dimension_out,
598 : fft_team_size(), data_in == data_out, key);
599 : offload_fftHandle *plan = lookup_plan_from_cache(key);
600 :
601 : if (plan == NULL) {
602 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
603 : bool dir, inplace;
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);
613 : }
614 :
615 : offload_fftExecZ2Z(*plan, data_in, data_out, direction);
616 : return_plan_to_cache(plan);
617 : }
618 :
619 : /*******************************************************************************
620 : * \brief Performs a scaled double precision complex 1D-FFT many times on
621 : * the GPU.
622 : * Input/output are DEVICE pointers (data_in, date_out).
623 : * \param direction FFT direction (forward or backward).
624 : * \param n Transform length.
625 : * \param m Number of transforms.
626 : * \param transpose_in Whether the input array uses the transposed layout.
627 : * \param transpose_out Whether the output array uses the transposed layout.
628 : * \param leading_dimension_in Leading dimension of the input array.
629 : * \param leading_dimension_out Leading dimension of the output array.
630 : * \param data_in Input data buffer.
631 : * \param data_out Output data buffer.
632 : * \author Andreas Gloess, Ole Schuett
633 : ******************************************************************************/
634 : static 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) {
639 : fft_key_t key;
640 : get_key_1d_r2c(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
641 : transpose_out, leading_dimension_in, leading_dimension_out,
642 : fft_team_size(), data_in == data_out, key);
643 : offload_fftHandle *plan = lookup_plan_from_cache(key);
644 :
645 : if (plan == NULL) {
646 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
647 : bool dir, inplace;
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,
655 : number_of_ffts);
656 : offload_fftSetStream(*plan, stream);
657 : add_plan_to_cache(key, plan);
658 : }
659 :
660 : if (direction == OFFLOAD_FFT_FORWARD) {
661 : offload_fftExecD2Z(*plan, data_in, data_out);
662 : } else {
663 : offload_fftExecZ2D(*plan, data_in, data_out);
664 : }
665 : return_plan_to_cache(plan);
666 : }
667 :
668 : /*******************************************************************************
669 : * \brief Performs a scaled double precision complex 1D-FFT many times on
670 : * the GPU.
671 : * Input/output are DEVICE pointers (data_in, date_out).
672 : * \param direction FFT direction (forward or backward).
673 : * \param n Transform length.
674 : * \param m Number of transforms.
675 : * \param transpose_in Whether the input array uses the transposed layout.
676 : * \param transpose_out Whether the output array uses the transposed layout.
677 : * \param data_in Input data buffer.
678 : * \param data_out Output data buffer.
679 : * \author Andreas Gloess, Ole Schuett
680 : ******************************************************************************/
681 : static 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) {
684 : fft_key_t key;
685 : get_key_2d(direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in,
686 : transpose_out, transpose_in ? m : n[0] * n[1],
687 : transpose_out ? m : n[0] * n[1], fft_team_size(),
688 : data_in == data_out, key);
689 : offload_fftHandle *plan = lookup_plan_from_cache(key);
690 :
691 : if (plan == NULL) {
692 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
693 : bool dir, inplace;
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);
703 : }
704 :
705 : offload_fftExecZ2Z(*plan, data_in, data_out, direction);
706 : return_plan_to_cache(plan);
707 : }
708 :
709 : /*******************************************************************************
710 : * \brief Performs a scaled double precision complex R2C/C2R-2D-FFT many times
711 : * on the GPU. Input/output are DEVICE pointers (data_in, date_out).
712 : * \param direction FFT direction (forward or backward).
713 : * \param n Transform length.
714 : * \param m Number of transforms.
715 : * \param transpose_in Whether the input array uses the transposed layout.
716 : * \param transpose_out Whether the output array uses the transposed layout.
717 : * \param data_in Input data buffer.
718 : * \param data_out Output data buffer.
719 : * \author Andreas Gloess, Ole Schuett
720 : ******************************************************************************/
721 : static 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) {
724 : fft_key_t key;
725 : get_key_2d_r2c(
726 : direction == OFFLOAD_FFT_FORWARD, n, m, transpose_in, transpose_out,
727 : transpose_in
728 : ? m
729 : : n[0] * (direction == OFFLOAD_FFT_FORWARD
730 : ? (data_in == data_out ? 2 * (n[1] / 2 + 1) : n[1])
731 : : n[1] / 2 + 1),
732 : transpose_out
733 : ? m
734 : : n[0] * (direction == OFFLOAD_FFT_FORWARD
735 : ? n[1] / 2 + 1
736 : : (data_in == data_out ? 2 * (n[1] / 2 + 1) : n[1])),
737 : fft_team_size(), data_in == data_out, key);
738 : offload_fftHandle *plan = lookup_plan_from_cache(key);
739 :
740 : if (plan == NULL) {
741 : int rank, number_of_threads, number_of_ffts, istride, ostride, idist, odist;
742 : bool dir, inplace;
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,
750 : number_of_ffts);
751 : offload_fftSetStream(*plan, stream);
752 : add_plan_to_cache(key, plan);
753 : }
754 :
755 : if (direction == OFFLOAD_FFT_FORWARD) {
756 : offload_fftExecD2Z(*plan, data_in, data_out);
757 : } else {
758 : offload_fftExecZ2D(*plan, data_in, data_out);
759 : }
760 : return_plan_to_cache(plan);
761 : }
762 :
763 : /*******************************************************************************
764 : * \brief Performs a scaled double precision complex 3D-FFT on the GPU.
765 : * Input/output is a DEVICE pointer (data).
766 : * \param direction FFT direction (forward or backward).
767 : * \param nx Transform size along x.
768 : * \param ny Transform size along y.
769 : * \param nz Transform size along z.
770 : * \param number_of_ffts Number of independent (batched) transforms.
771 : * \param transpose_in Whether the input array uses the transposed layout.
772 : * \param transpose_out Whether the output array uses the transposed layout.
773 : * \param data_in Input data buffer.
774 : * \param data_out Output data buffer.
775 : * \author Andreas Gloess, Ole Schuett
776 : ******************************************************************************/
777 : static 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) {
781 : fft_key_t key;
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);
786 :
787 : if (plan == NULL) {
788 : int rank, threads, nffts, istride, ostride, idist, odist;
789 : bool dir, inpl;
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,
793 : &ostride);
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);
799 : }
800 :
801 : offload_fftExecZ2Z(*plan, data_in, data_out, direction);
802 : return_plan_to_cache(plan);
803 : }
804 :
805 : /*******************************************************************************
806 : * \brief Performs a scaled double precision complex 3D-FFT on the GPU.
807 : * Input/output is a DEVICE pointer (data).
808 : * \param direction FFT direction (forward or backward).
809 : * \param nx Transform size along x.
810 : * \param ny Transform size along y.
811 : * \param nz Transform size along z.
812 : * \param number_of_ffts Number of independent (batched) transforms.
813 : * \param transpose_in Whether the input array uses the transposed layout.
814 : * \param transpose_out Whether the output array uses the transposed layout.
815 : * \param data_in Input data buffer.
816 : * \param data_out Output data buffer.
817 : * \author Andreas Gloess, Ole Schuett
818 : ******************************************************************************/
819 : static 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) {
823 : fft_key_t key;
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);
828 :
829 : if (plan == NULL) {
830 : int rank, threads, nffts, istride, ostride, idist, odist;
831 : bool dir, inpl;
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,
835 : &ostride);
836 : plan = malloc(sizeof(offload_fftHandle));
837 : offload_fftPlanMany(
838 : plan, rank, fft_size, inembed, istride, idist, onembed, ostride, odist,
839 : direction == OFFLOAD_FFT_FORWARD ? OFFLOAD_FFT_D2Z : OFFLOAD_FFT_Z2D,
840 : nffts);
841 : offload_fftSetStream(*plan, stream);
842 : add_plan_to_cache(key, plan);
843 : }
844 :
845 : if (direction == OFFLOAD_FFT_FORWARD) {
846 : offload_fftExecD2Z(*plan, data_in, data_out);
847 : } else {
848 : offload_fftExecZ2D(*plan, data_in, data_out);
849 : }
850 : return_plan_to_cache(plan);
851 : }
852 : #endif
853 :
854 : /*******************************************************************************
855 : * \brief Performs a (double precision complex) 3D-FFT on the GPU.
856 : * \param dir FFT direction (forward or backward).
857 : * \param npts Transform grid dimensions.
858 : * \param inplace Whether the transform is performed in-place.
859 : * \param number_of_ffts Number of independent (batched) transforms.
860 : * \param transpose_in Whether the input array uses the transposed layout.
861 : * \param transpose_out Whether the output array uses the transposed layout.
862 : * \author Andreas Gloess, Ole Schuett
863 : ******************************************************************************/
864 0 : void fft_gpu_register_3d_local(const bool dir, const int *npts,
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)
869 : // Check inputs.
870 : if (npts[0] == 0 || npts[1] == 0 || npts[2] == 0 || number_of_ffts == 0) {
871 : return; // Nothing to do.
872 : }
873 : // Run FFT on the device.
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);
877 : #else
878 0 : (void)dir;
879 0 : (void)npts;
880 0 : (void)inplace;
881 0 : (void)number_of_ffts;
882 0 : (void)transpose_in;
883 0 : (void)transpose_out;
884 : #endif
885 0 : }
886 :
887 : /*******************************************************************************
888 : * \brief Performs a 3D-R2C/C2R-FFT, on the GPU.
889 : * \param dir FFT direction (forward or backward).
890 : * \param npts Transform grid dimensions.
891 : * \param inplace Whether the transform is performed in-place.
892 : * \param number_of_ffts Number of independent (batched) transforms.
893 : * \param transpose_in Whether the input array uses the transposed layout.
894 : * \param transpose_out Whether the output array uses the transposed layout.
895 : * \author Andreas Gloess, Ole Schuett
896 : ******************************************************************************/
897 0 : void fft_gpu_register_3d_r2c_local(const bool dir, const int *npts,
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)
902 : // Check inputs.
903 : if (npts[0] == 0 || npts[1] == 0 || npts[2] == 0 || number_of_ffts == 0) {
904 : return; // Nothing to do.
905 : }
906 :
907 : // Run FFT on the device.
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);
911 : #else
912 0 : (void)dir;
913 0 : (void)npts;
914 0 : (void)inplace;
915 0 : (void)number_of_ffts;
916 0 : (void)transpose_in;
917 0 : (void)transpose_out;
918 : #endif
919 0 : }
920 :
921 : /*******************************************************************************
922 : * \brief Performs a (double precision complex) 1D-FFT on the GPU.
923 : * \param dir FFT direction (forward or backward).
924 : * \param n Transform length.
925 : * \param m Number of transforms.
926 : * \param transpose_in Whether the input array uses the transposed layout.
927 : * \param transpose_out Whether the output array uses the transposed layout.
928 : * \param leading_dimension_in Leading dimension of the input array.
929 : * \param leading_dimension_out Leading dimension of the output array.
930 : * \param inplace Whether the transform is performed in-place.
931 : * \author Andreas Gloess, Ole Schuett
932 : ******************************************************************************/
933 0 : void fft_gpu_register_1d_local(const bool dir, const int n, const int m,
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)
940 : // Check inputs.
941 : if (n == 0 || m == 0) {
942 : return; // Nothing to do.
943 : }
944 :
945 : // Run FFT on the device.
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);
949 : #else
950 0 : (void)dir;
951 0 : (void)n;
952 0 : (void)m;
953 0 : (void)transpose_in;
954 0 : (void)transpose_out;
955 0 : (void)leading_dimension_in;
956 0 : (void)leading_dimension_out;
957 0 : (void)inplace;
958 : #endif
959 0 : }
960 :
961 : /*******************************************************************************
962 : * \brief Performs a (double precision complex) 1D-FFT on the GPU.
963 : * \param dir FFT direction (forward or backward).
964 : * \param n Transform length.
965 : * \param m Number of transforms.
966 : * \param transpose_in Whether the input array uses the transposed layout.
967 : * \param transpose_out Whether the output array uses the transposed layout.
968 : * \param leading_dimension_in Leading dimension of the input array.
969 : * \param leading_dimension_out Leading dimension of the output array.
970 : * \param inplace Whether the transform is performed in-place.
971 : * \author Andreas Gloess, Ole Schuett
972 : ******************************************************************************/
973 0 : void fft_gpu_register_1d_r2c_local(const bool dir, const int n, const int m,
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)
980 : // Check inputs.
981 : const int nrpts = n * m;
982 : if (nrpts == 0) {
983 : return;
984 : }
985 :
986 : // Run FFT on the device.
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);
990 : #else
991 0 : (void)dir;
992 0 : (void)n;
993 0 : (void)m;
994 0 : (void)transpose_in;
995 0 : (void)transpose_out;
996 0 : (void)leading_dimension_in;
997 0 : (void)leading_dimension_out;
998 0 : (void)inplace;
999 : #endif
1000 0 : }
1001 :
1002 : /*******************************************************************************
1003 : * \brief Performs a (double precision complex) 2D-FFT on the GPU.
1004 : * \param dir FFT direction (forward or backward).
1005 : * \param n Transform length.
1006 : * \param m Number of transforms.
1007 : * \param transpose_in Whether the input array uses the transposed layout.
1008 : * \param transpose_out Whether the output array uses the transposed layout.
1009 : * \param inplace Whether the transform is performed in-place.
1010 : * \author Frederick Stein
1011 : ******************************************************************************/
1012 0 : void fft_gpu_register_2d_local(const bool dir, const int n[2], const int m,
1013 : const bool transpose_in,
1014 : const bool transpose_out, const bool inplace) {
1015 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1016 : // Check inputs.
1017 : const int nrpts = n[0] * n[1] * m;
1018 : if (nrpts == 0) {
1019 : return;
1020 : }
1021 :
1022 : // Run FFT on the device.
1023 : fft_gpu_register_2d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
1024 : transpose_in, transpose_out, inplace);
1025 : #else
1026 0 : (void)dir;
1027 0 : (void)n;
1028 0 : (void)m;
1029 0 : (void)transpose_in;
1030 0 : (void)transpose_out;
1031 0 : (void)inplace;
1032 : #endif
1033 0 : }
1034 :
1035 : /*******************************************************************************
1036 : * \brief Performs a (double precision complex) R2C/C2R2D-FFT on the GPU.
1037 : * \param dir FFT direction (forward or backward).
1038 : * \param n Transform length.
1039 : * \param m Number of transforms.
1040 : * \param transpose_in Whether the input array uses the transposed layout.
1041 : * \param transpose_out Whether the output array uses the transposed layout.
1042 : * \param inplace Whether the transform is performed in-place.
1043 : * \author Andreas Gloess, Ole Schuett
1044 : ******************************************************************************/
1045 0 : void fft_gpu_register_2d_r2c_local(const bool dir, const int n[2], const int m,
1046 : const bool transpose_in,
1047 : const bool transpose_out,
1048 : const bool inplace) {
1049 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1050 : // Check inputs.
1051 : if (n[0] == 0 || n[1] == 0 || m == 0) {
1052 : return;
1053 : }
1054 :
1055 : // Run FFT on the device.
1056 : fft_gpu_register_r2c_2d(dir ? OFFLOAD_FFT_FORWARD : OFFLOAD_FFT_INVERSE, n, m,
1057 : transpose_in, transpose_out, inplace);
1058 : #else
1059 0 : (void)dir;
1060 0 : (void)n;
1061 0 : (void)m;
1062 0 : (void)transpose_in;
1063 0 : (void)transpose_out;
1064 0 : (void)inplace;
1065 : #endif
1066 0 : }
1067 :
1068 : /*******************************************************************************
1069 : * \brief Performs a (double precision complex) FFT, followed by a (double
1070 : * precision complex) gather, on the GPU.
1071 : * \param din Input data buffer.
1072 : * \param zout Output complex data.
1073 : * \param ghatmap Index map for reciprocal-space points.
1074 : * \param npts Transform grid dimensions.
1075 : * \param ngpts Number of grid points.
1076 : * \param scale Scaling factor.
1077 : * \author Andreas Gloess, Ole Schuett
1078 : ******************************************************************************/
1079 0 : void fft_gpu_cfffg(const double *din, double *zout, const int *ghatmap,
1080 : const int *npts, const int ngpts, const double scale) {
1081 : // Check inputs.
1082 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1083 : const int nrpts = npts[0] * npts[1] * npts[2];
1084 : cp_assert(ngpts <= nrpts);
1085 : if (nrpts == 0 || ngpts == 0) {
1086 : return; // Nothing to do.
1087 : }
1088 :
1089 : // Allocate device memory.
1090 : offload_activate_chosen_device();
1091 : const size_t buffer_size = 2 * sizeof(double) * nrpts;
1092 : const size_t map_size = sizeof(int) * ngpts;
1093 : ensure_memory_sizes(buffer_size, map_size);
1094 :
1095 : // Upload REAL input and convert to COMPLEX on device.
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);
1098 :
1099 : // Run FFT on the device.
1100 : fft_gpu_3d(OFFLOAD_FFT_FORWARD, npts[2], npts[1], npts[0], 1, false, false,
1101 : buffer_dev_2, buffer_dev_2);
1102 :
1103 : // Upload map and run gather on the device.
1104 : offloadMemcpyAsyncHtoD(ghatmap_dev, ghatmap, map_size, stream);
1105 : fft_gpu_launch_gather(buffer_dev_1, buffer_dev_2, scale, ngpts, ghatmap_dev,
1106 : stream);
1107 :
1108 : // Download COMPLEX results to host.
1109 : offloadMemcpyAsyncDtoH(zout, buffer_dev_1, 2 * sizeof(double) * ngpts,
1110 : stream);
1111 : offloadStreamSynchronize(stream);
1112 : #else
1113 0 : (void)din;
1114 0 : (void)zout;
1115 0 : (void)ghatmap;
1116 0 : (void)npts;
1117 0 : (void)ngpts;
1118 0 : (void)scale;
1119 : #endif
1120 0 : }
1121 :
1122 : /*******************************************************************************
1123 : * \brief Performs a (double precision complex) scatter, followed by a
1124 : * (double precision complex) FFT, on the GPU.
1125 : * \param zin Input complex data.
1126 : * \param dout Output data buffer.
1127 : * \param ghatmap Index map for reciprocal-space points.
1128 : * \param npts Transform grid dimensions.
1129 : * \param ngpts Number of grid points.
1130 : * \param nmaps Number of index maps.
1131 : * \param scale Scaling factor.
1132 : * \author Andreas Gloess, Ole Schuett
1133 : ******************************************************************************/
1134 0 : void fft_gpu_sfffc(const double *zin, double *dout, const int *ghatmap,
1135 : const int *npts, const int ngpts, const int nmaps,
1136 : const double scale) {
1137 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1138 : // Check inputs.
1139 : const int nrpts = npts[0] * npts[1] * npts[2];
1140 : cp_assert(ngpts <= nrpts);
1141 : if (nrpts == 0 || ngpts == 0) {
1142 : return; // Nothing to do.
1143 : }
1144 :
1145 : // Allocate device memory.
1146 : offload_activate_chosen_device();
1147 : const size_t buffer_size = 2 * sizeof(double) * nrpts;
1148 : const size_t map_size = sizeof(int) * nmaps * ngpts;
1149 : ensure_memory_sizes(buffer_size, map_size);
1150 :
1151 : // Upload COMPLEX inputs to device.
1152 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, 2 * sizeof(double) * ngpts, stream);
1153 :
1154 : // Upload map and run scatter on the device.
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);
1159 :
1160 : // Run FFT on the device.
1161 : fft_gpu_3d(OFFLOAD_FFT_INVERSE, npts[2], npts[1], npts[0], 1, false, false,
1162 : buffer_dev_2, buffer_dev_2);
1163 :
1164 : // Convert COMPLEX results to REAL and download to host.
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);
1168 : #else
1169 0 : (void)zin;
1170 0 : (void)dout;
1171 0 : (void)ghatmap;
1172 0 : (void)npts;
1173 0 : (void)ngpts;
1174 0 : (void)nmaps;
1175 0 : (void)scale;
1176 : #endif
1177 0 : }
1178 :
1179 : /*******************************************************************************
1180 : * \brief Performs a (double precision complex) 3D-FFT on the GPU.
1181 : * \param zin Input complex data.
1182 : * \param zout Output complex data.
1183 : * \param dir FFT direction (forward or backward).
1184 : * \param npts Transform grid dimensions.
1185 : * \param number_of_ffts Number of independent (batched) transforms.
1186 : * \param transpose_in Whether the input array uses the transposed layout.
1187 : * \param transpose_out Whether the output array uses the transposed layout.
1188 : * \author Andreas Gloess, Ole Schuett
1189 : ******************************************************************************/
1190 0 : void fft_gpu_3d_local(const double *zin, double *zout, const bool dir,
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)
1194 : // Check inputs.
1195 : if (npts[0] == 0 || npts[1] == 0 || npts[2] == 0 || number_of_ffts == 0) {
1196 : return; // Nothing to do.
1197 : }
1198 :
1199 : // Allocate device memory.
1200 : offload_activate_chosen_device();
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;
1203 : ensure_memory_sizes(buffer_size, 0);
1204 :
1205 : // Upload COMPLEX inputs to device.
1206 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1207 :
1208 : // Run FFT on the device.
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);
1212 :
1213 : // Download to host
1214 : offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1215 : buffer_size, stream);
1216 : offloadStreamSynchronize(stream);
1217 : #else
1218 0 : (void)zin;
1219 0 : (void)zout;
1220 0 : (void)dir;
1221 0 : (void)npts;
1222 0 : (void)number_of_ffts;
1223 0 : (void)transpose_in;
1224 0 : (void)transpose_out;
1225 : #endif
1226 0 : }
1227 :
1228 : /*******************************************************************************
1229 : * \brief Performs a 3D-R2C/C2R-FFT, on the GPU.
1230 : * \param zin Input complex data.
1231 : * \param zout Output complex data.
1232 : * \param dir FFT direction (forward or backward).
1233 : * \param npts Transform grid dimensions.
1234 : * \param number_of_ffts Number of independent (batched) transforms.
1235 : * \param transpose_in Whether the input array uses the transposed layout.
1236 : * \param transpose_out Whether the output array uses the transposed layout.
1237 : * \author Andreas Gloess, Ole Schuett
1238 : ******************************************************************************/
1239 0 : void fft_gpu_3d_r2c_local(const double *zin, double *zout, const bool dir,
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)
1243 : // Check inputs.
1244 : if (npts[0] == 0 || npts[1] == 0 || npts[2] == 0 || number_of_ffts == 0) {
1245 : return; // Nothing to do.
1246 : }
1247 :
1248 : // Allocate device memory. cuFFT halves the last dimension, and the
1249 : // transform is done out-of-place so that neither side needs padding.
1250 : offload_activate_chosen_device();
1251 : const size_t real_size = sizeof(double) * npts[0] * npts[1] *
1252 : (zin != zout ? npts[2] : 2 * (npts[2] / 2 + 1)) *
1253 : number_of_ffts;
1254 : const size_t complex_size = sizeof(double) * 2 * npts[0] * npts[1] *
1255 : (npts[2] / 2 + 1) * number_of_ffts;
1256 : ensure_memory_sizes(complex_size > real_size ? complex_size : real_size, 0);
1257 :
1258 : // Upload inputs to device.
1259 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, dir ? real_size : complex_size,
1260 : stream);
1261 :
1262 : // Run FFT on the device.
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);
1266 :
1267 : // Download to host
1268 : offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1269 : dir ? complex_size : real_size, stream);
1270 : offloadStreamSynchronize(stream);
1271 : #else
1272 0 : (void)zin;
1273 0 : (void)zout;
1274 0 : (void)dir;
1275 0 : (void)npts;
1276 0 : (void)number_of_ffts;
1277 0 : (void)transpose_in;
1278 0 : (void)transpose_out;
1279 : #endif
1280 0 : }
1281 :
1282 : /*******************************************************************************
1283 : * \brief Performs a (double to complex double) blow-up and a (double
1284 : * precision complex) 2D-FFT on the GPU.
1285 : * \param din Input data buffer.
1286 : * \param zout Output complex data.
1287 : * \param npts Transform grid dimensions.
1288 : * \author Andreas Gloess, Ole Schuett
1289 : ******************************************************************************/
1290 0 : void fft_gpu_cff(const double *din, double *zout, const int *npts) {
1291 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1292 : // Check inputs.
1293 : const int nrpts = npts[0] * npts[1] * npts[2];
1294 : if (nrpts == 0) {
1295 : return; // Nothing to do.
1296 : }
1297 :
1298 : // Allocate device memory.
1299 : offload_activate_chosen_device();
1300 : const size_t buffer_size = 2 * sizeof(double) * nrpts;
1301 : ensure_memory_sizes(buffer_size, 0);
1302 :
1303 : // Upload REAL input and convert to COMPLEX on device.
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);
1306 :
1307 : // Run FFT on the device.
1308 : // NOTE: Could use 2D-FFT, but CUDA does them C-shaped which is not optimal.
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);
1313 :
1314 : // Download COMPLEX results to host.
1315 : offloadMemcpyAsyncDtoH(zout, buffer_dev_2, buffer_size, stream);
1316 : offloadStreamSynchronize(stream);
1317 : #else
1318 0 : (void)din;
1319 0 : (void)zout;
1320 0 : (void)npts;
1321 : #endif
1322 0 : }
1323 :
1324 : /*******************************************************************************
1325 : * \brief Performs a (double precision complex) 2D-FFT and a (double complex
1326 : * to double) shrink-down on the GPU.
1327 : * \param zin Input complex data.
1328 : * \param dout Output data buffer.
1329 : * \param npts Transform grid dimensions.
1330 : * \author Andreas Gloess, Ole Schuett
1331 : ******************************************************************************/
1332 0 : void fft_gpu_ffc(const double *zin, double *dout, const int *npts) {
1333 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1334 : // Check inputs.
1335 : const int nrpts = npts[0] * npts[1] * npts[2];
1336 : if (nrpts == 0) {
1337 : return; // Nothing to do.
1338 : }
1339 :
1340 : // Allocate device memory.
1341 : offload_activate_chosen_device();
1342 : const size_t buffer_size = 2 * sizeof(double) * nrpts;
1343 : ensure_memory_sizes(buffer_size, 0);
1344 :
1345 : // Upload COMPLEX input to device.
1346 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1347 :
1348 : // Run FFT on the device.
1349 : // NOTE: Could use 2D-FFT, but CUDA does them C-shaped which is not optimal.
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);
1355 :
1356 : // Download REAL results to host.
1357 : offloadMemcpyAsyncDtoH(dout, buffer_dev_2, buffer_size / 2, stream);
1358 : offloadStreamSynchronize(stream);
1359 : #else
1360 0 : (void)zin;
1361 0 : (void)dout;
1362 0 : (void)npts;
1363 : #endif
1364 0 : }
1365 :
1366 : /*******************************************************************************
1367 : * \brief Performs a (double to complex double) blow-up and a (double
1368 : * precision complex) 1D-FFT on the GPU.
1369 : * \param din Input data buffer.
1370 : * \param zout Output complex data.
1371 : * \param npts Transform grid dimensions.
1372 : * \author Andreas Gloess, Ole Schuett
1373 : ******************************************************************************/
1374 0 : void fft_gpu_cf(const double *din, double *zout, const int *npts) {
1375 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1376 : // Check inputs.
1377 : const int nrpts = npts[0] * npts[1] * npts[2];
1378 : if (nrpts == 0) {
1379 : return; // Nothing to do.
1380 : }
1381 :
1382 : // Allocate device memory.
1383 : offload_activate_chosen_device();
1384 : const size_t buffer_size = 2 * sizeof(double) * nrpts;
1385 : ensure_memory_sizes(buffer_size, 0);
1386 :
1387 : // Upload REAL input and convert to COMPLEX on device.
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);
1390 :
1391 : // Run FFT on the device.
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);
1394 :
1395 : // Download COMPLEX results from device.
1396 : offloadMemcpyAsyncDtoH(zout, buffer_dev_1, buffer_size, stream);
1397 : offloadStreamSynchronize(stream);
1398 : #else
1399 0 : (void)din;
1400 0 : (void)zout;
1401 0 : (void)npts;
1402 : #endif
1403 0 : }
1404 :
1405 : /*******************************************************************************
1406 : * \brief Performs a (double precision complex) 1D-FFT and a (double complex
1407 : * to double) shrink-down on the GPU.
1408 : * \param zin Input complex data.
1409 : * \param dout Output data buffer.
1410 : * \param npts Transform grid dimensions.
1411 : * \author Andreas Gloess, Ole Schuett
1412 : ******************************************************************************/
1413 0 : void fft_gpu_fc(const double *zin, double *dout, const int *npts) {
1414 : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_FFT)
1415 : // Check inputs.
1416 : const int nrpts = npts[0] * npts[1] * npts[2];
1417 : if (nrpts == 0) {
1418 : return; // Nothing to do.
1419 : }
1420 :
1421 : // Allocate device memory.
1422 : offload_activate_chosen_device();
1423 : const size_t buffer_size = 2 * sizeof(double) * nrpts;
1424 : ensure_memory_sizes(buffer_size, 0);
1425 :
1426 : // Upload COMPLEX input to device.
1427 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1428 :
1429 : // Run FFT on the device.
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);
1432 :
1433 : // Convert COMPLEX results to REAL and download to host.
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);
1437 : #else
1438 0 : (void)zin;
1439 0 : (void)dout;
1440 0 : (void)npts;
1441 : #endif
1442 0 : }
1443 :
1444 : /*******************************************************************************
1445 : * \brief Performs a (double precision complex) 1D-FFT on the GPU.
1446 : * \param zin Input complex data.
1447 : * \param zout Output complex data.
1448 : * \param dir FFT direction (forward or backward).
1449 : * \param n Transform length.
1450 : * \param m Number of transforms.
1451 : * \param transpose_in Whether the input array uses the transposed layout.
1452 : * \param transpose_out Whether the output array uses the transposed layout.
1453 : * \param leading_dimension_in Leading dimension of the input array.
1454 : * \param leading_dimension_out Leading dimension of the output array.
1455 : * \author Andreas Gloess, Ole Schuett
1456 : ******************************************************************************/
1457 0 : void fft_gpu_1d_local(const double *zin, double *zout, const bool dir,
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)
1462 : // Check inputs.
1463 : if (n == 0 || m == 0) {
1464 : return; // Nothing to do.
1465 : }
1466 :
1467 : // Allocate device memory.
1468 : offload_activate_chosen_device();
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;
1477 : ensure_memory_sizes(imax(input_size, output_size), 0);
1478 :
1479 : // Upload COMPLEX input to device.
1480 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, input_size, stream);
1481 :
1482 : // Run FFT on the device.
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);
1487 :
1488 : // Download only the valid (strided) results so that padding between
1489 : // transforms in the host array is left untouched.
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);
1496 : #else
1497 0 : (void)zin;
1498 0 : (void)zout;
1499 0 : (void)dir;
1500 0 : (void)n;
1501 0 : (void)m;
1502 0 : (void)transpose_in;
1503 0 : (void)transpose_out;
1504 0 : (void)leading_dimension_in;
1505 0 : (void)leading_dimension_out;
1506 : #endif
1507 0 : }
1508 :
1509 : /*******************************************************************************
1510 : * \brief Performs a (double precision complex) 1D-FFT on the GPU.
1511 : * \param zin Input complex data.
1512 : * \param zout Output complex data.
1513 : * \param dir FFT direction (forward or backward).
1514 : * \param n Transform length.
1515 : * \param m Number of transforms.
1516 : * \param transpose_in Whether the input array uses the transposed layout.
1517 : * \param transpose_out Whether the output array uses the transposed layout.
1518 : * \param leading_dimension_in Leading dimension of the input array.
1519 : * \param leading_dimension_out Leading dimension of the output array.
1520 : * \author Andreas Gloess, Ole Schuett
1521 : ******************************************************************************/
1522 0 : void fft_gpu_1d_r2c_local(const double *zin, double *zout, const bool dir,
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)
1528 : // Check inputs.
1529 : if (n == 0 || m == 0) {
1530 : return;
1531 : }
1532 :
1533 : // Allocate device memory.
1534 : offload_activate_chosen_device();
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));
1543 : ensure_memory_sizes(imax(input_size, output_size), 0);
1544 :
1545 : // Upload COMPLEX input to device.
1546 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, input_size, stream);
1547 :
1548 : // Run FFT on the device.
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);
1553 :
1554 : // Download COMPLEX results from device.
1555 : offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1556 : output_size, stream);
1557 : offloadStreamSynchronize(stream);
1558 : #else
1559 0 : (void)zin;
1560 0 : (void)zout;
1561 0 : (void)dir;
1562 0 : (void)n;
1563 0 : (void)m;
1564 0 : (void)transpose_in;
1565 0 : (void)transpose_out;
1566 0 : (void)leading_dimension_in;
1567 0 : (void)leading_dimension_out;
1568 : #endif
1569 0 : }
1570 :
1571 : /*******************************************************************************
1572 : * \brief Performs a (double precision complex) 2D-FFT on the GPU.
1573 : * \param zin Input complex data.
1574 : * \param zout Output complex data.
1575 : * \param dir FFT direction (forward or backward).
1576 : * \param n Transform length.
1577 : * \param m Number of transforms.
1578 : * \param transpose_in Whether the input array uses the transposed layout.
1579 : * \param transpose_out Whether the output array uses the transposed layout.
1580 : * \author Frederick Stein
1581 : ******************************************************************************/
1582 0 : void fft_gpu_2d_local(const double *zin, double *zout, const bool dir,
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)
1586 : // Check inputs.
1587 : if (n[0] == 0 || n[1] == 0 || m == 0) {
1588 : return;
1589 : }
1590 :
1591 : // Allocate device memory.
1592 : offload_activate_chosen_device();
1593 : const size_t buffer_size = 2 * sizeof(double) * n[0] * n[1] * m;
1594 : ensure_memory_sizes(buffer_size, 0);
1595 :
1596 : // Upload COMPLEX input to device.
1597 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1598 :
1599 : // Run FFT on the device.
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);
1603 :
1604 : // Download COMPLEX results from device.
1605 : offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1606 : buffer_size, stream);
1607 : offloadStreamSynchronize(stream);
1608 : #else
1609 0 : (void)zin;
1610 0 : (void)zout;
1611 0 : (void)dir;
1612 0 : (void)n;
1613 0 : (void)m;
1614 0 : (void)transpose_in;
1615 0 : (void)transpose_out;
1616 : #endif
1617 0 : }
1618 :
1619 : /*******************************************************************************
1620 : * \brief Performs a (double precision complex) R2C/C2R2D-FFT on the GPU.
1621 : * \param zin Input complex data.
1622 : * \param zout Output complex data.
1623 : * \param dir FFT direction (forward or backward).
1624 : * \param n Transform length.
1625 : * \param m Number of transforms.
1626 : * \param transpose_in Whether the input array uses the transposed layout.
1627 : * \param transpose_out Whether the output array uses the transposed layout.
1628 : * \author Andreas Gloess, Ole Schuett
1629 : ******************************************************************************/
1630 0 : void fft_gpu_2d_r2c_local(const double *zin, double *zout, const bool dir,
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)
1634 : // Check inputs.
1635 : if (n[0] == 0 || n[1] == 0 || m == 0) {
1636 : return;
1637 : }
1638 :
1639 : // Allocate device memory.
1640 : offload_activate_chosen_device();
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;
1645 : ensure_memory_sizes(imax(input_size, output_size), 0);
1646 :
1647 : // Upload COMPLEX input to device.
1648 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, input_size, stream);
1649 :
1650 : // Run FFT on the device.
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);
1654 :
1655 : // Download COMPLEX results from device.
1656 : offloadMemcpyAsyncDtoH(zout, zin != zout ? buffer_dev_2 : buffer_dev_1,
1657 : output_size, stream);
1658 : offloadStreamSynchronize(stream);
1659 : #else
1660 0 : (void)zin;
1661 0 : (void)zout;
1662 0 : (void)dir;
1663 0 : (void)n;
1664 0 : (void)m;
1665 0 : (void)transpose_in;
1666 0 : (void)transpose_out;
1667 : #endif
1668 0 : }
1669 :
1670 : /*******************************************************************************
1671 : * \brief Performs a (double precision complex) 1D-FFT, followed by a (double
1672 : * precision complex) gather, on the GPU.
1673 : * \param zin Input complex data.
1674 : * \param zout Output complex data.
1675 : * \param ghatmap Index map for reciprocal-space points.
1676 : * \param npts Transform grid dimensions.
1677 : * \param mmax Maximum Miller index.
1678 : * \param ngpts Number of grid points.
1679 : * \param scale Scaling factor.
1680 : * \author Andreas Gloess, Ole Schuett
1681 : ******************************************************************************/
1682 0 : void 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)
1686 : // Check inputs.
1687 : const int nrpts = npts[0] * mmax;
1688 : cp_assert(ngpts <= nrpts);
1689 : if (nrpts == 0 || ngpts == 0) {
1690 : return; // Nothing to do.
1691 : }
1692 :
1693 : // Allocate device memory.
1694 : offload_activate_chosen_device();
1695 : const size_t buffer_size = 2 * sizeof(double) * nrpts;
1696 : const size_t map_size = sizeof(int) * ngpts;
1697 : ensure_memory_sizes(buffer_size, map_size);
1698 :
1699 : // Upload COMPLEX inputs to device.
1700 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, buffer_size, stream);
1701 :
1702 : // Run FFT on the device.
1703 : fft_gpu_1d(OFFLOAD_FFT_FORWARD, npts[0], mmax, true, false, mmax, npts[0],
1704 : buffer_dev_1, buffer_dev_2);
1705 :
1706 : // Upload map and run gather on the device.
1707 : offloadMemcpyAsyncHtoD(ghatmap_dev, ghatmap, map_size, stream);
1708 : fft_gpu_launch_gather(buffer_dev_1, buffer_dev_2, scale, ngpts, ghatmap_dev,
1709 : stream);
1710 :
1711 : // Download COMPLEX results from device.
1712 : offloadMemcpyAsyncDtoH(zout, buffer_dev_1, 2 * sizeof(double) * ngpts,
1713 : stream);
1714 : offloadStreamSynchronize(stream);
1715 : #else
1716 0 : (void)zin;
1717 0 : (void)zout;
1718 0 : (void)ghatmap;
1719 0 : (void)npts;
1720 0 : (void)mmax;
1721 0 : (void)ngpts;
1722 0 : (void)scale;
1723 : #endif
1724 0 : }
1725 :
1726 : /*******************************************************************************
1727 : * \brief Performs a (double precision complex) scatter, followed by a
1728 : * (double precision complex) 1D-FFT, on the GPU.
1729 : * \param zin Input complex data.
1730 : * \param zout Output complex data.
1731 : * \param ghatmap Index map for reciprocal-space points.
1732 : * \param npts Transform grid dimensions.
1733 : * \param mmax Maximum Miller index.
1734 : * \param ngpts Number of grid points.
1735 : * \param nmaps Number of index maps.
1736 : * \param scale Scaling factor.
1737 : * \author Andreas Gloess, Ole Schuett
1738 : ******************************************************************************/
1739 0 : void 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)
1743 : // Check inputs.
1744 : const int nrpts = npts[0] * mmax;
1745 : cp_assert(ngpts <= nrpts);
1746 : if (nrpts == 0 || ngpts == 0) {
1747 : return; // Nothing to do.
1748 : }
1749 :
1750 : // Allocate device memory.
1751 : offload_activate_chosen_device();
1752 : const size_t buffer_size = 2 * sizeof(double) * nrpts;
1753 : const size_t map_size = sizeof(int) * nmaps * ngpts;
1754 : ensure_memory_sizes(buffer_size, map_size);
1755 :
1756 : // Upload COMPLEX inputs to device.
1757 : offloadMemcpyAsyncHtoD(buffer_dev_1, zin, 2 * sizeof(double) * ngpts, stream);
1758 :
1759 : // Upload map and run scatter on the device.
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);
1764 :
1765 : // Run FFT on the device.
1766 : fft_gpu_1d(OFFLOAD_FFT_INVERSE, npts[0], mmax, false, true, npts[0], mmax,
1767 : buffer_dev_2, buffer_dev_1);
1768 :
1769 : // Download COMPLEX results from device.
1770 : offloadMemcpyAsyncDtoH(zout, buffer_dev_1, buffer_size, stream);
1771 : offloadStreamSynchronize(stream);
1772 : #else
1773 0 : (void)zin;
1774 0 : (void)zout;
1775 0 : (void)ghatmap;
1776 0 : (void)npts;
1777 0 : (void)mmax;
1778 0 : (void)ngpts;
1779 0 : (void)nmaps;
1780 0 : (void)scale;
1781 : #endif
1782 0 : }
1783 :
1784 : // EOF
|