ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
fft.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21/** \file
22 *
23 * Routines, row decomposition, data structures and communication for the
24 * 3D-FFT.
25 *
26 */
27
28#include "fft.hpp"
29#include "vector.hpp"
30
31#include "p3m/packing.hpp"
32
33#include <utils/Vector.hpp>
34#include <utils/index.hpp>
37
38#include <boost/mpi/communicator.hpp>
39#include <boost/serialization/vector.hpp>
40
41#include <fftw3.h>
42
43#include <omp.h>
44
45#include <algorithm>
46#include <cassert>
47#include <cmath>
48#include <complex>
49#include <cstddef>
50#include <limits>
51#include <optional>
52#include <span>
53#include <stdexcept>
54#include <utility>
55#include <vector>
56
58
59/** @name MPI tags for FFT communication */
60/**@{*/
61/** Tag for communication in forw_grid_comm() */
62#define REQ_FFT_FORW 301
63/** Tag for communication in back_grid_comm() */
64#define REQ_FFT_BACK 302
65/**@}*/
66
67template <typename T>
68static void fft_sendrecv(T const *const sendbuf, int scount, int dest,
69 T *const recvbuf, int rcount, int source,
70 boost::mpi::communicator const &comm, int tag) {
71 auto const type = boost::mpi::get_mpi_datatype<T>(*sendbuf);
72 MPI_Sendrecv(reinterpret_cast<void const *>(sendbuf), scount, type, dest, tag,
73 reinterpret_cast<void *>(recvbuf), rcount, type, source, tag,
74 comm, MPI_STATUS_IGNORE);
75}
76
77namespace fft {
78
79template <typename FloatType = double> struct fftw {
80 using complex = fftw_complex;
81 static auto constexpr plan_many_dft = fftw_plan_many_dft;
82 static auto constexpr destroy_plan = fftw_destroy_plan;
83 static auto constexpr execute_dft = fftw_execute_dft;
84 static auto constexpr malloc = fftw_malloc;
85 static auto constexpr free = fftw_free;
86};
87template <> struct fftw<float> {
88 using complex = fftwf_complex;
89 static auto constexpr plan_many_dft = fftwf_plan_many_dft;
90 static auto constexpr destroy_plan = fftwf_destroy_plan;
91 static auto constexpr execute_dft = fftwf_execute_dft;
92 static auto constexpr malloc = fftwf_malloc;
93 static auto constexpr free = fftwf_free;
94};
95
96/** This ugly function does the bookkeeping: which nodes have to
97 * communicate to each other, when you change the node grid.
98 * Changing the regular decomposition requires communication. This
99 * function finds (hopefully) the best way to do this. As input it
100 * needs the two grids (@p grid1, @p grid2) and a linear list (@p node_list1)
101 * with the node identities for @p grid1. The linear list (@p node_list2)
102 * for the second grid is calculated. For the communication group of
103 * the calling node it calculates a list (@c group) with the node
104 * identities and the positions (@p my_pos, @p pos) of that nodes in @p grid1
105 * and @p grid2. The return value is the size of the communication
106 * group. It gives -1 if the two grids do not fit to each other
107 * (@p grid1 and @p grid2 have to be component-wise multiples of each
108 * other, see e.g. \ref calc_2d_grid for how to do this).
109 *
110 * \param[in] grid1 The node grid you start with.
111 * \param[in] grid2 The node grid you want to have.
112 * \param[in] node_list1 Linear node index list for grid1.
113 * \param[out] node_list2 Linear node index list for grid2.
114 * \param[out] pos Positions of the nodes in grid2
115 * \param[out] my_pos Position of comm.rank() in grid2.
116 * \param[in] rank MPI rank.
117 * \return Size of the communication group.
118 */
119std::optional<std::vector<int>>
121 std::span<int const> node_list1, std::span<int> node_list2,
122 std::span<int> pos, std::span<int> my_pos, int rank) {
123 int i;
124 /* communication group cell size on grid1 and grid2 */
125 int s1[3], s2[3];
126 /* The communication group cells build the same super grid on grid1 and grid2
127 */
128 int ds[3];
129 /* communication group size */
130 int g_size = 1;
131 /* comm. group cell index */
132 int gi[3];
133 /* position of a node in a grid */
134 Utils::Vector3i p1, p2;
135 /* node identity */
136 int n;
137 /* comm.rank() position in the communication group. */
138 int c_pos = -1;
139 /* flag for group identification */
140 int my_group = 0;
141
142 /* calculate dimension of comm. group cells for both grids */
143 if (Utils::product(grid1) != Utils::product(grid2))
144 return std::nullopt; /* unlike number of nodes */
145 for (i = 0; i < 3; i++) {
146 s1[i] = grid1[i] / grid2[i];
147 if (s1[i] == 0)
148 s1[i] = 1;
149 else if (grid1[i] != grid2[i] * s1[i])
150 return std::nullopt; /* grids do not match!!! */
151
152 s2[i] = grid2[i] / grid1[i];
153 if (s2[i] == 0)
154 s2[i] = 1;
155 else if (grid2[i] != grid1[i] * s2[i])
156 return std::nullopt; /* grids do not match!!! */
157
158 ds[i] = grid2[i] / s2[i];
159 g_size *= s2[i];
160 }
161
162 std::vector<int> group(g_size);
163
164 /* calc node_list2 */
165 /* loop through all comm. group cells */
166 for (gi[2] = 0; gi[2] < ds[2]; gi[2]++)
167 for (gi[1] = 0; gi[1] < ds[1]; gi[1]++)
168 for (gi[0] = 0; gi[0] < ds[0]; gi[0]++) {
169 /* loop through all nodes in that comm. group cell */
170 for (i = 0; i < g_size; i++) {
171 p1[0] = (gi[0] * s1[0]) + (i % s1[0]);
172 p1[1] = (gi[1] * s1[1]) + ((i / s1[0]) % s1[1]);
173 p1[2] = (gi[2] * s1[2]) + (i / (s1[0] * s1[1]));
174
175 p2[0] = (gi[0] * s2[0]) + (i % s2[0]);
176 p2[1] = (gi[1] * s2[1]) + ((i / s2[0]) % s2[1]);
177 p2[2] = (gi[2] * s2[2]) + (i / (s2[0] * s2[1]));
178
179 n = node_list1[Utils::get_linear_index(p1, grid1)];
180 node_list2[Utils::get_linear_index(p2, grid2)] = n;
181
182 pos[3 * n + 0] = p2[0];
183 pos[3 * n + 1] = p2[1];
184 pos[3 * n + 2] = p2[2];
185 if (my_group == 1)
186 group[i] = n;
187 if (n == rank && my_group == 0) {
188 my_group = 1;
189 c_pos = i;
190 my_pos[0] = p2[0];
191 my_pos[1] = p2[1];
192 my_pos[2] = p2[2];
193 i = -1; /* restart the loop */
194 }
195 }
196 my_group = 0;
197 }
198
199 /* permute comm. group according to the nodes position in the group */
200 /* This is necessary to have matching node pairs during communication! */
201 while (c_pos > 0) {
202 n = group[g_size - 1];
203 for (i = g_size - 1; i > 0; i--)
204 group[i] = group[i - 1];
205 group[0] = n;
206 c_pos--;
207 }
208 return {group};
209}
210
211namespace {
212/** Calculate the local fft mesh. Calculate the local mesh (@p loc_mesh)
213 * of a node at position (@p n_pos) in a node grid (@p n_grid) for a global
214 * mesh of size (@p mesh) and a mesh offset (@p mesh_off (in mesh units))
215 * and store also the first point (@p start) of the local mesh.
216 *
217 * \param[in] n_pos Position of the node in @p n_grid.
218 * \param[in] n_grid node grid.
219 * \param[in] mesh global mesh dimensions.
220 * \param[in] mesh_off global mesh offset.
221 * \param[out] loc_mesh local mesh dimension.
222 * \param[out] start first point of local mesh in global mesh.
223 * \return Number of mesh points in local mesh.
224 */
225int calc_local_mesh(const int *n_pos, const int *n_grid, const int *mesh,
226 const double *mesh_off, int *loc_mesh, int *start) {
227 int last[3], size = 1;
228
229 for (int i = 0; i < 3; i++) {
230 auto const ai = mesh[i] / static_cast<double>(n_grid[i]);
231 start[i] = static_cast<int>(ceil(ai * n_pos[i] - mesh_off[i]));
232 last[i] = static_cast<int>(floor(ai * (n_pos[i] + 1) - mesh_off[i]));
233 /* correct round off errors */
234 if (ai * (n_pos[i] + 1) - mesh_off[i] - last[i] < 1.0e-15)
235 last[i]--;
236 if (1.0 + ai * n_pos[i] - mesh_off[i] - start[i] < 1.0e-15)
237 start[i]--;
238 loc_mesh[i] = last[i] - start[i] + 1;
239 size *= loc_mesh[i];
240 }
241 return size;
242}
243
244/** Calculate a send (or recv.) block for grid communication during a
245 * decomposition change. Calculate the send block specification
246 * (block = lower left corner and upper right corner) which a node at
247 * position (@p pos1) in the actual node grid (@p grid1) has to send to
248 * another node at position (@p pos2) in the desired node grid (@p grid2).
249 * The global mesh, subject to communication, is specified via its size
250 * (@p mesh) and its mesh offset (@p mesh_off (in mesh units)).
251 *
252 * For the calculation of a receive block you have to change the arguments in
253 * the following way:
254 * - @p pos1: position of receiving node in the desired node grid.
255 * - @p grid1: desired node grid.
256 * - @p pos2: position of the node you intend to receive the data from in the
257 * actual node grid.
258 * - @p grid2: actual node grid.
259 *
260 * \param[in] pos1 Position of send node in @p grid1.
261 * \param[in] grid1 node grid 1.
262 * \param[in] pos2 Position of recv node in @p grid2.
263 * \param[in] grid2 node grid 2.
264 * \param[in] mesh global mesh dimensions.
265 * \param[in] mesh_off global mesh offset.
266 * \param[out] block send block specification.
267 * \return Size of the send block.
268 */
269int calc_send_block(const int *pos1, const int *grid1, const int *pos2,
270 const int *grid2, const int *mesh, const double *mesh_off,
271 int *block) {
272 int size = 1;
273 int mesh1[3], first1[3], last1[3];
274 int mesh2[3], first2[3], last2[3];
275
276 calc_local_mesh(pos1, grid1, mesh, mesh_off, mesh1, first1);
277 calc_local_mesh(pos2, grid2, mesh, mesh_off, mesh2, first2);
278
279 for (int i = 0; i < 3; i++) {
280 last1[i] = first1[i] + mesh1[i] - 1;
281 last2[i] = first2[i] + mesh2[i] - 1;
282 block[i] = std::max(first1[i], first2[i]) - first1[i];
283 block[i + 3] = (std::min(last1[i], last2[i]) - first1[i]) - block[i] + 1;
284 size *= block[i + 3];
285 }
286 return size;
287}
288
289/** Pack a block with dimensions <tt>size[0] * size[1] * size[2]</tt> starting
290 * at @p start of an input 3D-grid with dimension @p dim into an output
291 * 3D-grid with dimensions <tt>size[2] * size[0] * size[1]</tt> with
292 * a simultaneous one-fold permutation of the indices. The permutation is
293 * defined as: slow_in -> fast_out, mid_in ->slow_out, fast_in -> mid_out.
294 *
295 * An element <tt>(i0_in, i1_in, i2_in)</tt> is then
296 * <tt>(i0_out = i1_in-start[1], i1_out = i2_in-start[2],
297 * i2_out = i0_in-start[0])</tt> and for the linear indices we have:
298 * - <tt>li_in = i2_in + size[2] * (i1_in + (size[1]*i0_in))</tt>
299 * - <tt>li_out = i2_out + size[0] * (i1_out + (size[2]*i0_out))</tt>
300 *
301 * For index definition see \ref fft_pack_block.
302 *
303 * \param[in] in input 3D-grid.
304 * \param[out] out output 3D-grid (block).
305 * \param[in] start start index of the block in the in-grid.
306 * \param[in] size size of the block (=dimension of the out-grid).
307 * \param[in] dim size of the in-grid.
308 * \param[in] element size of a grid element (e.g. 1 for Real, 2 for Complex).
309 */
310template <typename FloatType>
311void pack_block_permute1(FloatType const *const in, FloatType *const out,
312 const int *start, const int *size, const int *dim,
313 int element) {
314
315 /* offsets for indices in input grid */
316 auto const m_in_offset = element * (dim[2] - size[2]);
317 auto const s_in_offset = element * (dim[2] * (dim[1] - size[1]));
318 /* offset for mid changing indices of output grid */
319 auto const m_out_offset = (element * size[0]) - element;
320 /* linear index of in grid */
321 int li_in = element * (start[2] + dim[2] * (start[1] + dim[1] * start[0]));
322
323 for (int s = 0; s < size[0]; s++) { /* fast changing out */
324 /* linear index of out grid */
325 int li_out = element * s;
326 for (int m = 0; m < size[1]; m++) { /* slow changing out */
327 for (int f = 0; f < size[2]; f++) { /* mid changing out */
328 for (int e = 0; e < element; e++)
329 out[li_out++] = in[li_in++];
330 li_out += m_out_offset;
331 }
332 li_in += m_in_offset;
333 }
334 li_in += s_in_offset;
335 }
336}
337
338/** Pack a block with dimensions <tt>size[0] * size[1] * size[2]</tt> starting
339 * at @p start of an input 3D-grid with dimension @p dim into an output
340 * 3D-grid with dimensions <tt>size[2] * size[0] * size[1]</tt> with
341 * a simultaneous two-fold permutation of the indices. The permutation is
342 * defined as: slow_in -> mid_out, mid_in ->fast_out, fast_in -> slow_out.
343 *
344 * An element <tt>(i0_in, i1_in, i2_in)</tt> is then
345 * <tt>(i0_out = i2_in-start[2], i1_out = i0_in-start[0],
346 * i2_out = i1_in-start[1])</tt> and for the linear indices we have:
347 * - <tt>li_in = i2_in + size[2] * (i1_in + (size[1]*i0_in))</tt>
348 * - <tt>li_out = i2_out + size[0] * (i1_out + (size[2]*i0_out))</tt>
349 *
350 * For index definition see \ref fft_pack_block.
351 *
352 * \param[in] in input 3D-grid.
353 * \param[out] out output 3D-grid (block).
354 * \param[in] start start index of the block in the in-grid.
355 * \param[in] size size of the block (=dimension of the out-grid).
356 * \param[in] dim size of the in-grid.
357 * \param[in] element size of a grid element (e.g. 1 for Real, 2 for Complex).
358 */
359template <typename FloatType>
360void pack_block_permute2(FloatType const *const in, FloatType *const out,
361 const int *start, const int *size, const int *dim,
362 int element) {
363
364 /* offsets for indices in input grid */
365 auto const m_in_offset = element * (dim[2] - size[2]);
366 auto const s_in_offset = element * (dim[2] * (dim[1] - size[1]));
367 /* offset for slow changing index of output grid */
368 auto const s_out_offset = (element * size[0] * size[1]) - element;
369 /* linear index of in grid */
370 int li_in = element * (start[2] + dim[2] * (start[1] + dim[1] * start[0]));
371
372 for (int s = 0; s < size[0]; s++) { /* mid changing out */
373 auto const m_out_start = element * (s * size[1]);
374 for (int m = 0; m < size[1]; m++) { /* fast changing out */
375 /* linear index of out grid */
376 int li_out = m_out_start + element * m;
377 for (int f = 0; f < size[2]; f++) { /* slow changing out */
378 for (int e = 0; e < element; e++)
379 out[li_out++] = in[li_in++];
380 li_out += s_out_offset;
381 }
382 li_in += m_in_offset;
383 }
384 li_in += s_in_offset;
385 }
386}
387
388} // namespace
389
390/** Communicate the grid data according to the given forward FFT plan.
391 * \param comm MPI communicator.
392 * \param plan FFT communication plan.
393 * \param in input mesh.
394 * \param out output mesh.
395 */
396template <typename FloatType>
397void fft_data_struct<FloatType>::forw_grid_comm(
398 boost::mpi::communicator const &comm, fft_forw_plan<FloatType> const &plan,
399 FloatType const *in, FloatType *out) {
400 for (std::size_t i = 0ul; i < plan.group.size(); i++) {
401 plan.pack_function(in, send_buf.data(), &(plan.send_block[6ul * i]),
402 &(plan.send_block[6ul * i + 3ul]), plan.old_mesh.data(),
403 plan.element);
404
405 if (plan.group[i] != comm.rank()) {
406 fft_sendrecv(send_buf.data(), plan.send_size[i], plan.group[i],
407 recv_buf.data(), plan.recv_size[i], plan.group[i], comm,
409 } else { /* Self communication... */
410 std::swap(send_buf, recv_buf);
411 }
412 fft_unpack_block(recv_buf.data(), out, &(plan.recv_block[6ul * i]),
413 &(plan.recv_block[6ul * i + 3ul]), plan.new_mesh.data(),
414 plan.element);
415 }
416}
417
418/** Communicate the grid data according to the given backward FFT plan.
419 * \param comm MPI communicator.
420 * \param plan_f Forward FFT plan.
421 * \param plan_b Backward FFT plan.
422 * \param in input mesh.
423 * \param out output mesh.
424 */
425template <typename FloatType>
426void fft_data_struct<FloatType>::back_grid_comm(
427 boost::mpi::communicator const &comm,
428 fft_forw_plan<FloatType> const &plan_f,
429 fft_back_plan<FloatType> const &plan_b, FloatType const *in,
430 FloatType *out) {
431 /* Back means: Use the send/receive stuff from the forward plan but
432 replace the receive blocks by the send blocks and vice
433 versa. Attention then also new_mesh and old_mesh are exchanged */
434
435 for (std::size_t i = 0ul; i < plan_f.group.size(); i++) {
436 plan_b.pack_function(in, send_buf.data(), &(plan_f.recv_block[6ul * i]),
437 &(plan_f.recv_block[6ul * i + 3ul]),
438 plan_f.new_mesh.data(), plan_f.element);
439
440 if (plan_f.group[i] != comm.rank()) { /* send first, receive second */
441 fft_sendrecv(send_buf.data(), plan_f.recv_size[i], plan_f.group[i],
442 recv_buf.data(), plan_f.send_size[i], plan_f.group[i], comm,
444 } else { /* Self communication... */
445 std::swap(send_buf, recv_buf);
446 }
447 fft_unpack_block(recv_buf.data(), out, &(plan_f.send_block[6ul * i]),
448 &(plan_f.send_block[6ul * i + 3ul]),
449 plan_f.old_mesh.data(), plan_f.element);
450 }
451}
452
453/** Calculate 'best' mapping between a 2D and 3D grid.
454 * Required for the communication from 3D regular domain
455 * decomposition to 2D regular row decomposition.
456 * The dimensions of the 2D grid are resorted, if necessary, in a way
457 * that they are multiples of the 3D grid dimensions.
458 * \param g3d 3D grid.
459 * \param g2d 2D grid.
460 * \return index of the row direction [0,1,2].
461 */
462int map_3don2d_grid(int const g3d[3], int g2d[3]) {
463 int row_dir = -1;
464 /* trivial case */
465 if (g3d[2] == 1) {
466 return 2;
467 }
468 if (g2d[0] % g3d[0] == 0) {
469 if (g2d[1] % g3d[1] == 0) {
470 row_dir = 2;
471 } else if (g2d[1] % g3d[2] == 0) {
472 row_dir = 1;
473 g2d[2] = g2d[1];
474 g2d[1] = 1;
475 }
476 } else if (g2d[0] % g3d[1] == 0) {
477 if (g2d[1] % g3d[0] == 0) {
478 row_dir = 2;
479 int const tmp = g2d[0];
480 g2d[0] = g2d[1];
481 g2d[1] = tmp;
482 } else if (g2d[1] % g3d[2] == 0) {
483 row_dir = 0;
484 g2d[2] = g2d[1];
485 g2d[1] = g2d[0];
486 g2d[0] = 1;
487 }
488 } else if (g2d[0] % g3d[2] == 0) {
489 if (g2d[1] % g3d[0] == 0) {
490 row_dir = 1;
491 g2d[2] = g2d[0];
492 g2d[0] = g2d[1];
493 g2d[1] = 1;
494 } else if (g2d[1] % g3d[1] == 0) {
495 row_dir = 0;
496 g2d[2] = g2d[0];
497 g2d[0] = 1;
498 }
499 }
500 return row_dir;
501}
502
503/** Calculate most square 2D grid. */
504inline void calc_2d_grid(int n, int grid[3]) {
505 grid[0] = n;
506 grid[1] = 1;
507 grid[2] = 1;
508 for (auto i = static_cast<int>(std::sqrt(n)); i >= 1; i--) {
509 if (n % i == 0) {
510 grid[0] = n / i;
511 grid[1] = i;
512 grid[2] = 1;
513 return;
514 }
515 }
516}
517
518template <typename FloatType>
520 boost::mpi::communicator const &comm, Utils::Vector3i const &ca_mesh_dim,
521 int const *ca_mesh_margin, Utils::Vector3i const &global_mesh_dim,
522 Utils::Vector3d const &global_mesh_off, int &ks_pnum,
523 Utils::Vector3i const &grid) {
524
525 int n_grid[4][3]; /* The four node grids. */
526 int my_pos[4][3]; /* The position of comm.rank() in the node grids. */
527 std::vector<int> n_id[4]; /* linear node identity lists for the node grids. */
528 std::vector<int> n_pos[4]; /* positions of nodes in the node grids. */
529
530 auto const rank = comm.rank();
531 auto const node_pos = Utils::Mpi::cart_coords<3>(comm, rank);
532
533 max_comm_size = 0;
534 max_mesh_size = 0;
535 for (int i = 0; i < 4; i++) {
536 n_id[i].resize(1 * comm.size());
537 n_pos[i].resize(3 * comm.size());
538 }
539
540 /* === node grids === */
541 /* real space node grid (n_grid[0]) */
542 for (int i = 0; i < 3; i++) {
543 n_grid[0][i] = grid[i];
544 my_pos[0][i] = node_pos[i];
545 }
546 for (int i = 0; i < comm.size(); i++) {
547 auto const n_pos_i = Utils::Mpi::cart_coords<3>(comm, i);
548 for (int j = 0; j < 3; ++j) {
549 n_pos[0][3 * i + j] = n_pos_i[j];
550 }
551 auto const lin_ind = Utils::get_linear_index(
552 n_pos_i, {n_grid[0][0], n_grid[0][1], n_grid[0][2]});
553 n_id[0][lin_ind] = i;
554 }
555
556 /* FFT node grids (n_grid[1 - 3]) */
557 calc_2d_grid(comm.size(), n_grid[1]);
558 /* resort n_grid[1] dimensions if necessary */
559 forw[1].row_dir = map_3don2d_grid(n_grid[0], n_grid[1]);
560 forw[0].n_permute = 0;
561 for (int i = 1; i < 4; i++)
562 forw[i].n_permute = (forw[1].row_dir + i) % 3;
563 for (int i = 0; i < 3; i++) {
564 n_grid[2][i] = n_grid[1][(i + 1) % 3];
565 n_grid[3][i] = n_grid[1][(i + 2) % 3];
566 }
567 forw[2].row_dir = (forw[1].row_dir - 1) % 3;
568 forw[3].row_dir = (forw[1].row_dir - 2) % 3;
569
570 /* === communication groups === */
571 /* copy local mesh off real space charge assignment grid */
572 for (int i = 0; i < 3; i++)
573 forw[0].new_mesh[i] = ca_mesh_dim[i];
574
575 for (int i = 1; i < 4; i++) {
576 auto group = find_comm_groups(
577 {n_grid[i - 1][0], n_grid[i - 1][1], n_grid[i - 1][2]},
578 {n_grid[i][0], n_grid[i][1], n_grid[i][2]}, n_id[i - 1],
579 std::span(n_id[i]), std::span(n_pos[i]), my_pos[i], rank);
580 if (not group) {
581 /* try permutation */
582 std::swap(n_grid[i][(forw[i].row_dir + 1) % 3],
583 n_grid[i][(forw[i].row_dir + 2) % 3]);
584
585 group = find_comm_groups(
586 {n_grid[i - 1][0], n_grid[i - 1][1], n_grid[i - 1][2]},
587 {n_grid[i][0], n_grid[i][1], n_grid[i][2]}, std::span(n_id[i - 1]),
588 std::span(n_id[i]), std::span(n_pos[i]), my_pos[i], rank);
589
590 if (not group) {
591 throw std::runtime_error("INTERNAL ERROR: fft_find_comm_groups error");
592 }
593 }
594
595 forw[i].group = group.value();
596
597 forw[i].send_block.resize(6 * forw[i].group.size());
598 forw[i].send_size.resize(forw[i].group.size());
599 forw[i].recv_block.resize(6 * forw[i].group.size());
600 forw[i].recv_size.resize(forw[i].group.size());
601
602 forw[i].new_size = calc_local_mesh(
603 my_pos[i], n_grid[i], global_mesh_dim.data(), global_mesh_off.data(),
604 forw[i].new_mesh.data(), forw[i].start.data());
605 permute_ifield(forw[i].new_mesh.data(), 3, -(forw[i].n_permute));
606 permute_ifield(forw[i].start.data(), 3, -(forw[i].n_permute));
607 forw[i].n_ffts = forw[i].new_mesh[0] * forw[i].new_mesh[1];
608
609 /* === send/recv block specifications === */
610 for (std::size_t j = 0ul; j < forw[i].group.size(); j++) {
611 /* send block: comm.rank() to comm-group-node i (identity: node) */
612 int node = forw[i].group[j];
613 forw[i].send_size[j] = calc_send_block(
614 my_pos[i - 1], n_grid[i - 1], &(n_pos[i][3 * node]), n_grid[i],
615 global_mesh_dim.data(), global_mesh_off.data(),
616 &(forw[i].send_block[6ul * j]));
617 permute_ifield(&(forw[i].send_block[6ul * j]), 3,
618 -(forw[i - 1].n_permute));
619 permute_ifield(&(forw[i].send_block[6ul * j + 3ul]), 3,
620 -(forw[i - 1].n_permute));
621 if (forw[i].send_size[j] > max_comm_size)
622 max_comm_size = forw[i].send_size[j];
623 /* First plan send blocks have to be adjusted, since the CA grid
624 may have an additional margin outside the actual domain of the
625 node */
626 if (i == 1) {
627 for (std::size_t k = 0ul; k < 3ul; k++)
628 forw[1].send_block[6ul * j + k] += ca_mesh_margin[2ul * k];
629 }
630 /* recv block: comm.rank() from comm-group-node i (identity: node) */
631 forw[i].recv_size[j] = calc_send_block(
632 my_pos[i], n_grid[i], &(n_pos[i - 1][3 * node]), n_grid[i - 1],
633 global_mesh_dim.data(), global_mesh_off.data(),
634 &(forw[i].recv_block[6ul * j]));
635 permute_ifield(&(forw[i].recv_block[6ul * j]), 3, -(forw[i].n_permute));
636 permute_ifield(&(forw[i].recv_block[6ul * j + 3ul]), 3,
637 -(forw[i].n_permute));
638 if (forw[i].recv_size[j] > max_comm_size)
639 max_comm_size = forw[i].recv_size[j];
640 }
641
642 for (std::size_t j = 0ul; j < 3ul; j++)
643 forw[i].old_mesh[j] = forw[i - 1].new_mesh[j];
644 if (i == 1) {
645 forw[i].element = 1;
646 } else {
647 forw[i].element = 2;
648 for (std::size_t j = 0ul; j < forw[i].group.size(); j++) {
649 forw[i].send_size[j] *= 2;
650 forw[i].recv_size[j] *= 2;
651 }
652 }
653 }
654
655 /* Factor 2 for complex fields */
656 max_comm_size *= 2;
657 max_mesh_size = Utils::product(ca_mesh_dim);
658 for (int i = 1; i < 4; i++)
659 if (2 * forw[i].new_size > max_mesh_size)
660 max_mesh_size = 2 * forw[i].new_size;
661
662 /* === pack function === */
663 for (int i = 1; i < 4; i++) {
664 forw[i].pack_function = pack_block_permute2;
665 }
666 ks_pnum = 6;
667 if (forw[1].row_dir == 2) {
668 forw[1].pack_function = fft_pack_block;
669 ks_pnum = 4;
670 } else if (forw[1].row_dir == 1) {
671 forw[1].pack_function = pack_block_permute1;
672 ks_pnum = 5;
673 }
674
675 send_buf.resize(max_comm_size);
676 recv_buf.resize(max_comm_size);
677 data_buf.resize(max_mesh_size);
678 auto *c_data = (typename fftw<FloatType>::complex *)(data_buf.data());
679
680 /* === FFT Routines (Using FFTW / RFFTW package)=== */
681 for (int i = 1; i < 4; i++) {
682 if (init_tag) {
683#pragma omp critical(fftw_destroy_plan_forward)
684 forw[i].destroy_plan();
685 }
686 forw[i].dir = FFTW_FORWARD;
687#pragma omp critical(fftw_create_plan_forward)
688 forw[i].plan_handle = fftw<FloatType>::plan_many_dft(
689 1, &forw[i].new_mesh[2], forw[i].n_ffts, c_data, nullptr, 1,
690 forw[i].new_mesh[2], c_data, nullptr, 1, forw[i].new_mesh[2],
691 forw[i].dir, FFTW_PATIENT);
692 assert(forw[i].plan_handle);
693 }
694
695 /* === The BACK Direction === */
696 /* this is needed because slightly different functions are used */
697 for (int i = 1; i < 4; i++) {
698 if (init_tag) {
699#pragma omp critical(fftw_destroy_plan_backward)
700 back[i].destroy_plan();
701 }
702 back[i].dir = FFTW_BACKWARD;
703#pragma omp critical(fftw_create_plan_backward)
704 back[i].plan_handle = fftw<FloatType>::plan_many_dft(
705 1, &forw[i].new_mesh[2], forw[i].n_ffts, c_data, nullptr, 1,
706 forw[i].new_mesh[2], c_data, nullptr, 1, forw[i].new_mesh[2],
707 back[i].dir, FFTW_PATIENT);
708 back[i].pack_function = pack_block_permute1;
709 assert(back[i].plan_handle);
710 }
711 if (forw[1].row_dir == 2) {
712 back[1].pack_function = fft_pack_block;
713 } else if (forw[1].row_dir == 1) {
714 back[1].pack_function = pack_block_permute2;
715 }
716
717 init_tag = true;
718
719 return max_mesh_size;
720}
721
722template <typename FloatType>
724 boost::mpi::communicator const &comm, FloatType *data) {
725 /* ===== first direction ===== */
726
727 auto *c_data = (typename fftw<FloatType>::complex *)data;
728 auto *c_data_buf = (typename fftw<FloatType>::complex *)data_buf.data();
729
730 /* communication to current dir row format (in is data) */
731 forw_grid_comm(comm, forw[1], data, data_buf.data());
732
733 /* complexify the real data array (in is data_buf) */
734 for (int i = 0; i < forw[1].new_size; i++) {
735 data[2 * i + 0] = data_buf[i]; /* real value */
736 data[2 * i + 1] = FloatType(0); /* complex value */
737 }
738 /* perform FFT (in/out is data)*/
739 fftw<FloatType>::execute_dft(forw[1].plan_handle, c_data, c_data);
740 /* ===== second direction ===== */
741 /* communication to current dir row format (in is data) */
742 forw_grid_comm(comm, forw[2], data, data_buf.data());
743 /* perform FFT (in/out is data_buf) */
744 fftw<FloatType>::execute_dft(forw[2].plan_handle, c_data_buf, c_data_buf);
745 /* ===== third direction ===== */
746 /* communication to current dir row format (in is data_buf) */
747 forw_grid_comm(comm, forw[3], data_buf.data(), data);
748 /* perform FFT (in/out is data)*/
749 fftw<FloatType>::execute_dft(forw[3].plan_handle, c_data, c_data);
750
751 /* REMARK: Result has to be in data. */
752}
753
754template <typename FloatType>
756 boost::mpi::communicator const &comm, FloatType *data) {
757
758 auto *c_data = (typename fftw<FloatType>::complex *)data;
759 auto *c_data_buf = (typename fftw<FloatType>::complex *)data_buf.data();
760
761 /* ===== third direction ===== */
762
763 /* perform FFT (in is data) */
764 fftw<FloatType>::execute_dft(back[3].plan_handle, c_data, c_data);
765 /* communicate (in is data)*/
766 back_grid_comm(comm, forw[3], back[3], data, data_buf.data());
767
768 /* ===== second direction ===== */
769 /* perform FFT (in is data_buf) */
770 fftw<FloatType>::execute_dft(back[2].plan_handle, c_data_buf, c_data_buf);
771 /* communicate (in is data_buf) */
772 back_grid_comm(comm, forw[2], back[2], data_buf.data(), data);
773
774 /* ===== first direction ===== */
775 /* perform FFT (in is data) */
776 fftw<FloatType>::execute_dft(back[1].plan_handle, c_data, c_data);
777 /* throw away the (hopefully) empty complex component (in is data) */
778 for (int i = 0; i < forw[1].new_size; i++) {
779 data_buf[i] = data[2 * i]; /* real value */
780 }
781 /* communicate (in is data_buf) */
782 back_grid_comm(comm, forw[1], back[1], data_buf.data(), data);
783
784 /* REMARK: Result has to be in data. */
785}
786
787template <typename FloatType> void fft_plan<FloatType>::destroy_plan() {
788 if (plan_handle) {
790 plan_handle = nullptr;
791 }
792}
793
794template <class FloatType>
795FloatType *allocator<FloatType>::allocate(std::size_t const n) const {
796 if (n == 0ul) {
797 return nullptr;
798 }
799 if (n > std::numeric_limits<std::size_t>::max() / sizeof(FloatType)) {
800 throw std::bad_array_new_length();
801 }
802 void *const pv = fftw<FloatType>::malloc(n * sizeof(FloatType));
803 if (!pv) {
804 throw std::bad_alloc();
805 }
806 return static_cast<FloatType *>(pv);
807}
808
809template <class FloatType>
810void allocator<FloatType>::deallocate(FloatType *const p,
811 std::size_t) const noexcept {
812 fftw<FloatType>::free(static_cast<void *>(p));
813}
814
815template struct allocator<float>;
816template struct allocator<double>;
817template struct allocator<std::complex<float>>;
818template struct allocator<std::complex<double>>;
819
820template struct fft_plan<float>;
821template struct fft_plan<double>;
822
823template struct fft_forw_plan<float>;
824template struct fft_forw_plan<double>;
825
826template struct fft_back_plan<float>;
827template struct fft_back_plan<double>;
828
829template struct fft_data_struct<float>;
830template struct fft_data_struct<double>;
831
832} // namespace fft
Vector implementation and trait types for boost qvm interoperability.
DEVICE_QUALIFIER constexpr pointer data() noexcept
Definition Array.hpp:133
static double * block(double *p, std::size_t index, std::size_t size)
Definition elc.cpp:169
void permute_ifield(int *field, int size, int permute)
permute an integer array field of size size about permute positions.
static void fft_sendrecv(T const *const sendbuf, int scount, int dest, T *const recvbuf, int rcount, int source, boost::mpi::communicator const &comm, int tag)
Definition fft.cpp:68
#define REQ_FFT_BACK
Tag for communication in back_grid_comm().
Definition fft.cpp:64
#define REQ_FFT_FORW
Tag for communication in forw_grid_comm().
Definition fft.cpp:62
Routines, row decomposition, data structures and communication for the 3D-FFT.
Vector3i cart_coords(boost::mpi::communicator const &comm, int rank)
Wrapper around MPI_Cart_coords.
Definition cart_comm.hpp:72
T product(Vector< T, N > const &v)
Definition Vector.hpp:379
void permute_ifield(int *field, int size, int permute)
permute an integer array field of size size about permute positions.
VectorXd< 3 > Vector3d
Definition Vector.hpp:207
DEVICE_QUALIFIER int get_linear_index(int a, int b, int c, Vector3i const &adim)
Definition index.hpp:36
VectorXi< 3 > Vector3i
Definition Vector.hpp:216
void pack_block_permute2(FloatType const *const in, FloatType *const out, const int *start, const int *size, const int *dim, int element)
Pack a block with dimensions size[0] * size[1] * size[2] starting at start of an input 3D-grid with d...
Definition fft.cpp:360
void pack_block_permute1(FloatType const *const in, FloatType *const out, const int *start, const int *size, const int *dim, int element)
Pack a block with dimensions size[0] * size[1] * size[2] starting at start of an input 3D-grid with d...
Definition fft.cpp:311
int calc_send_block(const int *pos1, const int *grid1, const int *pos2, const int *grid2, const int *mesh, const double *mesh_off, int *block)
Calculate a send (or recv.) block for grid communication during a decomposition change.
Definition fft.cpp:269
int calc_local_mesh(const int *n_pos, const int *n_grid, const int *mesh, const double *mesh_off, int *loc_mesh, int *start)
Calculate the local fft mesh.
Definition fft.cpp:225
Definition fft.cpp:77
std::optional< std::vector< int > > find_comm_groups(Utils::Vector3i const &grid1, Utils::Vector3i const &grid2, std::span< int const > node_list1, std::span< int > node_list2, std::span< int > pos, std::span< int > my_pos, int rank)
This ugly function does the bookkeeping: which nodes have to communicate to each other,...
Definition fft.cpp:120
void calc_2d_grid(int n, int grid[3])
Calculate most square 2D grid.
Definition fft.cpp:504
int map_3don2d_grid(int const g3d[3], int g2d[3])
Calculate 'best' mapping between a 2D and 3D grid.
Definition fft.cpp:462
void fft_pack_block(FloatType const *const in, FloatType *const out, int const *start, int const *size, int const *dim, int element)
Pack a 3D-block of size size starting at start of an input 3D-grid in with dimension dim into an outp...
Definition packing.hpp:44
void fft_unpack_block(FloatType const *const in, FloatType *const out, int const *start, int const *size, int const *dim, int element)
Unpack a 3D-block in of size size into an output 3D-grid out of size dim starting at position start.
Definition packing.hpp:83
Aligned allocator for FFT data.
Definition vector.hpp:35
FloatType * allocate(std::size_t n) const
Definition fft.cpp:795
void deallocate(FloatType *p, std::size_t) const noexcept
Definition fft.cpp:810
Plan for a backward 1D FFT of a flattened 3D array.
Definition fft.hpp:116
Information about the three one dimensional FFTs and how the nodes have to communicate inbetween.
Definition fft.hpp:125
void forward_fft(boost::mpi::communicator const &comm, FloatType *data)
Perform an in-place forward 3D FFT.
Definition fft.cpp:723
void backward_fft(boost::mpi::communicator const &comm, FloatType *data)
Perform an in-place backward 3D FFT.
Definition fft.cpp:755
int initialize_fft(boost::mpi::communicator const &comm, Utils::Vector3i const &ca_mesh_dim, int const *ca_mesh_margin, Utils::Vector3i const &global_mesh_dim, Utils::Vector3d const &global_mesh_off, int &ks_pnum, Utils::Vector3i const &grid)
Initialize everything connected to the 3D-FFT.
Definition fft.cpp:519
Plan for a forward 1D FFT of a flattened 3D array.
Definition fft.hpp:82
void destroy_plan()
Definition fft.cpp:787
fftw_plan plan_handle
plan for the FFT.
Definition fft.hpp:73
static auto constexpr execute_dft
Definition fft.cpp:91
static auto constexpr plan_many_dft
Definition fft.cpp:89
fftwf_complex complex
Definition fft.cpp:88
static auto constexpr destroy_plan
Definition fft.cpp:90
static auto constexpr free
Definition fft.cpp:93
static auto constexpr malloc
Definition fft.cpp:92
static auto constexpr malloc
Definition fft.cpp:84
static auto constexpr free
Definition fft.cpp:85
fftw_complex complex
Definition fft.cpp:80
static auto constexpr execute_dft
Definition fft.cpp:83
static auto constexpr destroy_plan
Definition fft.cpp:82
static auto constexpr plan_many_dft
Definition fft.cpp:81