113dcfaa877a464e5d97cac03b8a37d1422e8eda
[alexxy/gromacs.git] / src / api / cpp / mdsignals.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018, 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
36 #ifndef GMXAPI_MDSIGNALS_IMPL_H
37 #define GMXAPI_MDSIGNALS_IMPL_H
38
39 /*! \file
40  * \brief Implementation details for gmxapi::Signal and related gmxapi::md infrastructure.
41  *
42  * \ingroup gmxapi_md
43  */
44
45 #include <atomic>
46 #include <functional>
47
48 #include "gromacs/compat/make_unique.h"
49 #include "gromacs/mdlib/simulationsignal.h"
50 #include "gromacs/mdlib/stophandler.h"
51 #include "gromacs/mdrun/runner.h"
52
53 #include "gmxapi/session.h"
54 #include "gmxapi/md/mdsignals.h"
55
56 #include "session-impl.h"
57
58 namespace gmxapi
59 {
60
61
62 /*! \internal
63  * \brief The Signal Implementation interface.
64  *
65  * A SignalImpl concrete class must implement a `call()` method that issues the signal.
66  *
67  * \ingroup gmxapi_md
68  */
69 class Signal::SignalImpl
70 {
71     public:
72         //! Required functor behavior.
73         virtual void call()   = 0;
74
75         //! May be subclassed.
76         virtual ~SignalImpl() = default;
77 };
78
79 /*!
80  * \brief Manage signal paths exposed through session resources to gmxapi operations.
81  *
82  * Manages signals for a single gmx::Mdrunner. Currently only supports a stop signal that
83  * is required to be issued by all registered possible issuers before the signal is sent to
84  * the associated runner. This is not what we want in the long run.
85  * \todo This class should handle signal inputs to operations that take signals as input (like Mdrunner)
86  * and
87  * \todo should allow multiple subscribers.
88  * For additional signal processing, such as boolean operations,
89  * additional operations should be inserted in a chain.
90  *
91  * SignalManager objects are created during Session launch and are owned exclusively by session
92  * implementation objects. If Session::isOpen() is true, the SignalManager should still be valid,
93  * but the intended use case is that SignalManager handles should be retrieved immediately before use
94  * by implementation code within the library with SessionImpl::getSignalManager().
95  *
96  * A SignalManager should be created for each signal consumer (each gmx::Mdrunner) in a Session.
97  * This occurs in the SessionImpl::create() function.
98  *
99  * \ingroup gmxapi_md
100  */
101 class SignalManager
102 {
103     public:
104         /*!
105          * \brief Set up a manager to mediate access to an upcoming MD stop handler.
106          *
107          * \param mdStopHandlerBuilder access to a builder that can be used during construction.
108          */
109         explicit SignalManager(gmx::StopHandlerBuilder* mdStopHandlerBuilder);
110
111         //! \cond
112         ~SignalManager();
113         //! \endcond
114
115         /*!
116          * \brief Add a name to the list of operations that will be using this signal.
117          */
118         void addSignaller(std::string name);
119
120         /*!
121          * \brief Allow a registered signaller to retrieve a functor.
122          *
123          * \param name Registered signal issuer.
124          * \param signal type of signal the client would like to issue.
125          * \return Generic Signal object.
126          *
127          * \throws gmxapi::ProtocolError if named signaller was not previously registered.
128          */
129         Signal getSignal(std::string name,
130                          md::signals signal);
131
132         /*!
133          * \brief Signal operation that issues only when all sources have issued.
134          *
135          * Implemented as a member class that can access SignalManager's private members.
136          * \todo Decouple logical operations from SignalManager class definition.
137          */
138         class LogicalAND;
139
140     private:
141         //! Non-owning handle to the associated runner.
142         gmx::Mdrunner* runner_;
143
144
145         /*!
146          * \brief State of the stop condition to be returned by the registered MD signaller.
147          *
148          * Ownership is shared by the function objects in the StopConditionHandler
149          * (owned by the simulator), which read the value, and the
150          * SessionImpl SignalManager, which mediates write access.
151          *
152          * The signal state is either gmx::StopSignal::noSignal or gmx::StopSignal::stopAtNextNSStep,
153          * so atomicity is not important, and we share the state across
154          * threads in a tMPI simulation.
155          */
156         std::shared_ptr<gmx::StopSignal> state_;
157
158         /*!
159          * \brief Track whether the signal has been issued by each registrant.
160          *
161          * \todo This is an implementation detail of LogicalAND that should not be here.
162          */
163         std::map<std::string, std::atomic_bool> called_;
164 };
165
166
167
168 }      //end namespace gmxapi
169
170 #endif //GMXAPI_MDSIGNALS_IMPL_H