2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2006,2007,2008,2009,2010 by the GROMACS development team.
5 * Copyright (c) 2012,2013,2014,2015,2017 by the GROMACS development team.
6 * Copyright (c) 2018,2019,2020, 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.
40 * \brief This file implements functions for domdec to use
41 * while managing inter-atomic constraints.
43 * \author Berk Hess <hess@kth.se>
44 * \ingroup module_domdec
49 #include "domdec_specatomcomm.h"
55 #include "gromacs/domdec/dlb.h"
56 #include "gromacs/domdec/domdec.h"
57 #include "gromacs/domdec/domdec_network.h"
58 #include "gromacs/domdec/domdec_struct.h"
59 #include "gromacs/domdec/ga2la.h"
60 #include "gromacs/domdec/hashedmap.h"
61 #include "gromacs/domdec/partition.h"
62 #include "gromacs/math/vec.h"
63 #include "gromacs/mdtypes/commrec.h"
64 #include "gromacs/pbcutil/ishift.h"
65 #include "gromacs/utility/fatalerror.h"
66 #include "gromacs/utility/gmxassert.h"
68 void dd_move_f_specat(const gmx_domdec_t* dd, gmx_domdec_specat_comm_t* spac, rvec* f, rvec* fshift)
70 gmx_specatsend_t* spas;
72 int n, n0, n1, dim, dir;
75 gmx_bool bPBC, bScrew;
78 for (int d = dd->ndim - 1; d >= 0; d--)
81 if (dd->numCells[dim] > 2)
83 /* Pulse the grid forward and backward */
88 vbuf = as_rvec_array(spac->vbuf.data());
89 /* Send and receive the coordinates */
98 vbuf + spas[0].a.size(),
100 for (dir = 0; dir < 2; dir++)
102 bPBC = ((dir == 0 && dd->ci[dim] == 0)
103 || (dir == 1 && dd->ci[dim] == dd->numCells[dim] - 1));
104 bScrew = (bPBC && dd->unitCellInfo.haveScrewPBC && dim == XX);
106 spas = &spac->spas[d][dir];
107 /* Sum the buffer into the required forces */
108 if (!bPBC || (!bScrew && fshift == nullptr))
110 for (int a : spas->a)
112 rvec_inc(f[a], *vbuf);
119 vis[dim] = (dir == 0 ? 1 : -1);
123 /* Sum and add to shift forces */
124 for (int a : spas->a)
126 rvec_inc(f[a], *vbuf);
127 rvec_inc(fshift[is], *vbuf);
133 /* Rotate the forces */
134 for (int a : spas->a)
136 f[a][XX] += (*vbuf)[XX];
137 f[a][YY] -= (*vbuf)[YY];
138 f[a][ZZ] -= (*vbuf)[ZZ];
141 rvec_inc(fshift[is], *vbuf);
151 /* Two cells, so we only need to communicate one way */
152 spas = &spac->spas[d][0];
154 /* Send and receive the coordinates */
155 ddSendrecv(dd, d, dddirForward, f + n, spas->nrecv, as_rvec_array(spac->vbuf.data()), spas->a.size());
156 /* Sum the buffer into the required forces */
157 if (dd->unitCellInfo.haveScrewPBC && dim == XX
158 && (dd->ci[dim] == 0 || dd->ci[dim] == dd->numCells[dim] - 1))
161 for (int a : spas->a)
163 /* Rotate the force */
164 f[a][XX] += spac->vbuf[i][XX];
165 f[a][YY] -= spac->vbuf[i][YY];
166 f[a][ZZ] -= spac->vbuf[i][ZZ];
173 for (int a : spas->a)
175 rvec_inc(f[a], spac->vbuf[i]);
183 void dd_move_x_specat(const gmx_domdec_t* dd,
184 gmx_domdec_specat_comm_t* spac,
190 gmx_specatsend_t* spas;
191 int nvec, v, n, nn, ns0, ns1, nr0, nr1, nr, d, dim, dir, i;
192 gmx_bool bPBC, bScrew = FALSE;
193 rvec shift = { 0, 0, 0 };
202 for (d = 0; d < dd->ndim; d++)
205 if (dd->numCells[dim] > 2)
207 /* Pulse the grid forward and backward */
208 rvec* vbuf = as_rvec_array(spac->vbuf.data());
209 for (dir = 0; dir < 2; dir++)
211 if (dir == 0 && dd->ci[dim] == 0)
214 bScrew = (dd->unitCellInfo.haveScrewPBC && dim == XX);
215 copy_rvec(box[dim], shift);
217 else if (dir == 1 && dd->ci[dim] == dd->numCells[dim] - 1)
220 bScrew = (dd->unitCellInfo.haveScrewPBC && dim == XX);
221 for (i = 0; i < DIM; i++)
223 shift[i] = -box[dim][i];
231 spas = &spac->spas[d][dir];
232 for (v = 0; v < nvec; v++)
234 rvec* x = (v == 0 ? x0 : x1);
235 /* Copy the required coordinates to the send buffer */
236 if (!bPBC || (v == 1 && !bX1IsCoord))
239 for (int a : spas->a)
241 copy_rvec(x[a], *vbuf);
247 /* Shift coordinates */
248 for (int a : spas->a)
250 rvec_add(x[a], shift, *vbuf);
256 /* Shift and rotate coordinates */
257 for (int a : spas->a)
259 (*vbuf)[XX] = x[a][XX] + shift[XX];
260 (*vbuf)[YY] = box[YY][YY] - x[a][YY] + shift[YY];
261 (*vbuf)[ZZ] = box[ZZ][ZZ] - x[a][ZZ] + shift[ZZ];
267 /* Send and receive the coordinates */
268 spas = spac->spas[d];
269 ns0 = spas[0].a.size();
271 ns1 = spas[1].a.size();
275 rvec* vbuf = as_rvec_array(spac->vbuf.data());
276 dd_sendrecv2_rvec(dd, d, vbuf + ns0, ns1, x0 + n, nr1, vbuf, ns0, x0 + n + nr1, nr0);
280 rvec* vbuf = as_rvec_array(spac->vbuf.data());
281 /* Communicate both vectors in one buffer */
282 rvec* rbuf = as_rvec_array(spac->vbuf2.data());
284 dd, d, vbuf + 2 * ns0, 2 * ns1, rbuf, 2 * nr1, vbuf, 2 * ns0, rbuf + 2 * nr1, 2 * nr0);
285 /* Split the buffer into the two vectors */
287 for (dir = 1; dir >= 0; dir--)
289 nr = spas[dir].nrecv;
290 for (v = 0; v < 2; v++)
292 rvec* x = (v == 0 ? x0 : x1);
293 for (i = 0; i < nr; i++)
295 copy_rvec(*rbuf, x[nn + i]);
306 spas = &spac->spas[d][0];
307 /* Copy the required coordinates to the send buffer */
308 rvec* vbuf = as_rvec_array(spac->vbuf.data());
309 for (v = 0; v < nvec; v++)
311 rvec* x = (v == 0 ? x0 : x1);
312 if (dd->unitCellInfo.haveScrewPBC && dim == XX
313 && (dd->ci[XX] == 0 || dd->ci[XX] == dd->numCells[XX] - 1))
315 /* Here we only perform the rotation, the rest of the pbc
316 * is handled in the constraint or viste routines.
318 for (int a : spas->a)
320 (*vbuf)[XX] = x[a][XX];
321 (*vbuf)[YY] = box[YY][YY] - x[a][YY];
322 (*vbuf)[ZZ] = box[ZZ][ZZ] - x[a][ZZ];
328 for (int a : spas->a)
330 copy_rvec(x[a], *vbuf);
335 /* Send and receive the coordinates */
338 rvec* vbuf = as_rvec_array(spac->vbuf.data());
339 ddSendrecv(dd, d, dddirBackward, vbuf, spas->a.size(), x0 + n, spas->nrecv);
343 rvec* vbuf = as_rvec_array(spac->vbuf.data());
344 /* Communicate both vectors in one buffer */
345 rvec* rbuf = as_rvec_array(spac->vbuf2.data());
346 ddSendrecv(dd, d, dddirBackward, vbuf, 2 * spas->a.size(), rbuf, 2 * spas->nrecv);
347 /* Split the buffer into the two vectors */
349 for (v = 0; v < 2; v++)
351 rvec* x = (v == 0 ? x0 : x1);
352 for (i = 0; i < nr; i++)
354 copy_rvec(*rbuf, x[n + i]);
364 int setup_specat_communication(gmx_domdec_t* dd,
365 std::vector<int>* ireq,
366 gmx_domdec_specat_comm_t* spac,
367 gmx::HashedMap<int>* ga2la_specat,
370 const char* specat_type,
373 int nsend[2], nlast, nsend_zero[2] = { 0, 0 }, *nsend_ptr;
374 int dim, ndir, nr, ns, nrecv_local, n0, start, buf[2];
375 int nat_tot_specat, nat_tot_prev;
377 gmx_specatsend_t* spas;
381 fprintf(debug, "Begin setup_specat_communication for %s\n", specat_type);
384 /* nsend[0]: the number of atoms requested by this node only,
385 * we communicate this for more efficients checks
386 * nsend[1]: the total number of requested atoms
388 const int numRequested = ireq->size();
389 nsend[0] = ireq->size();
392 for (int d = dd->ndim - 1; d >= 0; d--)
394 /* Pulse the grid forward and backward */
396 bPBC = (dim < dd->unitCellInfo.npbcdim);
397 if (dd->numCells[dim] == 2)
399 /* Only 2 cells, so we only need to communicate once */
406 for (int dir = 0; dir < ndir; dir++)
408 if (!bPBC && dd->numCells[dim] > 2
409 && ((dir == 0 && dd->ci[dim] == dd->numCells[dim] - 1) || (dir == 1 && dd->ci[dim] == 0)))
411 /* No pbc: the fist/last cell should not request atoms */
412 nsend_ptr = nsend_zero;
418 /* Communicate the number of indices */
419 ddSendrecv(dd, d, dir == 0 ? dddirForward : dddirBackward, nsend_ptr, 2, spac->nreq[d][dir], 2);
420 nr = spac->nreq[d][dir][1];
421 ireq->resize(nlast + nr);
422 /* Communicate the indices */
425 dir == 0 ? dddirForward : dddirBackward,
428 ireq->data() + nlast,
436 fprintf(debug, "Communicated the counts\n");
439 /* Search for the requested atoms and communicate the indices we have */
440 nat_tot_specat = at_start;
442 for (int d = 0; d < dd->ndim; d++)
444 /* Pulse the grid forward and backward */
445 if (dd->dim[d] >= dd->unitCellInfo.npbcdim || dd->numCells[dd->dim[d]] > 2)
453 nat_tot_prev = nat_tot_specat;
454 for (int dir = ndir - 1; dir >= 0; dir--)
456 /* To avoid cost of clearing by resize(), we only increase size */
457 if (static_cast<size_t>(nat_tot_specat) > spac->sendAtom.size())
459 /* Note: resize initializes new elements to false, which is actually needed here */
460 spac->sendAtom.resize(nat_tot_specat);
462 spas = &spac->spas[d][dir];
463 n0 = spac->nreq[d][dir][0];
464 nr = spac->nreq[d][dir][1];
467 fprintf(debug, "dim=%d, dir=%d, searching for %d atoms\n", d, dir, nr);
473 for (int i = 0; i < nr; i++)
475 const int indr = (*ireq)[start + i];
477 /* Check if this is a home atom and if so ind will be set */
478 if (const int* homeIndex = dd->ga2la->findHome(indr))
484 /* Search in the communicated atoms */
485 if (const int* a = ga2la_specat->find(indr))
496 if (i < n0 || !spac->sendAtom[ind])
498 /* Store the local index so we know which coordinates
501 spas->a.push_back(ind);
502 spac->sendAtom[ind] = true;
503 /* Store the global index so we can send it now */
504 spac->ibuf.push_back(indr);
513 /* Clear the local flags */
514 for (int a : spas->a)
516 spac->sendAtom[a] = false;
518 /* Send and receive the number of indices to communicate */
519 nsend[1] = spas->a.size();
520 ddSendrecv(dd, d, dir == 0 ? dddirBackward : dddirForward, nsend, 2, buf, 2);
524 "Send to rank %d, %d (%d) indices, "
525 "receive from rank %d, %d (%d) indices\n",
526 dd->neighbor[d][1 - dir],
529 dd->neighbor[d][dir],
534 for (int i : spac->ibuf)
536 fprintf(debug, " %d", i + 1);
538 fprintf(debug, "\n");
541 nrecv_local += buf[0];
542 spas->nrecv = buf[1];
543 dd->globalAtomIndices.resize(nat_tot_specat + spas->nrecv);
544 /* Send and receive the indices */
547 dir == 0 ? dddirBackward : dddirForward,
550 dd->globalAtomIndices.data() + nat_tot_specat,
552 nat_tot_specat += spas->nrecv;
555 /* Increase the x/f communication buffer sizes, when necessary */
556 ns = spac->spas[d][0].a.size();
557 nr = spac->spas[d][0].nrecv;
560 ns += spac->spas[d][1].a.size();
561 nr += spac->spas[d][1].nrecv;
563 if (vbuf_fac * ns > gmx::index(spac->vbuf.size()))
565 spac->vbuf.resize(vbuf_fac * ns);
567 if (vbuf_fac == 2 && vbuf_fac * nr > gmx::index(spac->vbuf2.size()))
569 spac->vbuf2.resize(vbuf_fac * nr);
572 /* Make a global to local index for the communication atoms */
573 for (int i = nat_tot_prev; i < nat_tot_specat; i++)
575 ga2la_specat->insert_or_assign(dd->globalAtomIndices[i], i);
579 /* Check that in the end we got the number of atoms we asked for */
580 if (nrecv_local != numRequested)
585 "Requested %d, received %d (tot recv %d)\n",
588 nat_tot_specat - at_start);
591 for (int i = 0; i < numRequested; i++)
593 const int* ind = ga2la_specat->find((*ireq)[i]);
594 fprintf(debug, " %s%d", ind ? "" : "!", (*ireq)[i] + 1);
596 fprintf(debug, "\n");
600 "\nDD cell %d %d %d: Neighboring cells do not have atoms:",
604 for (int i = 0; i < numRequested; i++)
606 if (!ga2la_specat->find((*ireq)[i]))
608 fprintf(stderr, " %d", (*ireq)[i] + 1);
611 fprintf(stderr, "\n");
613 "DD cell %d %d %d could only obtain %d of the %d atoms that are connected via "
614 "%ss from the neighboring cells. This probably means your %s lengths are too "
615 "long compared to the domain decomposition cell size. Decrease the number of "
616 "domain decomposition grid cells%s%s.",
625 dd_dlb_is_on(dd) ? " or use the -rcon option of mdrun" : "");
628 spac->at_start = at_start;
629 spac->at_end = nat_tot_specat;
633 fprintf(debug, "Done setup_specat_communication\n");
636 return nat_tot_specat;