2 * This file is part of the GROMACS molecular simulation package.
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.
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.
36 #ifndef GMXAPI_MDSIGNALS_IMPL_H
37 #define GMXAPI_MDSIGNALS_IMPL_H
40 * \brief Implementation details for gmxapi::Signal and related gmxapi::md infrastructure.
49 #include "gromacs/mdlib/simulationsignal.h"
50 #include "gromacs/mdlib/stophandler.h"
51 #include "gromacs/mdrun/runner.h"
53 #include "gmxapi/session.h"
54 #include "gmxapi/md/mdsignals.h"
56 #include "session-impl.h"
63 * \brief The Signal Implementation interface.
65 * A SignalImpl concrete class must implement a `call()` method that issues the signal.
69 class Signal::SignalImpl
72 //! Required functor behavior.
73 virtual void call() = 0;
75 //! May be subclassed.
76 virtual ~SignalImpl() = default;
80 * \brief Manage signal paths exposed through session resources to gmxapi operations.
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)
87 * \todo should allow multiple subscribers.
88 * For additional signal processing, such as boolean operations,
89 * additional operations should be inserted in a chain.
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().
96 * A SignalManager should be created for each signal consumer (each gmx::Mdrunner) in a Session.
97 * This occurs in the SessionImpl::create() function.
105 * \brief Set up a manager to mediate access to an upcoming MD stop handler.
107 * \param mdStopHandlerBuilder access to a builder that can be used during construction.
109 explicit SignalManager(gmx::StopHandlerBuilder* mdStopHandlerBuilder);
116 * \brief Add a name to the list of operations that will be using this signal.
118 void addSignaller(std::string name);
121 * \brief Allow a registered signaller to retrieve a functor.
123 * \param name Registered signal issuer.
124 * \param signal type of signal the client would like to issue.
125 * \return Generic Signal object.
127 * \throws gmxapi::ProtocolError if named signaller was not previously registered.
129 Signal getSignal(std::string name,
133 * \brief Signal operation that issues only when all sources have issued.
135 * Implemented as a member class that can access SignalManager's private members.
136 * \todo Decouple logical operations from SignalManager class definition.
141 //! Non-owning handle to the associated runner.
142 gmx::Mdrunner* runner_;
146 * \brief State of the stop condition to be returned by the registered MD signaller.
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.
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.
156 std::shared_ptr<gmx::StopSignal> state_;
159 * \brief Track whether the signal has been issued by each registrant.
161 * \todo This is an implementation detail of LogicalAND that should not be here.
163 std::map<std::string, std::atomic_bool> called_;
168 } //end namespace gmxapi
170 #endif //GMXAPI_MDSIGNALS_IMPL_H