c9f9cfe5acb3d6a270f72ff8edb1945f99c97183
[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, 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/mdrun.h"
62 #include "gromacs/mdlib/simulationsignal.h"
63
64 struct gmx_walltime_accounting;
65
66 namespace gmx
67 {
68 /*! \brief Checkpoint signals
69  *
70  * Signals set and read by CheckpointHandler. Possible signals include
71  *   * nothing to signal
72  *   * do checkpoint (at next NS step)
73  */
74 enum class CheckpointSignal
75 {
76     noSignal = 0, doCheckpoint = 1
77 };
78
79 /*! \libinternal
80  * \brief Class handling the checkpoint signal
81  *
82  * Master rank sets the checkpointing signal periodically
83  * All ranks receive checkpointing signal and set the respective flag
84  */
85 class CheckpointHandler final
86 {
87     public:
88         /*! \brief CheckpointHandler constructor
89          *
90          * Needs a non-null pointer to the signal which is reduced by compute_globals, and
91          * (const) references to data it needs to determine whether a signal needs to be
92          * set or handled.
93          */
94         CheckpointHandler(
95             compat::not_null<SimulationSignal*> signal,
96             bool                                simulationsShareState,
97             bool                                neverUpdateNeighborList,
98             bool                                isMaster,
99             bool                                writeFinalCheckpoint,
100             real                                checkpointingPeriod);
101
102         /*! \brief Decides whether a checkpointing signal needs to be set
103          *
104          * Checkpointing signal is set based on the elapsed run time and the checkpointing
105          * interval.
106          */
107         void setSignal(gmx_walltime_accounting *walltime_accounting) const
108         {
109             if (rankCanSetSignal_)
110             {
111                 setSignalImpl(walltime_accounting);
112             }
113         }
114
115         /*! \brief Decides whether a checkpoint shall be written at this step
116          *
117          * Checkpointing is done if this is not the initial step, and
118          *   * a signal has been set and the current step is a neighborlist creation
119          *     step, or
120          *   * the current step is the last step and a the simulation is writing
121          *     configurations.
122          */
123         void decideIfCheckpointingThisStep(bool bNS, bool bFirstStep, bool bLastStep)
124         {
125             if (checkpointingIsActive_)
126             {
127                 decideIfCheckpointingThisStepImpl(bNS, bFirstStep, bLastStep);
128             }
129         }
130
131         //! Query decision in decideIfCheckpointingThisStep()
132         bool isCheckpointingStep() const
133         {
134             return checkpointThisStep_;
135         }
136
137     private:
138         void setSignalImpl(gmx_walltime_accounting *walltime_accounting) const;
139
140         void decideIfCheckpointingThisStepImpl(bool bNS, bool bFirstStep, bool bLastStep);
141
142         SimulationSignal &signal_;
143         bool              checkpointThisStep_;
144         int               numberOfNextCheckpoint_;
145
146         const bool        rankCanSetSignal_;
147         const bool        checkpointingIsActive_;
148         const bool        writeFinalCheckpoint_;
149         const bool        neverUpdateNeighborlist_;
150         const real        checkpointingPeriod_;
151 };
152 }      // namespace gmx
153
154 #endif // GMX_MDLIB_CHECKPOINTHANDLER_H