Create and use SimulationInput module.
[alexxy/gromacs.git] / src / gromacs / mdrun / runner.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2017,2018,2019,2020, 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 /*! \libinternal \file
36  *
37  * \brief Declares the routine running the inetgrators.
38  *
39  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
40  * \ingroup module_mdrun
41  */
42 #ifndef GMX_MDRUN_RUNNER_H
43 #define GMX_MDRUN_RUNNER_H
44
45 #include <cstdio>
46
47 #include <array>
48 #include <memory>
49
50 #include "gromacs/commandline/filenm.h"
51 #include "gromacs/compat/pointers.h"
52 #include "gromacs/domdec/options.h"
53 #include "gromacs/hardware/hw_info.h"
54 #include "gromacs/math/vec.h"
55 #include "gromacs/mdrun/mdmodules.h"
56 #include "gromacs/mdrun/simulationinputhandle.h"
57 #include "gromacs/mdrunutility/handlerestart.h"
58 #include "gromacs/mdtypes/mdrunoptions.h"
59 #include "gromacs/utility/arrayref.h"
60 #include "gromacs/utility/basedefinitions.h"
61 #include "gromacs/utility/gmxmpi.h"
62 #include "gromacs/utility/real.h"
63
64 #include "replicaexchange.h"
65
66 struct gmx_multisim_t;
67 struct gmx_output_env_t;
68 struct ReplicaExchangeParameters;
69 struct t_fileio;
70
71 namespace gmx
72 {
73
74 // Todo: move to forward declaration headers...
75 class MDModules;
76 class IRestraintPotential; // defined in restraint/restraintpotential.h
77 class RestraintManager;
78 class SimulationContext;
79 class StopHandlerBuilder;
80
81 /*! \libinternal \brief Runner object for supporting setup and execution of mdrun.
82  *
83  * This class has responsibility for the lifetime of data structures
84  * that exist for the life of the simulation, e.g. for logging and
85  * communication.
86  *
87  * It is also responsible for initializing data members that
88  * e.g. correspond to values potentially set by commmand-line
89  * options. Later these will be obtained directly from modules, and
90  * the results of command-line option handling returned directly to
91  * the modules, rather than propagated to them by data members of this
92  * class.
93  *
94  * \todo Most of the attributes should be declared by specific modules
95  * as command-line options. Accordingly, they do not conform to the
96  * naming scheme, because that would make for a lot of noise in the
97  * diff, only to have it change again when the options move to their
98  * modules.
99  *
100  * \todo Preparing logging and MPI contexts could probably be a
101  * higher-level responsibility, so that an Mdrunner would get made
102  * without needing to re-initialize these components (as currently
103  * happens always for the master rank, and differently for the spawned
104  * ranks with thread-MPI).
105  *
106  * \ingroup module_mdrun
107  */
108 class Mdrunner
109 {
110 public:
111     /*! \brief Builder class to manage object creation.
112      *
113      * This class is a member of gmx::Mdrunner so that it can initialize
114      * private members of gmx::Mdrunner.
115      *
116      * It is non-trivial to establish an initialized gmx::Mdrunner invariant,
117      * so objects can be obtained by clients using a Builder or a move.
118      * Clients cannot default initialize or copy gmx::Mdrunner.
119      */
120     class BuilderImplementation;
121
122     ~Mdrunner();
123
124     /*!
125      * \brief Copy not allowed.
126      *
127      * An Mdrunner has unique resources and it is not clear whether any of
128      * one of those resources should be duplicated or shared unless the
129      * specific use case is known. Either build a fresh runner or use a
130      * helper function for clearly indicated behavior. API clarification may
131      * allow unambiguous initialization by copy in future versions.
132      *
133      * \{
134      */
135     Mdrunner(const Mdrunner&) = delete;
136     Mdrunner& operator=(const Mdrunner&) = delete;
137     /* \} */
138
139     /*!
140      * \brief Mdrunner objects can be passed by value via move semantics.
141      *
142      * \param handle runner instance to be moved from.
143      * \{
144      */
145     Mdrunner(Mdrunner&& handle) noexcept;
146     Mdrunner& operator=(Mdrunner&& handle) noexcept;
147     /* \} */
148
149     /*! \brief Driver routine, that calls the different simulation methods. */
150     /*!
151      * Currently, thread-MPI does not spawn threads until during mdrunner() and parallelism
152      * is not initialized until some time during this call...
153      */
154     int mdrunner();
155
156     /*!
157      * \brief Add a potential to be evaluated during MD integration.
158      *
159      * \param restraint MD restraint potential to apply
160      * \param name User-friendly plain-text name to uniquely identify the puller
161      *
162      * This implementation attaches an object providing the gmx::IRestraintPotential
163      * interface.
164      * \todo Mdrunner should fetch such resources from the SimulationContext
165      * rather than offering this public interface.
166      */
167     void addPotential(std::shared_ptr<IRestraintPotential> restraint, const std::string& name);
168
169     /*! \brief Prepare the thread-MPI communicator to have \c
170      * numThreadsToLaunch ranks, by spawning new thread-MPI
171      * threads.
172      *
173      * Called by mdrunner() to start a specific number of threads
174      * (including the main thread) for thread-parallel runs. This
175      * in turn calls mdrunner() for each thread. */
176     void spawnThreads(int numThreadsToLaunch);
177
178     /*! \brief Initializes a new Mdrunner from the master.
179      *
180      * Run this method in a new thread from a master runner to get additional
181      * workers on spawned threads.
182      *
183      * \returns New Mdrunner instance suitable for thread-MPI work on new ranks.
184      *
185      * \internal
186      * \todo clarify (multiple) invariants during MD runner start-up.
187      * The runner state before and after launching threads is distinct enough that
188      * it should be codified in the invariants of different classes. That would
189      * mean that the object returned by this method would be of a different type
190      * than the object held by the client up to the point of call, and its name
191      * would be changed to "launchOnSpawnedThread" or something not including the
192      * word "clone".
193      */
194     Mdrunner cloneOnSpawnedThread() const;
195
196 private:
197     /*! \brief Constructor. */
198     explicit Mdrunner(std::unique_ptr<MDModules> mdModules);
199
200     //! Parallelism-related user options.
201     gmx_hw_opt_t hw_opt;
202
203     //! Filenames and properties from command-line argument values.
204     ArrayRef<const t_filenm> filenames;
205
206     /*! \brief Output context for writing text files
207      *
208      * \internal
209      * \todo push this data member down when the information can be queried from an encapsulated resource.
210      */
211     gmx_output_env_t* oenv = nullptr;
212     //! Ongoing collection of mdrun options
213     MdrunOptions mdrunOptions;
214     //! Options for the domain decomposition.
215     DomdecOptions domdecOptions;
216
217     /*! \brief Target short-range interations for "cpu", "gpu", or "auto". Default is "auto".
218      *
219      * \internal
220      * \todo replace with string or enum class and initialize with sensible value.
221      */
222     const char* nbpu_opt = nullptr;
223
224     /*! \brief Target long-range interactions for "cpu", "gpu", or "auto". Default is "auto".
225      *
226      * \internal
227      * \todo replace with string or enum class and initialize with sensible value.
228      */
229     const char* pme_opt = nullptr;
230
231     /*! \brief Target long-range interactions FFT/solve stages for "cpu", "gpu", or "auto". Default is "auto".
232      *
233      * \internal
234      * \todo replace with string or enum class and initialize with sensible value.
235      */
236     const char* pme_fft_opt = nullptr;
237
238     /*! \brief Target bonded interations for "cpu", "gpu", or "auto". Default is "auto".
239      *
240      * \internal
241      * \todo replace with string or enum class and initialize with sensible value.
242      */
243     const char* bonded_opt = nullptr;
244
245     /*! \brief Target update calculation for "cpu", "gpu", or "auto". Default is "auto".
246      *
247      * \internal
248      * \todo replace with string or enum class and initialize with sensible value.
249      */
250     const char* update_opt = nullptr;
251
252     //! Command-line override for the duration of a neighbor list with the Verlet scheme.
253     int nstlist_cmdline = 0;
254     //! Parameters for replica-exchange simulations.
255     ReplicaExchangeParameters replExParams;
256     //! Print a warning if any force is larger than this (in kJ/mol nm).
257     real pforce = -1;
258
259     //! Handle to file used for logging.
260     LogFilePtr logFileGuard = nullptr;
261     //! \brief Non-owning handle to file used for logging.
262     t_fileio* logFileHandle = nullptr;
263
264     /*! \brief Non-owning handle to communication data structure.
265      *
266      * With real MPI, gets a value from the SimulationContext
267      * supplied to the MdrunnerBuilder. With thread-MPI gets a
268      * value after threads have been spawned. */
269     MPI_Comm communicator = MPI_COMM_NULL;
270
271     //! \brief Non-owning handle to multi-simulation handler.
272     gmx_multisim_t* ms = nullptr;
273
274     //! Whether the simulation will start afresh, or restart with/without appending.
275     StartingBehavior startingBehavior = StartingBehavior::NewSimulation;
276
277     /*!
278      * \brief Handle to restraints manager for the current process.
279      *
280      * \internal
281      * Use opaque pointer for this implementation detail.
282      */
283     std::unique_ptr<RestraintManager> restraintManager_;
284
285     /*!
286      * \brief Builder for stop signal handler
287      *
288      * Optionally provided through MdrunnerBuilder. Client may create a
289      * StopHandlerBuilder and register any number of signal providers before
290      * launching the Mdrunner.
291      *
292      * Default is an empty signal handler that will have local signal issuers
293      * added after being passed into the integrator.
294      *
295      * \internal
296      * We do not need a full type specification here, so we use an opaque pointer.
297      */
298     std::unique_ptr<StopHandlerBuilder> stopHandlerBuilder_;
299     //! The modules that comprise mdrun.
300     std::unique_ptr<MDModules> mdModules_;
301
302     /*!
303      * \brief Holds simulation input specification provided by client, if any.
304      *
305      * If present on any instance (rank) of a simulation runner, an identical
306      * (or compatible) SimulationInput must be held on all cooperating instances.
307      */
308     SimulationInputHandle inputHolder_;
309 };
310
311 /*! \libinternal
312  * \brief Build a gmx::Mdrunner.
313  *
314  * Client code (such as `gmx mdrun`) uses this builder to get an initialized Mdrunner.
315  *
316  * A builder allows the library to ensure that client code cannot obtain an
317  * uninitialized or partially initialized runner by refusing to build() if the
318  * client has not provided sufficient or self-consistent direction. Director
319  * code can be implemented for different user interfaces, encapsulating any
320  * run-time functionality that does not belong in the library MD code, such
321  * as command-line option processing or interfacing to external libraries.
322  *
323  * \ingroup module_mdrun
324  *
325  * \internal
326  *
327  * The initial Builder implementation is neither extensible at run time nor
328  * at compile time. Future implementations should evolve to compose the runner,
329  * rather than just consolidating the parameters for initialization, but there
330  * is not yet a firm design for how flexibly module code will be coupled to
331  * the builder and how much of the client interface will be in this Builder
332  * versus Builders provided by the various modules.
333  *
334  * The named components for the initial builder implementation are descriptive
335  * of the state of mdrun at the time, and are not intended to be prescriptive of
336  * future design.
337  * The probable course of GROMACS development is for the modular components that
338  * support MD simulation to independently express their input parameters (required
339  * and optional) and to provide some sort of help to the UI for input preparation.
340  * If each module provides or aids the instantiation of a Director
341  * for the client code, the Directors could be constructed with a handle to this
342  * Builder and it would not need a public interface.
343  *
344  * As the modules are more clearly encapsulated, each module can provide its own
345  * builder, user interface helpers, and/or composable Director code.
346  * The runner and client code will also have to be updated as appropriate
347  * default behavior is clarified for
348  * (a) default behavior of client when user does not provide input,
349  * (b) default behavior of builder when client does not provide input, and
350  * (c) default behavior of runner when builder does not provide input.
351  */
352 class MdrunnerBuilder final
353 {
354 public:
355     /*!
356      * \brief Constructor requires a handle to a SimulationContext to share.
357      *
358      * \param mdModules  The handle to the set of modules active in mdrun
359      * \param context    Required handle to simulation context
360      *
361      * The calling code must guarantee that the
362      * pointer remains valid for the lifetime of the builder, and that the
363      * resources retrieved from the context remain valid for the lifetime of
364      * the runner produced.
365      */
366     explicit MdrunnerBuilder(std::unique_ptr<MDModules>           mdModules,
367                              compat::not_null<SimulationContext*> context);
368
369     //! \cond
370     MdrunnerBuilder()                       = delete;
371     MdrunnerBuilder(const MdrunnerBuilder&) = delete;
372     MdrunnerBuilder& operator=(const MdrunnerBuilder&) = delete;
373     //! \endcond
374
375     /*! \brief Allow transfer of ownership with move semantics.
376      *
377      * \param builder source object to transfer.
378      *
379      * \{
380      */
381     MdrunnerBuilder(MdrunnerBuilder&& builder) noexcept;
382     MdrunnerBuilder& operator=(MdrunnerBuilder&& builder) noexcept;
383     //! \}
384
385     /*!
386      * \brief Get ownership of an initialized gmx::Mdrunner.
387      *
388      * After build() is called, the Builder object should not be used
389      * again. It is an error to call build without first calling all builder
390      * methods described as "required."
391      *
392      * \return A new Mdrunner.
393      *
394      * \throws APIError if a required component has not been added before calling build().
395      */
396     Mdrunner build();
397
398     /*!
399      * \brief Set up non-bonded short-range force calculations.
400      *
401      * Required. Director code must provide valid options for the non-bonded
402      * interaction code. The builder does not apply any defaults.
403      *
404      * \param nbpu_opt Target short-range interactions for "cpu", "gpu", or "auto".
405      *
406      * Calling must guarantee that the pointed-to C string is valid through
407      * simulation launch.
408      *
409      * \internal
410      * \todo Replace with string or enum that we can have sensible defaults for.
411      * \todo Either the Builder or modular Director code should provide sensible defaults.
412      */
413     MdrunnerBuilder& addNonBonded(const char* nbpu_opt);
414
415     /*!
416      * \brief Set up long-range electrostatics calculations.
417      *
418      * Required. Director code should provide valid options for PME electrostatics,
419      * whether or not PME electrostatics are used. The builder does not apply
420      * any defaults, so client code should be prepared to provide (e.g.) "auto"
421      * in the event no user input or logic provides an alternative argument.
422      *
423      * \param pme_opt Target long-range interactions for "cpu", "gpu", or "auto".
424      * \param pme_fft_opt Target long-range interactions FFT/solve stages for "cpu", "gpu", or "auto".
425      *
426      * Calling must guarantee that the pointed-to C strings are valid through
427      * simulation launch.
428      *
429      * \internal
430      * The arguments are passed as references to elements of arrays of C strings.
431      * \todo Replace with modern strings or (better) enum classes.
432      * \todo Make optional and/or encapsulate into electrostatics module.
433      */
434     MdrunnerBuilder& addElectrostatics(const char* pme_opt, const char* pme_fft_opt);
435
436     /*!
437      * \brief Assign responsibility for tasks for bonded interactions.
438      *
439      * Required. Director code should provide valid options for
440      * bonded interaction task assignment, whether or not such
441      * interactions are present. The builder does not apply any
442      * defaults, so client code should be prepared to provide
443      * (e.g.) "auto" in the event no user input or logic provides
444      * an alternative argument.
445      *
446      * \param bonded_opt Target bonded interactions for "cpu", "gpu", or "auto".
447      *
448      * Calling must guarantee that the pointed-to C strings are valid through
449      * simulation launch.
450      *
451      * \internal
452      * The arguments are passed as references to elements of arrays of C strings.
453      * \todo Replace with modern strings or (better) enum classes.
454      * \todo Make optional and/or encapsulate into task assignment module.
455      */
456     MdrunnerBuilder& addBondedTaskAssignment(const char* bonded_opt);
457
458     /*! \brief
459      * Assign responsibility for tasks for update and constrain calculation.
460      *
461      * Required. Director code should provide valid options for
462      * update and constraint task assignment. The builder does not apply any
463      * defaults, so client code should be prepared to provide
464      * (e.g.) "auto" in the event no user input or logic provides
465      * an alternative argument.
466      *
467      * \param[in] update_opt Target update calculation for "cpu", "gpu", or "auto".
468      *
469      * Calling must guarantee that the pointed-to C strings are valid through
470      * simulation launch.
471      *
472      * \internal
473      * The arguments are passed as references to elements of arrays of C strings.
474      * \todo Replace with modern strings or (better) enum classes.
475      * \todo Make optional and/or encapsulate into task assignment module.
476      */
477     MdrunnerBuilder& addUpdateTaskAssignment(const char* update_opt);
478
479     /*!
480      * \brief Set MD options not owned by some other module.
481      *
482      * Optional. Override simulation parameters
483      *
484      * \param options structure to copy
485      * \param forceWarningThreshold Print a warning if any force is larger than this (in kJ/mol nm) (default -1)
486      * \param startingBehavior Whether the simulation will start afresh, or restart with/without appending.
487      *
488      * \internal
489      * \todo Map these parameters to more appropriate encapsulating types.
490      * Find a better way to indicate "unspecified" than a magic value of the parameter type.
491      */
492     MdrunnerBuilder& addSimulationMethod(const MdrunOptions& options,
493                                          real                forceWarningThreshold,
494                                          StartingBehavior    startingBehavior);
495
496     /*!
497      * \brief Set the domain decomposition module.
498      *
499      * Optional. Overrides default constructed DomdecOptions if provided.
500      *
501      * \param options options with which to construct domain decomposition.
502      *
503      * \internal
504      * \todo revisit whether we should be passing this parameter struct or a higher-level handle of some sort.
505      */
506     MdrunnerBuilder& addDomainDecomposition(const DomdecOptions& options);
507
508     /*!
509      * \brief Set Verlet list manager.
510      *
511      * Optional. Neighbor list existence, type, and parameters are mostly determined
512      * by the simulation parameters loaded elsewhere. This is just an override.
513      *
514      * \param rebuildInterval override for the duration of a neighbor list with the Verlet scheme.
515      */
516     MdrunnerBuilder& addNeighborList(int rebuildInterval);
517
518     /*!
519      * \brief Set replica exchange manager.
520      *
521      * Optional. For guidance on preparing a valid ReplicaExchangeParameters
522      * value, refer to the details in mdrun.cpp, the `t_pargs pa[]` defined there,
523      * and the action of parse_common_args() with regards to that structure.
524      * If not provided by client, a default constructed ReplicaExchangeParameters
525      * is used.
526      *
527      * \param params parameters with which to set up replica exchange.
528      *
529      * \internal
530      * \todo revisit whether we should be passing this parameter struct or a higher-level handle of some sort.
531      */
532     MdrunnerBuilder& addReplicaExchange(const ReplicaExchangeParameters& params);
533
534     /*!
535      * \brief Specify parameters determining hardware resource allocation.
536      *
537      * Optional. If not provided, default-constructed gmx_hw_opt_t will be used.
538      *
539      * \param hardwareOptions Parallelism-related user options.
540      */
541     MdrunnerBuilder& addHardwareOptions(const gmx_hw_opt_t& hardwareOptions);
542
543     /*!
544      * \brief Provide the filenames options structure with option values chosen
545      *
546      * Required. The object is assumed to have been updated by
547      * parse_common_args or equivalent.
548      *
549      * \param filenames Filenames and properties from command-line argument values or defaults.
550      *
551      * \internal
552      * \todo Modules should manage their own filename options and defaults.
553      */
554     MdrunnerBuilder& addFilenames(ArrayRef<const t_filenm> filenames);
555
556     /*!
557      * \brief Provide parameters for setting up output environment.
558      *
559      * Required. Handle is assumed to have been produced by output_env_init
560      * as in parse_common_args.
561      *
562      * \param outputEnvironment Output context for writing text files.
563      *
564      * \internal
565      * \todo Allow client code to set up output environment and provide as a resource.
566      * This parameter is used to set up resources that are dependent on the execution
567      * environment and API context. Such resources should be retrieved by the simulator
568      * from a client-provided resource, but currently the resources are only fully
569      * initialized in Mdrunner.
570      */
571     MdrunnerBuilder& addOutputEnvironment(gmx_output_env_t* outputEnvironment);
572
573     /*!
574      * \brief Provide the filehandle pointer to be used for the MD log.
575      *
576      * Required. Either nullptr if no log should be written, or
577      * valid and open reading for writing.
578      *
579      * \param logFileHandle Non-owning handle to file used for logging.
580      * \internal
581      */
582     MdrunnerBuilder& addLogFile(t_fileio* logFileHandle);
583
584     /*!
585      * \brief Provide a StopHandlerBuilder for the MD stop signal handling.
586      *
587      * Optional. Defaults to empty.
588      *
589      * Client may provide additional (non-default) issuers of simulation stop
590      * signals by preconfiguring the StopHandlerBuilder used later when the
591      * simulation runs.
592      *
593      * \param builder
594      */
595     MdrunnerBuilder& addStopHandlerBuilder(std::unique_ptr<StopHandlerBuilder> builder);
596
597     /*!
598      * \brief Acquire a handle to the SimulationInput.
599      *
600      * Required. SimulationInput will be taking responsibility for some of the
601      * input provided through other methods, such as addFilenames.
602      *
603      * See also issue https://gitlab.com/gromacs/gromacs/-/issues/3374
604      *
605      * \param input Shared ownership of a SimulationInput.
606      */
607     MdrunnerBuilder& addInput(SimulationInputHandle input);
608
609     ~MdrunnerBuilder();
610
611 private:
612     std::unique_ptr<Mdrunner::BuilderImplementation> impl_;
613 };
614
615 } // namespace gmx
616
617 #endif // GMX_MDRUN_RUNNER_H