2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 #ifndef GMXAPI_CONTEXT_H
36 #define GMXAPI_CONTEXT_H
38 * \brief Declares classes representing execution context.
40 * \author M. Eric Irrgang <ericirrgang@gmail.com>
45 * Client owns Context and MultiProcessingResources, which exist entirely in client scope.
47 * Note that MpiContextManager and Session live in library scope.
49 * # MPI-enabled clients
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)
57 * MultiProcessingResources is an opaque type of library-internal resources.
58 * Clients acquire such resources with the assignResource() template helper.
73 * \brief Container for arguments passed to the simulation runner.
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.
79 using MDArgs = std::vector<std::string>;
82 * \brief Context implementation abstract base class.
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.
92 * \brief Execution context.
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.
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
101 * The actual execution
102 * environment is not necessarily instantiated / configured until the work is
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.
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.
116 * \todo Further encapsulate resource state transitions with Session objects.
124 * \brief Get a handle to a new default context object.
130 * \brief Nearly trivial copy
134 Context(const Context&) = default;
135 Context& operator=(const Context&) = default;
143 Context(Context&&) = default;
144 Context& operator=(Context&&) = default;
148 * \brief Construct by wrapping an implementation object.
150 * \param impl Ownership of Context definition and resources.
152 explicit Context(std::shared_ptr<ContextImpl> impl);
155 * \brief Set the simulation runtime arguments for this instance.
157 * \param mdArgs User-provided runtime parameters (such as for `gmx mdrun`)
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.
163 void setMDArgs(const MDArgs& mdArgs);
166 * \brief Launch a workflow in the current context, if possible.
168 * \param work Configured workflow to instantiate.
169 * \return Ownership of a new session or nullptr if not possible.
171 std::shared_ptr<Session> launch(const Workflow& work);
175 * \brief Private implementation
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.
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.
186 std::shared_ptr<ContextImpl> impl_;
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;
194 // See template header gmxapi_mpi.h
195 template<typename CommT>
196 std::unique_ptr<ResourceAssignment> assignResource(CommT communicator);
199 * \brief Initialize a new API Context to manage resources and software environment.
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.
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.
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.
213 * \return Initialized Context instance.
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
225 Context createContext();
226 Context createContext(const ResourceAssignment& resources);
228 } // end namespace gmxapi
230 #endif // header guard