Eliminate mdlib/mdrun.h
[alexxy/gromacs.git] / src / gromacs / mdlib / checkpointhandler.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 /*! \libinternal \file
36  * \brief
37  * Declares the checkpoint handler class.
38  *
39  * This class sets the signal to checkpoint based on the elapsed simulation time,
40  * and handles the signal if it is received. When handling the signal (via
41  * decideIfCheckpointingThisStep()), it is deciding whether a checkpoint should be saved
42  * at the current step. This can be due to a received signal, or if the current simulation
43  * step is the last. This information can be queried via the isCheckpointingStep() function.
44  *
45  * The setting and handling is implemented in private functions. They are only called
46  * if a respective boolean is true. For the trivial case of no checkpointing (or no checkpoint
47  * signal setting on any other rank than master), the translation unit of the calling
48  * function is therefore never left. In the future, this will be achieved by adding
49  * (or not adding) handlers / setters to the task graph.
50  *
51  * \author Pascal Merz <pascal.merz@colorado.edu>
52  * \inlibraryapi
53  * \ingroup module_mdlib
54  */
55 #ifndef GMX_MDLIB_CHECKPOINTHANDLER_H
56 #define GMX_MDLIB_CHECKPOINTHANDLER_H
57
58 #include <memory>
59
60 #include "gromacs/compat/pointers.h"
61 #include "gromacs/mdlib/simulationsignal.h"
62
63 struct gmx_walltime_accounting;
64
65 namespace gmx
66 {
67 /*! \brief Checkpoint signals
68  *
69  * Signals set and read by CheckpointHandler. Possible signals include
70  *   * nothing to signal
71  *   * do checkpoint (at next NS step)
72  */
73 enum class CheckpointSignal
74 {
75     noSignal = 0, doCheckpoint = 1
76 };
77
78 /*! \libinternal
79  * \brief Class handling the checkpoint signal
80  *
81  * Master rank sets the checkpointing signal periodically
82  * All ranks receive checkpointing signal and set the respective flag
83  */
84 class CheckpointHandler final
85 {
86     public:
87         /*! \brief CheckpointHandler constructor
88          *
89          * Needs a non-null pointer to the signal which is reduced by compute_globals, and
90          * (const) references to data it needs to determine whether a signal needs to be
91          * set or handled.
92          */
93         CheckpointHandler(
94             compat::not_null<SimulationSignal*> signal,
95             bool                                simulationsShareState,
96             bool                                neverUpdateNeighborList,
97             bool                                isMaster,
98             bool                                writeFinalCheckpoint,
99             real                                checkpointingPeriod);
100
101         /*! \brief Decides whether a checkpointing signal needs to be set
102          *
103          * Checkpointing signal is set based on the elapsed run time and the checkpointing
104          * interval.
105          */
106         void setSignal(gmx_walltime_accounting *walltime_accounting) const
107         {
108             if (rankCanSetSignal_)
109             {
110                 setSignalImpl(walltime_accounting);
111             }
112         }
113
114         /*! \brief Decides whether a checkpoint shall be written at this step
115          *
116          * Checkpointing is done if this is not the initial step, and
117          *   * a signal has been set and the current step is a neighborlist creation
118          *     step, or
119          *   * the current step is the last step and a the simulation is writing
120          *     configurations.
121          */
122         void decideIfCheckpointingThisStep(bool bNS, bool bFirstStep, bool bLastStep)
123         {
124             if (checkpointingIsActive_)
125             {
126                 decideIfCheckpointingThisStepImpl(bNS, bFirstStep, bLastStep);
127             }
128         }
129
130         //! Query decision in decideIfCheckpointingThisStep()
131         bool isCheckpointingStep() const
132         {
133             return checkpointThisStep_;
134         }
135
136     private:
137         void setSignalImpl(gmx_walltime_accounting *walltime_accounting) const;
138
139         void decideIfCheckpointingThisStepImpl(bool bNS, bool bFirstStep, bool bLastStep);
140
141         SimulationSignal &signal_;
142         bool              checkpointThisStep_;
143         int               numberOfNextCheckpoint_;
144
145         const bool        rankCanSetSignal_;
146         const bool        checkpointingIsActive_;
147         const bool        writeFinalCheckpoint_;
148         const bool        neverUpdateNeighborlist_;
149         const real        checkpointingPeriod_;
150 };
151 }      // namespace gmx
152
153 #endif // GMX_MDLIB_CHECKPOINTHANDLER_H