ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
send_mesh.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
22#include <config/config.hpp>
23
24#if defined(ESPRESSO_P3M) or defined(ESPRESSO_DP3M)
25
26#include "fft/fft.hpp"
27#include "p3m/common.hpp"
28#include "p3m/packing.hpp"
29#include "p3m/send_mesh.hpp"
30
31#include <utils/Vector.hpp>
33
34#include <boost/mpi/communicator.hpp>
35#include <boost/mpi/datatype.hpp>
36
37#include <mpi.h>
38
39#include <algorithm>
40#include <array>
41#include <complex>
42#include <cstddef>
43#include <initializer_list>
44#include <span>
45#include <utility>
46
47template <typename T>
48static void mesh_sendrecv(T const *const sendbuf, int scount, int dest,
49 T *const recvbuf, int rcount, int source,
50 boost::mpi::communicator const &comm, int tag) {
51 auto const type = boost::mpi::get_mpi_datatype<T>(*sendbuf);
52 MPI_Sendrecv(reinterpret_cast<void const *>(sendbuf), scount, type, dest, tag,
53 reinterpret_cast<void *>(recvbuf), rcount, type, source, tag,
54 comm, MPI_STATUS_IGNORE);
55}
56
57/** Add values of a 3d-grid input block (size[3]) to values of 3d-grid
58 * output array with dimension dim[3] at start position start[3].
59 *
60 * \param in Pointer to first element of input block data.
61 * \param out Pointer to first element of output grid.
62 * \param start Start position of block in output grid.
63 * \param size Dimensions of the block
64 * \param dim Dimensions of the output grid.
65 */
66template <typename FloatType>
67static void p3m_add_block(FloatType const *in, FloatType *out,
68 int const start[3], int const size[3],
69 int const dim[3]) {
70 /* fast, mid and slow changing indices */
71 int f, m, s;
72 /* linear index of in grid, linear index of out grid */
73 int li_in = 0, li_out = 0;
74 /* offsets for indices in output grid */
75 int m_out_offset, s_out_offset;
76
77 li_out = start[2] + (dim[2] * (start[1] + (dim[1] * start[0])));
78 m_out_offset = dim[2] - size[2];
79 s_out_offset = (dim[2] * (dim[1] - size[1]));
80
81 for (s = 0; s < size[0]; s++) {
82 for (m = 0; m < size[1]; m++) {
83 for (f = 0; f < size[2]; f++) {
84 out[li_out++] += in[li_in++];
85 }
86 li_out += m_out_offset;
87 }
88 li_out += s_out_offset;
89 }
90}
91
92template <typename FloatType>
93void p3m_send_mesh<FloatType>::resize(boost::mpi::communicator const &comm,
94 P3MLocalMesh const &local_mesh) {
95 int done[3] = {0, 0, 0};
96 /* send grids */
97 for (int i = 0; i < 3; i++) {
98 for (int j = 0; j < 3; j++) {
99 /* left */
100 s_ld[i * 2][j] = 0 + done[j] * local_mesh.margin[j * 2];
101 if (j == i)
102 s_ur[i * 2][j] = local_mesh.margin[j * 2];
103 else
104 s_ur[i * 2][j] =
105 local_mesh.dim[j] - done[j] * local_mesh.margin[(j * 2) + 1];
106 /* right */
107 if (j == i)
108 s_ld[(i * 2) + 1][j] = local_mesh.in_ur[j];
109 else
110 s_ld[(i * 2) + 1][j] = 0 + done[j] * local_mesh.margin[j * 2];
111 s_ur[(i * 2) + 1][j] =
112 local_mesh.dim[j] - done[j] * local_mesh.margin[(j * 2) + 1];
113 }
114 done[i] = 1;
115 }
116 for (int i = 0; i < 6; i++) {
117 s_size[i] = 1;
118 for (int j = 0; j < 3; j++) {
119 s_dim[i][j] = s_ur[i][j] - s_ld[i][j];
120 s_size[i] *= s_dim[i][j];
121 }
122 }
123 /* communication */
124 auto const node_neighbors = Utils::Mpi::cart_neighbors<3>(comm);
125
126 int r_margin[6];
127 for (int i = 0; i < 6; i++) {
128 auto const j = (i % 2 == 0) ? i + 1 : i - 1;
129
130 if (node_neighbors[i] != comm.rank()) {
131 mesh_sendrecv(&(local_mesh.margin[i]), 1, node_neighbors[i],
132 &(r_margin[j]), 1, node_neighbors[j], comm, REQ_P3M_INIT);
133 } else {
134 r_margin[j] = local_mesh.margin[i];
135 }
136 }
137 /* recv grids */
138 for (int i = 0; i < 3; i++) {
139 for (int j = 0; j < 3; j++) {
140 if (j == i) {
141 r_ld[i * 2][j] = s_ld[i * 2][j] + local_mesh.margin[2 * j];
142 r_ur[i * 2][j] = s_ur[i * 2][j] + r_margin[2 * j];
143 r_ld[(i * 2) + 1][j] = s_ld[(i * 2) + 1][j] - r_margin[(2 * j) + 1];
144 r_ur[(i * 2) + 1][j] =
145 s_ur[(i * 2) + 1][j] - local_mesh.margin[(2 * j) + 1];
146 } else {
147 r_ld[i * 2][j] = s_ld[i * 2][j];
148 r_ur[i * 2][j] = s_ur[i * 2][j];
149 r_ld[(i * 2) + 1][j] = s_ld[(i * 2) + 1][j];
150 r_ur[(i * 2) + 1][j] = s_ur[(i * 2) + 1][j];
151 }
152 }
153 }
154 for (int i = 0; i < 6; i++) {
155 r_size[i] = 1;
156 for (int j = 0; j < 3; j++) {
157 r_dim[i][j] = r_ur[i][j] - r_ld[i][j];
158 r_size[i] *= r_dim[i][j];
159 }
160 }
161 /* both directions of a dimension share the send/recv buffers */
162 max = 0;
163 for (int d = 0; d < 3; ++d) {
164 auto const l = 2 * d;
165 auto const r = l + 1;
166 max = std::max(max, s_size[l] + s_size[r]);
167 max = std::max(max, r_size[l] + r_size[r]);
168 }
169}
170
171template <typename FloatType>
172void p3m_send_mesh<FloatType>::exchange(boost::mpi::communicator const &comm,
173 int left, int right, int send_left,
174 int send_right, int recv_right,
175 int recv_left, int tag) {
176 if (left == comm.rank()) {
177 // 1 MPI rank along this direction: exchange with self
178 std::swap(send_grid, recv_grid);
179 } else if (left == right) {
180 // 2 MPI ranks along this direction: send both blocks in a single message
181 mesh_sendrecv(send_grid.data(), send_left + send_right, left,
182 recv_grid.data(), recv_right + recv_left, left, comm, tag);
183 } else {
184 // 3 or more MPI ranks along this direction: overlapping communication
185 auto const type = boost::mpi::get_mpi_datatype(FloatType{});
186 std::array<MPI_Request, 4> reqs;
187 MPI_Irecv(recv_grid.data(), recv_right, type, right, tag, comm, &reqs[0]);
188 MPI_Irecv(recv_grid.data() + recv_right, recv_left, type, left, tag, comm,
189 &reqs[1]);
190 MPI_Isend(send_grid.data(), send_left, type, left, tag, comm, &reqs[2]);
191 MPI_Isend(send_grid.data() + send_left, send_right, type, right, tag, comm,
192 &reqs[3]);
193 MPI_Waitall(4, reqs.data(), MPI_STATUSES_IGNORE);
194 }
195}
196
197template <typename FloatType>
198void p3m_send_mesh<FloatType>::gather_grid(boost::mpi::communicator const &comm,
199 std::span<FloatType *> meshes,
200 Utils::Vector3i const &dim) {
201 auto const node_neighbors = Utils::Mpi::cart_neighbors<3>(comm);
202 auto const n_meshes = static_cast<int>(meshes.size());
203 send_grid.resize(static_cast<std::size_t>(max) * meshes.size());
204 recv_grid.resize(static_cast<std::size_t>(max) * meshes.size());
205
206 /* dimension loop: both directions of a dimension are independent, since
207 * margins are packed and received blocks are added to inner layers */
208 for (int d = 0; d < 3; d++) {
209 auto const l = 2 * d;
210 auto const r = l + 1;
211
212 /* pack send blocks */
213 auto *out = send_grid.data();
214 for (auto const dir : {l, r}) {
215 for (auto *mesh : meshes) {
216 fft_pack_block(mesh, out, s_ld[dir], s_dim[dir], dim.data(), 1);
217 out += s_size[dir];
218 }
219 }
220
221 /* communication */
222 exchange(comm, node_neighbors[l], node_neighbors[r], n_meshes * s_size[l],
223 n_meshes * s_size[r], n_meshes * r_size[r], n_meshes * r_size[l],
224 REQ_P3M_GATHER);
225
226 /* add recv blocks */
227 auto const *in = recv_grid.data();
228 for (auto const dir : {r, l}) {
229 for (auto *mesh : meshes) {
230 p3m_add_block(in, mesh, r_ld[dir], r_dim[dir], dim.data());
231 in += r_size[dir];
232 }
233 }
234 }
235}
236
237template <typename FloatType>
238void p3m_send_mesh<FloatType>::spread_grid(boost::mpi::communicator const &comm,
239 std::span<FloatType *> meshes,
240 Utils::Vector3i const &dim) {
241 auto const node_neighbors = Utils::Mpi::cart_neighbors<3>(comm);
242 auto const n_meshes = static_cast<int>(meshes.size());
243 send_grid.resize(static_cast<std::size_t>(max) * meshes.size());
244 recv_grid.resize(static_cast<std::size_t>(max) * meshes.size());
245
246 /* dimension loop: both directions of a dimension are independent, since
247 * inner layers are packed and received blocks overwrite the margins */
248 for (int d = 2; d >= 0; --d) {
249 auto const l = 2 * d;
250 auto const r = l + 1;
251
252 /* pack send blocks */
253 auto *out = send_grid.data();
254 for (auto const dir : {l, r}) {
255 for (auto *mesh : meshes) {
256 fft_pack_block(mesh, out, r_ld[dir], r_dim[dir], dim.data(), 1);
257 out += r_size[dir];
258 }
259 }
260
261 /* communication */
262 exchange(comm, node_neighbors[l], node_neighbors[r], n_meshes * r_size[l],
263 n_meshes * r_size[r], n_meshes * s_size[r], n_meshes * s_size[l],
264 REQ_P3M_SPREAD);
265
266 /* unpack recv blocks */
267 auto const *in = recv_grid.data();
268 for (auto const dir : {r, l}) {
269 for (auto *mesh : meshes) {
270 fft_unpack_block(in, mesh, s_ld[dir], s_dim[dir], dim.data(), 1);
271 in += s_size[dir];
272 }
273 }
274 }
275}
276
277template class p3m_send_mesh<float>;
278template class p3m_send_mesh<double>;
279
280#endif // defined(ESPRESSO_P3M) or defined(ESPRESSO_DP3M)
Vector implementation and trait types for boost qvm interoperability.
DEVICE_QUALIFIER constexpr pointer data() noexcept
Definition Array.hpp:133
P3M halo communicator.
Definition send_mesh.hpp:38
void gather_grid(boost::mpi::communicator const &comm, std::span< FloatType * > meshes, Utils::Vector3i const &dim)
void spread_grid(boost::mpi::communicator const &comm, std::span< FloatType * > meshes, Utils::Vector3i const &dim)
void resize(boost::mpi::communicator const &comm, P3MLocalMesh const &local_mesh)
Definition send_mesh.cpp:93
Routines, row decomposition, data structures and communication for the 3D-FFT.
Utils::Vector< int, 2 *dim > cart_neighbors(const boost::mpi::communicator &comm)
Calculates the numbers of the nearest neighbors for a node.
VectorXi< 3 > Vector3i
Definition Vector.hpp:216
Common functions for dipolar and charge P3M.
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
static void p3m_add_block(FloatType const *in, FloatType *out, int const start[3], int const size[3], int const dim[3])
Add values of a 3d-grid input block (size[3]) to values of 3d-grid output array with dimension dim[3]...
Definition send_mesh.cpp:67
static void mesh_sendrecv(T const *const sendbuf, int scount, int dest, T *const recvbuf, int rcount, int source, boost::mpi::communicator const &comm, int tag)
Definition send_mesh.cpp:48
Properties of the local mesh and halo region.
Utils::Vector3i dim
dimension (size) of local mesh including halo layers.
int margin[6]
number of margin mesh points.
Utils::Vector3i in_ur
inner up right grid point + (1,1,1)