2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 * \brief This file contains function definitions necessary for
40 * managing the offload of long-ranged PME work to separate MPI rank,
41 * for computing energies and forces (Coulomb and LJ).
43 * \author Berk Hess <hess@kth.se>
44 * \ingroup module_ewald
55 #include "gromacs/domdec/domdec.h"
56 #include "gromacs/domdec/domdec_struct.h"
57 #include "gromacs/ewald/pme.h"
58 #include "gromacs/gmxlib/network.h"
59 #include "gromacs/math/vec.h"
60 #include "gromacs/mdlib/gmx_omp_nthreads.h"
61 #include "gromacs/mdtypes/commrec.h"
62 #include "gromacs/mdtypes/forceoutput.h"
63 #include "gromacs/mdtypes/interaction_const.h"
64 #include "gromacs/mdtypes/md_enums.h"
65 #include "gromacs/timing/wallcycle.h"
66 #include "gromacs/utility/fatalerror.h"
67 #include "gromacs/utility/gmxmpi.h"
68 #include "gromacs/utility/smalloc.h"
70 #include "pme-internal.h"
71 #include "pme-pp-communication.h"
73 /*! \brief Block to wait for communication to PME ranks to complete
75 * This should be faster with a real non-blocking MPI implementation
77 static constexpr bool c_useDelayedWait = false;
79 /*! \brief Wait for the pending data send requests to PME ranks to complete */
80 static void gmx_pme_send_coeffs_coords_wait(gmx_domdec_t *dd)
85 MPI_Waitall(dd->nreq_pme, dd->req_pme, MPI_STATUSES_IGNORE);
91 /*! \brief Send data to PME ranks */
92 static void gmx_pme_send_coeffs_coords(const t_commrec *cr, unsigned int flags,
93 real gmx_unused *chargeA, real gmx_unused *chargeB,
94 real gmx_unused *c6A, real gmx_unused *c6B,
95 real gmx_unused *sigmaA, real gmx_unused *sigmaB,
96 matrix box, rvec gmx_unused *x,
97 real lambda_q, real lambda_lj,
98 int maxshift_x, int maxshift_y,
102 gmx_pme_comm_n_box_t *cnb;
106 n = dd_numHomeAtoms(*dd);
110 fprintf(debug, "PP rank %d sending to PME rank %d: %d%s%s%s%s\n",
111 cr->sim_nodeid, dd->pme_nodeid, n,
112 (flags & PP_PME_CHARGE) ? " charges" : "",
113 (flags & PP_PME_SQRTC6) ? " sqrtC6" : "",
114 (flags & PP_PME_SIGMA) ? " sigma" : "",
115 (flags & PP_PME_COORD) ? " coordinates" : "");
118 if (c_useDelayedWait)
120 /* We can not use cnb until pending communication has finished */
121 gmx_pme_send_coeffs_coords_wait(dd);
124 if (dd->pme_receive_vir_ener)
126 /* Peer PP node: communicate all data */
127 if (dd->cnb == nullptr)
135 cnb->maxshift_x = maxshift_x;
136 cnb->maxshift_y = maxshift_y;
137 cnb->lambda_q = lambda_q;
138 cnb->lambda_lj = lambda_lj;
140 if (flags & PP_PME_COORD)
142 copy_mat(box, cnb->box);
145 MPI_Isend(cnb, sizeof(*cnb), MPI_BYTE,
146 dd->pme_nodeid, eCommType_CNB, cr->mpi_comm_mysim,
147 &dd->req_pme[dd->nreq_pme++]);
150 else if (flags & (PP_PME_CHARGE | PP_PME_SQRTC6 | PP_PME_SIGMA))
153 /* Communicate only the number of atoms */
154 MPI_Isend(&n, sizeof(n), MPI_BYTE,
155 dd->pme_nodeid, eCommType_CNB, cr->mpi_comm_mysim,
156 &dd->req_pme[dd->nreq_pme++]);
163 if (flags & PP_PME_CHARGE)
165 MPI_Isend(chargeA, n*sizeof(real), MPI_BYTE,
166 dd->pme_nodeid, eCommType_ChargeA, cr->mpi_comm_mysim,
167 &dd->req_pme[dd->nreq_pme++]);
169 if (flags & PP_PME_CHARGEB)
171 MPI_Isend(chargeB, n*sizeof(real), MPI_BYTE,
172 dd->pme_nodeid, eCommType_ChargeB, cr->mpi_comm_mysim,
173 &dd->req_pme[dd->nreq_pme++]);
175 if (flags & PP_PME_SQRTC6)
177 MPI_Isend(c6A, n*sizeof(real), MPI_BYTE,
178 dd->pme_nodeid, eCommType_SQRTC6A, cr->mpi_comm_mysim,
179 &dd->req_pme[dd->nreq_pme++]);
181 if (flags & PP_PME_SQRTC6B)
183 MPI_Isend(c6B, n*sizeof(real), MPI_BYTE,
184 dd->pme_nodeid, eCommType_SQRTC6B, cr->mpi_comm_mysim,
185 &dd->req_pme[dd->nreq_pme++]);
187 if (flags & PP_PME_SIGMA)
189 MPI_Isend(sigmaA, n*sizeof(real), MPI_BYTE,
190 dd->pme_nodeid, eCommType_SigmaA, cr->mpi_comm_mysim,
191 &dd->req_pme[dd->nreq_pme++]);
193 if (flags & PP_PME_SIGMAB)
195 MPI_Isend(sigmaB, n*sizeof(real), MPI_BYTE,
196 dd->pme_nodeid, eCommType_SigmaB, cr->mpi_comm_mysim,
197 &dd->req_pme[dd->nreq_pme++]);
199 if (flags & PP_PME_COORD)
201 MPI_Isend(x[0], n*sizeof(rvec), MPI_BYTE,
202 dd->pme_nodeid, eCommType_COORD, cr->mpi_comm_mysim,
203 &dd->req_pme[dd->nreq_pme++]);
207 if (!c_useDelayedWait)
209 /* Wait for the data to arrive */
210 /* We can skip this wait as we are sure x and q will not be modified
211 * before the next call to gmx_pme_send_x_q or gmx_pme_receive_f.
213 gmx_pme_send_coeffs_coords_wait(dd);
217 void gmx_pme_send_parameters(const t_commrec *cr,
218 const interaction_const_t *ic,
219 gmx_bool bFreeEnergy_q, gmx_bool bFreeEnergy_lj,
220 real *chargeA, real *chargeB,
221 real *sqrt_c6A, real *sqrt_c6B,
222 real *sigmaA, real *sigmaB,
223 int maxshift_x, int maxshift_y)
225 unsigned int flags = 0;
227 if (EEL_PME(ic->eeltype))
229 flags |= PP_PME_CHARGE;
231 if (EVDW_PME(ic->vdwtype))
233 flags |= (PP_PME_SQRTC6 | PP_PME_SIGMA);
235 if (bFreeEnergy_q || bFreeEnergy_lj)
237 /* Assumes that the B state flags are in the bits just above
238 * the ones for the A state. */
239 flags |= (flags << 1);
242 gmx_pme_send_coeffs_coords(cr, flags,
244 sqrt_c6A, sqrt_c6B, sigmaA, sigmaB,
245 nullptr, nullptr, 0, 0, maxshift_x, maxshift_y, -1);
248 void gmx_pme_send_coordinates(const t_commrec *cr, matrix box, rvec *x,
249 real lambda_q, real lambda_lj,
251 int64_t step, gmx_wallcycle *wcycle)
253 wallcycle_start(wcycle, ewcPP_PMESENDX);
255 unsigned int flags = PP_PME_COORD;
258 flags |= PP_PME_ENER_VIR;
260 gmx_pme_send_coeffs_coords(cr, flags, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
261 box, x, lambda_q, lambda_lj, 0, 0, step);
263 wallcycle_stop(wcycle, ewcPP_PMESENDX);
266 void gmx_pme_send_finish(const t_commrec *cr)
268 unsigned int flags = PP_PME_FINISH;
270 gmx_pme_send_coeffs_coords(cr, flags, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, 0, 0, 0, 0, -1);
273 void gmx_pme_send_switchgrid(const t_commrec *cr,
279 gmx_pme_comm_n_box_t cnb;
281 /* Only let one PP node signal each PME node */
282 if (cr->dd->pme_receive_vir_ener)
284 cnb.flags = PP_PME_SWITCHGRID;
285 copy_ivec(grid_size, cnb.grid_size);
286 cnb.ewaldcoeff_q = ewaldcoeff_q;
287 cnb.ewaldcoeff_lj = ewaldcoeff_lj;
289 /* We send this, uncommon, message blocking to simplify the code */
290 MPI_Send(&cnb, sizeof(cnb), MPI_BYTE,
291 cr->dd->pme_nodeid, eCommType_CNB, cr->mpi_comm_mysim);
294 GMX_UNUSED_VALUE(cr);
295 GMX_UNUSED_VALUE(grid_size);
296 GMX_UNUSED_VALUE(ewaldcoeff_q);
297 GMX_UNUSED_VALUE(ewaldcoeff_lj);
301 void gmx_pme_send_resetcounters(const t_commrec gmx_unused *cr, int64_t gmx_unused step)
304 gmx_pme_comm_n_box_t cnb;
306 /* Only let one PP node signal each PME node */
307 if (cr->dd->pme_receive_vir_ener)
309 cnb.flags = PP_PME_RESETCOUNTERS;
312 /* We send this, uncommon, message blocking to simplify the code */
313 MPI_Send(&cnb, sizeof(cnb), MPI_BYTE,
314 cr->dd->pme_nodeid, eCommType_CNB, cr->mpi_comm_mysim);
319 /*! \brief Receive virial and energy from PME rank */
320 static void receive_virial_energy(const t_commrec *cr,
321 gmx::ForceWithVirial *forceWithVirial,
322 real *energy_q, real *energy_lj,
323 real *dvdlambda_q, real *dvdlambda_lj,
326 gmx_pme_comm_vir_ene_t cve;
328 if (cr->dd->pme_receive_vir_ener)
333 "PP rank %d receiving from PME rank %d: virial and energy\n",
334 cr->sim_nodeid, cr->dd->pme_nodeid);
337 MPI_Recv(&cve, sizeof(cve), MPI_BYTE, cr->dd->pme_nodeid, 1, cr->mpi_comm_mysim,
340 memset(&cve, 0, sizeof(cve));
343 forceWithVirial->addVirialContribution(cve.vir_q);
344 forceWithVirial->addVirialContribution(cve.vir_lj);
345 *energy_q = cve.energy_q;
346 *energy_lj = cve.energy_lj;
347 *dvdlambda_q += cve.dvdlambda_q;
348 *dvdlambda_lj += cve.dvdlambda_lj;
349 *pme_cycles = cve.cycles;
351 if (cve.stop_cond != gmx_stop_cond_none)
353 gmx_set_stop_condition(cve.stop_cond);
364 void gmx_pme_receive_f(const t_commrec *cr,
365 gmx::ForceWithVirial *forceWithVirial,
366 real *energy_q, real *energy_lj,
367 real *dvdlambda_q, real *dvdlambda_lj,
370 if (c_useDelayedWait)
372 /* Wait for the x request to finish */
373 gmx_pme_send_coeffs_coords_wait(cr->dd);
376 int natoms = dd_numHomeAtoms(*cr->dd);
378 if (natoms > cr->dd->pme_recv_f_alloc)
380 cr->dd->pme_recv_f_alloc = over_alloc_dd(natoms);
381 srenew(cr->dd->pme_recv_f_buf, cr->dd->pme_recv_f_alloc);
385 MPI_Recv(cr->dd->pme_recv_f_buf[0],
386 natoms*sizeof(rvec), MPI_BYTE,
387 cr->dd->pme_nodeid, 0, cr->mpi_comm_mysim,
391 int nt = gmx_omp_nthreads_get_simple_rvec_task(emntDefault, natoms);
393 gmx::ArrayRef<gmx::RVec> f = forceWithVirial->force_;
395 /* Note that we would like to avoid this conditional by putting it
396 * into the omp pragma instead, but then we still take the full
397 * omp parallel for overhead (at least with gcc5).
401 for (int i = 0; i < natoms; i++)
403 rvec_inc(f[i], cr->dd->pme_recv_f_buf[i]);
408 #pragma omp parallel for num_threads(nt) schedule(static)
409 for (int i = 0; i < natoms; i++)
411 rvec_inc(f[i], cr->dd->pme_recv_f_buf[i]);
415 receive_virial_energy(cr, forceWithVirial, energy_q, energy_lj, dvdlambda_q, dvdlambda_lj, pme_cycles);