2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2006,2007,2008,2009,2010,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
40 #include "gromacs/legacyheaders/constr.h"
41 #include "gromacs/legacyheaders/domdec.h"
42 #include "gromacs/legacyheaders/domdec_network.h"
43 #include "gromacs/legacyheaders/gmx_ga2la.h"
44 #include "gromacs/legacyheaders/gmx_hash.h"
45 #include "gromacs/legacyheaders/gmx_omp_nthreads.h"
46 #include "gromacs/legacyheaders/macros.h"
47 #include "gromacs/legacyheaders/types/commrec.h"
48 #include "gromacs/math/vec.h"
49 #include "gromacs/pbcutil/ishift.h"
50 #include "gromacs/topology/mtop_util.h"
51 #include "gromacs/utility/fatalerror.h"
52 #include "gromacs/utility/smalloc.h"
67 typedef struct gmx_domdec_specat_comm {
68 /* The number of indices to receive during the setup */
70 /* The atoms to send */
71 gmx_specatsend_t spas[DIM][2];
81 /* The range in the local buffer(s) for received atoms */
85 /* The atom indices we need from the surrounding cells.
86 * We can gather the indices over nthread threads.
90 } gmx_domdec_specat_comm_t;
92 typedef struct gmx_domdec_constraints {
95 /* The fully local and connected constraints */
97 /* The global constraint number, only required for clearing gc_req */
101 /* Boolean that tells if a global constraint index has been requested */
103 /* Global to local communicated constraint atom only index */
106 /* Multi-threading stuff */
109 } gmx_domdec_constraints_t;
112 static void dd_move_f_specat(gmx_domdec_t *dd, gmx_domdec_specat_comm_t *spac,
113 rvec *f, rvec *fshift)
115 gmx_specatsend_t *spas;
117 int n, n0, n1, d, dim, dir, i;
120 gmx_bool bPBC, bScrew;
123 for (d = dd->ndim-1; d >= 0; d--)
128 /* Pulse the grid forward and backward */
129 spas = spac->spas[d];
134 /* Send and receive the coordinates */
135 dd_sendrecv2_rvec(dd, d,
136 f+n+n1, n0, vbuf, spas[0].nsend,
137 f+n, n1, vbuf+spas[0].nsend, spas[1].nsend);
138 for (dir = 0; dir < 2; dir++)
140 bPBC = ((dir == 0 && dd->ci[dim] == 0) ||
141 (dir == 1 && dd->ci[dim] == dd->nc[dim]-1));
142 bScrew = (bPBC && dd->bScrewPBC && dim == XX);
144 spas = &spac->spas[d][dir];
145 /* Sum the buffer into the required forces */
146 if (!bPBC || (!bScrew && fshift == NULL))
148 for (i = 0; i < spas->nsend; i++)
150 rvec_inc(f[spas->a[i]], *vbuf);
157 vis[dim] = (dir == 0 ? 1 : -1);
161 /* Sum and add to shift forces */
162 for (i = 0; i < spas->nsend; i++)
164 rvec_inc(f[spas->a[i]], *vbuf);
165 rvec_inc(fshift[is], *vbuf);
171 /* Rotate the forces */
172 for (i = 0; i < spas->nsend; i++)
174 f[spas->a[i]][XX] += (*vbuf)[XX];
175 f[spas->a[i]][YY] -= (*vbuf)[YY];
176 f[spas->a[i]][ZZ] -= (*vbuf)[ZZ];
179 rvec_inc(fshift[is], *vbuf);
189 /* Two cells, so we only need to communicate one way */
190 spas = &spac->spas[d][0];
192 /* Send and receive the coordinates */
193 dd_sendrecv_rvec(dd, d, dddirForward,
194 f+n, spas->nrecv, spac->vbuf, spas->nsend);
195 /* Sum the buffer into the required forces */
196 if (dd->bScrewPBC && dim == XX &&
198 dd->ci[dim] == dd->nc[dim]-1))
200 for (i = 0; i < spas->nsend; i++)
202 /* Rotate the force */
203 f[spas->a[i]][XX] += spac->vbuf[i][XX];
204 f[spas->a[i]][YY] -= spac->vbuf[i][YY];
205 f[spas->a[i]][ZZ] -= spac->vbuf[i][ZZ];
210 for (i = 0; i < spas->nsend; i++)
212 rvec_inc(f[spas->a[i]], spac->vbuf[i]);
219 void dd_move_f_vsites(gmx_domdec_t *dd, rvec *f, rvec *fshift)
223 dd_move_f_specat(dd, dd->vsite_comm, f, fshift);
227 void dd_clear_f_vsites(gmx_domdec_t *dd, rvec *f)
233 for (i = dd->vsite_comm->at_start; i < dd->vsite_comm->at_end; i++)
240 static void dd_move_x_specat(gmx_domdec_t *dd, gmx_domdec_specat_comm_t *spac,
243 rvec *x1, gmx_bool bX1IsCoord)
245 gmx_specatsend_t *spas;
246 rvec *x, *vbuf, *rbuf;
247 int nvec, v, n, nn, ns0, ns1, nr0, nr1, nr, d, dim, dir, i;
248 gmx_bool bPBC, bScrew = FALSE;
249 rvec shift = {0, 0, 0};
258 for (d = 0; d < dd->ndim; d++)
263 /* Pulse the grid forward and backward */
265 for (dir = 0; dir < 2; dir++)
267 if (dir == 0 && dd->ci[dim] == 0)
270 bScrew = (dd->bScrewPBC && dim == XX);
271 copy_rvec(box[dim], shift);
273 else if (dir == 1 && dd->ci[dim] == dd->nc[dim]-1)
276 bScrew = (dd->bScrewPBC && dim == XX);
277 for (i = 0; i < DIM; i++)
279 shift[i] = -box[dim][i];
287 spas = &spac->spas[d][dir];
288 for (v = 0; v < nvec; v++)
290 x = (v == 0 ? x0 : x1);
291 /* Copy the required coordinates to the send buffer */
292 if (!bPBC || (v == 1 && !bX1IsCoord))
295 for (i = 0; i < spas->nsend; i++)
297 copy_rvec(x[spas->a[i]], *vbuf);
303 /* Shift coordinates */
304 for (i = 0; i < spas->nsend; i++)
306 rvec_add(x[spas->a[i]], shift, *vbuf);
312 /* Shift and rotate coordinates */
313 for (i = 0; i < spas->nsend; i++)
315 (*vbuf)[XX] = x[spas->a[i]][XX] + shift[XX];
316 (*vbuf)[YY] = box[YY][YY] - x[spas->a[i]][YY] + shift[YY];
317 (*vbuf)[ZZ] = box[ZZ][ZZ] - x[spas->a[i]][ZZ] + shift[ZZ];
323 /* Send and receive the coordinates */
324 spas = spac->spas[d];
331 dd_sendrecv2_rvec(dd, d,
332 spac->vbuf+ns0, ns1, x0+n, nr1,
333 spac->vbuf, ns0, x0+n+nr1, nr0);
337 /* Communicate both vectors in one buffer */
339 dd_sendrecv2_rvec(dd, d,
340 spac->vbuf+2*ns0, 2*ns1, rbuf, 2*nr1,
341 spac->vbuf, 2*ns0, rbuf+2*nr1, 2*nr0);
342 /* Split the buffer into the two vectors */
344 for (dir = 1; dir >= 0; dir--)
346 nr = spas[dir].nrecv;
347 for (v = 0; v < 2; v++)
349 x = (v == 0 ? x0 : x1);
350 for (i = 0; i < nr; i++)
352 copy_rvec(*rbuf, x[nn+i]);
363 spas = &spac->spas[d][0];
364 /* Copy the required coordinates to the send buffer */
366 for (v = 0; v < nvec; v++)
368 x = (v == 0 ? x0 : x1);
369 if (dd->bScrewPBC && dim == XX &&
370 (dd->ci[XX] == 0 || dd->ci[XX] == dd->nc[XX]-1))
372 /* Here we only perform the rotation, the rest of the pbc
373 * is handled in the constraint or viste routines.
375 for (i = 0; i < spas->nsend; i++)
377 (*vbuf)[XX] = x[spas->a[i]][XX];
378 (*vbuf)[YY] = box[YY][YY] - x[spas->a[i]][YY];
379 (*vbuf)[ZZ] = box[ZZ][ZZ] - x[spas->a[i]][ZZ];
385 for (i = 0; i < spas->nsend; i++)
387 copy_rvec(x[spas->a[i]], *vbuf);
392 /* Send and receive the coordinates */
395 dd_sendrecv_rvec(dd, d, dddirBackward,
396 spac->vbuf, spas->nsend, x0+n, spas->nrecv);
400 /* Communicate both vectors in one buffer */
402 dd_sendrecv_rvec(dd, d, dddirBackward,
403 spac->vbuf, 2*spas->nsend, rbuf, 2*spas->nrecv);
404 /* Split the buffer into the two vectors */
406 for (v = 0; v < 2; v++)
408 x = (v == 0 ? x0 : x1);
409 for (i = 0; i < nr; i++)
411 copy_rvec(*rbuf, x[n+i]);
421 void dd_move_x_constraints(gmx_domdec_t *dd, matrix box,
422 rvec *x0, rvec *x1, gmx_bool bX1IsCoord)
424 if (dd->constraint_comm)
426 dd_move_x_specat(dd, dd->constraint_comm, box, x0, x1, bX1IsCoord);
430 void dd_move_x_vsites(gmx_domdec_t *dd, matrix box, rvec *x)
434 dd_move_x_specat(dd, dd->vsite_comm, box, x, NULL, FALSE);
438 int *dd_constraints_nlocalatoms(gmx_domdec_t *dd)
442 return dd->constraints->con_nlocat;
450 void dd_clear_local_constraint_indices(gmx_domdec_t *dd)
452 gmx_domdec_constraints_t *dc;
455 dc = dd->constraints;
457 for (i = 0; i < dc->ncon; i++)
459 dc->gc_req[dc->con_gl[i]] = 0;
462 if (dd->constraint_comm)
464 gmx_hash_clear_and_optimize(dc->ga2la);
468 void dd_clear_local_vsite_indices(gmx_domdec_t *dd)
474 gmx_hash_clear_and_optimize(dd->ga2la_vsite);
478 static int setup_specat_communication(gmx_domdec_t *dd,
480 gmx_domdec_specat_comm_t *spac,
481 gmx_hash_t ga2la_specat,
484 const char *specat_type,
487 int nsend[2], nlast, nsend_zero[2] = {0, 0}, *nsend_ptr;
488 int d, dim, ndir, dir, nr, ns, i, nrecv_local, n0, start, indr, ind, buf[2];
489 int nat_tot_specat, nat_tot_prev, nalloc_old;
490 gmx_bool bPBC, bFirst;
491 gmx_specatsend_t *spas;
495 fprintf(debug, "Begin setup_specat_communication for %s\n", specat_type);
498 /* nsend[0]: the number of atoms requested by this node only,
499 * we communicate this for more efficients checks
500 * nsend[1]: the total number of requested atoms
505 for (d = dd->ndim-1; d >= 0; d--)
507 /* Pulse the grid forward and backward */
509 bPBC = (dim < dd->npbcdim);
510 if (dd->nc[dim] == 2)
512 /* Only 2 cells, so we only need to communicate once */
519 for (dir = 0; dir < ndir; dir++)
523 ((dir == 0 && dd->ci[dim] == dd->nc[dim] - 1) ||
524 (dir == 1 && dd->ci[dim] == 0)))
526 /* No pbc: the fist/last cell should not request atoms */
527 nsend_ptr = nsend_zero;
533 /* Communicate the number of indices */
534 dd_sendrecv_int(dd, d, dir == 0 ? dddirForward : dddirBackward,
535 nsend_ptr, 2, spac->nreq[d][dir], 2);
536 nr = spac->nreq[d][dir][1];
537 if (nlast+nr > ireq->nalloc)
539 ireq->nalloc = over_alloc_dd(nlast+nr);
540 srenew(ireq->ind, ireq->nalloc);
542 /* Communicate the indices */
543 dd_sendrecv_int(dd, d, dir == 0 ? dddirForward : dddirBackward,
544 ireq->ind, nsend_ptr[1], ireq->ind+nlast, nr);
551 fprintf(debug, "Communicated the counts\n");
554 /* Search for the requested atoms and communicate the indices we have */
555 nat_tot_specat = at_start;
557 for (d = 0; d < dd->ndim; d++)
560 /* Pulse the grid forward and backward */
561 if (dd->dim[d] >= dd->npbcdim || dd->nc[dd->dim[d]] > 2)
569 nat_tot_prev = nat_tot_specat;
570 for (dir = ndir-1; dir >= 0; dir--)
572 if (nat_tot_specat > spac->bSendAtom_nalloc)
574 nalloc_old = spac->bSendAtom_nalloc;
575 spac->bSendAtom_nalloc = over_alloc_dd(nat_tot_specat);
576 srenew(spac->bSendAtom, spac->bSendAtom_nalloc);
577 for (i = nalloc_old; i < spac->bSendAtom_nalloc; i++)
579 spac->bSendAtom[i] = FALSE;
582 spas = &spac->spas[d][dir];
583 n0 = spac->nreq[d][dir][0];
584 nr = spac->nreq[d][dir][1];
587 fprintf(debug, "dim=%d, dir=%d, searching for %d atoms\n",
593 for (i = 0; i < nr; i++)
595 indr = ireq->ind[start+i];
597 /* Check if this is a home atom and if so ind will be set */
598 if (!ga2la_get_home(dd->ga2la, indr, &ind))
600 /* Search in the communicated atoms */
601 ind = gmx_hash_get_minone(ga2la_specat, indr);
605 if (i < n0 || !spac->bSendAtom[ind])
607 if (spas->nsend+1 > spas->a_nalloc)
609 spas->a_nalloc = over_alloc_large(spas->nsend+1);
610 srenew(spas->a, spas->a_nalloc);
612 /* Store the local index so we know which coordinates
615 spas->a[spas->nsend] = ind;
616 spac->bSendAtom[ind] = TRUE;
617 if (spas->nsend+1 > spac->ibuf_nalloc)
619 spac->ibuf_nalloc = over_alloc_large(spas->nsend+1);
620 srenew(spac->ibuf, spac->ibuf_nalloc);
622 /* Store the global index so we can send it now */
623 spac->ibuf[spas->nsend] = indr;
633 /* Clear the local flags */
634 for (i = 0; i < spas->nsend; i++)
636 spac->bSendAtom[spas->a[i]] = FALSE;
638 /* Send and receive the number of indices to communicate */
639 nsend[1] = spas->nsend;
640 dd_sendrecv_int(dd, d, dir == 0 ? dddirBackward : dddirForward,
644 fprintf(debug, "Send to rank %d, %d (%d) indices, "
645 "receive from rank %d, %d (%d) indices\n",
646 dd->neighbor[d][1-dir], nsend[1], nsend[0],
647 dd->neighbor[d][dir], buf[1], buf[0]);
650 for (i = 0; i < spas->nsend; i++)
652 fprintf(debug, " %d", spac->ibuf[i]+1);
654 fprintf(debug, "\n");
657 nrecv_local += buf[0];
658 spas->nrecv = buf[1];
659 if (nat_tot_specat + spas->nrecv > dd->gatindex_nalloc)
661 dd->gatindex_nalloc =
662 over_alloc_dd(nat_tot_specat + spas->nrecv);
663 srenew(dd->gatindex, dd->gatindex_nalloc);
665 /* Send and receive the indices */
666 dd_sendrecv_int(dd, d, dir == 0 ? dddirBackward : dddirForward,
667 spac->ibuf, spas->nsend,
668 dd->gatindex+nat_tot_specat, spas->nrecv);
669 nat_tot_specat += spas->nrecv;
672 /* Allocate the x/f communication buffers */
673 ns = spac->spas[d][0].nsend;
674 nr = spac->spas[d][0].nrecv;
677 ns += spac->spas[d][1].nsend;
678 nr += spac->spas[d][1].nrecv;
680 if (vbuf_fac*ns > spac->vbuf_nalloc)
682 spac->vbuf_nalloc = over_alloc_dd(vbuf_fac*ns);
683 srenew(spac->vbuf, spac->vbuf_nalloc);
685 if (vbuf_fac == 2 && vbuf_fac*nr > spac->vbuf2_nalloc)
687 spac->vbuf2_nalloc = over_alloc_dd(vbuf_fac*nr);
688 srenew(spac->vbuf2, spac->vbuf2_nalloc);
691 /* Make a global to local index for the communication atoms */
692 for (i = nat_tot_prev; i < nat_tot_specat; i++)
694 gmx_hash_change_or_set(ga2la_specat, dd->gatindex[i], i);
698 /* Check that in the end we got the number of atoms we asked for */
699 if (nrecv_local != ireq->n)
703 fprintf(debug, "Requested %d, received %d (tot recv %d)\n",
704 ireq->n, nrecv_local, nat_tot_specat-at_start);
707 for (i = 0; i < ireq->n; i++)
709 ind = gmx_hash_get_minone(ga2la_specat, ireq->ind[i]);
710 fprintf(debug, " %s%d",
711 (ind >= 0) ? "" : "!",
714 fprintf(debug, "\n");
717 fprintf(stderr, "\nDD cell %d %d %d: Neighboring cells do not have atoms:",
718 dd->ci[XX], dd->ci[YY], dd->ci[ZZ]);
719 for (i = 0; i < ireq->n; i++)
721 if (gmx_hash_get_minone(ga2la_specat, ireq->ind[i]) < 0)
723 fprintf(stderr, " %d", ireq->ind[i]+1);
726 fprintf(stderr, "\n");
727 gmx_fatal(FARGS, "DD cell %d %d %d could only obtain %d of the %d atoms that are connected via %ss from the neighboring cells. This probably means your %s lengths are too long compared to the domain decomposition cell size. Decrease the number of domain decomposition grid cells%s%s.",
728 dd->ci[XX], dd->ci[YY], dd->ci[ZZ],
729 nrecv_local, ireq->n, specat_type,
730 specat_type, add_err,
731 dd->bGridJump ? " or use the -rcon option of mdrun" : "");
734 spac->at_start = at_start;
735 spac->at_end = nat_tot_specat;
739 fprintf(debug, "Done setup_specat_communication\n");
742 return nat_tot_specat;
745 static void walk_out(int con, int con_offset, int a, int offset, int nrec,
746 int ncon1, const t_iatom *ia1, const t_iatom *ia2,
747 const t_blocka *at2con,
748 const gmx_ga2la_t ga2la, gmx_bool bHomeConnect,
749 gmx_domdec_constraints_t *dc,
750 gmx_domdec_specat_comm_t *dcc,
754 int a1_gl, a2_gl, a_loc, i, coni, b;
757 if (dc->gc_req[con_offset+con] == 0)
759 /* Add this non-home constraint to the list */
760 if (dc->ncon+1 > dc->con_nalloc)
762 dc->con_nalloc = over_alloc_large(dc->ncon+1);
763 srenew(dc->con_gl, dc->con_nalloc);
764 srenew(dc->con_nlocat, dc->con_nalloc);
766 dc->con_gl[dc->ncon] = con_offset + con;
767 dc->con_nlocat[dc->ncon] = (bHomeConnect ? 1 : 0);
768 dc->gc_req[con_offset+con] = 1;
769 if (il_local->nr + 3 > il_local->nalloc)
771 il_local->nalloc = over_alloc_dd(il_local->nr+3);
772 srenew(il_local->iatoms, il_local->nalloc);
774 iap = constr_iatomptr(ncon1, ia1, ia2, con);
775 il_local->iatoms[il_local->nr++] = iap[0];
776 a1_gl = offset + iap[1];
777 a2_gl = offset + iap[2];
778 /* The following indexing code can probably be optizimed */
779 if (ga2la_get_home(ga2la, a1_gl, &a_loc))
781 il_local->iatoms[il_local->nr++] = a_loc;
785 /* We set this index later */
786 il_local->iatoms[il_local->nr++] = -a1_gl - 1;
788 if (ga2la_get_home(ga2la, a2_gl, &a_loc))
790 il_local->iatoms[il_local->nr++] = a_loc;
794 /* We set this index later */
795 il_local->iatoms[il_local->nr++] = -a2_gl - 1;
799 /* Check to not ask for the same atom more than once */
800 if (gmx_hash_get_minone(dc->ga2la, offset+a) == -1)
803 /* Add this non-home atom to the list */
804 if (ireq->n+1 > ireq->nalloc)
806 ireq->nalloc = over_alloc_large(ireq->n+1);
807 srenew(ireq->ind, ireq->nalloc);
809 ireq->ind[ireq->n++] = offset + a;
810 /* Temporarily mark with -2, we get the index later */
811 gmx_hash_set(dc->ga2la, offset+a, -2);
816 for (i = at2con->index[a]; i < at2con->index[a+1]; i++)
822 iap = constr_iatomptr(ncon1, ia1, ia2, coni);
831 if (!ga2la_get_home(ga2la, offset+b, &a_loc))
833 walk_out(coni, con_offset, b, offset, nrec-1,
834 ncon1, ia1, ia2, at2con,
835 ga2la, FALSE, dc, dcc, il_local, ireq);
842 static void atoms_to_settles(gmx_domdec_t *dd,
843 const gmx_mtop_t *mtop,
845 const int **at2settle_mt,
846 int cg_start, int cg_end,
851 gmx_mtop_atomlookup_t alook;
854 int cg, a, a_gl, a_glsa, a_gls[3], a_locs[3];
855 int mb, molnr, a_mol, offset;
856 const gmx_molblock_t *molb;
864 alook = gmx_mtop_atomlookup_settle_init(mtop);
866 nral = NRAL(F_SETTLE);
868 for (cg = cg_start; cg < cg_end; cg++)
870 if (GET_CGINFO_SETTLE(cginfo[cg]))
872 for (a = dd->cgindex[cg]; a < dd->cgindex[cg+1]; a++)
874 a_gl = dd->gatindex[a];
876 gmx_mtop_atomnr_to_molblock_ind(alook, a_gl, &mb, &molnr, &a_mol);
877 molb = &mtop->molblock[mb];
879 settle = at2settle_mt[molb->type][a_mol];
883 offset = a_gl - a_mol;
885 ia1 = mtop->moltype[molb->type].ilist[F_SETTLE].iatoms;
889 for (sa = 0; sa < nral; sa++)
891 a_glsa = offset + ia1[settle*(1+nral)+1+sa];
893 a_home[sa] = ga2la_get_home(ga2la, a_glsa, &a_locs[sa]);
896 if (nlocal == 0 && a_gl == a_glsa)
906 if (ils_local->nr+1+nral > ils_local->nalloc)
908 ils_local->nalloc = over_alloc_dd(ils_local->nr+1+nral);
909 srenew(ils_local->iatoms, ils_local->nalloc);
912 ils_local->iatoms[ils_local->nr++] = ia1[settle*4];
914 for (sa = 0; sa < nral; sa++)
916 if (ga2la_get_home(ga2la, a_gls[sa], &a_locs[sa]))
918 ils_local->iatoms[ils_local->nr++] = a_locs[sa];
922 ils_local->iatoms[ils_local->nr++] = -a_gls[sa] - 1;
923 /* Add this non-home atom to the list */
924 if (ireq->n+1 > ireq->nalloc)
926 ireq->nalloc = over_alloc_large(ireq->n+1);
927 srenew(ireq->ind, ireq->nalloc);
929 ireq->ind[ireq->n++] = a_gls[sa];
930 /* A check on double atom requests is
931 * not required for settle.
941 gmx_mtop_atomlookup_destroy(alook);
944 static void atoms_to_constraints(gmx_domdec_t *dd,
945 const gmx_mtop_t *mtop,
947 const t_blocka *at2con_mt, int nrec,
951 const t_blocka *at2con;
953 gmx_mtop_atomlookup_t alook;
955 gmx_molblock_t *molb;
956 t_iatom *ia1, *ia2, *iap;
957 int nhome, cg, a, a_gl, a_mol, a_loc, b_lo, offset, mb, molnr, b_mol, i, con, con_offset;
958 gmx_domdec_constraints_t *dc;
959 gmx_domdec_specat_comm_t *dcc;
961 dc = dd->constraints;
962 dcc = dd->constraint_comm;
966 alook = gmx_mtop_atomlookup_init(mtop);
969 for (cg = 0; cg < dd->ncg_home; cg++)
971 if (GET_CGINFO_CONSTR(cginfo[cg]))
973 for (a = dd->cgindex[cg]; a < dd->cgindex[cg+1]; a++)
975 a_gl = dd->gatindex[a];
977 gmx_mtop_atomnr_to_molblock_ind(alook, a_gl, &mb, &molnr, &a_mol);
978 molb = &mtop->molblock[mb];
980 ncon1 = mtop->moltype[molb->type].ilist[F_CONSTR].nr/NRAL(F_SETTLE);
982 ia1 = mtop->moltype[molb->type].ilist[F_CONSTR].iatoms;
983 ia2 = mtop->moltype[molb->type].ilist[F_CONSTRNC].iatoms;
985 /* Calculate the global constraint number offset for the molecule.
986 * This is only required for the global index to make sure
987 * that we use each constraint only once.
990 dc->molb_con_offset[mb] + molnr*dc->molb_ncon_mol[mb];
992 /* The global atom number offset for this molecule */
993 offset = a_gl - a_mol;
994 at2con = &at2con_mt[molb->type];
995 for (i = at2con->index[a_mol]; i < at2con->index[a_mol+1]; i++)
998 iap = constr_iatomptr(ncon1, ia1, ia2, con);
1007 if (ga2la_get_home(ga2la, offset+b_mol, &a_loc))
1009 /* Add this fully home constraint at the first atom */
1012 if (dc->ncon+1 > dc->con_nalloc)
1014 dc->con_nalloc = over_alloc_large(dc->ncon+1);
1015 srenew(dc->con_gl, dc->con_nalloc);
1016 srenew(dc->con_nlocat, dc->con_nalloc);
1018 dc->con_gl[dc->ncon] = con_offset + con;
1019 dc->con_nlocat[dc->ncon] = 2;
1020 if (ilc_local->nr + 3 > ilc_local->nalloc)
1022 ilc_local->nalloc = over_alloc_dd(ilc_local->nr + 3);
1023 srenew(ilc_local->iatoms, ilc_local->nalloc);
1026 ilc_local->iatoms[ilc_local->nr++] = iap[0];
1027 ilc_local->iatoms[ilc_local->nr++] = (a_gl == iap[1] ? a : b_lo);
1028 ilc_local->iatoms[ilc_local->nr++] = (a_gl == iap[1] ? b_lo : a );
1035 /* We need the nrec constraints coupled to this constraint,
1036 * so we need to walk out of the home cell by nrec+1 atoms,
1037 * since already atom bg is not locally present.
1038 * Therefore we call walk_out with nrec recursions to go
1039 * after this first call.
1041 walk_out(con, con_offset, b_mol, offset, nrec,
1042 ncon1, ia1, ia2, at2con,
1043 dd->ga2la, TRUE, dc, dcc, ilc_local, ireq);
1050 gmx_mtop_atomlookup_destroy(alook);
1055 "Constraints: home %3d border %3d atoms: %3d\n",
1056 nhome, dc->ncon-nhome,
1057 dd->constraint_comm ? ireq->n : 0);
1061 int dd_make_local_constraints(gmx_domdec_t *dd, int at_start,
1062 const gmx_mtop_t *mtop,
1064 gmx_constr_t constr, int nrec,
1067 gmx_domdec_constraints_t *dc;
1068 t_ilist *ilc_local, *ils_local;
1070 const t_blocka *at2con_mt;
1071 const int **at2settle_mt;
1072 gmx_hash_t ga2la_specat;
1076 dc = dd->constraints;
1078 ilc_local = &il_local[F_CONSTR];
1079 ils_local = &il_local[F_SETTLE];
1083 if (dd->constraint_comm)
1085 at2con_mt = atom2constraints_moltype(constr);
1086 ireq = &dd->constraint_comm->ireq[0];
1095 if (dd->bInterCGsettles)
1097 at2settle_mt = atom2settle_moltype(constr);
1102 /* Settle works inside charge groups, we assigned them already */
1103 at2settle_mt = NULL;
1106 if (at2settle_mt == NULL)
1108 atoms_to_constraints(dd, mtop, cginfo, at2con_mt, nrec,
1116 /* Do the constraints, if present, on the first thread.
1117 * Do the settles on all other threads.
1119 t0_set = ((at2con_mt != NULL && dc->nthread > 1) ? 1 : 0);
1121 #pragma omp parallel for num_threads(dc->nthread) schedule(static)
1122 for (thread = 0; thread < dc->nthread; thread++)
1124 if (at2con_mt && thread == 0)
1126 atoms_to_constraints(dd, mtop, cginfo, at2con_mt, nrec,
1130 if (thread >= t0_set)
1136 /* Distribute the settle check+assignments over
1137 * dc->nthread or dc->nthread-1 threads.
1139 cg0 = (dd->ncg_home*(thread-t0_set ))/(dc->nthread-t0_set);
1140 cg1 = (dd->ncg_home*(thread-t0_set+1))/(dc->nthread-t0_set);
1142 if (thread == t0_set)
1148 ilst = &dc->ils[thread];
1152 ireqt = &dd->constraint_comm->ireq[thread];
1158 atoms_to_settles(dd, mtop, cginfo, at2settle_mt,
1164 /* Combine the generate settles and requested indices */
1165 for (thread = 1; thread < dc->nthread; thread++)
1171 if (thread > t0_set)
1173 ilst = &dc->ils[thread];
1174 if (ils_local->nr + ilst->nr > ils_local->nalloc)
1176 ils_local->nalloc = over_alloc_large(ils_local->nr + ilst->nr);
1177 srenew(ils_local->iatoms, ils_local->nalloc);
1179 for (ia = 0; ia < ilst->nr; ia++)
1181 ils_local->iatoms[ils_local->nr+ia] = ilst->iatoms[ia];
1183 ils_local->nr += ilst->nr;
1186 ireqt = &dd->constraint_comm->ireq[thread];
1187 if (ireq->n+ireqt->n > ireq->nalloc)
1189 ireq->nalloc = over_alloc_large(ireq->n+ireqt->n);
1190 srenew(ireq->ind, ireq->nalloc);
1192 for (ia = 0; ia < ireqt->n; ia++)
1194 ireq->ind[ireq->n+ia] = ireqt->ind[ia];
1196 ireq->n += ireqt->n;
1201 fprintf(debug, "Settles: total %3d\n", ils_local->nr/4);
1205 if (dd->constraint_comm)
1210 setup_specat_communication(dd, ireq, dd->constraint_comm,
1211 dd->constraints->ga2la,
1213 "constraint", " or lincs-order");
1215 /* Fill in the missing indices */
1216 ga2la_specat = dd->constraints->ga2la;
1218 nral1 = 1 + NRAL(F_CONSTR);
1219 for (i = 0; i < ilc_local->nr; i += nral1)
1221 iap = ilc_local->iatoms + i;
1222 for (j = 1; j < nral1; j++)
1226 iap[j] = gmx_hash_get_minone(ga2la_specat, -iap[j]-1);
1231 nral1 = 1 + NRAL(F_SETTLE);
1232 for (i = 0; i < ils_local->nr; i += nral1)
1234 iap = ils_local->iatoms + i;
1235 for (j = 1; j < nral1; j++)
1239 iap[j] = gmx_hash_get_minone(ga2la_specat, -iap[j]-1);
1252 int dd_make_local_vsites(gmx_domdec_t *dd, int at_start, t_ilist *lil)
1254 gmx_domdec_specat_comm_t *spac;
1256 gmx_hash_t ga2la_specat;
1257 int ftype, nral, i, j, gat, a;
1262 spac = dd->vsite_comm;
1263 ireq = &spac->ireq[0];
1264 ga2la_specat = dd->ga2la_vsite;
1267 /* Loop over all the home vsites */
1268 for (ftype = 0; ftype < F_NRE; ftype++)
1270 if (interaction_function[ftype].flags & IF_VSITE)
1274 for (i = 0; i < lilf->nr; i += 1+nral)
1276 iatoms = lilf->iatoms + i;
1277 /* Check if we have the other atoms */
1278 for (j = 1; j < 1+nral; j++)
1282 /* This is not a home atom,
1283 * we need to ask our neighbors.
1286 /* Check to not ask for the same atom more than once */
1287 if (gmx_hash_get_minone(dd->ga2la_vsite, a) == -1)
1289 /* Add this non-home atom to the list */
1290 if (ireq->n+1 > ireq->nalloc)
1292 ireq->nalloc = over_alloc_large(ireq->n+1);
1293 srenew(ireq->ind, ireq->nalloc);
1295 ireq->ind[ireq->n++] = a;
1296 /* Temporarily mark with -2,
1297 * we get the index later.
1299 gmx_hash_set(ga2la_specat, a, -2);
1307 at_end = setup_specat_communication(dd, ireq, dd->vsite_comm, ga2la_specat,
1308 at_start, 1, "vsite", "");
1310 /* Fill in the missing indices */
1311 for (ftype = 0; ftype < F_NRE; ftype++)
1313 if (interaction_function[ftype].flags & IF_VSITE)
1317 for (i = 0; i < lilf->nr; i += 1+nral)
1319 iatoms = lilf->iatoms + i;
1320 for (j = 1; j < 1+nral; j++)
1324 iatoms[j] = gmx_hash_get_minone(ga2la_specat, -iatoms[j]-1);
1334 static gmx_domdec_specat_comm_t *specat_comm_init(int nthread)
1336 gmx_domdec_specat_comm_t *spac;
1339 spac->nthread = nthread;
1340 snew(spac->ireq, spac->nthread);
1345 void init_domdec_constraints(gmx_domdec_t *dd,
1348 gmx_domdec_constraints_t *dc;
1349 gmx_molblock_t *molb;
1354 fprintf(debug, "Begin init_domdec_constraints\n");
1357 snew(dd->constraints, 1);
1358 dc = dd->constraints;
1360 snew(dc->molb_con_offset, mtop->nmolblock);
1361 snew(dc->molb_ncon_mol, mtop->nmolblock);
1364 for (mb = 0; mb < mtop->nmolblock; mb++)
1366 molb = &mtop->molblock[mb];
1367 dc->molb_con_offset[mb] = ncon;
1368 dc->molb_ncon_mol[mb] =
1369 mtop->moltype[molb->type].ilist[F_CONSTR].nr/3 +
1370 mtop->moltype[molb->type].ilist[F_CONSTRNC].nr/3;
1371 ncon += molb->nmol*dc->molb_ncon_mol[mb];
1376 snew(dc->gc_req, ncon);
1377 for (c = 0; c < ncon; c++)
1383 /* Use a hash table for the global to local index.
1384 * The number of keys is a rough estimate, it will be optimized later.
1386 dc->ga2la = gmx_hash_init(min(mtop->natoms/20,
1387 mtop->natoms/(2*dd->nnodes)));
1389 dc->nthread = gmx_omp_nthreads_get(emntDomdec);
1390 snew(dc->ils, dc->nthread);
1392 dd->constraint_comm = specat_comm_init(dc->nthread);
1395 void init_domdec_vsites(gmx_domdec_t *dd, int n_intercg_vsite)
1398 gmx_domdec_constraints_t *dc;
1402 fprintf(debug, "Begin init_domdec_vsites\n");
1405 /* Use a hash table for the global to local index.
1406 * The number of keys is a rough estimate, it will be optimized later.
1408 dd->ga2la_vsite = gmx_hash_init(min(n_intercg_vsite/20,
1409 n_intercg_vsite/(2*dd->nnodes)));
1411 dd->vsite_comm = specat_comm_init(1);