2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018, 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.
37 * \brief Implements DD cell-size related functions.
39 * \author Berk Hess <hess@kth.se>
40 * \author Roland Schulz <roland.schulz@intel.com>
41 * \ingroup module_domdec
46 #include "cellsizes.h"
50 #include "gromacs/gmxlib/network.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/mdtypes/commrec.h"
53 #include "gromacs/utility/cstringutil.h"
54 #include "gromacs/utility/fatalerror.h"
56 #include "atomdistribution.h"
57 #include "domdec_internal.h"
60 static void set_pme_maxshift(gmx_domdec_t *dd, gmx_ddpme_t *ddpme,
61 gmx_bool bUniform, const gmx_ddbox_t *ddbox,
64 gmx_domdec_comm_t *comm;
67 real range, pme_boundary;
71 nc = dd->nc[ddpme->dim];
74 if (!ddpme->dim_match)
76 /* PP decomposition is not along dim: the worst situation */
79 else if (ns <= 3 || (bUniform && ns == nc))
81 /* The optimal situation */
86 /* We need to check for all pme nodes which nodes they
87 * could possibly need to communicate with.
91 /* Allow for atoms to be maximally 2/3 times the cut-off
92 * out of their DD cell. This is a reasonable balance between
93 * between performance and support for most charge-group/cut-off
96 range = 2.0/3.0*comm->cutoff/ddbox->box_size[ddpme->dim];
97 /* Avoid extra communication when we are exactly at a boundary */
101 for (s = 0; s < ns; s++)
103 /* PME slab s spreads atoms between box frac. s/ns and (s+1)/ns */
104 pme_boundary = (real)s/ns;
105 while (sh + 1 < ns &&
106 ((s - (sh + 1) >= 0 &&
107 cellFrac[xmax[s - (sh + 1) ] + 1] + range > pme_boundary) ||
109 cellFrac[xmax[s - (sh + 1) + ns] + 1] - 1 + range > pme_boundary)))
113 pme_boundary = (real)(s+1)/ns;
114 while (sh + 1 < ns &&
115 ((s + (sh + 1) < ns &&
116 cellFrac[xmin[s + (sh + 1) ] ] - range < pme_boundary) ||
117 (s + (sh + 1) >= ns &&
118 cellFrac[xmin[s +(sh + 1) - ns] ] + 1 - range < pme_boundary)))
125 ddpme->maxshift = sh;
129 fprintf(debug, "PME slab communication range for dim %d is %d\n",
130 ddpme->dim, ddpme->maxshift);
134 static void check_box_size(const gmx_domdec_t *dd,
135 const gmx_ddbox_t *ddbox)
139 for (d = 0; d < dd->ndim; d++)
142 if (dim < ddbox->nboundeddim &&
143 ddbox->box_size[dim]*ddbox->skew_fac[dim] <
144 dd->nc[dim]*dd->comm->cellsize_limit*DD_CELL_MARGIN)
146 gmx_fatal(FARGS, "The %c-size of the box (%f) times the triclinic skew factor (%f) is smaller than the number of DD cells (%d) times the smallest allowed cell size (%f)\n",
147 dim2char(dim), ddbox->box_size[dim], ddbox->skew_fac[dim],
148 dd->nc[dim], dd->comm->cellsize_limit);
153 real grid_jump_limit(const gmx_domdec_comm_t *comm,
157 real grid_jump_limit;
159 /* The distance between the boundaries of cells at distance
160 * x+-1,y+-1 or y+-1,z+-1 is limited by the cut-off restrictions
161 * and by the fact that cells should not be shifted by more than
162 * half their size, such that cg's only shift by one cell
163 * at redecomposition.
165 grid_jump_limit = comm->cellsize_limit;
166 if (!comm->bVacDLBNoLimit)
168 if (comm->bPMELoadBalDLBLimits)
170 cutoff = std::max(cutoff, comm->PMELoadBal_max_cutoff);
172 grid_jump_limit = std::max(grid_jump_limit,
173 cutoff/comm->cd[dim_ind].numPulses());
176 return grid_jump_limit;
179 /* This function should be used for moving the domain boudaries during DLB,
180 * for obtaining the minimum cell size. It checks the initially set limit
181 * comm->cellsize_min, for bonded and initial non-bonded cut-offs,
182 * and, possibly, a longer cut-off limit set for PME load balancing.
184 static real cellsize_min_dlb(gmx_domdec_comm_t *comm, int dim_ind, int dim)
188 cellsize_min = comm->cellsize_min[dim];
190 if (!comm->bVacDLBNoLimit)
192 /* The cut-off might have changed, e.g. by PME load balacning,
193 * from the value used to set comm->cellsize_min, so check it.
195 cellsize_min = std::max(cellsize_min, comm->cutoff/comm->cd[dim_ind].np_dlb);
197 if (comm->bPMELoadBalDLBLimits)
199 /* Check for the cut-off limit set by the PME load balancing */
200 cellsize_min = std::max(cellsize_min, comm->PMELoadBal_max_cutoff/comm->cd[dim_ind].np_dlb);
207 /* Set the domain boundaries. Use for static (or no) load balancing,
208 * and also for the starting state for dynamic load balancing.
209 * setmode determine if and where the boundaries are stored, use enum above.
210 * Returns the number communication pulses in npulse.
212 gmx::ArrayRef < const std::vector < real>>
213 set_dd_cell_sizes_slb(gmx_domdec_t *dd, const gmx_ddbox_t *ddbox,
214 int setmode, ivec npulse)
216 gmx_domdec_comm_t *comm = dd->comm;
218 gmx::ArrayRef < std::vector < real>> cell_x_master;
219 if (setmode == setcellsizeslbMASTER)
221 cell_x_master = dd->ma->cellSizesBuffer;
225 for (int d = 0; d < DIM; d++)
227 cellsize_min[d] = ddbox->box_size[d]*ddbox->skew_fac[d];
229 if (dd->nc[d] == 1 || comm->slb_frac[d] == nullptr)
232 real cell_dx = ddbox->box_size[d]/dd->nc[d];
235 case setcellsizeslbMASTER:
236 for (int j = 0; j < dd->nc[d]+1; j++)
238 cell_x_master[d][j] = ddbox->box0[d] + j*cell_dx;
241 case setcellsizeslbLOCAL:
242 comm->cell_x0[d] = ddbox->box0[d] + (dd->ci[d] )*cell_dx;
243 comm->cell_x1[d] = ddbox->box0[d] + (dd->ci[d]+1)*cell_dx;
248 real cellsize = cell_dx*ddbox->skew_fac[d];
249 while (cellsize*npulse[d] < comm->cutoff)
253 cellsize_min[d] = cellsize;
257 /* Statically load balanced grid */
258 /* Also when we are not doing a master distribution we determine
259 * all cell borders in a loop to obtain identical values
260 * to the master distribution case and to determine npulse.
262 gmx::ArrayRef<real> cell_x;
263 std::vector<real> cell_x_buffer;
264 if (setmode == setcellsizeslbMASTER)
266 cell_x = cell_x_master[d];
270 cell_x_buffer.resize(dd->nc[d] + 1);
271 cell_x = cell_x_buffer;
273 cell_x[0] = ddbox->box0[d];
274 for (int j = 0; j < dd->nc[d]; j++)
276 real cell_dx = ddbox->box_size[d]*comm->slb_frac[d][j];
277 cell_x[j+1] = cell_x[j] + cell_dx;
278 real cellsize = cell_dx*ddbox->skew_fac[d];
279 while (cellsize*npulse[d] < comm->cutoff &&
280 npulse[d] < dd->nc[d]-1)
284 cellsize_min[d] = std::min(cellsize_min[d], cellsize);
286 if (setmode == setcellsizeslbLOCAL)
288 comm->cell_x0[d] = cell_x[dd->ci[d]];
289 comm->cell_x1[d] = cell_x[dd->ci[d]+1];
292 /* The following limitation is to avoid that a cell would receive
293 * some of its own home charge groups back over the periodic boundary.
294 * Double charge groups cause trouble with the global indices.
296 if (d < ddbox->npbcdim &&
297 dd->nc[d] > 1 && npulse[d] >= dd->nc[d])
299 char error_string[STRLEN];
301 sprintf(error_string,
302 "The box size in direction %c (%f) times the triclinic skew factor (%f) is too small for a cut-off of %f with %d domain decomposition cells, use 1 or more than %d %s or increase the box size in this direction",
303 dim2char(d), ddbox->box_size[d], ddbox->skew_fac[d],
305 dd->nc[d], dd->nc[d],
306 dd->nnodes > dd->nc[d] ? "cells" : "ranks");
308 if (setmode == setcellsizeslbLOCAL)
310 gmx_fatal_collective(FARGS, dd->mpi_comm_all, DDMASTER(dd),
315 gmx_fatal(FARGS, "%s", error_string);
322 copy_rvec(cellsize_min, comm->cellsize_min);
325 for (int d = 0; d < comm->npmedecompdim; d++)
327 set_pme_maxshift(dd, &comm->ddpme[d],
328 comm->slb_frac[dd->dim[d]] == nullptr, ddbox,
329 comm->ddpme[d].slb_dim_f);
332 return cell_x_master;
336 static void dd_cell_sizes_dlb_root_enforce_limits(gmx_domdec_t *dd,
337 int d, int dim, RowMaster *rowMaster,
338 const gmx_ddbox_t *ddbox,
339 gmx_bool bUniform, gmx_int64_t step, real cellsize_limit_f, int range[])
341 gmx_domdec_comm_t *comm;
342 real halfway, cellsize_limit_f_i, region_size;
343 gmx_bool bLastHi = FALSE;
344 int nrange[] = {range[0], range[1]};
346 region_size = rowMaster->cellFrac[range[1]] - rowMaster->cellFrac[range[0]];
350 const int ncd = dd->nc[dim];
352 const bool dimHasPbc = (dim < ddbox->npbcdim);
354 gmx::ArrayRef<real> cell_size = rowMaster->buf_ncd;
358 fprintf(debug, "enforce_limits: %d %d\n", range[0], range[1]);
361 /* First we need to check if the scaling does not make cells
362 * smaller than the smallest allowed size.
363 * We need to do this iteratively, since if a cell is too small,
364 * it needs to be enlarged, which makes all the other cells smaller,
365 * which could in turn make another cell smaller than allowed.
367 for (int i = range[0]; i < range[1]; i++)
369 rowMaster->isCellMin[i] = false;
376 /* We need the total for normalization */
378 for (int i = range[0]; i < range[1]; i++)
380 if (!rowMaster->isCellMin[i])
385 fac = ( region_size - nmin*cellsize_limit_f)/fac; /* substracting cells already set to cellsize_limit_f */
386 /* Determine the cell boundaries */
387 for (int i = range[0]; i < range[1]; i++)
389 if (!rowMaster->isCellMin[i])
392 if (!dimHasPbc && (i == 0 || i == dd->nc[dim] - 1))
394 cellsize_limit_f_i = 0;
398 cellsize_limit_f_i = cellsize_limit_f;
400 if (cell_size[i] < cellsize_limit_f_i)
402 rowMaster->isCellMin[i] = true;
403 cell_size[i] = cellsize_limit_f_i;
407 rowMaster->cellFrac[i + 1] = rowMaster->cellFrac[i] + cell_size[i];
410 while (nmin > nmin_old);
412 const int i = range[1] - 1;
413 cell_size[i] = rowMaster->cellFrac[i + 1] - rowMaster->cellFrac[i];
414 /* For this check we should not use DD_CELL_MARGIN,
415 * but a slightly smaller factor,
416 * since rounding could get use below the limit.
419 cell_size[i] < cellsize_limit_f*DD_CELL_MARGIN2/DD_CELL_MARGIN)
422 gmx_fatal(FARGS, "step %s: the dynamic load balancing could not balance dimension %c: box size %f, triclinic skew factor %f, #cells %d, minimum cell size %f\n",
423 gmx_step_str(step, buf),
424 dim2char(dim), ddbox->box_size[dim], ddbox->skew_fac[dim],
425 ncd, comm->cellsize_min[dim]);
428 rowMaster->dlbIsLimited = (nmin > 0) || (range[0] > 0) || (range[1] < ncd);
432 /* Check if the boundary did not displace more than halfway
433 * each of the cells it bounds, as this could cause problems,
434 * especially when the differences between cell sizes are large.
435 * If changes are applied, they will not make cells smaller
436 * than the cut-off, as we check all the boundaries which
437 * might be affected by a change and if the old state was ok,
438 * the cells will at most be shrunk back to their old size.
440 for (int i = range[0]+1; i < range[1]; i++)
442 halfway = 0.5*(rowMaster->oldCellFrac[i] + rowMaster->oldCellFrac[i - 1]);
443 if (rowMaster->cellFrac[i] < halfway)
445 rowMaster->cellFrac[i] = halfway;
446 /* Check if the change also causes shifts of the next boundaries */
447 for (int j = i + 1; j < range[1]; j++)
449 if (rowMaster->cellFrac[j] < rowMaster->cellFrac[j - 1] + cellsize_limit_f)
451 rowMaster->cellFrac[j] = rowMaster->cellFrac[j - 1] + cellsize_limit_f;
455 halfway = 0.5*(rowMaster->oldCellFrac[i] + rowMaster->oldCellFrac[i + 1]);
456 if (rowMaster->cellFrac[i] > halfway)
458 rowMaster->cellFrac[i] = halfway;
459 /* Check if the change also causes shifts of the next boundaries */
460 for (int j = i - 1; j >= range[0] + 1; j--)
462 if (rowMaster->cellFrac[j] > rowMaster->cellFrac[j + 1] - cellsize_limit_f)
464 rowMaster->cellFrac[j] = rowMaster->cellFrac[j + 1] - cellsize_limit_f;
471 /* nrange is defined as [lower, upper) range for new call to enforce_limits */
472 /* find highest violation of LimLo (a) and the following violation of LimHi (thus the lowest following) (b)
473 * then call enforce_limits for (oldb,a), (a,b). In the next step: (b,nexta). oldb and nexta can be the boundaries.
474 * for a and b nrange is used */
477 /* Take care of the staggering of the cell boundaries */
480 for (int i = range[0]; i < range[1]; i++)
482 rowMaster->bounds[i].cellFracLowerMax = rowMaster->cellFrac[i];
483 rowMaster->bounds[i].cellFracUpperMin = rowMaster->cellFrac[i + 1];
488 for (int i = range[0] + 1; i < range[1]; i++)
490 const RowMaster::Bounds &bounds = rowMaster->bounds[i];
492 bool bLimLo = (rowMaster->cellFrac[i] < bounds.boundMin);
493 bool bLimHi = (rowMaster->cellFrac[i] > bounds.boundMax);
494 if (bLimLo && bLimHi)
496 /* Both limits violated, try the best we can */
497 /* For this case we split the original range (range) in two parts and care about the other limitiations in the next iteration. */
498 rowMaster->cellFrac[i] = 0.5*(bounds.boundMin + bounds.boundMax);
499 nrange[0] = range[0];
501 dd_cell_sizes_dlb_root_enforce_limits(dd, d, dim, rowMaster, ddbox, bUniform, step, cellsize_limit_f, nrange);
504 nrange[1] = range[1];
505 dd_cell_sizes_dlb_root_enforce_limits(dd, d, dim, rowMaster, ddbox, bUniform, step, cellsize_limit_f, nrange);
511 /* rowMaster->cellFrac[i] = rowMaster->boundMin[i]; */
512 nrange[1] = i; /* only store violation location. There could be a LimLo violation following with an higher index */
515 else if (bLimHi && !bLastHi)
518 if (nrange[1] < range[1]) /* found a LimLo before */
520 rowMaster->cellFrac[nrange[1]] = rowMaster->bounds[1].boundMin;
521 dd_cell_sizes_dlb_root_enforce_limits(dd, d, dim, rowMaster, ddbox, bUniform, step, cellsize_limit_f, nrange);
522 nrange[0] = nrange[1];
524 rowMaster->cellFrac[i] = rowMaster->bounds[i].boundMax;
526 dd_cell_sizes_dlb_root_enforce_limits(dd, d, dim, rowMaster, ddbox, bUniform, step, cellsize_limit_f, nrange);
528 nrange[1] = range[1];
531 if (nrange[1] < range[1]) /* found last a LimLo */
533 rowMaster->cellFrac[nrange[1]] = rowMaster->bounds[nrange[1]].boundMin;
534 dd_cell_sizes_dlb_root_enforce_limits(dd, d, dim, rowMaster, ddbox, bUniform, step, cellsize_limit_f, nrange);
535 nrange[0] = nrange[1];
536 nrange[1] = range[1];
537 dd_cell_sizes_dlb_root_enforce_limits(dd, d, dim, rowMaster, ddbox, bUniform, step, cellsize_limit_f, nrange);
539 else if (nrange[0] > range[0]) /* found at least one LimHi */
541 dd_cell_sizes_dlb_root_enforce_limits(dd, d, dim, rowMaster, ddbox, bUniform, step, cellsize_limit_f, nrange);
548 static void set_dd_cell_sizes_dlb_root(gmx_domdec_t *dd,
549 int d, int dim, RowMaster *rowMaster,
550 const gmx_ddbox_t *ddbox,
551 gmx_bool bDynamicBox,
552 gmx_bool bUniform, gmx_int64_t step)
554 gmx_domdec_comm_t *comm = dd->comm;
555 constexpr real c_relax = 0.5;
556 int range[] = { 0, 0 };
558 /* Convert the maximum change from the input percentage to a fraction */
559 const real change_limit = comm->dlb_scale_lim*0.01;
561 const int ncd = dd->nc[dim];
563 const bool bPBC = (dim < ddbox->npbcdim);
565 gmx::ArrayRef<real> cell_size = rowMaster->buf_ncd;
567 /* Store the original boundaries */
568 for (int i = 0; i < ncd + 1; i++)
570 rowMaster->oldCellFrac[i] = rowMaster->cellFrac[i];
574 for (int i = 0; i < ncd; i++)
576 cell_size[i] = 1.0/ncd;
579 else if (dd_load_count(comm) > 0)
581 real load_aver = comm->load[d].sum_m/ncd;
585 for (int i = 0; i < ncd; i++)
587 /* Determine the relative imbalance of cell i */
588 load_i = comm->load[d].load[i*comm->load[d].nload+2];
589 real imbalance = (load_i - load_aver)/(load_aver > 0 ? load_aver : 1);
590 /* Determine the change of the cell size using underrelaxation */
591 change = -c_relax*imbalance;
592 change_max = std::max(change_max, std::max(change, -change));
594 /* Limit the amount of scaling.
595 * We need to use the same rescaling for all cells in one row,
596 * otherwise the load balancing might not converge.
599 if (change_max > change_limit)
601 sc *= change_limit/change_max;
603 for (int i = 0; i < ncd; i++)
605 /* Determine the relative imbalance of cell i */
606 load_i = comm->load[d].load[i*comm->load[d].nload+2];
607 real imbalance = (load_i - load_aver)/(load_aver > 0 ? load_aver : 1);
608 /* Determine the change of the cell size using underrelaxation */
609 change = -sc*imbalance;
610 cell_size[i] = (rowMaster->cellFrac[i + 1] - rowMaster->cellFrac[i])*(1 + change);
614 real cellsize_limit_f = cellsize_min_dlb(comm, d, dim)/ddbox->box_size[dim];
615 cellsize_limit_f *= DD_CELL_MARGIN;
616 real dist_min_f_hard = grid_jump_limit(comm, comm->cutoff, d)/ddbox->box_size[dim];
617 real dist_min_f = dist_min_f_hard * DD_CELL_MARGIN;
618 if (ddbox->tric_dir[dim])
620 cellsize_limit_f /= ddbox->skew_fac[dim];
621 dist_min_f /= ddbox->skew_fac[dim];
623 if (bDynamicBox && d > 0)
625 dist_min_f *= DD_PRES_SCALE_MARGIN;
627 if (d > 0 && !bUniform)
629 /* Make sure that the grid is not shifted too much */
630 for (int i = 1; i < ncd; i++)
632 const RowMaster::Bounds &boundsNeighbor = rowMaster->bounds[i - 1];
633 RowMaster::Bounds &bounds = rowMaster->bounds[i];
635 if (bounds.cellFracUpperMin - boundsNeighbor.cellFracLowerMax < 2 * dist_min_f_hard)
637 gmx_incons("Inconsistent DD boundary staggering limits!");
639 bounds.boundMin = boundsNeighbor.cellFracLowerMax + dist_min_f;
640 real space = rowMaster->cellFrac[i] - (boundsNeighbor.cellFracLowerMax + dist_min_f);
643 bounds.boundMin += 0.5*space;
645 bounds.boundMax = bounds.cellFracUpperMin - dist_min_f;
646 space = rowMaster->cellFrac[i] - (bounds.cellFracUpperMin - dist_min_f);
649 rowMaster->bounds[i].boundMax += 0.5*space;
654 "dim %d boundary %d %.3f < %.3f < %.3f < %.3f < %.3f\n",
656 boundsNeighbor.cellFracLowerMax + dist_min_f,
657 bounds.boundMin, rowMaster->cellFrac[i], bounds.boundMax,
658 bounds.cellFracUpperMin - dist_min_f);
663 rowMaster->cellFrac[0] = 0;
664 rowMaster->cellFrac[ncd] = 1;
665 dd_cell_sizes_dlb_root_enforce_limits(dd, d, dim, rowMaster, ddbox, bUniform, step, cellsize_limit_f, range);
668 /* After the checks above, the cells should obey the cut-off
669 * restrictions, but it does not hurt to check.
671 for (int i = 0; i < ncd; i++)
675 fprintf(debug, "Relative bounds dim %d cell %d: %f %f\n",
676 dim, i, rowMaster->cellFrac[i], rowMaster->cellFrac[i + 1]);
679 if ((bPBC || (i != 0 && i != dd->nc[dim]-1)) &&
680 rowMaster->cellFrac[i + 1] - rowMaster->cellFrac[i] <
681 cellsize_limit_f/DD_CELL_MARGIN)
685 "\nWARNING step %s: direction %c, cell %d too small: %f\n",
686 gmx_step_str(step, buf), dim2char(dim), i,
687 (rowMaster->cellFrac[i + 1] - rowMaster->cellFrac[i])
688 *ddbox->box_size[dim]*ddbox->skew_fac[dim]);
693 /* Store the cell boundaries of the lower dimensions at the end */
694 for (int d1 = 0; d1 < d; d1++)
696 rowMaster->cellFrac[pos++] = comm->cellsizesWithDlb[d1].fracLower;
697 rowMaster->cellFrac[pos++] = comm->cellsizesWithDlb[d1].fracUpper;
700 if (d < comm->npmedecompdim)
702 /* The master determines the maximum shift for
703 * the coordinate communication between separate PME nodes.
705 set_pme_maxshift(dd, &comm->ddpme[d], bUniform, ddbox, rowMaster->cellFrac.data());
707 rowMaster->cellFrac[pos++] = comm->ddpme[0].maxshift;
710 rowMaster->cellFrac[pos++] = comm->ddpme[1].maxshift;
714 static void relative_to_absolute_cell_bounds(gmx_domdec_t *dd,
715 const gmx_ddbox_t *ddbox,
718 gmx_domdec_comm_t *comm = dd->comm;
719 const DDCellsizesWithDlb &cellsizes = comm->cellsizesWithDlb[dimind];
721 /* Set the cell dimensions */
722 int dim = dd->dim[dimind];
723 comm->cell_x0[dim] = cellsizes.fracLower*ddbox->box_size[dim];
724 comm->cell_x1[dim] = cellsizes.fracUpper*ddbox->box_size[dim];
725 if (dim >= ddbox->nboundeddim)
727 comm->cell_x0[dim] += ddbox->box0[dim];
728 comm->cell_x1[dim] += ddbox->box0[dim];
732 static void distribute_dd_cell_sizes_dlb(gmx_domdec_t *dd,
734 gmx::ArrayRef<real> cellFracRow,
735 const gmx_ddbox_t *ddbox)
737 gmx_domdec_comm_t &comm = *dd->comm;
740 /* Each node would only need to know two fractions,
741 * but it is probably cheaper to broadcast the whole array.
743 MPI_Bcast(cellFracRow.data(), ddCellFractionBufferSize(dd, d)*sizeof(real), MPI_BYTE,
744 0, comm.mpi_comm_load[d]);
746 /* Copy the fractions for this dimension from the buffer */
747 comm.cellsizesWithDlb[d].fracLower = cellFracRow[dd->ci[dim] ];
748 comm.cellsizesWithDlb[d].fracUpper = cellFracRow[dd->ci[dim] + 1];
749 /* The whole array was communicated, so set the buffer position */
750 int pos = dd->nc[dim] + 1;
751 for (int d1 = 0; d1 <= d; d1++)
755 /* Copy the cell fractions of the lower dimensions */
756 comm.cellsizesWithDlb[d1].fracLower = cellFracRow[pos++];
757 comm.cellsizesWithDlb[d1].fracUpper = cellFracRow[pos++];
759 relative_to_absolute_cell_bounds(dd, ddbox, d1);
761 /* Convert the communicated shift from float to int */
762 comm.ddpme[0].maxshift = (int)(cellFracRow[pos++] + 0.5);
765 comm.ddpme[1].maxshift = (int)(cellFracRow[pos++] + 0.5);
769 static void set_dd_cell_sizes_dlb_change(gmx_domdec_t *dd,
770 const gmx_ddbox_t *ddbox,
771 gmx_bool bDynamicBox,
772 gmx_bool bUniform, gmx_int64_t step)
774 for (int d = 0; d < dd->ndim; d++)
776 const int dim = dd->dim[d];
777 bool isRowMember = true;
778 bool isRowRoot = true;
779 for (int d1 = d; d1 < dd->ndim; d1++)
781 if (dd->ci[dd->dim[d1]] > 0)
792 DDCellsizesWithDlb &cellsizes = dd->comm->cellsizesWithDlb[d];
793 gmx::ArrayRef<real> cellFracRow;
797 RowMaster *rowMaster = cellsizes.rowMaster.get();
798 set_dd_cell_sizes_dlb_root(dd, d, dim, rowMaster,
799 ddbox, bDynamicBox, bUniform, step);
800 cellFracRow = rowMaster->cellFrac;
804 cellFracRow = cellsizes.fracRow;
806 distribute_dd_cell_sizes_dlb(dd, d, dim, cellFracRow, ddbox);
811 static void set_dd_cell_sizes_dlb_nochange(gmx_domdec_t *dd,
812 const gmx_ddbox_t *ddbox)
816 /* This function assumes the box is static and should therefore
817 * not be called when the box has changed since the last
818 * call to dd_partition_system.
820 for (d = 0; d < dd->ndim; d++)
822 relative_to_absolute_cell_bounds(dd, ddbox, d);
828 static void set_dd_cell_sizes_dlb(gmx_domdec_t *dd,
829 const gmx_ddbox_t *ddbox, gmx_bool bDynamicBox,
830 gmx_bool bUniform, gmx_bool bDoDLB, gmx_int64_t step,
831 gmx_wallcycle_t wcycle)
833 gmx_domdec_comm_t *comm;
840 wallcycle_start(wcycle, ewcDDCOMMBOUND);
841 set_dd_cell_sizes_dlb_change(dd, ddbox, bDynamicBox, bUniform, step);
842 wallcycle_stop(wcycle, ewcDDCOMMBOUND);
844 else if (bDynamicBox)
846 set_dd_cell_sizes_dlb_nochange(dd, ddbox);
849 /* Set the dimensions for which no DD is used */
850 for (dim = 0; dim < DIM; dim++)
852 if (dd->nc[dim] == 1)
854 comm->cell_x0[dim] = 0;
855 comm->cell_x1[dim] = ddbox->box_size[dim];
856 if (dim >= ddbox->nboundeddim)
858 comm->cell_x0[dim] += ddbox->box0[dim];
859 comm->cell_x1[dim] += ddbox->box0[dim];
865 void set_dd_cell_sizes(gmx_domdec_t *dd,
866 const gmx_ddbox_t *ddbox, gmx_bool bDynamicBox,
867 gmx_bool bUniform, gmx_bool bDoDLB, gmx_int64_t step,
868 gmx_wallcycle_t wcycle)
870 gmx_domdec_comm_t *comm = dd->comm;
872 /* Copy the old cell boundaries for the cg displacement check */
873 copy_rvec(comm->cell_x0, comm->old_cell_x0);
874 copy_rvec(comm->cell_x1, comm->old_cell_x1);
880 check_box_size(dd, ddbox);
882 set_dd_cell_sizes_dlb(dd, ddbox, bDynamicBox, bUniform, bDoDLB, step, wcycle);
887 set_dd_cell_sizes_slb(dd, ddbox, setcellsizeslbLOCAL, numPulses);
889 /* Check if the change in cell size requires a different number
890 * of communication pulses and if so change the number.
892 for (int d = 0; d < dd->ndim; d++)
894 gmx_domdec_comm_dim_t &cd = comm->cd[d];
895 int numPulsesDim = numPulses[dd->dim[d]];
896 if (cd.numPulses() != numPulsesDim)
900 fprintf(debug, "Changing the number of halo communication pulses along dim %c from %d to %d\n",
901 dim2char(dd->dim[d]), cd.numPulses(), numPulsesDim);
903 cd.ind.resize(numPulsesDim);
910 for (int d = 0; d < DIM; d++)
912 fprintf(debug, "cell_x[%d] %f - %f skew_fac %f\n",
913 d, comm->cell_x0[d], comm->cell_x1[d], ddbox->skew_fac[d]);