Move reverse topology code to its own translation unit
[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 The sorting state of bondeds for free energy
91     int ilsort = ilsortUNKNOWN;
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     /*! \brief Data to help check reverse topology construction
101      *
102      * Partitioning could incorrectly miss a bonded interaction.
103      * However, checking for that requires a global communication
104      * stage, which does not otherwise happen during partitioning. So,
105      * for performance, we do that alongside the first global energy
106      * reduction after a new DD is made. These variables handle
107      * whether the check happens, its input for this domain, output
108      * across all domains, and the expected value it should match. */
109     /*! \{ */
110     /*! \brief Number of bonded interactions found in the reverse
111      * topology for this domain. */
112     int numBondedInteractions = 0;
113     /*! \brief Whether to check at the next global communication
114      * stage the total number of bonded interactions found.
115      *
116      * Cleared after that number is found. */
117     bool shouldCheckNumberOfBondedInteractions = false;
118     /*! \brief The total number of bonded interactions found in
119      * the reverse topology across all domains.
120      *
121      * Only has a value after reduction across all ranks, which is
122      * removed when it is again time to check after a new
123      * partition. */
124     std::optional<int> numBondedInteractionsOverAllDomains;
125     //! The number of bonded interactions computed from the full topology
126     int expectedNumGlobalBondedInteractions = 0;
127     /*! \} */
128
129     /* Work data structures for multi-threading */
130     //! \brief Thread work array for local topology generation
131     std::vector<thread_work_t> th_work;
132     //! @endcond
133 };
134
135
136 int nral_rt(int ftype)
137 {
138     int nral = NRAL(ftype);
139     if (interaction_function[ftype].flags & IF_VSITE)
140     {
141         /* With vsites the reverse topology contains an extra entry
142          * for storing if constructing atoms are vsites.
143          */
144         nral += 1;
145     }
146
147     return nral;
148 }
149
150 bool dd_check_ftype(const int ftype, const ReverseTopOptions& rtOptions)
151 {
152     return ((((interaction_function[ftype].flags & IF_BOND) != 0U)
153              && ((interaction_function[ftype].flags & IF_VSITE) == 0U)
154              && ((rtOptions.ddBondedChecking == DDBondedChecking::All)
155                  || ((interaction_function[ftype].flags & IF_LIMZERO) == 0U)))
156             || (rtOptions.includeConstraints && (ftype == F_CONSTR || ftype == F_CONSTRNC))
157             || (rtOptions.includeSettles && ftype == F_SETTLE));
158 }
159
160 void global_atomnr_to_moltype_ind(ArrayRef<const MolblockIndices> molblockIndices,
161                                   int                             i_gl,
162                                   int*                            mb,
163                                   int*                            mt,
164                                   int*                            mol,
165                                   int*                            i_mol)
166 {
167     const MolblockIndices* mbi   = molblockIndices.data();
168     int                    start = 0;
169     int                    end   = molblockIndices.size(); /* exclusive */
170     int                    mid   = 0;
171
172     /* binary search for molblock_ind */
173     while (TRUE)
174     {
175         mid = (start + end) / 2;
176         if (i_gl >= mbi[mid].a_end)
177         {
178             start = mid + 1;
179         }
180         else if (i_gl < mbi[mid].a_start)
181         {
182             end = mid;
183         }
184         else
185         {
186             break;
187         }
188     }
189
190     *mb = mid;
191     mbi += mid;
192
193     *mt    = mbi->type;
194     *mol   = (i_gl - mbi->a_start) / mbi->natoms_mol;
195     *i_mol = (i_gl - mbi->a_start) - (*mol) * mbi->natoms_mol;
196 }
197
198 /*! \brief Returns the maximum number of exclusions per atom */
199 static int getMaxNumExclusionsPerAtom(const ListOfLists<int>& excls)
200 {
201     int maxNumExcls = 0;
202     for (gmx::index at = 0; at < excls.ssize(); at++)
203     {
204         const auto list     = excls[at];
205         const int  numExcls = list.ssize();
206
207         GMX_RELEASE_ASSERT(numExcls != 1 || list[0] == at,
208                            "With 1 exclusion we expect a self-exclusion");
209
210         maxNumExcls = std::max(maxNumExcls, numExcls);
211     }
212
213     return maxNumExcls;
214 }
215
216 /*! \brief Run the reverse ilist generation and store it in r_il when \p bAssign = TRUE */
217 static int low_make_reverse_ilist(const InteractionLists&  il_mt,
218                                   const t_atom*            atom,
219                                   int*                     count,
220                                   const ReverseTopOptions& rtOptions,
221                                   gmx::ArrayRef<const int> r_index,
222                                   gmx::ArrayRef<int>       r_il,
223                                   const AtomLinkRule       atomLinkRule,
224                                   const bool               assignReverseIlist)
225 {
226     const bool             includeConstraints = rtOptions.includeConstraints;
227     const bool             includeSettles     = rtOptions.includeSettles;
228     const DDBondedChecking ddBondedChecking   = rtOptions.ddBondedChecking;
229
230     int nint = 0;
231
232     for (int ftype = 0; ftype < F_NRE; ftype++)
233     {
234         if ((interaction_function[ftype].flags & (IF_BOND | IF_VSITE))
235             || (includeConstraints && (ftype == F_CONSTR || ftype == F_CONSTRNC))
236             || (includeSettles && ftype == F_SETTLE))
237         {
238             const bool  isVSite = ((interaction_function[ftype].flags & IF_VSITE) != 0U);
239             const int   nral    = NRAL(ftype);
240             const auto& il      = il_mt[ftype];
241             for (int i = 0; i < il.size(); i += 1 + nral)
242             {
243                 const int* ia = il.iatoms.data() + i;
244                 // Virtual sites should not be linked for bonded interactions
245                 const int nlink = (atomLinkRule == AtomLinkRule::FirstAtom) ? 1 : (isVSite ? 0 : nral);
246                 for (int link = 0; link < nlink; link++)
247                 {
248                     const int a = ia[1 + link];
249                     if (assignReverseIlist)
250                     {
251                         GMX_ASSERT(!r_il.empty(), "with bAssign not allowed to be empty");
252                         GMX_ASSERT(!r_index.empty(), "with bAssign not allowed to be empty");
253                         r_il[r_index[a] + count[a]]     = (ftype == F_CONSTRNC ? F_CONSTR : ftype);
254                         r_il[r_index[a] + count[a] + 1] = ia[0];
255                         for (int j = 1; j < 1 + nral; j++)
256                         {
257                             /* Store the molecular atom number */
258                             r_il[r_index[a] + count[a] + 1 + j] = ia[j];
259                         }
260                     }
261                     if (interaction_function[ftype].flags & IF_VSITE)
262                     {
263                         if (assignReverseIlist)
264                         {
265                             /* Add an entry to iatoms for storing
266                              * which of the constructing atoms are
267                              * vsites again.
268                              */
269                             r_il[r_index[a] + count[a] + 2 + nral] = 0;
270                             for (int j = 2; j < 1 + nral; j++)
271                             {
272                                 if (atom[ia[j]].ptype == ParticleType::VSite)
273                                 {
274                                     r_il[r_index[a] + count[a] + 2 + nral] |= (2 << j);
275                                 }
276                             }
277                         }
278                     }
279                     else
280                     {
281                         /* We do not count vsites since they are always
282                          * uniquely assigned and can be assigned
283                          * to multiple nodes with recursive vsites.
284                          */
285                         if (ddBondedChecking == DDBondedChecking::All
286                             || !(interaction_function[ftype].flags & IF_LIMZERO))
287                         {
288                             nint++;
289                         }
290                     }
291                     count[a] += 2 + nral_rt(ftype);
292                 }
293             }
294         }
295     }
296
297     return nint;
298 }
299
300 int make_reverse_ilist(const InteractionLists&  ilist,
301                        const t_atoms*           atoms,
302                        const ReverseTopOptions& rtOptions,
303                        const AtomLinkRule       atomLinkRule,
304                        reverse_ilist_t*         ril_mt)
305 {
306     /* Count the interactions */
307     const int        nat_mt = atoms->nr;
308     std::vector<int> count(nat_mt);
309     low_make_reverse_ilist(ilist, atoms->atom, count.data(), rtOptions, {}, {}, atomLinkRule, FALSE);
310
311     ril_mt->index.push_back(0);
312     for (int i = 0; i < nat_mt; i++)
313     {
314         ril_mt->index.push_back(ril_mt->index[i] + count[i]);
315         count[i] = 0;
316     }
317     ril_mt->il.resize(ril_mt->index[nat_mt]);
318
319     /* Store the interactions */
320     int nint_mt = low_make_reverse_ilist(
321             ilist, atoms->atom, count.data(), rtOptions, ril_mt->index, ril_mt->il, atomLinkRule, TRUE);
322
323     ril_mt->numAtomsInMolecule = atoms->nr;
324
325     return nint_mt;
326 }
327
328 gmx_reverse_top_t::gmx_reverse_top_t(const gmx_mtop_t&        mtop,
329                                      bool                     useFreeEnergy,
330                                      const ReverseTopOptions& reverseTopOptions) :
331     impl_(std::make_unique<Impl>(mtop, useFreeEnergy, reverseTopOptions))
332 {
333 }
334
335 gmx_reverse_top_t::~gmx_reverse_top_t() {}
336
337 const ReverseTopOptions& gmx_reverse_top_t::options() const
338 {
339     return impl_->options;
340 }
341
342 const reverse_ilist_t& gmx_reverse_top_t::interactionListForMoleculeType(int moleculeType) const
343 {
344     return impl_->ril_mt[moleculeType];
345 }
346
347 int gmx_reverse_top_t::expectedNumGlobalBondedInteractions() const
348 {
349     return impl_->expectedNumGlobalBondedInteractions;
350 }
351
352 ArrayRef<const MolblockIndices> gmx_reverse_top_t::molblockIndices() const
353 {
354     return impl_->mbi;
355 }
356
357 bool gmx_reverse_top_t::hasIntermolecularInteractions() const
358 {
359     return impl_->bIntermolecularInteractions;
360 }
361
362 const reverse_ilist_t& gmx_reverse_top_t::interactionListForIntermolecularInteractions() const
363 {
364     return impl_->ril_intermol;
365 }
366
367 bool gmx_reverse_top_t::hasInterAtomicInteractions() const
368 {
369     return impl_->bInterAtomicInteractions;
370 }
371
372 bool gmx_reverse_top_t::hasPositionRestraints() const
373 {
374     return impl_->hasPositionRestraints;
375 }
376
377 ArrayRef<thread_work_t> gmx_reverse_top_t::threadWorkObjects() const
378 {
379     return impl_->th_work;
380 }
381
382 bool gmx_reverse_top_t::doSorting() const
383 {
384     return impl_->ilsort != ilsortNO_FE;
385 }
386
387 /*! \brief Generate the reverse topology */
388 gmx_reverse_top_t::Impl::Impl(const gmx_mtop_t&        mtop,
389                               const bool               useFreeEnergy,
390                               const ReverseTopOptions& reverseTopOptions) :
391     options(reverseTopOptions),
392     hasPositionRestraints(gmx_mtop_ftype_count(mtop, F_POSRES) + gmx_mtop_ftype_count(mtop, F_FBPOSRES) > 0),
393     bInterAtomicInteractions(mtop.bIntermolecularInteractions)
394 {
395     bInterAtomicInteractions = mtop.bIntermolecularInteractions;
396     ril_mt.resize(mtop.moltype.size());
397     ril_mt_tot_size = 0;
398     std::vector<int> nint_mt;
399     for (size_t mt = 0; mt < mtop.moltype.size(); mt++)
400     {
401         const gmx_moltype_t& molt = mtop.moltype[mt];
402         if (molt.atoms.nr > 1)
403         {
404             bInterAtomicInteractions = true;
405         }
406
407         /* Make the atom to interaction list for this molecule type */
408         int numberOfInteractions = make_reverse_ilist(
409                 molt.ilist, &molt.atoms, options, AtomLinkRule::FirstAtom, &ril_mt[mt]);
410         nint_mt.push_back(numberOfInteractions);
411
412         ril_mt_tot_size += ril_mt[mt].index[molt.atoms.nr];
413     }
414     if (debug)
415     {
416         fprintf(debug, "The total size of the atom to interaction index is %d integers\n", ril_mt_tot_size);
417     }
418
419     expectedNumGlobalBondedInteractions = 0;
420     for (const gmx_molblock_t& molblock : mtop.molblock)
421     {
422         expectedNumGlobalBondedInteractions += molblock.nmol * nint_mt[molblock.type];
423     }
424
425     /* Make an intermolecular reverse top, if necessary */
426     bIntermolecularInteractions = mtop.bIntermolecularInteractions;
427     if (bIntermolecularInteractions)
428     {
429         t_atoms atoms_global;
430
431         atoms_global.nr   = mtop.natoms;
432         atoms_global.atom = nullptr; /* Only used with virtual sites */
433
434         GMX_RELEASE_ASSERT(mtop.intermolecular_ilist,
435                            "We should have an ilist when intermolecular interactions are on");
436
437         expectedNumGlobalBondedInteractions += make_reverse_ilist(
438                 *mtop.intermolecular_ilist, &atoms_global, options, AtomLinkRule::FirstAtom, &ril_intermol);
439     }
440
441     if (useFreeEnergy && gmx_mtop_bondeds_free_energy(&mtop))
442     {
443         ilsort = ilsortFE_UNSORTED;
444     }
445     else
446     {
447         ilsort = ilsortNO_FE;
448     }
449
450     /* Make a molblock index for fast searching */
451     int i = 0;
452     for (size_t mb = 0; mb < mtop.molblock.size(); mb++)
453     {
454         const gmx_molblock_t& molb           = mtop.molblock[mb];
455         const int             numAtomsPerMol = mtop.moltype[molb.type].atoms.nr;
456         MolblockIndices       mbiMolblock;
457         mbiMolblock.a_start = i;
458         i += molb.nmol * numAtomsPerMol;
459         mbiMolblock.a_end      = i;
460         mbiMolblock.natoms_mol = numAtomsPerMol;
461         mbiMolblock.type       = molb.type;
462         mbi.push_back(mbiMolblock);
463     }
464
465     for (int th = 0; th < gmx_omp_nthreads_get(ModuleMultiThread::Domdec); th++)
466     {
467         th_work.emplace_back(mtop.ffparams);
468     }
469 }
470
471 void dd_make_reverse_top(FILE*                           fplog,
472                          gmx_domdec_t*                   dd,
473                          const gmx_mtop_t&               mtop,
474                          const gmx::VirtualSitesHandler* vsite,
475                          const t_inputrec&               inputrec,
476                          const DDBondedChecking          ddBondedChecking)
477 {
478     if (fplog)
479     {
480         fprintf(fplog, "\nLinking all bonded interactions to atoms\n");
481     }
482
483     /* If normal and/or settle constraints act only within charge groups,
484      * we can store them in the reverse top and simply assign them to domains.
485      * Otherwise we need to assign them to multiple domains and set up
486      * the parallel version constraint algorithm(s).
487      */
488     GMX_RELEASE_ASSERT(ddBondedChecking == DDBondedChecking::ExcludeZeroLimit
489                                || ddBondedChecking == DDBondedChecking::All,
490                        "Invalid enum value for mdrun -ddcheck");
491     const ReverseTopOptions rtOptions(ddBondedChecking,
492                                       !dd->comm->systemInfo.mayHaveSplitConstraints,
493                                       !dd->comm->systemInfo.mayHaveSplitSettles);
494
495     dd->reverse_top = std::make_unique<gmx_reverse_top_t>(
496             mtop, inputrec.efep != FreeEnergyPerturbationType::No, rtOptions);
497
498     dd->haveExclusions = false;
499     for (const gmx_molblock_t& molb : mtop.molblock)
500     {
501         const int maxNumExclusionsPerAtom = getMaxNumExclusionsPerAtom(mtop.moltype[molb.type].excls);
502         // We checked above that max 1 exclusion means only self exclusions
503         if (maxNumExclusionsPerAtom > 1)
504         {
505             dd->haveExclusions = true;
506         }
507     }
508
509     const int numInterUpdategroupVirtualSites =
510             (vsite == nullptr ? 0 : vsite->numInterUpdategroupVirtualSites());
511     if (numInterUpdategroupVirtualSites > 0)
512     {
513         if (fplog)
514         {
515             fprintf(fplog,
516                     "There are %d inter update-group virtual sites,\n"
517                     "will perform an extra communication step for selected coordinates and "
518                     "forces\n",
519                     numInterUpdategroupVirtualSites);
520         }
521         init_domdec_vsites(dd, numInterUpdategroupVirtualSites);
522     }
523
524     if (dd->comm->systemInfo.mayHaveSplitConstraints || dd->comm->systemInfo.mayHaveSplitSettles)
525     {
526         init_domdec_constraints(dd, mtop);
527     }
528     if (fplog)
529     {
530         fprintf(fplog, "\n");
531     }
532 }