2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
39 * \brief Declares the PairSearch class and helper structs
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.
46 * \author Berk Hess <hess@kth.se>
48 * \ingroup module_nbnxm
51 #ifndef GMX_NBNXM_PAIRSEARCH_H
52 #define GMX_NBNXM_PAIRSEARCH_H
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"
67 struct gmx_domdec_zones_t;
68 struct PairsearchWork;
71 /*! \brief Convenience declaration for an std::vector with aligned memory */
73 using AlignedVector = std::vector<T, gmx::AlignedAllocator<T>>;
76 //! Local cycle count struct for profiling \internal
80 //! Start counting cycles
81 void start() { start_ = gmx_cycles_read(); }
82 //! Stop counting cycles
85 cycles_ += gmx_cycles_read() - start_;
88 //! Return the number of periods of cycle counting
89 int count() const { return count_; }
91 //! Return the average number of million cycles per counting period
92 double averageMCycles() const
96 return static_cast<double>(cycles_) * 1e-6 / count_;
105 //! Number of counting periods
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;
113 //! Local cycle count enum for profiling different parts of search
123 * \brief Struct for collecting detailed cycle counts for the search
125 struct SearchCycleCounting
127 //! Start a pair search cycle counter
128 void start(const int enbsCC) { cc_[enbsCC].start(); }
130 //! Stop a pair search cycle counter
131 void stop(const int enbsCC) { cc_[enbsCC].stop(); }
133 //! Print the cycle counts to \p fp
134 void printCycles(FILE* fp, gmx::ArrayRef<const PairsearchWork> work) const;
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];
144 // TODO: Move nbnxn_search_work_t definition to its own file
146 //! Thread-local work struct, contains working data for Grid \internal
147 struct PairsearchWork
153 //! Buffer to avoid cache pollution
154 gmx_cache_protect_t cp0;
156 //! Temporary buffer for sorting atoms within a grid column
157 std::vector<int> sortBuffer;
159 //! Flags for force buffer access
160 std::vector<gmx_bitmask_t> buffer_flags;
162 //! Number of distance checks for flop counting
166 //! Temporary FEP list for load balancing
167 std::unique_ptr<t_nblist> nbl_fep;
169 //! Counter for thread-local cycles
170 nbnxn_cycle_t cycleCounter;
172 //! Buffer to avoid cache polution
173 gmx_cache_protect_t cp1;
176 //! Main pair-search struct, contains the grid(s), not the pair-list(s) \internal
180 //! Puts the atoms in \p ddZone on the grid and copies the coordinates to \p nbat
181 void putOnGrid(const matrix box,
183 const rvec lowerCorner,
184 const rvec upperCorner,
185 const gmx::UpdateGroupsCog* updateGroupsCog,
186 gmx::Range<int> atomRange,
188 gmx::ArrayRef<const int> atomInfo,
189 gmx::ArrayRef<const gmx::RVec> x,
192 nbnxn_atomdata_t* nbat)
194 cycleCounting_.start(enbsCCgrid);
196 gridSet_.putOnGrid(box,
209 cycleCounting_.stop(enbsCCgrid);
212 /*! \brief Constructor
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
223 PairSearch(PbcType pbcType,
224 bool doTestParticleInsertion,
225 const ivec* numDDCells,
226 const gmx_domdec_zones_t* zones,
227 PairlistType pairlistType,
230 gmx::PinningPolicy pinningPolicy);
232 //! Sets the order of the local atoms to the order grid atom ordering
233 void setLocalAtomOrder() { gridSet_.setLocalAtomOrder(); }
235 //! Returns the set of search grids
236 const Nbnxm::GridSet& gridSet() const { return gridSet_; }
238 //! Returns the list of thread-local work objects
239 gmx::ArrayRef<const PairsearchWork> work() const { return work_; }
241 //! Returns the list of thread-local work objects
242 gmx::ArrayRef<PairsearchWork> work() { return work_; }
245 //! The set of search grids
246 Nbnxm::GridSet gridSet_;
247 //! Work objects, one entry for each thread
248 std::vector<PairsearchWork> work_;
251 //! Cycle counting for measuring components of the search
252 SearchCycleCounting cycleCounting_;