SYCL: Use acc.bind(cgh) instead of cgh.require(acc)
[alexxy/gromacs.git] / src / gromacs / nbnxm / pairsearch.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36
37 /*! \internal \file
38  *
39  * \brief Declares the PairSearch class and helper structs
40  *
41  * The PairSearch class holds the domain setup, the search grids
42  * and helper object for the pair search. It manages the search work.
43  * The actual gridding and pairlist generation is performed by the
44  * GridSet/Grid and PairlistSet/Pairlist classes, respectively.
45  *
46  * \author Berk Hess <hess@kth.se>
47  *
48  * \ingroup module_nbnxm
49  */
50
51 #ifndef GMX_NBNXM_PAIRSEARCH_H
52 #define GMX_NBNXM_PAIRSEARCH_H
53
54 #include <memory>
55 #include <vector>
56
57 #include "gromacs/math/vectypes.h"
58 #include "gromacs/nbnxm/atomdata.h"
59 #include "gromacs/timing/cyclecounter.h"
60 #include "gromacs/utility/alignedallocator.h"
61 #include "gromacs/utility/arrayref.h"
62 #include "gromacs/utility/real.h"
63
64 #include "gridset.h"
65 #include "pairlist.h"
66
67 struct gmx_domdec_zones_t;
68 struct PairsearchWork;
69
70
71 /*! \brief Convenience declaration for an std::vector with aligned memory */
72 template<class T>
73 using AlignedVector = std::vector<T, gmx::AlignedAllocator<T>>;
74
75
76 //! Local cycle count struct for profiling \internal
77 class nbnxn_cycle_t
78 {
79 public:
80     //! Start counting cycles
81     void start() { start_ = gmx_cycles_read(); }
82     //! Stop counting cycles
83     void stop()
84     {
85         cycles_ += gmx_cycles_read() - start_;
86         count_++;
87     }
88     //! Return the number of periods of cycle counting
89     int count() const { return count_; }
90
91     //! Return the average number of million cycles per counting period
92     double averageMCycles() const
93     {
94         if (count_ > 0)
95         {
96             return static_cast<double>(cycles_) * 1e-6 / count_;
97         }
98         else
99         {
100             return 0;
101         }
102     }
103
104 private:
105     //! Number of counting periods
106     int count_ = 0;
107     //! Total cycles in all counting periods
108     gmx_cycles_t cycles_ = 0;
109     //! Cycle count at the most recent start
110     gmx_cycles_t start_ = 0;
111 };
112
113 //! Local cycle count enum for profiling different parts of search
114 enum
115 {
116     enbsCCgrid,
117     enbsCCsearch,
118     enbsCCcombine,
119     enbsCCnr
120 };
121
122 /*! \internal
123  * \brief Struct for collecting detailed cycle counts for the search
124  */
125 struct SearchCycleCounting
126 {
127     //! Start a pair search cycle counter
128     void start(const int enbsCC) { cc_[enbsCC].start(); }
129
130     //! Stop a pair search cycle counter
131     void stop(const int enbsCC) { cc_[enbsCC].stop(); }
132
133     //! Print the cycle counts to \p fp
134     void printCycles(FILE* fp, gmx::ArrayRef<const PairsearchWork> work) const;
135
136     //! Tells whether we record cycles
137     bool recordCycles_ = false;
138     //! The number of times pairsearching has been performed, local+non-local count as 1
139     int searchCount_ = 0;
140     //! The set of cycle counters
141     nbnxn_cycle_t cc_[enbsCCnr];
142 };
143
144 // TODO: Move nbnxn_search_work_t definition to its own file
145
146 //! Thread-local work struct, contains working data for Grid \internal
147 struct PairsearchWork
148 {
149     PairsearchWork();
150
151     ~PairsearchWork();
152
153     //! Buffer to avoid cache pollution
154     gmx_cache_protect_t cp0;
155
156     //! Temporary buffer for sorting atoms within a grid column
157     std::vector<int> sortBuffer;
158
159     //! Flags for force buffer access
160     std::vector<gmx_bitmask_t> buffer_flags;
161
162     //! Number of distance checks for flop counting
163     int ndistc;
164
165
166     //! Temporary FEP list for load balancing
167     std::unique_ptr<t_nblist> nbl_fep;
168
169     //! Counter for thread-local cycles
170     nbnxn_cycle_t cycleCounter;
171
172     //! Buffer to avoid cache polution
173     gmx_cache_protect_t cp1;
174 };
175
176 //! Main pair-search struct, contains the grid(s), not the pair-list(s) \internal
177 class PairSearch
178 {
179 public:
180     //! Puts the atoms in \p ddZone on the grid and copies the coordinates to \p nbat
181     void putOnGrid(const matrix                   box,
182                    int                            ddZone,
183                    const rvec                     lowerCorner,
184                    const rvec                     upperCorner,
185                    const gmx::UpdateGroupsCog*    updateGroupsCog,
186                    gmx::Range<int>                atomRange,
187                    real                           atomDensity,
188                    gmx::ArrayRef<const int64_t>   atomInfo,
189                    gmx::ArrayRef<const gmx::RVec> x,
190                    int                            numAtomsMoved,
191                    const int*                     move,
192                    nbnxn_atomdata_t*              nbat)
193     {
194         cycleCounting_.start(enbsCCgrid);
195
196         gridSet_.putOnGrid(box,
197                            ddZone,
198                            lowerCorner,
199                            upperCorner,
200                            updateGroupsCog,
201                            atomRange,
202                            atomDensity,
203                            atomInfo,
204                            x,
205                            numAtomsMoved,
206                            move,
207                            nbat);
208
209         cycleCounting_.stop(enbsCCgrid);
210     }
211
212     /*! \brief Constructor
213      *
214      * \param[in] pbcType                  The periodic boundary conditions
215      * \param[in] doTestParticleInsertion  Whether test-particle insertion is active
216      * \param[in] numDDCells               The number of domain decomposition cells per dimension, without DD nullptr should be passed
217      * \param[in] zones                    The domain decomposition zone setup, without DD nullptr should be passed
218      * \param[in] pairlistType             The type of tte pair list
219      * \param[in] haveFep                  Tells whether non-bonded interactions are perturbed
220      * \param[in] maxNumThreads            The maximum number of threads used in the search
221      * \param[in] pinningPolicy            Sets the pinning policy for all buffers used on the GPU
222      */
223     PairSearch(PbcType                   pbcType,
224                bool                      doTestParticleInsertion,
225                const ivec*               numDDCells,
226                const gmx_domdec_zones_t* zones,
227                PairlistType              pairlistType,
228                bool                      haveFep,
229                int                       maxNumThreads,
230                gmx::PinningPolicy        pinningPolicy);
231
232     //! Sets the order of the local atoms to the order grid atom ordering
233     void setLocalAtomOrder() { gridSet_.setLocalAtomOrder(); }
234
235     //! Returns the set of search grids
236     const Nbnxm::GridSet& gridSet() const { return gridSet_; }
237
238     //! Returns the list of thread-local work objects
239     gmx::ArrayRef<const PairsearchWork> work() const { return work_; }
240
241     //! Returns the list of thread-local work objects
242     gmx::ArrayRef<PairsearchWork> work() { return work_; }
243
244 private:
245     //! The set of search grids
246     Nbnxm::GridSet gridSet_;
247     //! Work objects, one entry for each thread
248     std::vector<PairsearchWork> work_;
249
250 public:
251     //! Cycle counting for measuring components of the search
252     SearchCycleCounting cycleCounting_;
253 };
254
255 #endif