Allow MPI-enabled clients to provide the library communicator.
[alexxy/gromacs.git] / api / gmxapi / include / gmxapi / context.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 #ifndef GMXAPI_CONTEXT_H
36 #define GMXAPI_CONTEXT_H
37 /*! \file
38  * \brief Declares classes representing execution context.
39  *
40  * \author M. Eric Irrgang <ericirrgang@gmail.com>
41  * \ingroup gmxapi
42  *
43  * # Notes on scope
44  *
45  * Client owns Context and MultiProcessingResources, which exist entirely in client scope.
46  *
47  * Note that MpiContextManager and Session live in library scope.
48  *
49  * # MPI-enabled clients
50  *
51  * When an MPI toolchain is compatible with the GROMACS build
52  * (see [#3672](https://gitlab.com/gromacs/gromacs/-/issues/3672)),
53  * multi-process clients may determine how processes are allocated to GROMACS
54  * simulations by providing Contexts with specific MPI Communicators to
55  * createContext(const MultiProcessingResources& resources)
56  *
57  * MultiProcessingResources is an opaque type of library-internal resources.
58  * Clients acquire such resources with the assignResource() template helper.
59  * See gmxapi_mpi.h
60  */
61
62 #include <memory>
63 #include <string>
64 #include <vector>
65
66 namespace gmxapi
67 {
68
69 class Workflow;
70 class Session;
71
72 /*!
73  * \brief Container for arguments passed to the simulation runner.
74  *
75  * This is part of an implementation that essentially wraps the command line
76  * implementation at a high level.
77  * \todo Modernize expression and handling of MD user options.
78  */
79 using MDArgs = std::vector<std::string>;
80
81 /*!
82  * \brief Context implementation abstract base class.
83  *
84  * Context Implementations depend on the execution environment, hardware resources, and
85  * possibly other factors, and so are not constructed directly but by helper functions.
86  * Their details are not exposed at the high level API.
87  * \ingroup gmxapi
88  */
89 class ContextImpl;
90
91 /*!
92  * \brief Execution context.
93  *
94  * The execution context represents computing resources and zero, one, or more
95  * workflows to execute. All API objects exist in some context, which determines
96  * how the API objects interact underneath the hood.
97  *
98  * A proxy can be configured with information needed to initialize a runtime
99  * environment capable of executing a work load, independently of defining the
100  * work.
101  * The actual execution
102  * environment is not necessarily instantiated / configured until the work is
103  * performed.
104  * Thus, construction of a Context object does not necessarily imply
105  * initialization of compute resources, but any active compute resources are
106  * appropriately deinitialized when the object is destroyed. However, to allow
107  * opportunities for exception handling, resources should be deinitialized when
108  * and as work is completed by the Runner.
109  *
110  * Ultimately, the class in this header file should just define an interface.
111  * Implementations for different execution environments will be provided by the
112  * library or extension code and documented separately,
113  * and it should be straight-forward to
114  * write extension code for other execution environments.
115  *
116  * \todo Further encapsulate resource state transitions with Session objects.
117  *
118  * \ingroup gmxapi
119  */
120 class Context
121 {
122 public:
123     /*!
124      * \brief Get a handle to a new default context object.
125      */
126     Context() = delete;
127     ~Context();
128
129     /*!
130      * \brief Nearly trivial copy
131      *
132      * \{
133      */
134     Context(const Context&) = default;
135     Context& operator=(const Context&) = default;
136     //! \}
137
138     /*!
139      * \brief Allow move
140      *
141      * \{
142      */
143     Context(Context&&) = default;
144     Context& operator=(Context&&) = default;
145     //! \}
146
147     /*!
148      * \brief Construct by wrapping an implementation object.
149      *
150      * \param impl Ownership of Context definition and resources.
151      */
152     explicit Context(std::shared_ptr<ContextImpl> impl);
153
154     /*!
155      * \brief Set the simulation runtime arguments for this instance.
156      *
157      * \param mdArgs User-provided runtime parameters (such as for `gmx mdrun`)
158      *
159      * This is awkwardly named and due for some evolution, since most of the mdrun CLI options
160      * pertain to the execution environment rather than the simulation parameters. For the first
161      * implementation, we just map user arguments to the equivalent command-line substrings.
162      */
163     void setMDArgs(const MDArgs& mdArgs);
164
165     /*!
166      * \brief Launch a workflow in the current context, if possible.
167      *
168      * \param work Configured workflow to instantiate.
169      * \return Ownership of a new session or nullptr if not possible.
170      */
171     std::shared_ptr<Session> launch(const Workflow& work);
172
173 private:
174     /*!
175      * \brief Private implementation
176      *
177      * Early implementation has not yet developed distinct handle classes
178      * for different levels of access to the Context. In this implementation,
179      * new Context handles to the same resources can be created (in library code)
180      * by constructing from a smart pointer to an implementation object.
181      *
182      * \todo Consider client requirements and ownership contracts.
183      * Whether/how API client might be required to create and hold a Context
184      * and/or Session object for the length of a process.
185      */
186     std::shared_ptr<ContextImpl> impl_;
187 };
188
189 // Forward declaration for interoperation with the library.
190 // Client code implements a concrete ResourceAssignment indirectly by instantiating
191 // assignResource() through the gmxapi_mpi.h template header.
192 class ResourceAssignment;
193
194 // See template header gmxapi_mpi.h
195 template<typename CommT>
196 std::unique_ptr<ResourceAssignment> assignResource(CommT communicator);
197
198 /*!
199  * \brief Initialize a new API Context to manage resources and software environment.
200  *
201  * The client is responsible for keeping the Context instance alive for at least
202  * as long as any API objects initialized from it. We allow this responsibility
203  * to be placed on the client (rather than using a global Singleton) because the
204  * library is theoretically reentrant, and multiple Context objects may exist.
205  *
206  * Client may explicitly assign run time computing resources for use within the Context.
207  * Otherwise, the library will try to automatically detect and allocate computing resources.
208  *
209  * Currently, only MPI communication scope can be assigned explicitly.
210  * Future development is needed before this interface can influence the subdivision
211  * of available GPUs or thread-based processor allocation. See #3650 and #3688.
212  *
213  * \return Initialized Context instance.
214  *
215  * \internal
216  * Use cases:
217  *
218  * 1. tMPI and client-provided comm: provide a place for safety checks, then construct a suitable
219  * dummy MpiContextManager.
220  * 2. tMPI and no client-provided comm: construct suitable dummy MpiContextManager
221  * 3. MPI and client-provided comm: use compatible comm. error if COMM_NULL
222  * 4. MPI and no client-provided comm: generate MpiContextManager with COMM_WORLD
223  *
224  */
225 Context createContext();
226 Context createContext(const ResourceAssignment& resources);
227
228 } // end namespace gmxapi
229
230 #endif // header guard