1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROwing Monsters And Cloning Shrimps
47 #include "gmx_fatal.h"
52 #include "sighandler.h"
54 #include "gromacs/utility/gmxmpi.h"
56 #define PP_PME_CHARGE (1<<0)
57 #define PP_PME_CHARGEB (1<<1)
58 #define PP_PME_COORD (1<<2)
59 #define PP_PME_FEP (1<<3)
60 #define PP_PME_ENER_VIR (1<<4)
61 #define PP_PME_FINISH (1<<5)
62 #define PP_PME_SWITCHGRID (1<<6)
63 #define PP_PME_RESETCOUNTERS (1<<7)
66 #define PME_PP_SIGSTOP (1<<0)
67 #define PME_PP_SIGSTOPNSS (1<<1)
69 typedef struct gmx_pme_pp {
71 MPI_Comm mpi_comm_mysim;
73 int nnode; /* The number of PP node to communicate with */
74 int *node; /* The PP node ranks */
75 int node_peer; /* The peer PP node rank */
76 int *nat; /* The number of atom for each PP node */
77 int flags_charge; /* The flags sent along with the last charges */
89 typedef struct gmx_pme_comm_n_box {
97 ivec grid_size; /* For PME grid tuning */
98 real ewaldcoeff; /* For PME grid tuning */
99 } gmx_pme_comm_n_box_t;
106 gmx_stop_cond_t stop_cond;
107 } gmx_pme_comm_vir_ene_t;
112 gmx_pme_pp_t gmx_pme_pp_init(t_commrec *cr)
114 struct gmx_pme_pp *pme_pp;
120 pme_pp->mpi_comm_mysim = cr->mpi_comm_mysim;
121 MPI_Comm_rank(cr->mpi_comm_mygroup, &rank);
122 get_pme_ddnodes(cr, rank, &pme_pp->nnode, &pme_pp->node, &pme_pp->node_peer);
123 snew(pme_pp->nat, pme_pp->nnode);
124 snew(pme_pp->req, 2*pme_pp->nnode);
125 snew(pme_pp->stat, 2*pme_pp->nnode);
127 pme_pp->flags_charge = 0;
133 /* This should be faster with a real non-blocking MPI implementation */
134 /* #define GMX_PME_DELAYED_WAIT */
136 static void gmx_pme_send_q_x_wait(gmx_domdec_t *dd)
141 MPI_Waitall(dd->nreq_pme, dd->req_pme, MPI_STATUSES_IGNORE);
147 static void gmx_pme_send_q_x(t_commrec *cr, int flags,
148 real *chargeA, real *chargeB,
151 int maxshift_x, int maxshift_y,
152 gmx_large_int_t step)
155 gmx_pme_comm_n_box_t *cnb;
163 fprintf(debug, "PP node %d sending to PME node %d: %d%s%s\n",
164 cr->sim_nodeid, dd->pme_nodeid, n,
165 flags & PP_PME_CHARGE ? " charges" : "",
166 flags & PP_PME_COORD ? " coordinates" : "");
169 #ifdef GMX_PME_DELAYED_WAIT
170 /* When can not use cnb until pending communication has finished */
171 gmx_pme_send_x_q_wait(dd);
174 if (dd->pme_receive_vir_ener)
176 /* Peer PP node: communicate all data */
185 cnb->maxshift_x = maxshift_x;
186 cnb->maxshift_y = maxshift_y;
187 cnb->lambda = lambda;
189 if (flags & PP_PME_COORD)
191 copy_mat(box, cnb->box);
194 MPI_Isend(cnb, sizeof(*cnb), MPI_BYTE,
195 dd->pme_nodeid, 0, cr->mpi_comm_mysim,
196 &dd->req_pme[dd->nreq_pme++]);
199 else if (flags & PP_PME_CHARGE)
202 /* Communicate only the number of atoms */
203 MPI_Isend(&n, sizeof(n), MPI_BYTE,
204 dd->pme_nodeid, 0, cr->mpi_comm_mysim,
205 &dd->req_pme[dd->nreq_pme++]);
212 if (flags & PP_PME_CHARGE)
214 MPI_Isend(chargeA, n*sizeof(real), MPI_BYTE,
215 dd->pme_nodeid, 1, cr->mpi_comm_mysim,
216 &dd->req_pme[dd->nreq_pme++]);
218 if (flags & PP_PME_CHARGEB)
220 MPI_Isend(chargeB, n*sizeof(real), MPI_BYTE,
221 dd->pme_nodeid, 2, cr->mpi_comm_mysim,
222 &dd->req_pme[dd->nreq_pme++]);
224 if (flags & PP_PME_COORD)
226 MPI_Isend(x[0], n*sizeof(rvec), MPI_BYTE,
227 dd->pme_nodeid, 3, cr->mpi_comm_mysim,
228 &dd->req_pme[dd->nreq_pme++]);
232 #ifndef GMX_PME_DELAYED_WAIT
233 /* Wait for the data to arrive */
234 /* We can skip this wait as we are sure x and q will not be modified
235 * before the next call to gmx_pme_send_x_q or gmx_pme_receive_f.
237 gmx_pme_send_q_x_wait(dd);
242 void gmx_pme_send_q(t_commrec *cr,
243 gmx_bool bFreeEnergy, real *chargeA, real *chargeB,
244 int maxshift_x, int maxshift_y)
248 flags = PP_PME_CHARGE;
251 flags |= PP_PME_CHARGEB;
254 gmx_pme_send_q_x(cr, flags,
255 chargeA, chargeB, NULL, NULL, 0, maxshift_x, maxshift_y, -1);
258 void gmx_pme_send_x(t_commrec *cr, matrix box, rvec *x,
259 gmx_bool bFreeEnergy, real lambda,
261 gmx_large_int_t step)
265 flags = PP_PME_COORD;
272 flags |= PP_PME_ENER_VIR;
275 gmx_pme_send_q_x(cr, flags, NULL, NULL, box, x, lambda, 0, 0, step);
278 void gmx_pme_send_finish(t_commrec *cr)
282 flags = PP_PME_FINISH;
284 gmx_pme_send_q_x(cr, flags, NULL, NULL, NULL, NULL, 0, 0, 0, -1);
287 void gmx_pme_send_switchgrid(t_commrec *cr, ivec grid_size, real ewaldcoeff)
290 gmx_pme_comm_n_box_t cnb;
292 /* Only let one PP node signal each PME node */
293 if (cr->dd->pme_receive_vir_ener)
295 cnb.flags = PP_PME_SWITCHGRID;
296 copy_ivec(grid_size, cnb.grid_size);
297 cnb.ewaldcoeff = ewaldcoeff;
299 /* We send this, uncommon, message blocking to simplify the code */
300 MPI_Send(&cnb, sizeof(cnb), MPI_BYTE,
301 cr->dd->pme_nodeid, 0, cr->mpi_comm_mysim);
306 void gmx_pme_send_resetcounters(t_commrec *cr, gmx_large_int_t step)
309 gmx_pme_comm_n_box_t cnb;
311 /* Only let one PP node signal each PME node */
312 if (cr->dd->pme_receive_vir_ener)
314 cnb.flags = PP_PME_RESETCOUNTERS;
317 /* We send this, uncommon, message blocking to simplify the code */
318 MPI_Send(&cnb, sizeof(cnb), MPI_BYTE,
319 cr->dd->pme_nodeid, 0, cr->mpi_comm_mysim);
324 int gmx_pme_recv_q_x(struct gmx_pme_pp *pme_pp,
326 real **chargeA, real **chargeB,
327 matrix box, rvec **x, rvec **f,
328 int *maxshift_x, int *maxshift_y,
329 gmx_bool *bFreeEnergy, real *lambda,
331 gmx_large_int_t *step,
332 ivec grid_size, real *ewaldcoeff)
334 gmx_pme_comm_n_box_t cnb;
335 int nat = 0, q, messages, sender;
340 /* avoid compiler warning about unused variable without MPI support */
345 /* Receive the send count, box and time step from the peer PP node */
346 MPI_Recv(&cnb, sizeof(cnb), MPI_BYTE,
347 pme_pp->node_peer, 0,
348 pme_pp->mpi_comm_mysim, MPI_STATUS_IGNORE);
352 fprintf(debug, "PME only node receiving:%s%s%s%s%s\n",
353 (cnb.flags & PP_PME_CHARGE) ? " charges" : "",
354 (cnb.flags & PP_PME_COORD ) ? " coordinates" : "",
355 (cnb.flags & PP_PME_FINISH) ? " finish" : "",
356 (cnb.flags & PP_PME_SWITCHGRID) ? " switch grid" : "",
357 (cnb.flags & PP_PME_RESETCOUNTERS) ? " reset counters" : "");
360 if (cnb.flags & PP_PME_SWITCHGRID)
362 /* Special case, receive the new parameters and return */
363 copy_ivec(cnb.grid_size, grid_size);
364 *ewaldcoeff = cnb.ewaldcoeff;
366 return pmerecvqxSWITCHGRID;
369 if (cnb.flags & PP_PME_RESETCOUNTERS)
371 /* Special case, receive the step and return */
374 return pmerecvqxRESETCOUNTERS;
377 if (cnb.flags & PP_PME_CHARGE)
379 /* Receive the send counts from the other PP nodes */
380 for (sender = 0; sender < pme_pp->nnode; sender++)
382 if (pme_pp->node[sender] == pme_pp->node_peer)
384 pme_pp->nat[sender] = cnb.natoms;
388 MPI_Irecv(&(pme_pp->nat[sender]), sizeof(pme_pp->nat[0]),
390 pme_pp->node[sender], 0,
391 pme_pp->mpi_comm_mysim, &pme_pp->req[messages++]);
394 MPI_Waitall(messages, pme_pp->req, pme_pp->stat);
398 for (sender = 0; sender < pme_pp->nnode; sender++)
400 nat += pme_pp->nat[sender];
403 if (nat > pme_pp->nalloc)
405 pme_pp->nalloc = over_alloc_dd(nat);
406 srenew(pme_pp->chargeA, pme_pp->nalloc);
407 if (cnb.flags & PP_PME_CHARGEB)
409 srenew(pme_pp->chargeB, pme_pp->nalloc);
411 srenew(pme_pp->x, pme_pp->nalloc);
412 srenew(pme_pp->f, pme_pp->nalloc);
415 /* maxshift is sent when the charges are sent */
416 *maxshift_x = cnb.maxshift_x;
417 *maxshift_y = cnb.maxshift_y;
419 /* Receive the charges in place */
420 for (q = 0; q < ((cnb.flags & PP_PME_CHARGEB) ? 2 : 1); q++)
424 charge_pp = pme_pp->chargeA;
428 charge_pp = pme_pp->chargeB;
431 for (sender = 0; sender < pme_pp->nnode; sender++)
433 if (pme_pp->nat[sender] > 0)
435 MPI_Irecv(charge_pp+nat,
436 pme_pp->nat[sender]*sizeof(real),
438 pme_pp->node[sender], 1+q,
439 pme_pp->mpi_comm_mysim,
440 &pme_pp->req[messages++]);
441 nat += pme_pp->nat[sender];
444 fprintf(debug, "Received from PP node %d: %d "
446 pme_pp->node[sender], pme_pp->nat[sender]);
452 pme_pp->flags_charge = cnb.flags;
455 if (cnb.flags & PP_PME_COORD)
457 if (!(pme_pp->flags_charge & PP_PME_CHARGE))
459 gmx_incons("PME-only node received coordinates before charges"
463 /* The box, FE flag and lambda are sent along with the coordinates
465 copy_mat(cnb.box, box);
466 *bFreeEnergy = (cnb.flags & PP_PME_FEP);
467 *lambda = cnb.lambda;
468 *bEnerVir = (cnb.flags & PP_PME_ENER_VIR);
470 if (*bFreeEnergy && !(pme_pp->flags_charge & PP_PME_CHARGEB))
472 gmx_incons("PME-only node received free energy request, but "
473 "did not receive B-state charges");
476 /* Receive the coordinates in place */
478 for (sender = 0; sender < pme_pp->nnode; sender++)
480 if (pme_pp->nat[sender] > 0)
482 MPI_Irecv(pme_pp->x[nat], pme_pp->nat[sender]*sizeof(rvec),
484 pme_pp->node[sender], 3,
485 pme_pp->mpi_comm_mysim, &pme_pp->req[messages++]);
486 nat += pme_pp->nat[sender];
489 fprintf(debug, "Received from PP node %d: %d "
491 pme_pp->node[sender], pme_pp->nat[sender]);
497 /* Wait for the coordinates and/or charges to arrive */
498 MPI_Waitall(messages, pme_pp->req, pme_pp->stat);
501 while (!(cnb.flags & (PP_PME_COORD | PP_PME_FINISH)));
507 *chargeA = pme_pp->chargeA;
508 *chargeB = pme_pp->chargeB;
512 return ((cnb.flags & PP_PME_FINISH) ? pmerecvqxFINISH : pmerecvqxX);
515 static void receive_virial_energy(t_commrec *cr,
516 matrix vir, real *energy, real *dvdlambda,
519 gmx_pme_comm_vir_ene_t cve;
521 if (cr->dd->pme_receive_vir_ener)
526 "PP node %d receiving from PME node %d: virial and energy\n",
527 cr->sim_nodeid, cr->dd->pme_nodeid);
530 MPI_Recv(&cve, sizeof(cve), MPI_BYTE, cr->dd->pme_nodeid, 1, cr->mpi_comm_mysim,
533 memset(&cve, 0, sizeof(cve));
536 m_add(vir, cve.vir, vir);
537 *energy = cve.energy;
538 *dvdlambda += cve.dvdlambda;
539 *pme_cycles = cve.cycles;
541 if (cve.stop_cond != gmx_stop_cond_none)
543 gmx_set_stop_condition(cve.stop_cond);
553 void gmx_pme_receive_f(t_commrec *cr,
554 rvec f[], matrix vir,
555 real *energy, real *dvdlambda,
560 #ifdef GMX_PME_DELAYED_WAIT
561 /* Wait for the x request to finish */
562 gmx_pme_send_q_x_wait(cr->dd);
565 natoms = cr->dd->nat_home;
567 if (natoms > cr->dd->pme_recv_f_alloc)
569 cr->dd->pme_recv_f_alloc = over_alloc_dd(natoms);
570 srenew(cr->dd->pme_recv_f_buf, cr->dd->pme_recv_f_alloc);
574 MPI_Recv(cr->dd->pme_recv_f_buf[0],
575 natoms*sizeof(rvec), MPI_BYTE,
576 cr->dd->pme_nodeid, 0, cr->mpi_comm_mysim,
580 for (i = 0; i < natoms; i++)
582 rvec_inc(f[i], cr->dd->pme_recv_f_buf[i]);
586 receive_virial_energy(cr, vir, energy, dvdlambda, pme_cycles);
589 void gmx_pme_send_force_vir_ener(struct gmx_pme_pp *pme_pp,
591 real energy, real dvdlambda,
594 gmx_pme_comm_vir_ene_t cve;
595 int messages, ind_start, ind_end, receiver;
599 /* Now the evaluated forces have to be transferred to the PP nodes */
602 for (receiver = 0; receiver < pme_pp->nnode; receiver++)
605 ind_end = ind_start + pme_pp->nat[receiver];
607 if (MPI_Isend(f[ind_start], (ind_end-ind_start)*sizeof(rvec), MPI_BYTE,
608 pme_pp->node[receiver], 0,
609 pme_pp->mpi_comm_mysim, &pme_pp->req[messages++]) != 0)
611 gmx_comm("MPI_Isend failed in do_pmeonly");
616 /* send virial and energy to our last PP node */
617 copy_mat(vir, cve.vir);
619 cve.dvdlambda = dvdlambda;
620 /* check for the signals to send back to a PP node */
621 cve.stop_cond = gmx_get_stop_condition();
627 fprintf(debug, "PME node sending to PP node %d: virial and energy\n",
631 MPI_Isend(&cve, sizeof(cve), MPI_BYTE,
632 pme_pp->node_peer, 1,
633 pme_pp->mpi_comm_mysim, &pme_pp->req[messages++]);
635 /* Wait for the forces to arrive */
636 MPI_Waitall(messages, pme_pp->req, pme_pp->stat);