Unify handling of GMX_ENABLE_GPU_TIMING and GMX_DISABLE_GPU_TIMING
[alexxy/gromacs.git] / src / gromacs / mdrun / simulatorbuilder.cpp
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 Defines the simulator builder for mdrun
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_mdrun
40  */
41
42 #include "gmxpre.h"
43
44 #include "simulatorbuilder.h"
45
46 #include <memory>
47
48 #include "gromacs/mdlib/vsite.h"
49 #include "gromacs/mdtypes/checkpointdata.h"
50 #include "gromacs/mdtypes/mdrunoptions.h"
51 #include "gromacs/mdtypes/state.h"
52 #include "gromacs/modularsimulator/modularsimulator.h"
53 #include "gromacs/topology/topology.h"
54 #include "gromacs/utility/mdmodulesnotifiers.h"
55
56 #include "legacysimulator.h"
57 #include "membedholder.h"
58 #include "replicaexchange.h"
59
60
61 namespace gmx
62 {
63
64 //! \brief Build a Simulator object
65 std::unique_ptr<ISimulator> SimulatorBuilder::build(bool useModularSimulator)
66 {
67     // TODO: Reduce protocol complexity.
68     //     Investigate individual paramters. Identify default-constructable parameters and clarify
69     //     usage requirements.
70     if (!stopHandlerBuilder_)
71     {
72         throw APIError("You must add a StopHandlerBuilder before calling build().");
73     }
74     if (!membedHolder_)
75     {
76         throw APIError("You must add a MembedHolder before calling build().");
77     }
78     if (!simulatorStateData_)
79     {
80         throw APIError("Simulator State Data has not been added to the builder");
81     }
82     if (!simulatorConfig_)
83     {
84         throw APIError("Simulator config should be set before building the simulator");
85     }
86     if (!simulatorEnv_)
87     {
88         throw APIError("You must add a SimulatorEnv before calling build().");
89     }
90     if (!profiling_)
91     {
92         throw APIError("You must add a Profiling before calling build().");
93     }
94     if (!constraintsParam_)
95     {
96         throw APIError("You must add a ConstraintsParam before calling build().");
97     }
98     if (!legacyInput_)
99     {
100         throw APIError("You must add a LegacyInput before calling build().");
101     }
102     if (!replicaExchangeParameters_)
103     {
104         throw APIError("You must add a ReplicaExchangeParameters before calling build().");
105     }
106     if (!interactiveMD_)
107     {
108         throw APIError("You must add a InteractiveMD before calling build().");
109     }
110     if (!simulatorModules_)
111     {
112         throw APIError("You must add a SimulatorModules before calling build().");
113     }
114     if (!centerOfMassPulling_)
115     {
116         throw APIError("You must add a CenterOfMassPulling before calling build().");
117     }
118     if (!ionSwapping_)
119     {
120         throw APIError("You must add a IonSwapping before calling build().");
121     }
122     if (!topologyData_)
123     {
124         throw APIError("You must add a TopologyData before calling build().");
125     }
126
127     if (useModularSimulator)
128     {
129         // NOLINTNEXTLINE(modernize-make-unique): make_unique does not work with private constructor
130         return std::unique_ptr<ModularSimulator>(new ModularSimulator(
131                 std::make_unique<LegacySimulatorData>(simulatorEnv_->fplog_,
132                                                       simulatorEnv_->commRec_,
133                                                       simulatorEnv_->multisimCommRec_,
134                                                       simulatorEnv_->logger_,
135                                                       legacyInput_->numFile,
136                                                       legacyInput_->filenames,
137                                                       simulatorEnv_->outputEnv_,
138                                                       simulatorConfig_->mdrunOptions_,
139                                                       simulatorConfig_->startingBehavior_,
140                                                       constraintsParam_->vsite,
141                                                       constraintsParam_->constr,
142                                                       constraintsParam_->enforcedRotation,
143                                                       boxDeformation_->deform,
144                                                       simulatorModules_->outputProvider,
145                                                       simulatorModules_->mdModulesNotifiers,
146                                                       legacyInput_->inputrec,
147                                                       interactiveMD_->imdSession,
148                                                       centerOfMassPulling_->pull_work,
149                                                       ionSwapping_->ionSwap,
150                                                       topologyData_->top_global,
151                                                       topologyData_->localTopology,
152                                                       simulatorStateData_->globalState_p,
153                                                       simulatorStateData_->localState_p,
154                                                       simulatorStateData_->observablesHistory_p,
155                                                       topologyData_->mdAtoms,
156                                                       profiling_->nrnb,
157                                                       profiling_->wallCycle,
158                                                       legacyInput_->forceRec,
159                                                       simulatorStateData_->enerdata_p,
160                                                       simulatorEnv_->observablesReducerBuilder_,
161                                                       simulatorStateData_->ekindata_p,
162                                                       simulatorConfig_->runScheduleWork_,
163                                                       *replicaExchangeParameters_,
164                                                       membedHolder_->membed(),
165                                                       profiling_->walltimeAccounting,
166                                                       std::move(stopHandlerBuilder_),
167                                                       simulatorConfig_->mdrunOptions_.rerun),
168                 std::move(modularSimulatorCheckpointData_)));
169     }
170     // NOLINTNEXTLINE(modernize-make-unique): make_unique does not work with private constructor
171     return std::unique_ptr<LegacySimulator>(new LegacySimulator(simulatorEnv_->fplog_,
172                                                                 simulatorEnv_->commRec_,
173                                                                 simulatorEnv_->multisimCommRec_,
174                                                                 simulatorEnv_->logger_,
175                                                                 legacyInput_->numFile,
176                                                                 legacyInput_->filenames,
177                                                                 simulatorEnv_->outputEnv_,
178                                                                 simulatorConfig_->mdrunOptions_,
179                                                                 simulatorConfig_->startingBehavior_,
180                                                                 constraintsParam_->vsite,
181                                                                 constraintsParam_->constr,
182                                                                 constraintsParam_->enforcedRotation,
183                                                                 boxDeformation_->deform,
184                                                                 simulatorModules_->outputProvider,
185                                                                 simulatorModules_->mdModulesNotifiers,
186                                                                 legacyInput_->inputrec,
187                                                                 interactiveMD_->imdSession,
188                                                                 centerOfMassPulling_->pull_work,
189                                                                 ionSwapping_->ionSwap,
190                                                                 topologyData_->top_global,
191                                                                 topologyData_->localTopology,
192                                                                 simulatorStateData_->globalState_p,
193                                                                 simulatorStateData_->localState_p,
194                                                                 simulatorStateData_->observablesHistory_p,
195                                                                 topologyData_->mdAtoms,
196                                                                 profiling_->nrnb,
197                                                                 profiling_->wallCycle,
198                                                                 legacyInput_->forceRec,
199                                                                 simulatorStateData_->enerdata_p,
200                                                                 simulatorEnv_->observablesReducerBuilder_,
201                                                                 simulatorStateData_->ekindata_p,
202                                                                 simulatorConfig_->runScheduleWork_,
203                                                                 *replicaExchangeParameters_,
204                                                                 membedHolder_->membed(),
205                                                                 profiling_->walltimeAccounting,
206                                                                 std::move(stopHandlerBuilder_),
207                                                                 simulatorConfig_->mdrunOptions_.rerun));
208 }
209
210 void SimulatorBuilder::add(MembedHolder&& membedHolder)
211 {
212     membedHolder_ = std::make_unique<MembedHolder>(std::move(membedHolder));
213 }
214
215 void SimulatorBuilder::add(ReplicaExchangeParameters&& replicaExchangeParameters)
216 {
217     replicaExchangeParameters_ = std::make_unique<ReplicaExchangeParameters>(replicaExchangeParameters);
218 }
219
220 void SimulatorBuilder::add(std::unique_ptr<ReadCheckpointDataHolder> modularSimulatorCheckpointData)
221 {
222     modularSimulatorCheckpointData_ = std::move(modularSimulatorCheckpointData);
223 }
224
225
226 } // namespace gmx