Apply clang-format to source tree
[alexxy/gromacs.git] / src / api / cpp / include / gmxapi / session.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 /*! \file
36  * \brief Launch and manage a GROMACS session.
37  *
38  * \author M. Eric Irrgang <ericirrgang@gmail.com>
39  *
40  * \ingroup gmxapi
41  */
42
43 #ifndef GROMACS_SESSION_H
44 #define GROMACS_SESSION_H
45
46 /*! \file
47  * \brief Declarations for a workflow execution session and helper functions.
48  *
49  * \ingroup gmxapi
50  */
51
52 #include <memory>
53 #include <string>
54
55 namespace gmxapi
56 {
57
58 // forward declarations
59 class Context; // defined in gmxapi/context.h
60 class MDModule;
61 class Status;   // defined in gmxapi/status.h
62 class Workflow; // implementation detail
63
64 /*!
65  * \brief Private implementation class for a session.
66  *
67  * Actual implementation class may depend on the execution context, but this should be irrelevant to
68  * a client. The implementation details are not exposed in the high-level API, but may be in the
69  * extension API. See developer documentation for details.
70  *
71  * \ingroup gmxapi
72  */
73 class SessionImpl;
74
75 /*!
76  * \brief Workflow execution session.
77  *
78  * When a workflow is launched in an execution context, the result is a Session object
79  * that serves as a handle to interact with the running workflow. The handle allows dynamic changes
80  * to the workflow, or control or data crossing the API boundary.
81  *
82  * Separating run() from construction allows the client to examine the running execution
83  * environment or to retrieve the communicator before beginning long-running computation.
84  *
85  * The session should be explicitly `close()`ed before destruction to allow exception
86  * handling during shutdown. The destructor will close() the session if it is not
87  * closed already, but errors may be hidden.
88  *
89  * The session owns a Status object that can be shared and retained by client code
90  * for further inspection.
91  *
92  * \ingroup gmxapi
93  */
94 class Session
95 {
96 public:
97     /// A session must be created by launching a workflow in an execution context.
98     Session() = delete;
99
100     /*! \brief A session cannot be copied, only moved.
101      *
102      * For shared ownership of a session, use a shared pointer.
103      * \{
104      */
105     Session(const Session&) = delete;
106     Session& operator=(const Session&) = delete;
107     //! \}
108
109     /*!
110      * \brief Pass ownership of a Session.
111      *
112      * \{
113      */
114     Session(Session&&) noexcept = default;
115     Session& operator=(Session&&) noexcept = default;
116     //! \}
117
118     /*!
119      * \brief Construct by taking ownership of an implementation object.
120      *
121      * \param impl Concrete object to take ownership of.
122      */
123     explicit Session(std::unique_ptr<SessionImpl> impl) noexcept;
124
125     /*!
126      * \brief Destroy Session.
127      *
128      * If the session is still active (has not been closed) then it will be closed
129      * with exceptions suppressed. If possible, problems encountered will be
130      * noted in the Status object, which the client may have retained shared
131      * ownership of.
132      */
133     ~Session();
134
135     /*!
136      * \brief Close a running session.
137      *
138      * close() should be called before destroying the Session object so that the
139      * client can catch any exceptions thrown during shut down that may be
140      * unavoidable in the parallel computing environment.
141      *
142      * \return status of close() operation.
143      */
144     Status close();
145
146     /*!
147      * \brief Run the current workflow to completion.
148      *
149      * The client should examine the Status object for errors and resulting workflow state.
150      * \return the Session's Status after the run.
151      */
152     Status run() noexcept;
153
154     /*!
155      * \brief Check if session is running.
156      *
157      * A Session will be "open" from the point of launch until "close" is
158      * called. If this is not true, either an error has occurred or there is
159      * a bug in the implementation.
160      *
161      * \return `true` if the session is running, else `false`
162      */
163     bool isOpen() const noexcept;
164
165     /*! \cond internal
166      * \brief Get a non-owning handle to the implementation object.
167      *
168      * Get a raw pointer to the implementation object. The pointer is valid only during the lifetime of the Session,
169      * so retain a shared pointer to this Session object or only hold the pointer for the duration of a code block
170      * guaranteed to exist entirely within the lifetime of a Session object.
171      *
172      * \return opaque pointer used by gmxapi implementation and extension code.
173      */
174     SessionImpl* getRaw() const noexcept;
175     //! \endcond
176
177 private:
178     //! \brief opaque pointer to implementation
179     std::unique_ptr<SessionImpl> impl_;
180 };
181
182 /*!
183  * \brief Add a uniquely identifiable restraint instance to the MD simulator.
184  *
185  * \param session Session with an MD runner to attach a restraint to.
186  * \param restraint wrapped restraint force calculation object.
187  * \return success if restraint was attached successfully, else failure.
188  *
189  * \todo Clarify whether we are adding modules generally, adding restraint modules, or adding restraints.
190  * \todo Update for new scheme in which all restraints are managed by a single Restraint module.
191  * \todo Figure out what this object should return to provide more utility and rigorous error checking.
192  */
193 Status addSessionRestraint(Session* session, std::shared_ptr<gmxapi::MDModule> restraint);
194
195 /*!
196  * \brief Launch a workflow in the provided execution context.
197  *
198  * \param context Execution environment
199  * \param work Directed acyclic graph defining workflow and data flow.
200  * \return non-unique ownership of the session or nullptr in failure.
201  *
202  * The provided context maintains a weak reference to the executing session, while
203  * the session extends the life of the context.
204  *
205  * If session fails to launch (nullptr returned) examine the Status of the context
206  * for details.
207  * \ingroup gmxapi
208  */
209 std::shared_ptr<Session> launchSession(Context* context, const Workflow& work) noexcept;
210
211
212 } // end namespace gmxapi
213
214 #endif // GROMACS_SESSION_H