a57d3ad684b692448d4459a09498908a6187d52c
[alexxy/gromacs.git] / src / gromacs / nbnxm / grid.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 /*! \internal \file
37  *
38  * \brief
39  * Declares the Grid class.
40  *
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.
48  *
49  * \author Berk Hess <hess@kth.se>
50  * \ingroup module_nbnxm
51  */
52
53 #ifndef GMX_NBNXM_GRID_H
54 #define GMX_NBNXM_GRID_H
55
56 #include <memory>
57 #include <vector>
58
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"
64
65
66 struct gmx_domdec_zones_t;
67 struct nbnxn_atomdata_t;
68 struct nbnxn_search;
69 enum class PairlistType;
70
71 namespace gmx
72 {
73 class UpdateGroupsCog;
74 } // namespace gmx
75
76 namespace Nbnxm
77 {
78
79 struct GridSetData;
80 struct GridWork;
81
82 /*! \internal
83  * \brief Bounding box for a nbnxm atom cluster
84  *
85  * \note Should be aligned in memory to enable 4-wide SIMD operations.
86  */
87 struct BoundingBox
88 {
89     /*! \internal
90      * \brief Corner for the bounding box, padded with one element to enable 4-wide SIMD operations
91      */
92     struct Corner
93     {
94         //! Returns a corner with the minimum coordinates along each dimension
95         static Corner min(const Corner &c1,
96                           const Corner &c2)
97         {
98             Corner cMin;
99
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.
105              */
106             cMin.padding = std::min(c1.padding, c2.padding);
107
108             return cMin;
109         }
110
111         //! Returns a corner with the maximum coordinates along each dimension
112         static Corner max(const Corner &c1,
113                           const Corner &c2)
114         {
115             Corner cMax;
116
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);
121
122             return cMax;
123         }
124
125         //! Returns a pointer for SIMD loading of a Corner object
126         const float *ptr() const
127         {
128             return &x;
129         }
130
131         //! Returns a pointer for SIMD storing of a Corner object
132         float *ptr()
133         {
134             return &x;
135         }
136
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
141     };
142
143     Corner lower; //!< lower, along x and y and z, corner
144     Corner upper; //!< upper, along x and y and z, corner
145 };
146
147 /*! \internal
148  * \brief Bounding box for one dimension of a grid cell
149  */
150 struct BoundingBox1D
151 {
152     float lower; //!< lower bound
153     float upper; //!< upper bound
154 };
155
156 /*! \brief The number of bounds along one dimension of a bounding box */
157 static constexpr int c_numBoundingBoxBounds1D = 2;
158
159 } // namespace Nbnxm
160
161 #ifndef DOXYGEN
162
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.
166  */
167 #if GMX_SIMD4_HAVE_FLOAT
168 #    define NBNXN_SEARCH_BB_SIMD4      1
169 #else
170 #    define NBNXN_SEARCH_BB_SIMD4      0
171 #endif
172
173
174 #if NBNXN_SEARCH_BB_SIMD4
175 /* Always use 4-wide SIMD for bounding box calculations */
176
177 #    if !GMX_DOUBLE
178 /* Single precision BBs + coordinates, we can also load coordinates with SIMD */
179 #        define NBNXN_SEARCH_SIMD4_FLOAT_X_BB  1
180 #    else
181 #        define NBNXN_SEARCH_SIMD4_FLOAT_X_BB  0
182 #    endif
183
184 /* Store bounding boxes corners as quadruplets: xxxxyyyyzzzz
185  *
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.
188  */
189 #    define NBNXN_BBXXXX  1
190
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;
193
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;
197
198 //! Returns the starting index of the bouding box pack that contains the given cluster
199 static constexpr inline int packedBoundingBoxesIndex(int clusterIndex)
200 {
201     return (clusterIndex/c_packedBoundingBoxesDimSize)*c_packedBoundingBoxesSize;
202 }
203
204 #else  /* NBNXN_SEARCH_BB_SIMD4 */
205
206 #    define NBNXN_SEARCH_SIMD4_FLOAT_X_BB  0
207 #    define NBNXN_BBXXXX                   0
208
209 #endif /* NBNXN_SEARCH_BB_SIMD4 */
210
211 #endif // !DOXYGEN
212
213 namespace Nbnxm
214 {
215
216 /*! \internal
217  * \brief A pair-search grid object for one domain decomposition zone
218  *
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.
226  *
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.
232  *
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).
237  */
238 class Grid
239 {
240     public:
241         /*! \internal
242          * \brief The cluster and cell geometry of a grid
243          */
244         struct Geometry
245         {
246             //! Constructs the cluster/cell geometry given the type of pairlist
247             Geometry(PairlistType pairlistType);
248
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
254         };
255
256         // The physical dimensions of a grid
257         struct Dimensions
258         {
259             //! The lower corner of the (local) grid
260             rvec lowerCorner;
261             //! The upper corner of the (local) grid
262             rvec upperCorner;
263             //! The physical grid size: upperCorner - lowerCorner
264             rvec gridSize;
265             //! An estimate for the atom number density of the region targeted by the grid
266             real atomDensity;
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];
275         };
276
277         //! Constructs a grid given the type of pairlist
278         Grid(PairlistType  pairlistType,
279              const bool   &haveFep);
280
281         //! Returns the geometry of the grid cells
282         const Geometry &geometry() const
283         {
284             return geometry_;
285         }
286
287         //! Returns the dimensions of the grid
288         const Dimensions &dimensions() const
289         {
290             return dimensions_;
291         }
292
293         //! Returns the total number of grid columns
294         int numColumns() const
295         {
296             return dimensions_.numCells[XX]*dimensions_.numCells[YY];
297         }
298
299         //! Returns the total number of grid cells
300         int numCells() const
301         {
302             return numCellsTotal_;
303         }
304
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
307         {
308             return cellOffset_;
309         }
310
311         //! Returns the maximum number of grid cells in a column
312         int numCellsColumnMax() const
313         {
314             return numCellsColumnMax_;
315         }
316
317         //! Returns the start of the source atom range mapped to this grid
318         int srcAtomBegin() const
319         {
320             return srcAtomBegin_;
321         }
322
323         //! Returns the end of the source atom range mapped to this grid
324         int srcAtomEnd() const
325         {
326             return srcAtomEnd_;
327         }
328
329         //! Returns the first cell index in the grid, starting at 0 in this grid
330         int firstCellInColumn(int columnIndex) const
331         {
332             return cxy_ind_[columnIndex];
333         }
334
335         //! Returns the number of cells in the column
336         int numCellsInColumn(int columnIndex) const
337         {
338             return cxy_ind_[columnIndex + 1] - cxy_ind_[columnIndex];
339         }
340
341         //! Returns the index of the first atom in the column
342         int firstAtomInColumn(int columnIndex) const
343         {
344             return (cellOffset_ + cxy_ind_[columnIndex])*geometry_.numAtomsPerCell;
345         }
346
347         //! Returns the number of real atoms in the column
348         int numAtomsInColumn(int columnIndex) const
349         {
350             return cxy_na_[columnIndex];
351         }
352
353         /*! \brief Returns a view of the number of non-filler, atoms for each grid column
354          *
355          * \todo Needs a useful name. */
356         gmx::ArrayRef<const int> cxy_na() const
357         {
358             return cxy_na_;
359         }
360         /*! \brief Returns a view of the grid-local cell index for each grid column
361          *
362          * \todo Needs a useful name. */
363         gmx::ArrayRef<const int> cxy_ind() const
364         {
365             return cxy_ind_;
366         }
367
368         //! Returns the number of real atoms in the column
369         int numAtomsPerCell() const
370         {
371             return geometry_.numAtomsPerCell;
372         }
373
374         //! Returns the number of atoms in the column including padding
375         int paddedNumAtomsInColumn(int columnIndex) const
376         {
377             return numCellsInColumn(columnIndex)*geometry_.numAtomsPerCell;
378         }
379
380         //! Returns the end of the atom index range on the grid, including padding
381         int atomIndexEnd() const
382         {
383             return (cellOffset_ + numCellsTotal_)*geometry_.numAtomsPerCell;
384         }
385
386         //! Returns whether any atom in the cluster is perturbed
387         bool clusterIsPerturbed(int clusterIndex) const
388         {
389             return fep_[clusterIndex] != 0u;
390         }
391
392         //! Returns whether the given atom in the cluster is perturbed
393         bool atomIsPerturbed(int clusterIndex,
394                              int atomIndexInCluster) const
395         {
396             return (fep_[clusterIndex] & (1 << atomIndexInCluster)) != 0u;
397         }
398
399         //! Returns the free-energy perturbation bits for the cluster
400         unsigned int fepBits(int clusterIndex) const
401         {
402             return fep_[clusterIndex];
403         }
404
405         //! Returns the i-bounding boxes for all clusters on the grid
406         gmx::ArrayRef<const BoundingBox> iBoundingBoxes() const
407         {
408             return bb_;
409         }
410
411         //! Returns the j-bounding boxes for all clusters on the grid
412         gmx::ArrayRef<const BoundingBox> jBoundingBoxes() const
413         {
414             return bbj_;
415         }
416
417         //! Returns the packed bounding boxes for all clusters on the grid, empty with a CPU list
418         gmx::ArrayRef<const float> packedBoundingBoxes() const
419         {
420             return pbb_;
421         }
422
423         //! Returns the bounding boxes along z for all cells on the grid
424         gmx::ArrayRef<const BoundingBox1D> zBoundingBoxes() const
425         {
426             return bbcz_;
427         }
428
429         //! Returns the flags for all clusters on the grid
430         gmx::ArrayRef<const int> clusterFlags() const
431         {
432             return flags_;
433         }
434
435         //! Returns the number of clusters for all cells on the grid, empty with a CPU geometry
436         gmx::ArrayRef<const int> numClustersPerCell() const
437         {
438             return numClusters_;
439         }
440
441         //! Returns the cluster index for an atom
442         int atomToCluster(int atomIndex) const
443         {
444             return (atomIndex >> geometry_.numAtomsICluster2Log);
445         }
446
447         //! Returns the total number of clusters on the grid
448         int numClusters() const
449         {
450             if (geometry_.isSimple)
451             {
452                 return numCellsTotal_;
453             }
454             else
455             {
456                 return numClustersTotal_;
457             }
458         }
459
460         //! Sets the grid dimensions
461         void setDimensions(int                   ddZone,
462                            int                   numAtoms,
463                            const rvec            lowerCorner,
464                            const rvec            upperCorner,
465                            real                  atomDensity,
466                            real                  maxAtomGroupRadius,
467                            bool                  haveFep,
468                            gmx::PinningPolicy    pinningPolicy);
469
470         //! Sets the cell indices using indices in \p gridSetData and \p gridWork
471         void setCellIndices(int                             ddZone,
472                             int                             cellOffset,
473                             GridSetData                    *gridSetData,
474                             gmx::ArrayRef<GridWork>         gridWork,
475                             int                             atomStart,
476                             int                             atomEnd,
477                             const int                      *atinfo,
478                             gmx::ArrayRef<const gmx::RVec>  x,
479                             int                             numAtomsMoved,
480                             nbnxn_atomdata_t               *nbat);
481
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,
485                                       int                             atomStart,
486                                       int                             atomEnd,
487                                       gmx::ArrayRef<const gmx::RVec>  x,
488                                       int                             dd_zone,
489                                       const int                      *move,
490                                       int                             thread,
491                                       int                             nthread,
492                                       gmx::ArrayRef<int>              cell,
493                                       gmx::ArrayRef<int>              cxy_na);
494
495     private:
496         /*! \brief Fill a pair search cell with atoms
497          *
498          * Potentially sorts atoms and sets the interaction flags.
499          */
500         void fillCell(GridSetData                    *gridSetData,
501                       nbnxn_atomdata_t               *nbat,
502                       int                             atomStart,
503                       int                             atomEnd,
504                       const int                      *atinfo,
505                       gmx::ArrayRef<const gmx::RVec>  x,
506                       BoundingBox gmx_unused         *bb_work_aligned);
507
508         //! Spatially sort the atoms within one grid column
509         void sortColumnsCpuGeometry(GridSetData *gridSetData,
510                                     int dd_zone,
511                                     int atomStart, int atomEnd,
512                                     const int *atinfo,
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);
517
518         //! Spatially sort the atoms within one grid column
519         void sortColumnsGpuGeometry(GridSetData *gridSetData,
520                                     int dd_zone,
521                                     int atomStart, int atomEnd,
522                                     const int *atinfo,
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);
527
528         /* Data members */
529         //! The geometry of the grid clusters and cells
530         Geometry   geometry_;
531         //! The physical dimensions of the grid
532         Dimensions dimensions_;
533
534         //! The total number of cells in this grid
535         int        numCellsTotal_;
536         //! Index in nbs->cell corresponding to cell 0
537         int        cellOffset_;
538         //! The maximum number of cells in a column
539         int        numCellsColumnMax_;
540
541         //! The start of the source atom range mapped to this grid
542         int        srcAtomBegin_;
543         //! The end of the source atom range mapped to this grid
544         int        srcAtomEnd_;
545
546         /* Grid data */
547         /*! \brief The number of, non-filler, atoms for each grid column.
548          *
549          * \todo Needs a useful name. */
550         gmx::HostVector<int>    cxy_na_;
551         /*! \brief The grid-local cell index for each grid column
552          *
553          * \todo Needs a useful name. */
554         gmx::HostVector<int>    cxy_ind_;
555
556         //! The number of cluster for each cell
557         std::vector<int> numClusters_;
558
559         /* Bounding boxes */
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_;
570
571         //! Tells whether we have perturbed interactions, authorative source is in GridSet (never modified)
572         const bool               &haveFep_;
573
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_;
579
580         /* Statistics */
581         //! Total number of clusters, used for printing
582         int numClustersTotal_;
583 };
584
585 } // namespace Nbnxm
586
587 #endif