(git:e70887f)
Loading...
Searching...
No Matches
fft_key.h
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* CP2K: A general program to perform molecular dynamics simulations */
3/* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: BSD-3-Clause */
6/*----------------------------------------------------------------------------*/
7
8#ifndef FFT_KEY_H
9#define FFT_KEY_H
10
11// Does not work with fft_gpu, so each file including fft_key.h must include
12// cp_mpi.h on this own
13// #include "../mpiwrap/cp_mpi.h"
14#include "fft_utils.h"
15
16#if defined(__FFTW3)
17#include <fftw3.h>
18
19#include "../base/cp_assert.h"
20
21#define KEY_SIZE 15
22typedef int fft_key_t[KEY_SIZE];
23// Meaning of the elements:
24// 0: rank, r2c, inplace, forward (see below)
25// 1: communicator (currently only the null-communicator -> local FFTs, to be
26// used later for distributed FFTs of FFTW3)
27// 2: number of threads
28// 3-6: FFT sizes for the transform dimensions followed by the batch size
29// (unused trailing slots are zero, so rank + 1 dimensions are stored)
30// 7-10: input strides in the same order (transform dimensions, then batch)
31// 11-14: output strides in the same order (transform dimensions, then batch)
32
33// These constants are used to encode general information on a FFT into bits
34// Modulo 4 encodes the rank (1, 2, 3)
35// The third bit encodes whether a R2C/C2R-FFT is used
36// 4 == 2^2
37#define FFT_KEY_R2C 4
38// The fourth bit encodes inplace FFTs
39// 8 == 2^3
40#define FFT_KEY_INPLACE 8
41// The fifth bit encodes the direction (1 is forward, 0 is backward FFT)
42// 16 == 2^4
43#define FFT_KEY_FORWARD 16
44
45/*******************************************************************************
46 * \brief Fill the size and stride fields of a key for a non-Guru FFT.
47 * \param key Encoded FFT key.
48 * \param rank Number of transform dimensions.
49 * \param fft_size Transform sizes (up to three dimensions).
50 * \param number_of_ffts Number of batched transforms.
51 * \param direction FFT direction (forward or backward).
52 * \param inplace Whether the transform is in-place.
53 * \param is_r2c Whether this is a real-to-complex or complex-to-real FFT.
54 * \param istride Stride of the first input dimension.
55 * \param idist Distance between consecutive input transforms.
56 * \param ostride Stride of the first output dimension.
57 * \param odist Distance between consecutive output transforms.
58 ******************************************************************************/
59static inline void set_nd_key(int *key, const int rank, const int fft_size[3],
60 const int number_of_ffts, const bool direction,
61 const bool inplace, const bool is_r2c,
62 const int istride, const int idist,
63 const int ostride, const int odist) {
64 int inembed[3] = {0, 0, 0};
65 int onembed[3] = {0, 0, 0};
66 for (int r = 0; r < rank; r++) {
67 inembed[r] = fft_size[r];
68 onembed[r] = fft_size[r];
69 }
70 if (is_r2c && rank > 0) {
71 const int r = rank - 1;
72 if (direction) {
73 onembed[r] = fft_size[r] / 2 + 1;
74 if (inplace)
75 inembed[r] = 2 * (fft_size[r] / 2 + 1);
76 } else {
77 inembed[r] = fft_size[r] / 2 + 1;
78 if (inplace)
79 onembed[r] = 2 * (fft_size[r] / 2 + 1);
80 }
81 }
82 for (int d = 0; d < 4; d++) {
83 key[3 + d] = 0;
84 key[7 + d] = 0;
85 key[11 + d] = 0;
86 }
87 for (int d = 0; d < rank; d++)
88 key[3 + d] = fft_size[d];
89 key[3 + rank] = number_of_ffts;
90 key[7] = istride;
91 for (int d = 1; d < rank; d++)
92 key[7 + d] = key[7 + d - 1] * inembed[d - 1];
93 key[7 + rank] = idist;
94 key[11] = ostride;
95 for (int d = 1; d < rank; d++)
96 key[11 + d] = key[11 + d - 1] * onembed[d - 1];
97 key[11 + rank] = odist;
98}
99
100/*******************************************************************************
101 * \brief Get key from FFT input parameters (1D, C2C-case)
102 * \author Frederick Stein
103 ******************************************************************************/
104static inline void
105get_key_1d(const bool direction, const int fft_size, const int number_of_ffts,
106 const bool transpose_in, const bool transpose_out,
107 const int leading_dimension_in, const int leading_dimension_out,
108 const int number_of_threads, const bool inplace, int *key) {
109 cp_assert((!inplace || (transpose_in == transpose_out)) &&
110 "Inplace plans need the same strides for input and output arrays!");
111 const int fft_size_3d[3] = {fft_size, 0, 0};
112 const int istride = transpose_in ? leading_dimension_in : 1;
113 const int idist = transpose_in ? 1 : leading_dimension_in;
114 const int ostride = transpose_out ? leading_dimension_out : 1;
115 const int odist = transpose_out ? 1 : leading_dimension_out;
116 key[0] = 1 + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
118 key[2] = number_of_threads;
119 set_nd_key(key, 1, fft_size_3d, number_of_ffts, direction, inplace, false,
120 istride, idist, ostride, odist);
121}
122
123/*******************************************************************************
124 * \brief Get key from FFT input parameters (1D, R2C-case)
125 * \author Frederick Stein
126 ******************************************************************************/
127static inline void
128get_key_1d_r2c(const bool direction, const int fft_size,
129 const int number_of_ffts, const bool transpose_in,
130 const bool transpose_out, const int leading_dimension_in,
131 const int leading_dimension_out, const int number_of_threads,
132 const bool inplace, int *key) {
133 cp_assert((!inplace || (transpose_in == transpose_out)) &&
134 "Inplace plans need the same strides for input and output arrays!");
135 const int fft_size_3d[3] = {fft_size, 0, 0};
136 const int istride = transpose_in ? leading_dimension_in : 1;
137 const int idist = transpose_in ? 1 : leading_dimension_in;
138 const int ostride = transpose_out ? leading_dimension_out : 1;
139 const int odist = transpose_out ? 1 : leading_dimension_out;
140 key[0] =
141 1 + FFT_KEY_R2C + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
143 key[2] = number_of_threads;
144 set_nd_key(key, 1, fft_size_3d, number_of_ffts, direction, inplace, true,
145 istride, idist, ostride, odist);
146}
147
148/*******************************************************************************
149 * \brief Get key from FFT input parameters (2D, C2C-case)
150 * \author Frederick Stein
151 ******************************************************************************/
152static inline void get_key_2d(const bool direction, const int fft_size[2],
153 const int number_of_ffts, const bool transpose_in,
154 const bool transpose_out,
155 const int leading_dimension_in,
156 const int leading_dimension_out,
157 const int number_of_threads, const bool inplace,
158 int *key) {
159 cp_assert((!inplace || (transpose_in == transpose_out)) &&
160 "Inplace plans need the same strides for input and output arrays!");
161 const int fft_size_3d[3] = {fft_size[0], fft_size[1], 0};
162 const int istride = transpose_in ? leading_dimension_in : 1;
163 const int idist = transpose_in ? 1 : leading_dimension_in;
164 const int ostride = transpose_out ? leading_dimension_out : 1;
165 const int odist = transpose_out ? 1 : leading_dimension_out;
166 key[0] = 2 + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
168 key[2] = number_of_threads;
169 set_nd_key(key, 2, fft_size_3d, number_of_ffts, direction, inplace, false,
170 istride, idist, ostride, odist);
171}
172
173/*******************************************************************************
174 * \brief Get key from FFT input parameters (2D, R2C-case)
175 * \author Frederick Stein
176 ******************************************************************************/
177static inline void
178get_key_2d_r2c(const bool direction, const int fft_size[2],
179 const int number_of_ffts, const bool transpose_in,
180 const bool transpose_out, const int leading_dimension_in,
181 const int leading_dimension_out, const int number_of_threads,
182 const bool inplace, int *key) {
183 cp_assert((!inplace || (transpose_in == transpose_out)) &&
184 "Inplace plans need the same strides for input and output arrays!");
185 const int fft_size_3d[3] = {fft_size[0], fft_size[1], 0};
186 const int istride = transpose_in ? leading_dimension_in : 1;
187 const int idist = transpose_in ? 1 : leading_dimension_in;
188 const int ostride = transpose_out ? leading_dimension_out : 1;
189 const int odist = transpose_out ? 1 : leading_dimension_out;
190 key[0] =
191 2 + FFT_KEY_R2C + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
193 key[2] = number_of_threads;
194 set_nd_key(key, 2, fft_size_3d, number_of_ffts, direction, inplace, true,
195 istride, idist, ostride, odist);
196}
197
198/*******************************************************************************
199 * \brief Get key from FFT input parameters (3D, C2C-case)
200 * \author Frederick Stein
201 ******************************************************************************/
202static inline void get_key_3d(const bool direction, const int fft_size[3],
203 const int number_of_ffts, const bool transpose_in,
204 const bool transpose_out,
205 const int number_of_threads, const bool inplace,
206 int *key) {
207 cp_assert((!inplace || (transpose_in == transpose_out)) &&
208 "Inplace plans need the same strides for input and output arrays!");
209 const int volume = fft_size[0] * fft_size[1] * fft_size[2];
210 const int istride = transpose_in ? number_of_ffts : 1;
211 const int idist = transpose_in ? 1 : volume;
212 const int ostride = transpose_out ? number_of_ffts : 1;
213 const int odist = transpose_out ? 1 : volume;
214 key[0] = 3 + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
216 key[2] = number_of_threads;
217 set_nd_key(key, 3, fft_size, number_of_ffts, direction, inplace, false,
218 istride, idist, ostride, odist);
219}
220
221/*******************************************************************************
222 * \brief Get key from FFT input parameters (3D, R2C-case)
223 * \author Frederick Stein
224 ******************************************************************************/
225static inline void get_key_3d_r2c(const bool direction, const int fft_size[3],
226 const int number_of_ffts,
227 const bool transpose_in,
228 const bool transpose_out,
229 const int number_of_threads,
230 const bool inplace, int *key) {
231 cp_assert((!inplace || (transpose_in == transpose_out)) &&
232 "Inplace plans need the same strides for input and output arrays!");
233 const int real_volume = fft_size[0] * fft_size[1] * fft_size[2];
234 const int complex_volume = fft_size[0] * fft_size[1] * (fft_size[2] / 2 + 1);
235 const int input_volume =
236 direction ? (inplace ? 2 * complex_volume : real_volume) : complex_volume;
237 const int output_volume =
238 direction ? complex_volume : (inplace ? 2 * complex_volume : real_volume);
239 const int istride = transpose_in ? number_of_ffts : 1;
240 const int idist = transpose_in ? 1 : input_volume;
241 const int ostride = transpose_out ? number_of_ffts : 1;
242 const int odist = transpose_out ? 1 : output_volume;
243 key[0] =
244 3 + FFT_KEY_R2C + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
246 key[2] = number_of_threads;
247 set_nd_key(key, 3, fft_size, number_of_ffts, direction, inplace, true,
248 istride, idist, ostride, odist);
249}
250
251/*******************************************************************************
252 * \brief Get key from FFT input parameters (Guru, C2C-case)
253 * \author Frederick Stein
254 ******************************************************************************/
255static inline void get_key_guru(const bool direction, int rank,
256 const fftw_iodim *dims, int howmany_rank,
257 const fftw_iodim *howmany_dims,
258 const int number_of_threads, const bool inplace,
259 int *key) {
260 key[0] = rank + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
261 cp_assert(rank + howmany_rank <= 4 &&
262 "Larger combined ranks than 4 are not implemented\n");
263
265 key[2] = number_of_threads;
266 for (int d = 0; d < 4; d++) {
267 key[3 + d] = 0;
268 key[7 + d] = 0;
269 key[11 + d] = 0;
270 }
271 for (int d = 0; d < rank; d++) {
272 key[3 + d] = dims[d].n;
273 key[7 + d] = dims[d].is;
274 key[11 + d] = dims[d].os;
275 }
276 for (int d = 0; d < howmany_rank; d++) {
277 key[3 + rank + d] = howmany_dims[d].n;
278 key[7 + rank + d] = howmany_dims[d].is;
279 key[11 + rank + d] = howmany_dims[d].os;
280 }
281}
282
283/*******************************************************************************
284 * \brief Get key from FFT input parameters (Guru, R2C-case)
285 * \author Frederick Stein
286 ******************************************************************************/
287static inline void get_key_guru_r2c(const bool direction, int rank,
288 const fftw_iodim *dims, int howmany_rank,
289 const fftw_iodim *howmany_dims,
290 const int number_of_threads,
291 const bool inplace, int *key) {
292 key[0] = rank + FFT_KEY_R2C + FFT_KEY_INPLACE * inplace +
293 direction * FFT_KEY_FORWARD;
294 cp_assert(rank + howmany_rank <= 4 &&
295 "Larger combined ranks than 4 are not implemented\n");
296
298 key[2] = number_of_threads;
299 for (int d = 0; d < 4; d++) {
300 key[3 + d] = 0;
301 key[7 + d] = 0;
302 key[11 + d] = 0;
303 }
304 for (int d = 0; d < rank; d++) {
305 key[3 + d] = dims[d].n;
306 key[7 + d] = dims[d].is;
307 key[11 + d] = dims[d].os;
308 }
309 for (int d = 0; d < howmany_rank; d++) {
310 key[3 + rank + d] = howmany_dims[d].n;
311 key[7 + rank + d] = howmany_dims[d].is;
312 key[11 + rank + d] = howmany_dims[d].os;
313 }
314}
315
316/*******************************************************************************
317 * \brief Determine buffer size for a local FFT from a key
318 * \author Frederick Stein
319 ******************************************************************************/
320static inline int get_buffer_size_from_key(const fft_key_t key) {
321 const int rank = key[0] % 4;
322 const bool is_r2c = (key[0] & FFT_KEY_R2C) == FFT_KEY_R2C;
323 const bool direction = (key[0] & FFT_KEY_FORWARD) == FFT_KEY_FORWARD;
324 const bool inplace = (key[0] & FFT_KEY_INPLACE) == FFT_KEY_INPLACE;
325 int used = 4;
326 while (used > 0 && key[3 + used - 1] == 0)
327 used--;
328 if (used == 0)
329 return 0;
330 int max_span = 1;
331 for (int side = 0; side < 2; side++) {
332 const int *strides = (side == 0) ? &key[7] : &key[11];
333 int span = 1;
334 for (int d = 0; d < used; d++) {
335 int n = key[3 + d];
336 const bool complex_side =
337 (side == 0 && !direction) || (side == 1 && direction);
338 if (is_r2c && d == rank - 1) {
339 const int half = n / 2 + 1;
340 if (complex_side)
341 n = inplace ? 2 * half : half;
342 else
343 n = inplace ? 2 * half : n;
344 }
345 if (n > 1)
346 span += (n - 1) * strides[d];
347 }
348 if (span > max_span)
349 max_span = span;
350 }
351 return max_span;
352}
353
354/*******************************************************************************
355 * \brief Decode a key for a local (non-Guru) FFT into its plan parameters.
356 * \param key Encoded FFT key.
357 * \param rank Number of transform dimensions.
358 * \param fft_size Transform sizes (up to three dimensions).
359 * \param number_of_ffts Number of batched transforms.
360 * \param number_of_threads Number of FFTW threads used by the plan.
361 * \param direction FFT direction (forward or backward).
362 * \param inplace Whether the transform is in-place.
363 * \param inembed Input array dimensions.
364 * \param onembed Output array dimensions.
365 * \param idist Distance between consecutive input transforms.
366 * \param odist Distance between consecutive output transforms.
367 * \param istride Stride between consecutive input elements.
368 * \param ostride Stride between consecutive output elements.
369 ******************************************************************************/
370static inline void fetch_data_from_key_nd(
371 const fft_key_t key, int *rank, int *fft_size, int *number_of_ffts,
372 int *number_of_threads, bool *direction, bool *inplace, int *inembed,
373 int *onembed, int *idist, int *odist, int *istride, int *ostride) {
375 "Distributed FFTs are not supported in this function!");
376 *rank = key[0] % 4;
377 *inplace = (key[0] & FFT_KEY_INPLACE) == FFT_KEY_INPLACE;
378 *number_of_threads = key[2];
379 *direction = (key[0] & FFT_KEY_FORWARD) == FFT_KEY_FORWARD;
380 const int is_r2c = (key[0] & FFT_KEY_R2C) == FFT_KEY_R2C;
381 for (int r = 0; r < *rank; r++) {
382 fft_size[r] = key[3 + r];
383 if (r < *rank - 1) {
384 inembed[r] = key[3 + r];
385 onembed[r] = key[3 + r];
386 } else {
387 if (*direction) {
388 inembed[r] = is_r2c && *inplace ? 2 * (key[3 + r] / 2 + 1) : key[3 + r];
389 onembed[r] = is_r2c ? key[3 + r] / 2 + 1 : key[3 + r];
390 } else {
391 inembed[r] = is_r2c ? key[3 + r] / 2 + 1 : key[3 + r];
392 onembed[r] = is_r2c && *inplace ? 2 * (key[3 + r] / 2 + 1) : key[3 + r];
393 }
394 }
395 }
396 *number_of_ffts = key[3 + *rank];
397 *idist = key[7 + *rank];
398 *odist = key[11 + *rank];
399 *istride = key[7];
400 *ostride = key[11];
401}
402
403/*******************************************************************************
404 * \brief Decode a key for a local Guru FFT into its plan parameters.
405 * \param key Encoded FFT key.
406 * \param direction FFT direction (forward or backward).
407 * \param rank Number of transform dimensions.
408 * \param dims Transform dimensions with their input and output strides.
409 * \param howmany_rank Number of howmany dimensions.
410 * \param howmany_dims Howmany dimensions with their in/output strides.
411 * \param number_of_threads Number of FFTW threads used by the plan.
412 * \param inplace Whether the transform is in-place.
413 ******************************************************************************/
414static inline void fetch_data_from_key_guru(const fft_key_t key,
415 bool *direction, int *rank,
416 fftw_iodim *dims, int *howmany_rank,
417 fftw_iodim *howmany_dims,
418 int *number_of_threads,
419 bool *inplace) {
421 "Distributed FFTs are not supported in this function!");
422 *direction = (key[0] & FFT_KEY_FORWARD) == FFT_KEY_FORWARD;
423 *inplace = (key[0] & FFT_KEY_INPLACE) == FFT_KEY_INPLACE;
424 *rank = key[0] % 4;
425 int used = 4;
426 while (used > 0 && key[3 + used - 1] == 0)
427 used--;
428 *howmany_rank = used > *rank ? used - *rank : 0;
429 for (int r = 0; r < *rank; r++) {
430 dims[r].n = key[3 + r];
431 dims[r].is = key[7 + r];
432 dims[r].os = key[11 + r];
433 }
434 for (int r = 0; r < *howmany_rank; r++) {
435 howmany_dims[r].n = key[3 + *rank + r];
436 howmany_dims[r].is = key[7 + *rank + r];
437 howmany_dims[r].os = key[11 + *rank + r];
438 }
439 *number_of_threads = key[2];
440}
441#endif
442
443#endif /* FFT_KEY_H */
#define cp_assert(condition)
Definition cp_assert.h:22
int cp_mpi_comm_c2f(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_c2f.
Definition cp_mpi.c:178
cp_mpi_comm_t cp_mpi_get_comm_null(void)
Returns MPI_COMM_WORLD.
Definition cp_mpi.c:150
real(dp), dimension(3) d
real(kind=dp), parameter, public half