15f0fd76abe832333f4e56da9064201cfcf84817
[alexxy/gromacs.git] / src / gromacs / modularsimulator / domdechelper.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020,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 /*! \internal \file
36  * \brief Declares the domain decomposition helper for the modular simulator
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_modularsimulator
40  *
41  * This header is only used within the modular simulator module
42  */
43
44 #ifndef GMX_MODULARSIMULATOR_DOMDECHELPER_H
45 #define GMX_MODULARSIMULATOR_DOMDECHELPER_H
46
47 #include "modularsimulatorinterfaces.h"
48
49 struct gmx_localtop_t;
50 struct gmx_wallcycle;
51 struct pull_t;
52 struct t_commrec;
53 struct t_forcerec;
54 struct t_inputrec;
55 struct t_nrnb;
56
57 namespace gmx
58 {
59 class Constraints;
60 class FreeEnergyPerturbationData;
61 class ImdSession;
62 class MDAtoms;
63 class MDLogger;
64 class StatePropagatorData;
65 class TopologyHolder;
66 class VirtualSitesHandler;
67
68 //! \addtogroup module_modularsimulator
69 //! \{
70
71 /*! \internal
72  * \brief Infrastructure element responsible for domain decomposition
73  *
74  * This encapsulates the function call to domain decomposition, which is
75  * important for performance but outside of the current scope of the modular
76  * simulator project. This relies on legacy data structures for the state
77  * and the topology.
78  *
79  * This element does not implement the ISimulatorElement interface, as
80  * the Simulator is calling it explicitly between task queue population
81  * steps. This allows elements to be aware of any changes before
82  * deciding what functionality they need to run.
83  */
84 class DomDecHelper final : public INeighborSearchSignallerClient
85 {
86 public:
87     //! Constructor
88     DomDecHelper(bool                          isVerbose,
89                  int                           verbosePrintInterval,
90                  StatePropagatorData*          statePropagatorData,
91                  TopologyHolder*               topologyHolder,
92                  int                           nstglobalcomm,
93                  FILE*                         fplog,
94                  t_commrec*                    cr,
95                  const MDLogger&               mdlog,
96                  Constraints*                  constr,
97                  const t_inputrec*             inputrec,
98                  MDAtoms*                      mdAtoms,
99                  t_nrnb*                       nrnb,
100                  gmx_wallcycle*                wcycle,
101                  t_forcerec*                   fr,
102                  VirtualSitesHandler*          vsite,
103                  ImdSession*                   imdSession,
104                  pull_t*                       pull_work,
105                  std::vector<DomDecCallback>&& domdecCallbacks);
106
107     /*! \brief Run domain decomposition
108      *
109      * Does domain decomposition partitioning at neighbor searching steps
110      *
111      * \param step  The step number
112      * \param time  The time
113      */
114     void run(Step step, Time time);
115
116     /*! \brief The initial domain decomposition partitioning
117      *
118      */
119     void setup();
120
121 private:
122     //! INeighborSearchSignallerClient implementation
123     std::optional<SignallerCallback> registerNSCallback() override;
124
125     //! The next NS step
126     Step nextNSStep_;
127     //! Whether we're being verbose
128     const bool isVerbose_;
129     //! If we're being verbose, how often?
130     const int verbosePrintInterval_;
131     //! The global communication frequency
132     const int nstglobalcomm_;
133
134     //! List of callbacks to inform clients that DD happened
135     std::vector<DomDecCallback> domdecCallbacks_;
136
137     // TODO: Clarify relationship to data objects and find a more robust alternative to raw pointers (#3583)
138     //! Pointer to the micro state
139     StatePropagatorData* statePropagatorData_;
140     //! Pointer to the topology
141     TopologyHolder* topologyHolder_;
142
143     //! Helper function unifying the DD partitioning calls in setup() and run()
144     void partitionSystem(bool                     verbose,
145                          bool                     isMasterState,
146                          int                      nstglobalcomm,
147                          gmx_wallcycle*           wcycle,
148                          std::unique_ptr<t_state> localState,
149                          t_state*                 globalState);
150
151     // Access to ISimulator data
152     //! Handles logging.
153     FILE* fplog_;
154     //! Handles communication.
155     t_commrec* cr_;
156     //! Handles logging.
157     const MDLogger& mdlog_;
158     //! Handles constraints.
159     Constraints* constr_;
160     //! Contains user input mdp options.
161     const t_inputrec* inputrec_;
162     //! Atom parameters for this domain.
163     MDAtoms* mdAtoms_;
164     //! Manages flop accounting.
165     t_nrnb* nrnb_;
166     //! Manages wall cycle accounting.
167     gmx_wallcycle* wcycle_;
168     //! Parameters for force calculations.
169     t_forcerec* fr_;
170     //! Handles virtual sites.
171     VirtualSitesHandler* vsite_;
172     //! The Interactive Molecular Dynamics session.
173     ImdSession* imdSession_;
174     //! The pull work object.
175     pull_t* pull_work_;
176 };
177
178 /*! \internal
179  * \brief Builder for DomDecHelper
180  *
181  * This builder allows clients to register to the DomDecHelper in order to get
182  * informed whenever system re-partitioning is performed.
183  */
184 class DomDecHelperBuilder
185 {
186 public:
187     //! Register DomDecHelper client
188     void registerClient(IDomDecHelperClient* client);
189
190     //! Return DomDecHelper instance
191     template<typename... Args>
192     std::unique_ptr<DomDecHelper> build(Args&&... args)
193     {
194         state_ = ModularSimulatorBuilderState::NotAcceptingClientRegistrations;
195         std::vector<DomDecCallback> callbacks;
196         for (const auto& client : clients_)
197         {
198             callbacks.emplace_back(client->registerDomDecCallback());
199         }
200         return std::make_unique<DomDecHelper>(std::forward<Args>(args)..., std::move(callbacks));
201     }
202
203 private:
204     //! List of clients to be updated after system partition
205     std::vector<IDomDecHelperClient*> clients_;
206     //! The state of the builder
207     ModularSimulatorBuilderState state_ = ModularSimulatorBuilderState::AcceptingClientRegistrations;
208 };
209
210 //! \}
211 } // namespace gmx
212
213 #endif // GMX_MODULARSIMULATOR_DOMDECHELPER_H