SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / domdec / reversetopology.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2021, 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 This file defines functions used in making the
39  * reverse topology.
40  *
41  * \author Berk Hess <hess@kth.se>
42  * \ingroup module_domdec
43  */
44
45 #include "gmxpre.h"
46
47 #include "gromacs/domdec/reversetopology.h"
48
49 #include <cstdio>
50
51 #include <memory>
52 #include <vector>
53
54 #include "gromacs/math/vec.h"
55 #include "gromacs/domdec/domdec_constraints.h"
56 #include "gromacs/domdec/domdec_internal.h"
57 #include "gromacs/domdec/domdec_vsite.h"
58 #include "gromacs/domdec/options.h"
59 #include "gromacs/mdlib/gmx_omp_nthreads.h"
60 #include "gromacs/mdlib/vsite.h"
61 #include "gromacs/mdtypes/inputrec.h"
62 #include "gromacs/topology/atoms.h"
63 #include "gromacs/topology/mtop_util.h"
64 #include "gromacs/topology/topsort.h"
65 #include "gromacs/utility/arrayref.h"
66 #include "gromacs/utility/fatalerror.h"
67
68 using gmx::ArrayRef;
69 using gmx::DDBondedChecking;
70 using gmx::ListOfLists;
71 using gmx::RVec;
72
73 /*! \brief Struct for the reverse topology: links bonded interactions to atoms */
74 struct gmx_reverse_top_t::Impl
75 {
76     //! Constructs a reverse topology from \p mtop
77     Impl(const gmx_mtop_t& mtop, bool useFreeEnergy, const ReverseTopOptions& reverseTopOptions);
78
79     //! @cond Doxygen_Suppress
80     //! Options for the setup of this reverse topology
81     const ReverseTopOptions options;
82     //! Are there interaction of type F_POSRES and/or F_FBPOSRES
83     bool hasPositionRestraints;
84     //! \brief Are there bondeds/exclusions between atoms?
85     bool bInterAtomicInteractions = false;
86     //! \brief Reverse ilist for all moltypes
87     std::vector<reverse_ilist_t> ril_mt;
88     //! \brief The size of ril_mt[?].index summed over all entries
89     int ril_mt_tot_size = 0;
90     //! \brief Whether listed-force interaction lists should be sorted for free energy
91     bool doListedForcesSorting;
92     //! \brief molblock to global atom index for quick lookup of molblocks on atom index
93     std::vector<MolblockIndices> mbi;
94
95     //! \brief Do we have intermolecular interactions?
96     bool bIntermolecularInteractions = false;
97     //! \brief Intermolecular reverse ilist
98     reverse_ilist_t ril_intermol;
99
100     /* Work data structures for multi-threading */
101     //! \brief Thread work array for local topology generation
102     std::vector<thread_work_t> th_work;
103     //! @endcond
104 };
105
106
107 int nral_rt(int ftype)
108 {
109     int nral = NRAL(ftype);
110     if (interaction_function[ftype].flags & IF_VSITE)
111     {
112         /* With vsites the reverse topology contains an extra entry
113          * for storing if constructing atoms are vsites.
114          */
115         nral += 1;
116     }
117
118     return nral;
119 }
120
121 bool dd_check_ftype(const int ftype, const ReverseTopOptions& rtOptions)
122 {
123     return ((((interaction_function[ftype].flags & IF_BOND) != 0U)
124              && ((interaction_function[ftype].flags & IF_VSITE) == 0U)
125              && ((rtOptions.ddBondedChecking == DDBondedChecking::All)
126                  || ((interaction_function[ftype].flags & IF_LIMZERO) == 0U)))
127             || (rtOptions.includeConstraints && (ftype == F_CONSTR || ftype == F_CONSTRNC))
128             || (rtOptions.includeSettles && ftype == F_SETTLE));
129 }
130
131 MolecularTopologyAtomIndices globalAtomIndexToMoltypeIndices(const gmx::ArrayRef<const MolblockIndices> molblockIndices,
132                                                              const int globalAtomIndex)
133 {
134     // Find the molblock the atom belongs to using bisection
135     int start = 0;
136     int end   = molblockIndices.size(); /* exclusive */
137     int mid   = 0;
138
139     while (true)
140     {
141         mid = (start + end) / 2;
142         if (globalAtomIndex >= molblockIndices[mid].a_end)
143         {
144             start = mid + 1;
145         }
146         else if (globalAtomIndex < molblockIndices[mid].a_start)
147         {
148             end = mid;
149         }
150         else
151         {
152             break;
153         }
154     }
155
156     const MolblockIndices& mbi = molblockIndices[mid];
157
158     MolecularTopologyAtomIndices mtai;
159
160     mtai.blockIndex    = mid;
161     mtai.moleculeType  = mbi.type;
162     mtai.moleculeIndex = (globalAtomIndex - mbi.a_start) / mbi.natoms_mol;
163     mtai.atomIndex     = (globalAtomIndex - mbi.a_start) - mtai.moleculeIndex * mbi.natoms_mol;
164
165     return mtai;
166 }
167
168 /*! \brief Returns the maximum number of exclusions per atom */
169 static int getMaxNumExclusionsPerAtom(const ListOfLists<int>& excls)
170 {
171     int maxNumExcls = 0;
172     for (gmx::index at = 0; at < excls.ssize(); at++)
173     {
174         const auto list     = excls[at];
175         const int  numExcls = list.ssize();
176
177         GMX_RELEASE_ASSERT(numExcls != 1 || list[0] == at,
178                            "With 1 exclusion we expect a self-exclusion");
179
180         maxNumExcls = std::max(maxNumExcls, numExcls);
181     }
182
183     return maxNumExcls;
184 }
185
186 /*! \brief Run the reverse ilist generation and store it in r_il when \p bAssign = TRUE */
187 static void low_make_reverse_ilist(const InteractionLists&  il_mt,
188                                    const t_atom*            atom,
189                                    int*                     count,
190                                    const ReverseTopOptions& rtOptions,
191                                    gmx::ArrayRef<const int> r_index,
192                                    gmx::ArrayRef<int>       r_il,
193                                    const AtomLinkRule       atomLinkRule,
194                                    const bool               assignReverseIlist)
195 {
196     const bool includeConstraints = rtOptions.includeConstraints;
197     const bool includeSettles     = rtOptions.includeSettles;
198
199     for (int ftype = 0; ftype < F_NRE; ftype++)
200     {
201         if ((interaction_function[ftype].flags & (IF_BOND | IF_VSITE))
202             || (includeConstraints && (ftype == F_CONSTR || ftype == F_CONSTRNC))
203             || (includeSettles && ftype == F_SETTLE))
204         {
205             const bool  isVSite = ((interaction_function[ftype].flags & IF_VSITE) != 0U);
206             const int   nral    = NRAL(ftype);
207             const auto& il      = il_mt[ftype];
208             for (int i = 0; i < il.size(); i += 1 + nral)
209             {
210                 const int* ia = il.iatoms.data() + i;
211                 // Virtual sites should not be linked for bonded interactions
212                 const int nlink = (atomLinkRule == AtomLinkRule::FirstAtom) ? 1 : (isVSite ? 0 : nral);
213                 for (int link = 0; link < nlink; link++)
214                 {
215                     const int a = ia[1 + link];
216                     if (assignReverseIlist)
217                     {
218                         GMX_ASSERT(!r_il.empty(), "with bAssign not allowed to be empty");
219                         GMX_ASSERT(!r_index.empty(), "with bAssign not allowed to be empty");
220                         r_il[r_index[a] + count[a]]     = (ftype == F_CONSTRNC ? F_CONSTR : ftype);
221                         r_il[r_index[a] + count[a] + 1] = ia[0];
222                         for (int j = 1; j < 1 + nral; j++)
223                         {
224                             /* Store the molecular atom number */
225                             r_il[r_index[a] + count[a] + 1 + j] = ia[j];
226                         }
227                     }
228                     if (interaction_function[ftype].flags & IF_VSITE)
229                     {
230                         if (assignReverseIlist)
231                         {
232                             /* Add an entry to iatoms for storing
233                              * which of the constructing atoms are
234                              * vsites again.
235                              */
236                             r_il[r_index[a] + count[a] + 2 + nral] = 0;
237                             for (int j = 2; j < 1 + nral; j++)
238                             {
239                                 if (atom[ia[j]].ptype == ParticleType::VSite)
240                                 {
241                                     r_il[r_index[a] + count[a] + 2 + nral] |= (2 << j);
242                                 }
243                             }
244                         }
245                     }
246                     count[a] += 2 + nral_rt(ftype);
247                 }
248             }
249         }
250     }
251 }
252
253 void make_reverse_ilist(const InteractionLists&  ilist,
254                         const t_atoms*           atoms,
255                         const ReverseTopOptions& rtOptions,
256                         const AtomLinkRule       atomLinkRule,
257                         reverse_ilist_t*         ril_mt)
258 {
259     /* Count the interactions */
260     const int        nat_mt = atoms->nr;
261     std::vector<int> count(nat_mt);
262     low_make_reverse_ilist(ilist, atoms->atom, count.data(), rtOptions, {}, {}, atomLinkRule, FALSE);
263
264     ril_mt->index.push_back(0);
265     for (int i = 0; i < nat_mt; i++)
266     {
267         ril_mt->index.push_back(ril_mt->index[i] + count[i]);
268         count[i] = 0;
269     }
270     ril_mt->il.resize(ril_mt->index[nat_mt]);
271
272     /* Store the interactions */
273     low_make_reverse_ilist(
274             ilist, atoms->atom, count.data(), rtOptions, ril_mt->index, ril_mt->il, atomLinkRule, TRUE);
275
276     ril_mt->numAtomsInMolecule = atoms->nr;
277 }
278
279 gmx_reverse_top_t::gmx_reverse_top_t(const gmx_mtop_t&        mtop,
280                                      bool                     useFreeEnergy,
281                                      const ReverseTopOptions& reverseTopOptions) :
282     impl_(std::make_unique<Impl>(mtop, useFreeEnergy, reverseTopOptions))
283 {
284 }
285
286 gmx_reverse_top_t::~gmx_reverse_top_t() {}
287
288 const ReverseTopOptions& gmx_reverse_top_t::options() const
289 {
290     return impl_->options;
291 }
292
293 const reverse_ilist_t& gmx_reverse_top_t::interactionListForMoleculeType(int moleculeType) const
294 {
295     return impl_->ril_mt[moleculeType];
296 }
297
298 ArrayRef<const MolblockIndices> gmx_reverse_top_t::molblockIndices() const
299 {
300     return impl_->mbi;
301 }
302
303 bool gmx_reverse_top_t::hasIntermolecularInteractions() const
304 {
305     return impl_->bIntermolecularInteractions;
306 }
307
308 const reverse_ilist_t& gmx_reverse_top_t::interactionListForIntermolecularInteractions() const
309 {
310     return impl_->ril_intermol;
311 }
312
313 bool gmx_reverse_top_t::hasInterAtomicInteractions() const
314 {
315     return impl_->bInterAtomicInteractions;
316 }
317
318 bool gmx_reverse_top_t::hasPositionRestraints() const
319 {
320     return impl_->hasPositionRestraints;
321 }
322
323 ArrayRef<thread_work_t> gmx_reverse_top_t::threadWorkObjects() const
324 {
325     return impl_->th_work;
326 }
327
328 bool gmx_reverse_top_t::doListedForcesSorting() const
329 {
330     return impl_->doListedForcesSorting;
331 }
332
333 /*! \brief Generate the reverse topology */
334 gmx_reverse_top_t::Impl::Impl(const gmx_mtop_t&        mtop,
335                               const bool               useFreeEnergy,
336                               const ReverseTopOptions& reverseTopOptions) :
337     options(reverseTopOptions),
338     hasPositionRestraints(gmx_mtop_ftype_count(mtop, F_POSRES) + gmx_mtop_ftype_count(mtop, F_FBPOSRES) > 0),
339     bInterAtomicInteractions(mtop.bIntermolecularInteractions)
340 {
341     bInterAtomicInteractions = mtop.bIntermolecularInteractions;
342     ril_mt.resize(mtop.moltype.size());
343     ril_mt_tot_size = 0;
344     for (size_t mt = 0; mt < mtop.moltype.size(); mt++)
345     {
346         const gmx_moltype_t& molt = mtop.moltype[mt];
347         if (molt.atoms.nr > 1)
348         {
349             bInterAtomicInteractions = true;
350         }
351
352         /* Make the atom to interaction list for this molecule type */
353         make_reverse_ilist(molt.ilist, &molt.atoms, options, AtomLinkRule::FirstAtom, &ril_mt[mt]);
354
355         ril_mt_tot_size += ril_mt[mt].index[molt.atoms.nr];
356     }
357     if (debug)
358     {
359         fprintf(debug, "The total size of the atom to interaction index is %d integers\n", ril_mt_tot_size);
360     }
361
362     /* Make an intermolecular reverse top, if necessary */
363     bIntermolecularInteractions = mtop.bIntermolecularInteractions;
364     if (bIntermolecularInteractions)
365     {
366         t_atoms atoms_global;
367
368         atoms_global.nr   = mtop.natoms;
369         atoms_global.atom = nullptr; /* Only used with virtual sites */
370
371         GMX_RELEASE_ASSERT(mtop.intermolecular_ilist,
372                            "We should have an ilist when intermolecular interactions are on");
373
374         make_reverse_ilist(
375                 *mtop.intermolecular_ilist, &atoms_global, options, AtomLinkRule::FirstAtom, &ril_intermol);
376     }
377
378     doListedForcesSorting = useFreeEnergy && gmx_mtop_bondeds_free_energy(&mtop);
379
380     /* Make a molblock index for fast searching */
381     int i = 0;
382     for (size_t mb = 0; mb < mtop.molblock.size(); mb++)
383     {
384         const gmx_molblock_t& molb           = mtop.molblock[mb];
385         const int             numAtomsPerMol = mtop.moltype[molb.type].atoms.nr;
386         MolblockIndices       mbiMolblock;
387         mbiMolblock.a_start = i;
388         i += molb.nmol * numAtomsPerMol;
389         mbiMolblock.a_end      = i;
390         mbiMolblock.natoms_mol = numAtomsPerMol;
391         mbiMolblock.type       = molb.type;
392         mbi.push_back(mbiMolblock);
393     }
394
395     for (int th = 0; th < gmx_omp_nthreads_get(ModuleMultiThread::Domdec); th++)
396     {
397         th_work.emplace_back(mtop.ffparams);
398     }
399 }
400
401 void dd_make_reverse_top(FILE*                           fplog,
402                          gmx_domdec_t*                   dd,
403                          const gmx_mtop_t&               mtop,
404                          const gmx::VirtualSitesHandler* vsite,
405                          const t_inputrec&               inputrec,
406                          const DDBondedChecking          ddBondedChecking)
407 {
408     if (fplog)
409     {
410         fprintf(fplog, "\nLinking all bonded interactions to atoms\n");
411     }
412
413     /* If normal and/or settle constraints act only within charge groups,
414      * we can store them in the reverse top and simply assign them to domains.
415      * Otherwise we need to assign them to multiple domains and set up
416      * the parallel version constraint algorithm(s).
417      */
418     GMX_RELEASE_ASSERT(ddBondedChecking == DDBondedChecking::ExcludeZeroLimit
419                                || ddBondedChecking == DDBondedChecking::All,
420                        "Invalid enum value for mdrun -ddcheck");
421     const ReverseTopOptions rtOptions(ddBondedChecking,
422                                       !dd->comm->systemInfo.mayHaveSplitConstraints,
423                                       !dd->comm->systemInfo.mayHaveSplitSettles);
424
425     dd->reverse_top = std::make_unique<gmx_reverse_top_t>(
426             mtop, inputrec.efep != FreeEnergyPerturbationType::No, rtOptions);
427
428     dd->haveExclusions = false;
429     for (const gmx_molblock_t& molb : mtop.molblock)
430     {
431         const int maxNumExclusionsPerAtom = getMaxNumExclusionsPerAtom(mtop.moltype[molb.type].excls);
432         // We checked above that max 1 exclusion means only self exclusions
433         if (maxNumExclusionsPerAtom > 1)
434         {
435             dd->haveExclusions = true;
436         }
437     }
438
439     const int numInterUpdategroupVirtualSites =
440             (vsite == nullptr ? 0 : vsite->numInterUpdategroupVirtualSites());
441     if (numInterUpdategroupVirtualSites > 0)
442     {
443         if (fplog)
444         {
445             fprintf(fplog,
446                     "There are %d inter update-group virtual sites,\n"
447                     "will perform an extra communication step for selected coordinates and "
448                     "forces\n",
449                     numInterUpdategroupVirtualSites);
450         }
451         init_domdec_vsites(dd, numInterUpdategroupVirtualSites);
452     }
453
454     if (dd->comm->systemInfo.mayHaveSplitConstraints || dd->comm->systemInfo.mayHaveSplitSettles)
455     {
456         init_domdec_constraints(dd, mtop);
457     }
458     if (fplog)
459     {
460         fprintf(fplog, "\n");
461     }
462 }