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/simd/simd.h"
62 #include "gromacs/utility/alignedallocator.h"
63 #include "gromacs/utility/arrayref.h"
66 struct gmx_domdec_zones_t
;
67 struct nbnxn_atomdata_t
;
69 enum class PairlistType
;
73 class UpdateGroupsCog
;
83 * \brief Bounding box for a nbnxm atom cluster
85 * \note Should be aligned in memory to enable 4-wide SIMD operations.
90 * \brief Corner for the bounding box, padded with one element to enable 4-wide SIMD operations
94 //! Returns a corner with the minimum coordinates along each dimension
95 static Corner
min(const Corner
&c1
,
100 cMin
.x
= std::min(c1
.x
, c2
.x
);
101 cMin
.y
= std::min(c1
.y
, c2
.y
);
102 cMin
.z
= std::min(c1
.z
, c2
.z
);
103 /* This value of the padding is irrelevant, as long as it
104 * is initialized. We use min to allow auto-vectorization.
106 cMin
.padding
= std::min(c1
.padding
, c2
.padding
);
111 //! Returns a corner with the maximum coordinates along each dimension
112 static Corner
max(const Corner
&c1
,
117 cMax
.x
= std::max(c1
.x
, c2
.x
);
118 cMax
.y
= std::max(c1
.y
, c2
.y
);
119 cMax
.z
= std::max(c1
.z
, c2
.z
);
120 cMax
.padding
= std::max(c1
.padding
, c2
.padding
);
125 //! Returns a pointer for SIMD loading of a Corner object
126 const float *ptr() const
131 //! Returns a pointer for SIMD storing of a Corner object
137 float x
; //!< x coordinate
138 float y
; //!< y coordinate
139 float z
; //!< z coordinate
140 float padding
; //!< padding, unused, but should be set to avoid operations on unitialized data
143 Corner lower
; //!< lower, along x and y and z, corner
144 Corner upper
; //!< upper, along x and y and z, corner
148 * \brief Bounding box for one dimension of a grid cell
152 float lower
; //!< lower bound
153 float upper
; //!< upper bound
156 /*! \brief The number of bounds along one dimension of a bounding box */
157 static constexpr int c_numBoundingBoxBounds1D
= 2;
163 /* Bounding box calculations are (currently) always in single precision, so
164 * we only need to check for single precision support here.
165 * This uses less (cache-)memory and SIMD is faster, at least on x86.
167 #if GMX_SIMD4_HAVE_FLOAT
168 # define NBNXN_SEARCH_BB_SIMD4 1
170 # define NBNXN_SEARCH_BB_SIMD4 0
174 #if NBNXN_SEARCH_BB_SIMD4
175 /* Always use 4-wide SIMD for bounding box calculations */
178 /* Single precision BBs + coordinates, we can also load coordinates with SIMD */
179 # define NBNXN_SEARCH_SIMD4_FLOAT_X_BB 1
181 # define NBNXN_SEARCH_SIMD4_FLOAT_X_BB 0
184 /* Store bounding boxes corners as quadruplets: xxxxyyyyzzzz
186 * The packed bounding box coordinate stride is always set to 4.
187 * With AVX we could use 8, but that turns out not to be faster.
189 # define NBNXN_BBXXXX 1
191 //! The number of bounding boxes in a pack, also the size of a pack along one dimension
192 static constexpr int c_packedBoundingBoxesDimSize
= GMX_SIMD4_WIDTH
;
194 //! Total number of corners (floats) in a pack of bounding boxes
195 static constexpr int c_packedBoundingBoxesSize
=
196 c_packedBoundingBoxesDimSize
*DIM
*Nbnxm::c_numBoundingBoxBounds1D
;
198 //! Returns the starting index of the bouding box pack that contains the given cluster
199 static constexpr inline int packedBoundingBoxesIndex(int clusterIndex
)
201 return (clusterIndex
/c_packedBoundingBoxesDimSize
)*c_packedBoundingBoxesSize
;
204 #else /* NBNXN_SEARCH_BB_SIMD4 */
206 # define NBNXN_SEARCH_SIMD4_FLOAT_X_BB 0
207 # define NBNXN_BBXXXX 0
209 #endif /* NBNXN_SEARCH_BB_SIMD4 */
217 * \brief A pair-search grid object for one domain decomposition zone
219 * This is a rectangular 3D grid covering a potentially non-rectangular
220 * volume which is either the whole unit cell or the local zone or part
221 * of a non-local zone when using domain decomposition. The grid cells
222 * are even spaced along x/y and irregular along z. Each cell is sub-divided
223 * into atom clusters. With a CPU geometry, each cell contains 1 or 2 clusters.
224 * With a GPU geometry, each cell contains up to 8 clusters. The geometry is
225 * set by the pairlist type which is the only argument of the constructor.
227 * When multiple grids are used, i.e. with domain decomposition, we want
228 * to avoid the overhead of multiple coordinate arrays or extra indexing.
229 * Therefore each grid stores a cell offset, so a contiguous cell index
230 * can be used to index atom arrays. All methods returning atom indices
231 * return indices which index into a full atom array.
233 * Note that when atom groups, instead of individual atoms, are assigned
234 * to grid cells, individual atoms can be geometrically outside the cell
235 * and grid that they have been assigned to (as determined by the center
236 * or geometry of the atom group they belong to).
242 * \brief The cluster and cell geometry of a grid
246 //! Constructs the cluster/cell geometry given the type of pairlist
247 Geometry(PairlistType pairlistType
);
249 bool isSimple
; //!< Is this grid simple (CPU) or hierarchical (GPU)
250 int numAtomsICluster
; //!< Number of atoms per cluster
251 int numAtomsJCluster
; //!< Number of atoms for list j-clusters
252 int numAtomsPerCell
; //!< Number of atoms per cell
253 int numAtomsICluster2Log
; //!< 2log of na_c
256 // The physical dimensions of a grid
259 //! The lower corner of the (local) grid
261 //! The upper corner of the (local) grid
263 //! The physical grid size: upperCorner - lowerCorner
265 //! An estimate for the atom number density of the region targeted by the grid
267 //! The maximum distance an atom can be outside of a cell and outside of the grid
268 real maxAtomGroupRadius
;
269 //! Size of cell along dimension x and y
270 real cellSize
[DIM
- 1];
271 //! 1/size of a cell along dimensions x and y
272 real invCellSize
[DIM
- 1];
273 //! The number of grid cells along dimensions x and y
274 int numCells
[DIM
- 1];
277 //! Constructs a grid given the type of pairlist
278 Grid(PairlistType pairlistType
,
279 const bool &haveFep
);
281 //! Returns the geometry of the grid cells
282 const Geometry
&geometry() const
287 //! Returns the dimensions of the grid
288 const Dimensions
&dimensions() const
293 //! Returns the total number of grid columns
294 int numColumns() const
296 return dimensions_
.numCells
[XX
]*dimensions_
.numCells
[YY
];
299 //! Returns the total number of grid cells
302 return numCellsTotal_
;
305 //! Returns the cell offset of (the first cell of) this grid in the list of cells combined over all grids
306 int cellOffset() const
311 //! Returns the maximum number of grid cells in a column
312 int numCellsColumnMax() const
314 return numCellsColumnMax_
;
317 //! Returns the start of the source atom range mapped to this grid
318 int srcAtomBegin() const
320 return srcAtomBegin_
;
323 //! Returns the end of the source atom range mapped to this grid
324 int srcAtomEnd() const
329 //! Returns the first cell index in the grid, starting at 0 in this grid
330 int firstCellInColumn(int columnIndex
) const
332 return cxy_ind_
[columnIndex
];
335 //! Returns the number of cells in the column
336 int numCellsInColumn(int columnIndex
) const
338 return cxy_ind_
[columnIndex
+ 1] - cxy_ind_
[columnIndex
];
341 //! Returns the index of the first atom in the column
342 int firstAtomInColumn(int columnIndex
) const
344 return (cellOffset_
+ cxy_ind_
[columnIndex
])*geometry_
.numAtomsPerCell
;
347 //! Returns the number of real atoms in the column
348 int numAtomsInColumn(int columnIndex
) const
350 return cxy_na_
[columnIndex
];
353 /*! \brief Returns a view of the number of non-filler, atoms for each grid column
355 * \todo Needs a useful name. */
356 gmx::ArrayRef
<const int> cxy_na() const
360 /*! \brief Returns a view of the grid-local cell index for each grid column
362 * \todo Needs a useful name. */
363 gmx::ArrayRef
<const int> cxy_ind() const
368 //! Returns the number of real atoms in the column
369 int numAtomsPerCell() const
371 return geometry_
.numAtomsPerCell
;
374 //! Returns the number of atoms in the column including padding
375 int paddedNumAtomsInColumn(int columnIndex
) const
377 return numCellsInColumn(columnIndex
)*geometry_
.numAtomsPerCell
;
380 //! Returns the end of the atom index range on the grid, including padding
381 int atomIndexEnd() const
383 return (cellOffset_
+ numCellsTotal_
)*geometry_
.numAtomsPerCell
;
386 //! Returns whether any atom in the cluster is perturbed
387 bool clusterIsPerturbed(int clusterIndex
) const
389 return fep_
[clusterIndex
] != 0u;
392 //! Returns whether the given atom in the cluster is perturbed
393 bool atomIsPerturbed(int clusterIndex
,
394 int atomIndexInCluster
) const
396 return (fep_
[clusterIndex
] & (1 << atomIndexInCluster
)) != 0u;
399 //! Returns the free-energy perturbation bits for the cluster
400 unsigned int fepBits(int clusterIndex
) const
402 return fep_
[clusterIndex
];
405 //! Returns the i-bounding boxes for all clusters on the grid
406 gmx::ArrayRef
<const BoundingBox
> iBoundingBoxes() const
411 //! Returns the j-bounding boxes for all clusters on the grid
412 gmx::ArrayRef
<const BoundingBox
> jBoundingBoxes() const
417 //! Returns the packed bounding boxes for all clusters on the grid, empty with a CPU list
418 gmx::ArrayRef
<const float> packedBoundingBoxes() const
423 //! Returns the bounding boxes along z for all cells on the grid
424 gmx::ArrayRef
<const BoundingBox1D
> zBoundingBoxes() const
429 //! Returns the flags for all clusters on the grid
430 gmx::ArrayRef
<const int> clusterFlags() const
435 //! Returns the number of clusters for all cells on the grid, empty with a CPU geometry
436 gmx::ArrayRef
<const int> numClustersPerCell() const
441 //! Returns the cluster index for an atom
442 int atomToCluster(int atomIndex
) const
444 return (atomIndex
>> geometry_
.numAtomsICluster2Log
);
447 //! Returns the total number of clusters on the grid
448 int numClusters() const
450 if (geometry_
.isSimple
)
452 return numCellsTotal_
;
456 return numClustersTotal_
;
460 //! Sets the grid dimensions
461 void setDimensions(int ddZone
,
463 const rvec lowerCorner
,
464 const rvec upperCorner
,
466 real maxAtomGroupRadius
,
468 gmx::PinningPolicy pinningPolicy
);
470 //! Sets the cell indices using indices in \p gridSetData and \p gridWork
471 void setCellIndices(int ddZone
,
473 GridSetData
*gridSetData
,
474 gmx::ArrayRef
<GridWork
> gridWork
,
478 gmx::ArrayRef
<const gmx::RVec
> x
,
480 nbnxn_atomdata_t
*nbat
);
482 //! Determine in which grid columns atoms should go, store cells and atom counts in \p cell and \p cxy_na
483 static void calcColumnIndices(const Grid::Dimensions
&gridDims
,
484 const gmx::UpdateGroupsCog
*updateGroupsCog
,
487 gmx::ArrayRef
<const gmx::RVec
> x
,
492 gmx::ArrayRef
<int> cell
,
493 gmx::ArrayRef
<int> cxy_na
);
496 /*! \brief Fill a pair search cell with atoms
498 * Potentially sorts atoms and sets the interaction flags.
500 void fillCell(GridSetData
*gridSetData
,
501 nbnxn_atomdata_t
*nbat
,
505 gmx::ArrayRef
<const gmx::RVec
> x
,
506 BoundingBox gmx_unused
*bb_work_aligned
);
508 //! Spatially sort the atoms within one grid column
509 void sortColumnsCpuGeometry(GridSetData
*gridSetData
,
511 int atomStart
, int atomEnd
,
513 gmx::ArrayRef
<const gmx::RVec
> x
,
514 nbnxn_atomdata_t
*nbat
,
515 int cxy_start
, int cxy_end
,
516 gmx::ArrayRef
<int> sort_work
);
518 //! Spatially sort the atoms within one grid column
519 void sortColumnsGpuGeometry(GridSetData
*gridSetData
,
521 int atomStart
, int atomEnd
,
523 gmx::ArrayRef
<const gmx::RVec
> x
,
524 nbnxn_atomdata_t
*nbat
,
525 int cxy_start
, int cxy_end
,
526 gmx::ArrayRef
<int> sort_work
);
529 //! The geometry of the grid clusters and cells
531 //! The physical dimensions of the grid
532 Dimensions dimensions_
;
534 //! The total number of cells in this grid
536 //! Index in nbs->cell corresponding to cell 0
538 //! The maximum number of cells in a column
539 int numCellsColumnMax_
;
541 //! The start of the source atom range mapped to this grid
543 //! The end of the source atom range mapped to this grid
547 /*! \brief The number of, non-filler, atoms for each grid column.
549 * \todo Needs a useful name. */
550 gmx::HostVector
<int> cxy_na_
;
551 /*! \brief The grid-local cell index for each grid column
553 * \todo Needs a useful name. */
554 gmx::HostVector
<int> cxy_ind_
;
556 //! The number of cluster for each cell
557 std::vector
<int> numClusters_
;
560 //! Bounding boxes in z for the cells
561 std::vector
<BoundingBox1D
> bbcz_
;
562 //! 3D bounding boxes for the sub cells
563 std::vector
< BoundingBox
, gmx::AlignedAllocator
< BoundingBox
>> bb_
;
564 //! 3D j-bounding boxes for the case where the i- and j-cluster sizes are different
565 std::vector
< BoundingBox
, gmx::AlignedAllocator
< BoundingBox
>> bbjStorage_
;
566 //! 3D j-bounding boxes
567 gmx::ArrayRef
<BoundingBox
> bbj_
;
568 //! 3D bounding boxes in packed xxxx format per cell
569 std::vector
< float, gmx::AlignedAllocator
< float>> pbb_
;
571 //! Tells whether we have perturbed interactions, authorative source is in GridSet (never modified)
572 const bool &haveFep_
;
574 /* Bit-flag information */
575 //! Flags for properties of clusters in each cell
576 std::vector
<int> flags_
;
577 //! Signal bits for atoms in each cell that tell whether an atom is perturbed
578 std::vector
<unsigned int> fep_
;
581 //! Total number of clusters, used for printing
582 int numClustersTotal_
;