Line data Source code
1 : /*----------------------------------------------------------------------------*/
2 : /* CP2K: A general program to perform molecular dynamics simulations */
3 : /* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4 : /* */
5 : /* SPDX-License-Identifier: BSD-3-Clause */
6 : /*----------------------------------------------------------------------------*/
7 :
8 : #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
22 : typedef 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 : ******************************************************************************/
59 19243880 : static 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 19243880 : int inembed[3] = {0, 0, 0};
65 19243880 : int onembed[3] = {0, 0, 0};
66 43662628 : for (int r = 0; r < rank; r++) {
67 24418748 : inembed[r] = fft_size[r];
68 24418748 : onembed[r] = fft_size[r];
69 : }
70 19243880 : if (is_r2c && rank > 0) {
71 608 : const int r = rank - 1;
72 608 : if (direction) {
73 304 : onembed[r] = fft_size[r] / 2 + 1;
74 304 : if (inplace)
75 96 : inembed[r] = 2 * (fft_size[r] / 2 + 1);
76 : } else {
77 304 : inembed[r] = fft_size[r] / 2 + 1;
78 304 : if (inplace)
79 96 : onembed[r] = 2 * (fft_size[r] / 2 + 1);
80 : }
81 : }
82 96219400 : for (int d = 0; d < 4; d++) {
83 76975520 : key[3 + d] = 0;
84 76975520 : key[7 + d] = 0;
85 76975520 : key[11 + d] = 0;
86 : }
87 43662628 : for (int d = 0; d < rank; d++)
88 24418748 : key[3 + d] = fft_size[d];
89 19243880 : key[3 + rank] = number_of_ffts;
90 19243880 : key[7] = istride;
91 24418748 : for (int d = 1; d < rank; d++)
92 5174868 : key[7 + d] = key[7 + d - 1] * inembed[d - 1];
93 19243880 : key[7 + rank] = idist;
94 19243880 : key[11] = ostride;
95 24418748 : for (int d = 1; d < rank; d++)
96 5174868 : key[11 + d] = key[11 + d - 1] * onembed[d - 1];
97 19243880 : key[11 + rank] = odist;
98 19243880 : }
99 :
100 : /*******************************************************************************
101 : * \brief Get key from FFT input parameters (1D, C2C-case)
102 : * \author Frederick Stein
103 : ******************************************************************************/
104 : static inline void
105 14746863 : get_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 14746863 : cp_assert((!inplace || (transpose_in == transpose_out)) &&
110 : "Inplace plans need the same strides for input and output arrays!");
111 14746863 : const int fft_size_3d[3] = {fft_size, 0, 0};
112 14746863 : const int istride = transpose_in ? leading_dimension_in : 1;
113 1912206 : const int idist = transpose_in ? 1 : leading_dimension_in;
114 14746863 : const int ostride = transpose_out ? leading_dimension_out : 1;
115 1908432 : const int odist = transpose_out ? 1 : leading_dimension_out;
116 14746863 : key[0] = 1 + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
117 14746863 : key[1] = cp_mpi_comm_c2f(cp_mpi_get_comm_null());
118 14746863 : key[2] = number_of_threads;
119 14746863 : set_nd_key(key, 1, fft_size_3d, number_of_ffts, direction, inplace, false,
120 : istride, idist, ostride, odist);
121 14746863 : }
122 :
123 : /*******************************************************************************
124 : * \brief Get key from FFT input parameters (1D, R2C-case)
125 : * \author Frederick Stein
126 : ******************************************************************************/
127 : static inline void
128 224 : get_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 224 : cp_assert((!inplace || (transpose_in == transpose_out)) &&
134 : "Inplace plans need the same strides for input and output arrays!");
135 224 : const int fft_size_3d[3] = {fft_size, 0, 0};
136 224 : const int istride = transpose_in ? leading_dimension_in : 1;
137 128 : const int idist = transpose_in ? 1 : leading_dimension_in;
138 224 : const int ostride = transpose_out ? leading_dimension_out : 1;
139 128 : const int odist = transpose_out ? 1 : leading_dimension_out;
140 224 : key[0] =
141 224 : 1 + FFT_KEY_R2C + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
142 224 : key[1] = cp_mpi_comm_c2f(cp_mpi_get_comm_null());
143 224 : key[2] = number_of_threads;
144 224 : set_nd_key(key, 1, fft_size_3d, number_of_ffts, direction, inplace, true,
145 : istride, idist, ostride, odist);
146 224 : }
147 :
148 : /*******************************************************************************
149 : * \brief Get key from FFT input parameters (2D, C2C-case)
150 : * \author Frederick Stein
151 : ******************************************************************************/
152 3818494 : static 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 3818494 : cp_assert((!inplace || (transpose_in == transpose_out)) &&
160 : "Inplace plans need the same strides for input and output arrays!");
161 3818494 : const int fft_size_3d[3] = {fft_size[0], fft_size[1], 0};
162 3818494 : const int istride = transpose_in ? leading_dimension_in : 1;
163 1911150 : const int idist = transpose_in ? 1 : leading_dimension_in;
164 3818494 : const int ostride = transpose_out ? leading_dimension_out : 1;
165 1907376 : const int odist = transpose_out ? 1 : leading_dimension_out;
166 3818494 : key[0] = 2 + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
167 3818494 : key[1] = cp_mpi_comm_c2f(cp_mpi_get_comm_null());
168 3818494 : key[2] = number_of_threads;
169 3818494 : set_nd_key(key, 2, fft_size_3d, number_of_ffts, direction, inplace, false,
170 : istride, idist, ostride, odist);
171 3818494 : }
172 :
173 : /*******************************************************************************
174 : * \brief Get key from FFT input parameters (2D, R2C-case)
175 : * \author Frederick Stein
176 : ******************************************************************************/
177 : static inline void
178 224 : get_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 224 : cp_assert((!inplace || (transpose_in == transpose_out)) &&
184 : "Inplace plans need the same strides for input and output arrays!");
185 224 : const int fft_size_3d[3] = {fft_size[0], fft_size[1], 0};
186 224 : const int istride = transpose_in ? leading_dimension_in : 1;
187 128 : const int idist = transpose_in ? 1 : leading_dimension_in;
188 224 : const int ostride = transpose_out ? leading_dimension_out : 1;
189 128 : const int odist = transpose_out ? 1 : leading_dimension_out;
190 224 : key[0] =
191 224 : 2 + FFT_KEY_R2C + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
192 224 : key[1] = cp_mpi_comm_c2f(cp_mpi_get_comm_null());
193 224 : key[2] = number_of_threads;
194 224 : set_nd_key(key, 2, fft_size_3d, number_of_ffts, direction, inplace, true,
195 : istride, idist, ostride, odist);
196 224 : }
197 :
198 : /*******************************************************************************
199 : * \brief Get key from FFT input parameters (3D, C2C-case)
200 : * \author Frederick Stein
201 : ******************************************************************************/
202 677915 : static 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 677915 : cp_assert((!inplace || (transpose_in == transpose_out)) &&
208 : "Inplace plans need the same strides for input and output arrays!");
209 677915 : const int volume = fft_size[0] * fft_size[1] * fft_size[2];
210 677915 : const int istride = transpose_in ? number_of_ffts : 1;
211 675196 : const int idist = transpose_in ? 1 : volume;
212 677915 : const int ostride = transpose_out ? number_of_ffts : 1;
213 675196 : const int odist = transpose_out ? 1 : volume;
214 677915 : key[0] = 3 + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
215 677915 : key[1] = cp_mpi_comm_c2f(cp_mpi_get_comm_null());
216 677915 : key[2] = number_of_threads;
217 677915 : set_nd_key(key, 3, fft_size, number_of_ffts, direction, inplace, false,
218 : istride, idist, ostride, odist);
219 677915 : }
220 :
221 : /*******************************************************************************
222 : * \brief Get key from FFT input parameters (3D, R2C-case)
223 : * \author Frederick Stein
224 : ******************************************************************************/
225 160 : static 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 160 : cp_assert((!inplace || (transpose_in == transpose_out)) &&
232 : "Inplace plans need the same strides for input and output arrays!");
233 160 : const int real_volume = fft_size[0] * fft_size[1] * fft_size[2];
234 160 : const int complex_volume = fft_size[0] * fft_size[1] * (fft_size[2] / 2 + 1);
235 32 : const int input_volume =
236 160 : direction ? (inplace ? 2 * complex_volume : real_volume) : complex_volume;
237 32 : const int output_volume =
238 80 : direction ? complex_volume : (inplace ? 2 * complex_volume : real_volume);
239 160 : const int istride = transpose_in ? number_of_ffts : 1;
240 96 : const int idist = transpose_in ? 1 : input_volume;
241 160 : const int ostride = transpose_out ? number_of_ffts : 1;
242 96 : const int odist = transpose_out ? 1 : output_volume;
243 160 : key[0] =
244 160 : 3 + FFT_KEY_R2C + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
245 160 : key[1] = cp_mpi_comm_c2f(cp_mpi_get_comm_null());
246 160 : key[2] = number_of_threads;
247 160 : set_nd_key(key, 3, fft_size, number_of_ffts, direction, inplace, true,
248 : istride, idist, ostride, odist);
249 160 : }
250 :
251 : /*******************************************************************************
252 : * \brief Get key from FFT input parameters (Guru, C2C-case)
253 : * \author Frederick Stein
254 : ******************************************************************************/
255 0 : static 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 0 : key[0] = rank + FFT_KEY_INPLACE * inplace + direction * FFT_KEY_FORWARD;
261 0 : cp_assert(rank + howmany_rank <= 4 &&
262 : "Larger combined ranks than 4 are not implemented\n");
263 :
264 0 : key[1] = cp_mpi_comm_c2f(cp_mpi_get_comm_null());
265 0 : key[2] = number_of_threads;
266 0 : for (int d = 0; d < 4; d++) {
267 0 : key[3 + d] = 0;
268 0 : key[7 + d] = 0;
269 0 : key[11 + d] = 0;
270 : }
271 0 : for (int d = 0; d < rank; d++) {
272 0 : key[3 + d] = dims[d].n;
273 0 : key[7 + d] = dims[d].is;
274 0 : key[11 + d] = dims[d].os;
275 : }
276 0 : for (int d = 0; d < howmany_rank; d++) {
277 0 : key[3 + rank + d] = howmany_dims[d].n;
278 0 : key[7 + rank + d] = howmany_dims[d].is;
279 0 : key[11 + rank + d] = howmany_dims[d].os;
280 : }
281 0 : }
282 :
283 : /*******************************************************************************
284 : * \brief Get key from FFT input parameters (Guru, R2C-case)
285 : * \author Frederick Stein
286 : ******************************************************************************/
287 0 : static 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 0 : key[0] = rank + FFT_KEY_R2C + FFT_KEY_INPLACE * inplace +
293 0 : direction * FFT_KEY_FORWARD;
294 0 : cp_assert(rank + howmany_rank <= 4 &&
295 : "Larger combined ranks than 4 are not implemented\n");
296 :
297 0 : key[1] = cp_mpi_comm_c2f(cp_mpi_get_comm_null());
298 0 : key[2] = number_of_threads;
299 0 : for (int d = 0; d < 4; d++) {
300 0 : key[3 + d] = 0;
301 0 : key[7 + d] = 0;
302 0 : key[11 + d] = 0;
303 : }
304 0 : for (int d = 0; d < rank; d++) {
305 0 : key[3 + d] = dims[d].n;
306 0 : key[7 + d] = dims[d].is;
307 0 : key[11 + d] = dims[d].os;
308 : }
309 0 : for (int d = 0; d < howmany_rank; d++) {
310 0 : key[3 + rank + d] = howmany_dims[d].n;
311 0 : key[7 + rank + d] = howmany_dims[d].is;
312 0 : key[11 + rank + d] = howmany_dims[d].os;
313 : }
314 0 : }
315 :
316 : /*******************************************************************************
317 : * \brief Determine buffer size for a local FFT from a key
318 : * \author Frederick Stein
319 : ******************************************************************************/
320 249 : static inline int get_buffer_size_from_key(const fft_key_t key) {
321 249 : const int rank = key[0] % 4;
322 249 : const bool is_r2c = (key[0] & FFT_KEY_R2C) == FFT_KEY_R2C;
323 249 : const bool direction = (key[0] & FFT_KEY_FORWARD) == FFT_KEY_FORWARD;
324 249 : const bool inplace = (key[0] & FFT_KEY_INPLACE) == FFT_KEY_INPLACE;
325 249 : int used = 4;
326 407 : while (used > 0 && key[3 + used - 1] == 0)
327 158 : used--;
328 249 : if (used == 0)
329 : return 0;
330 : int max_span = 1;
331 747 : for (int side = 0; side < 2; side++) {
332 498 : const int *strides = (side == 0) ? &key[7] : &key[11];
333 498 : int span = 1;
334 2174 : for (int d = 0; d < used; d++) {
335 1676 : int n = key[3 + d];
336 3352 : const bool complex_side =
337 1676 : (side == 0 && !direction) || (side == 1 && direction);
338 1676 : if (is_r2c && d == rank - 1) {
339 0 : const int half = n / 2 + 1;
340 0 : if (complex_side)
341 0 : n = inplace ? 2 * half : half;
342 : else
343 0 : n = inplace ? 2 * half : n;
344 : }
345 1676 : if (n > 1)
346 1470 : span += (n - 1) * strides[d];
347 : }
348 498 : 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 : ******************************************************************************/
370 127955 : static 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) {
374 127955 : cp_assert(key[1] == cp_mpi_comm_c2f(cp_mpi_get_comm_null()) &&
375 : "Distributed FFTs are not supported in this function!");
376 127955 : *rank = key[0] % 4;
377 127955 : *inplace = (key[0] & FFT_KEY_INPLACE) == FFT_KEY_INPLACE;
378 127955 : *number_of_threads = key[2];
379 127955 : *direction = (key[0] & FFT_KEY_FORWARD) == FFT_KEY_FORWARD;
380 127955 : const int is_r2c = (key[0] & FFT_KEY_R2C) == FFT_KEY_R2C;
381 325574 : for (int r = 0; r < *rank; r++) {
382 197619 : fft_size[r] = key[3 + r];
383 197619 : if (r < *rank - 1) {
384 69664 : inembed[r] = key[3 + r];
385 69664 : onembed[r] = key[3 + r];
386 : } else {
387 127955 : if (*direction) {
388 63035 : inembed[r] = is_r2c && *inplace ? 2 * (key[3 + r] / 2 + 1) : key[3 + r];
389 63035 : onembed[r] = is_r2c ? key[3 + r] / 2 + 1 : key[3 + r];
390 : } else {
391 64920 : inembed[r] = is_r2c ? key[3 + r] / 2 + 1 : key[3 + r];
392 64920 : onembed[r] = is_r2c && *inplace ? 2 * (key[3 + r] / 2 + 1) : key[3 + r];
393 : }
394 : }
395 : }
396 127955 : *number_of_ffts = key[3 + *rank];
397 127955 : *idist = key[7 + *rank];
398 127955 : *odist = key[11 + *rank];
399 127955 : *istride = key[7];
400 127955 : *ostride = key[11];
401 127955 : }
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 : ******************************************************************************/
414 0 : static 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) {
420 0 : cp_assert(key[1] == cp_mpi_comm_c2f(cp_mpi_get_comm_null()) &&
421 : "Distributed FFTs are not supported in this function!");
422 0 : *direction = (key[0] & FFT_KEY_FORWARD) == FFT_KEY_FORWARD;
423 0 : *inplace = (key[0] & FFT_KEY_INPLACE) == FFT_KEY_INPLACE;
424 0 : *rank = key[0] % 4;
425 0 : int used = 4;
426 0 : while (used > 0 && key[3 + used - 1] == 0)
427 0 : used--;
428 0 : *howmany_rank = used > *rank ? used - *rank : 0;
429 0 : for (int r = 0; r < *rank; r++) {
430 0 : dims[r].n = key[3 + r];
431 0 : dims[r].is = key[7 + r];
432 0 : dims[r].os = key[11 + r];
433 : }
434 0 : for (int r = 0; r < *howmany_rank; r++) {
435 0 : howmany_dims[r].n = key[3 + *rank + r];
436 0 : howmany_dims[r].is = key[7 + *rank + r];
437 0 : howmany_dims[r].os = key[11 + *rank + r];
438 : }
439 0 : *number_of_threads = key[2];
440 0 : }
441 : #endif
442 :
443 : #endif /* FFT_KEY_H */
|