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, 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.
48 #include "gmx_fatal.h"
53 #include "sighandler.h"
55 #include "gromacs/utility/gmxmpi.h"
58 eCommType_ChargeA, eCommType_ChargeB, eCommType_SQRTC6A, eCommType_SQRTC6B,
59 eCommType_SigmaA, eCommType_SigmaB, eCommType_NR, eCommType_COORD,
63 /* Some parts of the code(gmx_pme_send_q, gmx_pme_recv_q_x) assume
64 * that the six first flags are exactly in this order.
65 * If more PP_PME_...-flags are to be introduced be aware of some of
66 * the PME-specific flags in pme.h. Currently, they are also passed
70 #define PP_PME_CHARGE (1<<0)
71 #define PP_PME_CHARGEB (1<<1)
72 #define PP_PME_SQRTC6 (1<<2)
73 #define PP_PME_SQRTC6B (1<<3)
74 #define PP_PME_SIGMA (1<<4)
75 #define PP_PME_SIGMAB (1<<5)
76 #define PP_PME_COORD (1<<6)
77 #define PP_PME_FEP_Q (1<<7)
78 #define PP_PME_FEP_LJ (1<<8)
79 #define PP_PME_ENER_VIR (1<<9)
80 #define PP_PME_FINISH (1<<10)
81 #define PP_PME_SWITCHGRID (1<<11)
82 #define PP_PME_RESETCOUNTERS (1<<12)
84 #define PME_PP_SIGSTOP (1<<0)
85 #define PME_PP_SIGSTOPNSS (1<<1)
87 typedef struct gmx_pme_pp {
89 MPI_Comm mpi_comm_mysim;
91 int nnode; /* The number of PP node to communicate with */
92 int *node; /* The PP node ranks */
93 int node_peer; /* The peer PP node rank */
94 int *nat; /* The number of atom for each PP node */
95 int flags_charge; /* The flags sent along with the last charges */
111 typedef struct gmx_pme_comm_n_box {
120 ivec grid_size; /* For PME grid tuning */
121 real ewaldcoeff_q; /* For PME grid tuning */
123 } gmx_pme_comm_n_box_t;
133 gmx_stop_cond_t stop_cond;
134 } gmx_pme_comm_vir_ene_t;
139 gmx_pme_pp_t gmx_pme_pp_init(t_commrec gmx_unused *cr)
141 struct gmx_pme_pp *pme_pp;
147 pme_pp->mpi_comm_mysim = cr->mpi_comm_mysim;
148 MPI_Comm_rank(cr->mpi_comm_mygroup, &rank);
149 get_pme_ddnodes(cr, rank, &pme_pp->nnode, &pme_pp->node, &pme_pp->node_peer);
150 snew(pme_pp->nat, pme_pp->nnode);
151 snew(pme_pp->req, eCommType_NR*pme_pp->nnode);
152 snew(pme_pp->stat, eCommType_NR*pme_pp->nnode);
154 pme_pp->flags_charge = 0;
160 /* This should be faster with a real non-blocking MPI implementation */
161 /* #define GMX_PME_DELAYED_WAIT */
163 static void gmx_pme_send_params_coords_wait(gmx_domdec_t gmx_unused *dd)
168 MPI_Waitall(dd->nreq_pme, dd->req_pme, MPI_STATUSES_IGNORE);
174 static void gmx_pme_send_params_coords(t_commrec *cr, int flags,
175 real gmx_unused *chargeA, real gmx_unused *chargeB,
176 real gmx_unused *c6A, real gmx_unused *c6B,
177 real gmx_unused *sigmaA, real gmx_unused *sigmaB,
178 matrix box, rvec gmx_unused *x,
179 real lambda_q, real lambda_lj,
180 int maxshift_x, int maxshift_y,
184 gmx_pme_comm_n_box_t *cnb;
192 fprintf(debug, "PP node %d sending to PME node %d: %d%s%s\n",
193 cr->sim_nodeid, dd->pme_nodeid, n,
194 flags & PP_PME_CHARGE ? " charges" : "",
195 flags & PP_PME_COORD ? " coordinates" : "");
198 #ifdef GMX_PME_DELAYED_WAIT
199 /* When can not use cnb until pending communication has finished */
200 gmx_pme_send_params_coords_wait(dd);
203 if (dd->pme_receive_vir_ener)
205 /* Peer PP node: communicate all data */
214 cnb->maxshift_x = maxshift_x;
215 cnb->maxshift_y = maxshift_y;
216 cnb->lambda_q = lambda_q;
217 cnb->lambda_lj = lambda_lj;
219 if (flags & PP_PME_COORD)
221 copy_mat(box, cnb->box);
224 MPI_Isend(cnb, sizeof(*cnb), MPI_BYTE,
225 dd->pme_nodeid, eCommType_CNB, cr->mpi_comm_mysim,
226 &dd->req_pme[dd->nreq_pme++]);
229 else if (flags & (PP_PME_CHARGE | PP_PME_SQRTC6 | PP_PME_SIGMA))
232 /* Communicate only the number of atoms */
233 MPI_Isend(&n, sizeof(n), MPI_BYTE,
234 dd->pme_nodeid, eCommType_CNB, cr->mpi_comm_mysim,
235 &dd->req_pme[dd->nreq_pme++]);
242 if (flags & PP_PME_CHARGE)
244 MPI_Isend(chargeA, n*sizeof(real), MPI_BYTE,
245 dd->pme_nodeid, eCommType_ChargeA, cr->mpi_comm_mysim,
246 &dd->req_pme[dd->nreq_pme++]);
248 if (flags & PP_PME_CHARGEB)
250 MPI_Isend(chargeB, n*sizeof(real), MPI_BYTE,
251 dd->pme_nodeid, eCommType_ChargeB, cr->mpi_comm_mysim,
252 &dd->req_pme[dd->nreq_pme++]);
254 if (flags & PP_PME_SQRTC6)
256 MPI_Isend(c6A, n*sizeof(real), MPI_BYTE,
257 dd->pme_nodeid, eCommType_SQRTC6A, cr->mpi_comm_mysim,
258 &dd->req_pme[dd->nreq_pme++]);
260 if (flags & PP_PME_SQRTC6B)
262 MPI_Isend(c6B, n*sizeof(real), MPI_BYTE,
263 dd->pme_nodeid, eCommType_SQRTC6B, cr->mpi_comm_mysim,
264 &dd->req_pme[dd->nreq_pme++]);
266 if (flags & PP_PME_SIGMA)
268 MPI_Isend(sigmaA, n*sizeof(real), MPI_BYTE,
269 dd->pme_nodeid, eCommType_SigmaA, cr->mpi_comm_mysim,
270 &dd->req_pme[dd->nreq_pme++]);
272 if (flags & PP_PME_SIGMAB)
274 MPI_Isend(sigmaB, n*sizeof(real), MPI_BYTE,
275 dd->pme_nodeid, eCommType_SigmaB, cr->mpi_comm_mysim,
276 &dd->req_pme[dd->nreq_pme++]);
278 if (flags & PP_PME_COORD)
280 MPI_Isend(x[0], n*sizeof(rvec), MPI_BYTE,
281 dd->pme_nodeid, eCommType_COORD, cr->mpi_comm_mysim,
282 &dd->req_pme[dd->nreq_pme++]);
286 #ifndef GMX_PME_DELAYED_WAIT
287 /* Wait for the data to arrive */
288 /* We can skip this wait as we are sure x and q will not be modified
289 * before the next call to gmx_pme_send_x_q or gmx_pme_receive_f.
291 gmx_pme_send_params_coords_wait(dd);
296 void gmx_pme_send_parameters(t_commrec *cr,
297 gmx_bool bFreeEnergy_q, gmx_bool bFreeEnergy_lj,
298 real *chargeA, real *chargeB,
299 real *sqrt_c6A, real *sqrt_c6B,
300 real *sigmaA, real *sigmaB,
301 int maxshift_x, int maxshift_y)
305 /* We always send the charges, even with only LJ- and no Coulomb-PME */
306 flags = PP_PME_CHARGE;
307 if (sqrt_c6A != NULL)
309 flags |= PP_PME_SQRTC6;
313 flags |= PP_PME_SIGMA;
315 if (bFreeEnergy_q || bFreeEnergy_lj)
317 /* Assumes that the B state flags are in the bits just above
318 * the ones for the A state. */
319 flags |= (flags << 1);
322 gmx_pme_send_params_coords(cr, flags,
324 sqrt_c6A, sqrt_c6B, sigmaA, sigmaB,
325 NULL, NULL, 0, 0, maxshift_x, maxshift_y, -1);
328 void gmx_pme_send_coordinates(t_commrec *cr, matrix box, rvec *x,
329 gmx_bool bFreeEnergy_q, gmx_bool bFreeEnergy_lj,
330 real lambda_q, real lambda_lj,
331 gmx_bool bEnerVir, int pme_flags,
336 flags = pme_flags | PP_PME_COORD;
339 flags |= PP_PME_FEP_Q;
343 flags |= PP_PME_FEP_LJ;
347 flags |= PP_PME_ENER_VIR;
349 gmx_pme_send_params_coords(cr, flags, NULL, NULL, NULL, NULL, NULL, NULL,
350 box, x, lambda_q, lambda_lj, 0, 0, step);
353 void gmx_pme_send_finish(t_commrec *cr)
357 flags = PP_PME_FINISH;
359 gmx_pme_send_params_coords(cr, flags, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, -1);
362 void gmx_pme_send_switchgrid(t_commrec gmx_unused *cr,
363 ivec gmx_unused grid_size,
364 real gmx_unused ewaldcoeff_q,
365 real gmx_unused ewaldcoeff_lj)
368 gmx_pme_comm_n_box_t cnb;
370 /* Only let one PP node signal each PME node */
371 if (cr->dd->pme_receive_vir_ener)
373 cnb.flags = PP_PME_SWITCHGRID;
374 copy_ivec(grid_size, cnb.grid_size);
375 cnb.ewaldcoeff_q = ewaldcoeff_q;
376 cnb.ewaldcoeff_lj = ewaldcoeff_lj;
378 /* We send this, uncommon, message blocking to simplify the code */
379 MPI_Send(&cnb, sizeof(cnb), MPI_BYTE,
380 cr->dd->pme_nodeid, eCommType_CNB, cr->mpi_comm_mysim);
385 void gmx_pme_send_resetcounters(t_commrec gmx_unused *cr, gmx_int64_t gmx_unused step)
388 gmx_pme_comm_n_box_t cnb;
390 /* Only let one PP node signal each PME node */
391 if (cr->dd->pme_receive_vir_ener)
393 cnb.flags = PP_PME_RESETCOUNTERS;
396 /* We send this, uncommon, message blocking to simplify the code */
397 MPI_Send(&cnb, sizeof(cnb), MPI_BYTE,
398 cr->dd->pme_nodeid, eCommType_CNB, cr->mpi_comm_mysim);
403 int gmx_pme_recv_params_coords(struct gmx_pme_pp *pme_pp,
411 matrix gmx_unused box,
414 int gmx_unused *maxshift_x,
415 int gmx_unused *maxshift_y,
416 gmx_bool gmx_unused *bFreeEnergy_q,
417 gmx_bool gmx_unused *bFreeEnergy_lj,
418 real gmx_unused *lambda_q,
419 real gmx_unused *lambda_lj,
420 gmx_bool gmx_unused *bEnerVir,
422 gmx_int64_t gmx_unused *step,
423 ivec gmx_unused grid_size,
424 real gmx_unused *ewaldcoeff_q,
425 real gmx_unused *ewaldcoeff_lj)
427 gmx_pme_comm_n_box_t cnb;
428 int nat = 0, q, messages, sender;
433 /* avoid compiler warning about unused variable without MPI support */
439 /* Receive the send count, box and time step from the peer PP node */
440 MPI_Recv(&cnb, sizeof(cnb), MPI_BYTE,
441 pme_pp->node_peer, eCommType_CNB,
442 pme_pp->mpi_comm_mysim, MPI_STATUS_IGNORE);
446 fprintf(debug, "PME only node receiving:%s%s%s%s%s\n",
447 (cnb.flags & PP_PME_CHARGE) ? " charges" : "",
448 (cnb.flags & PP_PME_COORD ) ? " coordinates" : "",
449 (cnb.flags & PP_PME_FINISH) ? " finish" : "",
450 (cnb.flags & PP_PME_SWITCHGRID) ? " switch grid" : "",
451 (cnb.flags & PP_PME_RESETCOUNTERS) ? " reset counters" : "");
454 if (cnb.flags & PP_PME_SWITCHGRID)
456 /* Special case, receive the new parameters and return */
457 copy_ivec(cnb.grid_size, grid_size);
458 *ewaldcoeff_q = cnb.ewaldcoeff_q;
459 *ewaldcoeff_lj = cnb.ewaldcoeff_lj;
460 return pmerecvqxSWITCHGRID;
463 if (cnb.flags & PP_PME_RESETCOUNTERS)
465 /* Special case, receive the step and return */
468 return pmerecvqxRESETCOUNTERS;
471 if (cnb.flags & (PP_PME_CHARGE | PP_PME_SQRTC6 | PP_PME_SIGMA))
473 /* Receive the send counts from the other PP nodes */
474 for (sender = 0; sender < pme_pp->nnode; sender++)
476 if (pme_pp->node[sender] == pme_pp->node_peer)
478 pme_pp->nat[sender] = cnb.natoms;
482 MPI_Irecv(&(pme_pp->nat[sender]), sizeof(pme_pp->nat[0]),
484 pme_pp->node[sender], eCommType_CNB,
485 pme_pp->mpi_comm_mysim, &pme_pp->req[messages++]);
488 MPI_Waitall(messages, pme_pp->req, pme_pp->stat);
492 for (sender = 0; sender < pme_pp->nnode; sender++)
494 nat += pme_pp->nat[sender];
497 if (nat > pme_pp->nalloc)
499 pme_pp->nalloc = over_alloc_dd(nat);
500 if (cnb.flags & PP_PME_CHARGE)
502 srenew(pme_pp->chargeA, pme_pp->nalloc);
504 if (cnb.flags & PP_PME_CHARGEB)
506 srenew(pme_pp->chargeB, pme_pp->nalloc);
508 if (cnb.flags & PP_PME_SQRTC6)
510 srenew(pme_pp->sqrt_c6A, pme_pp->nalloc);
512 if (cnb.flags & PP_PME_SQRTC6B)
514 srenew(pme_pp->sqrt_c6B, pme_pp->nalloc);
516 if (cnb.flags & PP_PME_SIGMA)
518 srenew(pme_pp->sigmaA, pme_pp->nalloc);
520 if (cnb.flags & PP_PME_SIGMAB)
522 srenew(pme_pp->sigmaB, pme_pp->nalloc);
524 srenew(pme_pp->x, pme_pp->nalloc);
525 srenew(pme_pp->f, pme_pp->nalloc);
528 /* maxshift is sent when the charges are sent */
529 *maxshift_x = cnb.maxshift_x;
530 *maxshift_y = cnb.maxshift_y;
532 /* Receive the charges in place */
533 for (q = 0; q < eCommType_NR; q++)
535 if (!(cnb.flags & (PP_PME_CHARGE<<q)))
541 case eCommType_ChargeA: charge_pp = pme_pp->chargeA; break;
542 case eCommType_ChargeB: charge_pp = pme_pp->chargeB; break;
543 case eCommType_SQRTC6A: charge_pp = pme_pp->sqrt_c6A; break;
544 case eCommType_SQRTC6B: charge_pp = pme_pp->sqrt_c6B; break;
545 case eCommType_SigmaA: charge_pp = pme_pp->sigmaA; break;
546 case eCommType_SigmaB: charge_pp = pme_pp->sigmaB; break;
547 default: gmx_incons("Wrong eCommType");
550 for (sender = 0; sender < pme_pp->nnode; sender++)
552 if (pme_pp->nat[sender] > 0)
554 MPI_Irecv(charge_pp+nat,
555 pme_pp->nat[sender]*sizeof(real),
557 pme_pp->node[sender], q,
558 pme_pp->mpi_comm_mysim,
559 &pme_pp->req[messages++]);
560 nat += pme_pp->nat[sender];
563 fprintf(debug, "Received from PP node %d: %d "
565 pme_pp->node[sender], pme_pp->nat[sender]);
571 pme_pp->flags_charge = cnb.flags;
574 if (cnb.flags & PP_PME_COORD)
576 if (!(pme_pp->flags_charge & (PP_PME_CHARGE | PP_PME_SQRTC6)))
578 gmx_incons("PME-only node received coordinates before charges and/or C6-values"
582 /* The box, FE flag and lambda are sent along with the coordinates
584 copy_mat(cnb.box, box);
585 *bFreeEnergy_q = ((cnb.flags & GMX_PME_DO_COULOMB) &&
586 (cnb.flags & PP_PME_FEP_Q));
587 *bFreeEnergy_lj = ((cnb.flags & GMX_PME_DO_LJ) &&
588 (cnb.flags & PP_PME_FEP_LJ));
589 *lambda_q = cnb.lambda_q;
590 *lambda_lj = cnb.lambda_lj;
591 *bEnerVir = (cnb.flags & PP_PME_ENER_VIR);
592 *pme_flags = cnb.flags;
594 if (*bFreeEnergy_q && !(pme_pp->flags_charge & PP_PME_CHARGEB))
596 gmx_incons("PME-only node received free energy request, but "
597 "did not receive B-state charges");
600 if (*bFreeEnergy_lj && !(pme_pp->flags_charge & PP_PME_SQRTC6B))
602 gmx_incons("PME-only node received free energy request, but "
603 "did not receive B-state C6-values");
606 /* Receive the coordinates in place */
608 for (sender = 0; sender < pme_pp->nnode; sender++)
610 if (pme_pp->nat[sender] > 0)
612 MPI_Irecv(pme_pp->x[nat], pme_pp->nat[sender]*sizeof(rvec),
614 pme_pp->node[sender], eCommType_COORD,
615 pme_pp->mpi_comm_mysim, &pme_pp->req[messages++]);
616 nat += pme_pp->nat[sender];
619 fprintf(debug, "Received from PP node %d: %d "
621 pme_pp->node[sender], pme_pp->nat[sender]);
627 /* Wait for the coordinates and/or charges to arrive */
628 MPI_Waitall(messages, pme_pp->req, pme_pp->stat);
631 while (!(cnb.flags & (PP_PME_COORD | PP_PME_FINISH)));
637 *chargeA = pme_pp->chargeA;
638 *chargeB = pme_pp->chargeB;
639 *sqrt_c6A = pme_pp->sqrt_c6A;
640 *sqrt_c6B = pme_pp->sqrt_c6B;
641 *sigmaA = pme_pp->sigmaA;
642 *sigmaB = pme_pp->sigmaB;
646 return ((cnb.flags & PP_PME_FINISH) ? pmerecvqxFINISH : pmerecvqxX);
649 static void receive_virial_energy(t_commrec *cr,
650 matrix vir_q, real *energy_q,
651 matrix vir_lj, real *energy_lj,
652 real *dvdlambda_q, real *dvdlambda_lj,
655 gmx_pme_comm_vir_ene_t cve;
657 if (cr->dd->pme_receive_vir_ener)
662 "PP node %d receiving from PME node %d: virial and energy\n",
663 cr->sim_nodeid, cr->dd->pme_nodeid);
666 MPI_Recv(&cve, sizeof(cve), MPI_BYTE, cr->dd->pme_nodeid, 1, cr->mpi_comm_mysim,
669 memset(&cve, 0, sizeof(cve));
672 m_add(vir_q, cve.vir_q, vir_q);
673 m_add(vir_lj, cve.vir_lj, vir_lj);
674 *energy_q = cve.energy_q;
675 *energy_lj = cve.energy_lj;
676 *dvdlambda_q += cve.dvdlambda_q;
677 *dvdlambda_lj += cve.dvdlambda_lj;
678 *pme_cycles = cve.cycles;
680 if (cve.stop_cond != gmx_stop_cond_none)
682 gmx_set_stop_condition(cve.stop_cond);
693 void gmx_pme_receive_f(t_commrec *cr,
694 rvec f[], matrix vir_q, real *energy_q,
695 matrix vir_lj, real *energy_lj,
696 real *dvdlambda_q, real *dvdlambda_lj,
701 #ifdef GMX_PME_DELAYED_WAIT
702 /* Wait for the x request to finish */
703 gmx_pme_send_params_coords_wait(cr->dd);
706 natoms = cr->dd->nat_home;
708 if (natoms > cr->dd->pme_recv_f_alloc)
710 cr->dd->pme_recv_f_alloc = over_alloc_dd(natoms);
711 srenew(cr->dd->pme_recv_f_buf, cr->dd->pme_recv_f_alloc);
715 MPI_Recv(cr->dd->pme_recv_f_buf[0],
716 natoms*sizeof(rvec), MPI_BYTE,
717 cr->dd->pme_nodeid, 0, cr->mpi_comm_mysim,
721 for (i = 0; i < natoms; i++)
723 rvec_inc(f[i], cr->dd->pme_recv_f_buf[i]);
727 receive_virial_energy(cr, vir_q, energy_q, vir_lj, energy_lj, dvdlambda_q, dvdlambda_lj, pme_cycles);
730 void gmx_pme_send_force_vir_ener(struct gmx_pme_pp *pme_pp,
732 matrix vir_q, real energy_q,
733 matrix vir_lj, real energy_lj,
734 real dvdlambda_q, real dvdlambda_lj,
737 gmx_pme_comm_vir_ene_t cve;
738 int messages, ind_start, ind_end, receiver;
742 /* Now the evaluated forces have to be transferred to the PP nodes */
745 for (receiver = 0; receiver < pme_pp->nnode; receiver++)
748 ind_end = ind_start + pme_pp->nat[receiver];
750 if (MPI_Isend(f[ind_start], (ind_end-ind_start)*sizeof(rvec), MPI_BYTE,
751 pme_pp->node[receiver], 0,
752 pme_pp->mpi_comm_mysim, &pme_pp->req[messages++]) != 0)
754 gmx_comm("MPI_Isend failed in do_pmeonly");
759 /* send virial and energy to our last PP node */
760 copy_mat(vir_q, cve.vir_q);
761 copy_mat(vir_lj, cve.vir_lj);
762 cve.energy_q = energy_q;
763 cve.energy_lj = energy_lj;
764 cve.dvdlambda_q = dvdlambda_q;
765 cve.dvdlambda_lj = dvdlambda_lj;
766 /* check for the signals to send back to a PP node */
767 cve.stop_cond = gmx_get_stop_condition();
773 fprintf(debug, "PME node sending to PP node %d: virial and energy\n",
777 MPI_Isend(&cve, sizeof(cve), MPI_BYTE,
778 pme_pp->node_peer, 1,
779 pme_pp->mpi_comm_mysim, &pme_pp->req[messages++]);
781 /* Wait for the forces to arrive */
782 MPI_Waitall(messages, pme_pp->req, pme_pp->stat);