24#if defined(ESPRESSO_P3M) or defined(ESPRESSO_DP3M)
34#include <boost/mpi/communicator.hpp>
35#include <boost/mpi/datatype.hpp>
43#include <initializer_list>
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);
66template <
typename FloatType>
68 int const start[3],
int const size[3],
73 int li_in = 0, li_out = 0;
75 int m_out_offset, s_out_offset;
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]));
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++];
86 li_out += m_out_offset;
88 li_out += s_out_offset;
92template <
typename FloatType>
95 int done[3] = {0, 0, 0};
97 for (
int i = 0; i < 3; i++) {
98 for (
int j = 0; j < 3; j++) {
100 s_ld[i * 2][j] = 0 + done[j] * local_mesh.
margin[j * 2];
102 s_ur[i * 2][j] = local_mesh.
margin[j * 2];
105 local_mesh.
dim[j] - done[j] * local_mesh.
margin[(j * 2) + 1];
108 s_ld[(i * 2) + 1][j] = local_mesh.
in_ur[j];
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];
116 for (
int i = 0; i < 6; i++) {
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];
127 for (
int i = 0; i < 6; i++) {
128 auto const j = (i % 2 == 0) ? i + 1 : i - 1;
130 if (node_neighbors[i] != comm.rank()) {
132 &(r_margin[j]), 1, node_neighbors[j], comm, REQ_P3M_INIT);
134 r_margin[j] = local_mesh.
margin[i];
138 for (
int i = 0; i < 3; i++) {
139 for (
int j = 0; j < 3; j++) {
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];
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];
154 for (
int i = 0; i < 6; i++) {
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];
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]);
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()) {
178 std::swap(send_grid, recv_grid);
179 }
else if (left == right) {
181 mesh_sendrecv(send_grid.data(), send_left + send_right, left,
182 recv_grid.data(), recv_right + recv_left, left, comm, tag);
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,
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,
193 MPI_Waitall(4, reqs.data(), MPI_STATUSES_IGNORE);
197template <
typename FloatType>
199 std::span<FloatType *> meshes,
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());
208 for (
int d = 0; d < 3; d++) {
209 auto const l = 2 * d;
210 auto const r = l + 1;
213 auto *out = send_grid.data();
214 for (
auto const dir : {l, r}) {
215 for (
auto *mesh : meshes) {
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],
227 auto const *in = recv_grid.data();
228 for (
auto const dir : {r, l}) {
229 for (
auto *mesh : meshes) {
237template <
typename FloatType>
239 std::span<FloatType *> meshes,
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());
248 for (
int d = 2; d >= 0; --d) {
249 auto const l = 2 * d;
250 auto const r = l + 1;
253 auto *out = send_grid.data();
254 for (
auto const dir : {l, r}) {
255 for (
auto *mesh : meshes) {
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],
267 auto const *in = recv_grid.data();
268 for (
auto const dir : {r, l}) {
269 for (
auto *mesh : meshes) {
Vector implementation and trait types for boost qvm interoperability.
DEVICE_QUALIFIER constexpr pointer data() noexcept
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)
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.
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...
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.
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]...
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)
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)