2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019, 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.
39 * Declares the Grid class.
41 * This class provides functionality for setting up and accessing atoms
42 * on a grid for one domain decomposition zone. This grid is used for
43 * generating cluster pair lists for computing non-bonded pair interactions.
44 * The grid consists of a regular array of columns along dimensions x and y.
45 * Along z the number of cells and their boundaries vary between the columns.
46 * Each cell can hold one or more clusters of atoms, depending on the grid
47 * geometry, which is set by the pair-list type.
49 * \author Berk Hess <hess@kth.se>
50 * \ingroup module_nbnxm
53 #ifndef GMX_NBNXM_GRID_H
54 #define GMX_NBNXM_GRID_H
59 #include "gromacs/gpu_utils/hostallocator.h"
60 #include "gromacs/math/vectypes.h"
61 #include "gromacs/utility/alignedallocator.h"
62 #include "gromacs/utility/arrayref.h"
63 #include "gromacs/utility/range.h"
65 struct gmx_domdec_zones_t;
66 struct nbnxn_atomdata_t;
68 enum class PairlistType;
72 class UpdateGroupsCog;
82 * \brief Bounding box for a nbnxm atom cluster
84 * \note Should be aligned in memory to enable 4-wide SIMD operations.
89 * \brief Corner for the bounding box, padded with one element to enable 4-wide SIMD operations
93 //! Returns a corner with the minimum coordinates along each dimension
94 static Corner min(const Corner &c1,
99 cMin.x = std::min(c1.x, c2.x);
100 cMin.y = std::min(c1.y, c2.y);
101 cMin.z = std::min(c1.z, c2.z);
102 /* This value of the padding is irrelevant, as long as it
103 * is initialized. We use min to allow auto-vectorization.
105 cMin.padding = std::min(c1.padding, c2.padding);
110 //! Returns a corner with the maximum coordinates along each dimension
111 static Corner max(const Corner &c1,
116 cMax.x = std::max(c1.x, c2.x);
117 cMax.y = std::max(c1.y, c2.y);
118 cMax.z = std::max(c1.z, c2.z);
119 cMax.padding = std::max(c1.padding, c2.padding);
124 //! Returns a pointer for SIMD loading of a Corner object
125 const float *ptr() const
130 //! Returns a pointer for SIMD storing of a Corner object
136 float x; //!< x coordinate
137 float y; //!< y coordinate
138 float z; //!< z coordinate
139 float padding; //!< padding, unused, but should be set to avoid operations on unitialized data
142 Corner lower; //!< lower, along x and y and z, corner
143 Corner upper; //!< upper, along x and y and z, corner
147 * \brief Bounding box for one dimension of a grid cell
151 float lower; //!< lower bound
152 float upper; //!< upper bound
161 * \brief A pair-search grid object for one domain decomposition zone
163 * This is a rectangular 3D grid covering a potentially non-rectangular
164 * volume which is either the whole unit cell or the local zone or part
165 * of a non-local zone when using domain decomposition. The grid cells
166 * are even spaced along x/y and irregular along z. Each cell is sub-divided
167 * into atom clusters. With a CPU geometry, each cell contains 1 or 2 clusters.
168 * With a GPU geometry, each cell contains up to 8 clusters. The geometry is
169 * set by the pairlist type which is the only argument of the constructor.
171 * When multiple grids are used, i.e. with domain decomposition, we want
172 * to avoid the overhead of multiple coordinate arrays or extra indexing.
173 * Therefore each grid stores a cell offset, so a contiguous cell index
174 * can be used to index atom arrays. All methods returning atom indices
175 * return indices which index into a full atom array.
177 * Note that when atom groups, instead of individual atoms, are assigned
178 * to grid cells, individual atoms can be geometrically outside the cell
179 * and grid that they have been assigned to (as determined by the center
180 * or geometry of the atom group they belong to).
186 * \brief The cluster and cell geometry of a grid
190 //! Constructs the cluster/cell geometry given the type of pairlist
191 Geometry(PairlistType pairlistType);
193 bool isSimple; //!< Is this grid simple (CPU) or hierarchical (GPU)
194 int numAtomsICluster; //!< Number of atoms per cluster
195 int numAtomsJCluster; //!< Number of atoms for list j-clusters
196 int numAtomsPerCell; //!< Number of atoms per cell
197 int numAtomsICluster2Log; //!< 2log of na_c
200 // The physical dimensions of a grid
203 //! The lower corner of the (local) grid
205 //! The upper corner of the (local) grid
207 //! The physical grid size: upperCorner - lowerCorner
209 //! An estimate for the atom number density of the region targeted by the grid
211 //! The maximum distance an atom can be outside of a cell and outside of the grid
212 real maxAtomGroupRadius;
213 //! Size of cell along dimension x and y
214 real cellSize[DIM - 1];
215 //! 1/size of a cell along dimensions x and y
216 real invCellSize[DIM - 1];
217 //! The number of grid cells along dimensions x and y
218 int numCells[DIM - 1];
221 //! Constructs a grid given the type of pairlist
222 Grid(PairlistType pairlistType,
223 const bool &haveFep);
225 //! Returns the geometry of the grid cells
226 const Geometry &geometry() const
231 //! Returns the dimensions of the grid
232 const Dimensions &dimensions() const
237 //! Returns the total number of grid columns
238 int numColumns() const
240 return dimensions_.numCells[XX]*dimensions_.numCells[YY];
243 //! Returns the total number of grid cells
246 return numCellsTotal_;
249 //! Returns the cell offset of (the first cell of) this grid in the list of cells combined over all grids
250 int cellOffset() const
255 //! Returns the maximum number of grid cells in a column
256 int numCellsColumnMax() const
258 return numCellsColumnMax_;
261 //! Returns the start of the source atom range mapped to this grid
262 int srcAtomBegin() const
264 return srcAtomBegin_;
267 //! Returns the end of the source atom range mapped to this grid
268 int srcAtomEnd() const
273 //! Returns the first cell index in the grid, starting at 0 in this grid
274 int firstCellInColumn(int columnIndex) const
276 return cxy_ind_[columnIndex];
279 //! Returns the number of cells in the column
280 int numCellsInColumn(int columnIndex) const
282 return cxy_ind_[columnIndex + 1] - cxy_ind_[columnIndex];
285 //! Returns the index of the first atom in the column
286 int firstAtomInColumn(int columnIndex) const
288 return (cellOffset_ + cxy_ind_[columnIndex])*geometry_.numAtomsPerCell;
291 //! Returns the number of real atoms in the column
292 int numAtomsInColumn(int columnIndex) const
294 return cxy_na_[columnIndex];
297 /*! \brief Returns a view of the number of non-filler, atoms for each grid column
299 * \todo Needs a useful name. */
300 gmx::ArrayRef<const int> cxy_na() const
304 /*! \brief Returns a view of the grid-local cell index for each grid column
306 * \todo Needs a useful name. */
307 gmx::ArrayRef<const int> cxy_ind() const
312 //! Returns the number of real atoms in the column
313 int numAtomsPerCell() const
315 return geometry_.numAtomsPerCell;
318 //! Returns the number of atoms in the column including padding
319 int paddedNumAtomsInColumn(int columnIndex) const
321 return numCellsInColumn(columnIndex)*geometry_.numAtomsPerCell;
324 //! Returns the end of the atom index range on the grid, including padding
325 int atomIndexEnd() const
327 return (cellOffset_ + numCellsTotal_)*geometry_.numAtomsPerCell;
330 //! Returns whether any atom in the cluster is perturbed
331 bool clusterIsPerturbed(int clusterIndex) const
333 return fep_[clusterIndex] != 0U;
336 //! Returns whether the given atom in the cluster is perturbed
337 bool atomIsPerturbed(int clusterIndex,
338 int atomIndexInCluster) const
340 return (fep_[clusterIndex] & (1 << atomIndexInCluster)) != 0U;
343 //! Returns the free-energy perturbation bits for the cluster
344 unsigned int fepBits(int clusterIndex) const
346 return fep_[clusterIndex];
349 //! Returns the i-bounding boxes for all clusters on the grid
350 gmx::ArrayRef<const BoundingBox> iBoundingBoxes() const
355 //! Returns the j-bounding boxes for all clusters on the grid
356 gmx::ArrayRef<const BoundingBox> jBoundingBoxes() const
361 //! Returns the packed bounding boxes for all clusters on the grid, empty with a CPU list
362 gmx::ArrayRef<const float> packedBoundingBoxes() const
367 //! Returns the bounding boxes along z for all cells on the grid
368 gmx::ArrayRef<const BoundingBox1D> zBoundingBoxes() const
373 //! Returns the flags for all clusters on the grid
374 gmx::ArrayRef<const int> clusterFlags() const
379 //! Returns the number of clusters for all cells on the grid, empty with a CPU geometry
380 gmx::ArrayRef<const int> numClustersPerCell() const
385 //! Returns the cluster index for an atom
386 int atomToCluster(int atomIndex) const
388 return (atomIndex >> geometry_.numAtomsICluster2Log);
391 //! Returns the total number of clusters on the grid
392 int numClusters() const
394 if (geometry_.isSimple)
396 return numCellsTotal_;
400 return numClustersTotal_;
404 //! Sets the grid dimensions
405 void setDimensions(int ddZone,
407 gmx::RVec lowerCorner,
408 gmx::RVec upperCorner,
410 real maxAtomGroupRadius,
412 gmx::PinningPolicy pinningPolicy);
414 //! Sets the cell indices using indices in \p gridSetData and \p gridWork
415 void setCellIndices(int ddZone,
417 GridSetData *gridSetData,
418 gmx::ArrayRef<GridWork> gridWork,
419 gmx::Range<int> atomRange,
421 gmx::ArrayRef<const gmx::RVec> x,
423 nbnxn_atomdata_t *nbat);
425 //! Determine in which grid columns atoms should go, store cells and atom counts in \p cell and \p cxy_na
426 static void calcColumnIndices(const Grid::Dimensions &gridDims,
427 const gmx::UpdateGroupsCog *updateGroupsCog,
428 gmx::Range<int> atomRange,
429 gmx::ArrayRef<const gmx::RVec> x,
434 gmx::ArrayRef<int> cell,
435 gmx::ArrayRef<int> cxy_na);
438 /*! \brief Fill a pair search cell with atoms
440 * Potentially sorts atoms and sets the interaction flags.
442 void fillCell(GridSetData *gridSetData,
443 nbnxn_atomdata_t *nbat,
447 gmx::ArrayRef<const gmx::RVec> x,
448 BoundingBox gmx_unused *bb_work_aligned);
450 //! Spatially sort the atoms within the given column range, for CPU geometry
451 void sortColumnsCpuGeometry(GridSetData *gridSetData,
454 gmx::ArrayRef<const gmx::RVec> x,
455 nbnxn_atomdata_t *nbat,
456 gmx::Range<int> columnRange,
457 gmx::ArrayRef<int> sort_work);
459 //! Spatially sort the atoms within the given column range, for GPU geometry
460 void sortColumnsGpuGeometry(GridSetData *gridSetData,
463 gmx::ArrayRef<const gmx::RVec> x,
464 nbnxn_atomdata_t *nbat,
465 gmx::Range<int> columnRange,
466 gmx::ArrayRef<int> sort_work);
469 //! The geometry of the grid clusters and cells
471 //! The physical dimensions of the grid
472 Dimensions dimensions_;
474 //! The total number of cells in this grid
476 //! Index in nbs->cell corresponding to cell 0
478 //! The maximum number of cells in a column
479 int numCellsColumnMax_;
481 //! The start of the source atom range mapped to this grid
483 //! The end of the source atom range mapped to this grid
487 /*! \brief The number of, non-filler, atoms for each grid column.
489 * \todo Needs a useful name. */
490 gmx::HostVector<int> cxy_na_;
491 /*! \brief The grid-local cell index for each grid column
493 * \todo Needs a useful name. */
494 gmx::HostVector<int> cxy_ind_;
496 //! The number of cluster for each cell
497 std::vector<int> numClusters_;
500 //! Bounding boxes in z for the cells
501 std::vector<BoundingBox1D> bbcz_;
502 //! 3D bounding boxes for the sub cells
503 std::vector < BoundingBox, gmx::AlignedAllocator < BoundingBox>> bb_;
504 //! 3D j-bounding boxes for the case where the i- and j-cluster sizes are different
505 std::vector < BoundingBox, gmx::AlignedAllocator < BoundingBox>> bbjStorage_;
506 //! 3D j-bounding boxes
507 gmx::ArrayRef<BoundingBox> bbj_;
508 //! 3D bounding boxes in packed xxxx format per cell
509 std::vector < float, gmx::AlignedAllocator < float>> pbb_;
511 //! Tells whether we have perturbed interactions, authorative source is in GridSet (never modified)
512 const bool &haveFep_;
514 /* Bit-flag information */
515 //! Flags for properties of clusters in each cell
516 std::vector<int> flags_;
517 //! Signal bits for atoms in each cell that tell whether an atom is perturbed
518 std::vector<unsigned int> fep_;
521 //! Total number of clusters, used for printing
522 int numClustersTotal_;