c705d08a432d0d4015dee2d0d930b31451f2341a
[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
44 #include <memory>
45 #include <string>
46 #include <vector>
47
48 namespace gmxapi
49 {
50
51 class Workflow;
52 class Session;
53
54 /*!
55  * \brief Container for arguments passed to the simulation runner.
56  *
57  * This is part of an implementation that essentially wraps the command line
58  * implementation at a high level.
59  * \todo Modernize expression and handling of MD user options.
60  */
61 using MDArgs = std::vector<std::string>;
62
63 /*!
64  * \brief Context implementation abstract base class.
65  *
66  * Context Implementations depend on the execution environment, hardware resources, and
67  * possibly other factors, and so are not constructed directly but by helper functions.
68  * Their details are not exposed at the high level API.
69  * \ingroup gmxapi
70  */
71 class ContextImpl;
72
73 /*!
74  * \brief Execution context.
75  *
76  * The execution context represents computing resources and zero, one, or more
77  * workflows to execute. All API objects exist in some context, which determines
78  * how the API objects interact underneath the hood.
79  *
80  * A proxy can be configured with information needed to initialize a runtime
81  * environment capable of executing a work load, independently of defining the
82  * work.
83  * The actual execution
84  * environment is not necessarily instantiated / configured until the work is
85  * performed.
86  * Thus, construction of a Context object does not necessarily imply
87  * initialization of compute resources, but any active compute resources are
88  * appropriately deinitialized when the object is destroyed. However, to allow
89  * opportunities for exception handling, resources should be deinitialized when
90  * and as work is completed by the Runner.
91  *
92  * Ultimately, the class in this header file should just define an interface.
93  * Implementations for different execution environments will be provided by the
94  * library or extension code and documented separately,
95  * and it should be straight-forward to
96  * write extension code for other execution environments.
97  *
98  * \todo Further encapsulate resource state transitions with Session objects.
99  *
100  * \ingroup gmxapi
101  */
102 class Context
103 {
104 public:
105     /*!
106      * \brief Get a handle to a new default context object.
107      */
108     Context();
109     ~Context();
110
111     /*!
112      * \brief Nearly trivial copy
113      *
114      * \{
115      */
116     Context(const Context&) = default;
117     Context& operator=(const Context&) = default;
118     //! \}
119
120     /*!
121      * \brief Allow move
122      *
123      * \{
124      */
125     Context(Context&&) = default;
126     Context& operator=(Context&&) = default;
127     //! \}
128
129     /*!
130      * \brief Construct by wrapping an implementation object.
131      *
132      * \param impl Ownership of Context definition and resources.
133      */
134     explicit Context(std::shared_ptr<ContextImpl> impl);
135
136     /*!
137      * \brief Set the simulation runtime arguments for this instance.
138      *
139      * \param mdArgs User-provided runtime parameters (such as for `gmx mdrun`)
140      *
141      * This is awkwardly named and due for some evolution, since most of the mdrun CLI options
142      * pertain to the execution environment rather than the simulation parameters. For the first
143      * implementation, we just map user arguments to the equivalent command-line substrings.
144      */
145     void setMDArgs(const MDArgs& mdArgs);
146
147     /*!
148      * \brief Launch a workflow in the current context, if possible.
149      *
150      * \param work Configured workflow to instantiate.
151      * \return Ownership of a new session or nullptr if not possible.
152      */
153     std::shared_ptr<Session> launch(const Workflow& work);
154
155 private:
156     /*!
157      * \brief Private implementation
158      *
159      * Early implementation has not yet developed distinct handle classes
160      * for different levels of access to the Context. In this implementation,
161      * new Context handles to the same resources can be created (in library code)
162      * by constructing from a smart pointer to an implementation object.
163      *
164      * \todo Consider client requirements and ownership contracts.
165      * Whether/how API client might be required to create and hold a Context
166      * and/or Session object for the length of a process.
167      */
168     std::shared_ptr<ContextImpl> impl_;
169 };
170
171 } // end namespace gmxapi
172
173 #endif // header guard