2e4ca42e3145db57dc089babf2f1b8be17d7a92c
[alexxy/gromacs.git] / src / gromacs / mdrun / simulationcontext.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019, 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 #ifndef GROMACS_SIMULATIONCONTEXT_H
36 #define GROMACS_SIMULATIONCONTEXT_H
37
38 /*! \libinternal
39  * \file
40  * \brief Provide ways for client code to own simulation resources.
41  *
42  * For `gmx mdrun` to be implemented as a client program, public API needs to
43  * provide a way to create and manipulate the SimulationContext.
44  *
45  * \author M. Eric Irrgang <ericirrgang@gmail.com>
46  * \ingroup module_mdrun
47  * \inlibraryapi
48  */
49
50 #include <memory>
51 #include <string>
52
53 #include "gromacs/mdrunutility/multisim.h"
54 #include "gromacs/utility/gmxmpi.h"
55
56 struct gmx_multisim_t;
57
58 namespace gmx
59 {
60
61 template<typename T>
62 class ArrayRef;
63
64 /*! \cond libapi
65  * \libinternal
66  * \brief Simulation environment and configuration.
67  *
68  * SimulationContext allows a simulation module (\relates gmx::mdrun) to retrieve
69  * runtime parameters and resources from client code. The client retains ownership
70  * of the context and its resources, with exceptions as noted.
71  *
72  * A client must share ownership of some resources and transfer ownership of
73  * other resources to create or configure the context. The simulation may in
74  * turn consume or borrow some resources from the context. This is a new
75  * framework that will evolve in the contexts of
76  * https://redmine.gromacs.org/issues/2375
77  * https://redmine.gromacs.org/issues/2587
78  *
79  * The public interface of SimulationContext is not yet well-specified.
80  * Client code can create an instance with gmx::createSimulationContext()
81  *
82  * \todo This class should also handle aspects of simulation
83  * environment such as working directory and environment variables.
84  *
85  * \ingroup module_mdrun
86  * \inlibraryapi
87  *
88  * \internal
89  * This is a minimal placeholder for a more complete implementation.
90  * Interfaces for different API levels are not yet final.
91  * \todo Impose sensible access restrictions.
92  * Either the SimulationContext should be passed to the Mdrunner as logically constant or
93  * a separate handle class can provide access to resources that have been
94  * allocated by (negotiated with) the client for the current simulation
95  * (or simulation segment).
96  * Non-const accessors to shared resources need to be associated with update
97  * signals that the simulation components (modules and runner) can subscribe to.
98  *
99  * Also reference https://redmine.gromacs.org/issues/2587
100  */
101 class SimulationContext final
102 {
103 public:
104     /*!
105      * \brief Object must be initialized with non-default constructor.
106      */
107     SimulationContext() = delete;
108     /*!
109      * \brief Construct
110      *
111      * \param communicator            MPI communicator for this (set of) simulations
112      * \param multiSimDirectoryNames  Names of any directories used with -multidir
113      */
114     explicit SimulationContext(MPI_Comm communicator, ArrayRef<const std::string> multiSimDirectoryNames);
115
116     /*!
117      * \brief MPI communicator object for this simulation object.
118      *
119      * With real MPI,
120      *   the gmx wrapper binary has called MPI_Init, thus
121      *     MPI_COMM_WORLD is now valid to use, and
122      *   (in future) the gmxapi runner will handle such details
123      *     (e.g. via mpi4py) before creating its SimulationContext.
124      * In both cases, if a multi-simulation is in use, then its
125      * communicator(s) are found in multiSimulation_. This
126      * communicator is that of all ranks from all simulations, and
127      * will later be split into one for each simulation.
128      * TODO Perhaps (for simplicity) that communicator splitting
129      * task can be undertaken during multi-sim setup.
130      *
131      * With thread-MPI in both cases, the communicator is set up later
132      * during the process of spawning the threads that will be the MPI
133      * ranks. (Multi-simulation is not supported with thread-MPI.)
134      */
135     MPI_Comm communicator_ = MPI_COMM_NULL;
136
137     /*!
138      * \brief Multi-sim handler (if required by e.g. gmx mdrun
139      * -multidir; only supported with real MPI)
140      */
141     std::unique_ptr<gmx_multisim_t> multiSimulation_;
142 };
143 //! \endcond
144
145 } // end namespace gmx
146
147 #endif // GROMACS_SIMULATIONCONTEXT_H