Fix random typos
[alexxy/gromacs.git] / src / gromacs / mdlib / resethandler.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020,2021, 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 /*! \internal \file
36  * \brief
37  * Defines the reset handler class.
38  *
39  * \author Pascal Merz <pascal.merz@colorado.edu>
40  * \ingroup module_mdlib
41  */
42
43 #include "gmxpre.h"
44
45 #include "resethandler.h"
46
47 #include "gromacs/domdec/domdec.h"
48 #include "gromacs/ewald/pme.h"
49 #include "gromacs/ewald/pme_load_balancing.h"
50 #include "gromacs/ewald/pme_pp.h"
51 #include "gromacs/gmxlib/nrnb.h"
52 #include "gromacs/gpu_utils/gpu_utils.h"
53 #include "gromacs/mdrunutility/printtime.h"
54 #include "gromacs/mdtypes/commrec.h"
55 #include "gromacs/nbnxm/gpu_data_mgmt.h"
56 #include "gromacs/nbnxm/nbnxm.h"
57 #include "gromacs/timing/walltime_accounting.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/utility/fatalerror.h"
60
61 namespace gmx
62 {
63
64 /*! \brief Convert signed char (as used by SimulationSignal) to ResetSignal enum
65  *
66  * Expected values are
67  *   \p sig == 0 -- no signal
68  *   \p sig >= 1 -- signal received
69  */
70 static inline ResetSignal convertToResetSignal(signed char sig)
71 {
72     GMX_ASSERT(sig >= 0, "Unexpected reset signal < 0 received");
73     return sig >= 1 ? ResetSignal::doResetCounters : ResetSignal::noSignal;
74 }
75
76 ResetHandler::ResetHandler(compat::not_null<SimulationSignal*> signal,
77                            bool                                simulationsShareState,
78                            int64_t                             nsteps,
79                            bool                                isMaster,
80                            bool                                resetHalfway,
81                            real                                maximumHoursToRun,
82                            const MDLogger&                     mdlog,
83                            gmx_wallcycle*                      wcycle,
84                            gmx_walltime_accounting_t           walltime_accounting) :
85     signal_(*signal), rankCanSetSignal_(false), simulationNeedsReset_(false), maximumHoursToRun_(maximumHoursToRun)
86 {
87     if (simulationsShareState)
88     {
89         signal_.isLocal = false;
90     }
91     if (resetHalfway)
92     {
93         GMX_LOG(mdlog.info)
94                 .asParagraph()
95                 .appendText(
96                         "The -resethway functionality is deprecated, and may be removed in a "
97                         "future version.");
98         if (nsteps > 0)
99         {
100             /* Signal to reset the counters half the simulation steps. */
101             wcycle_set_reset_counters(wcycle, nsteps / 2);
102         }
103         simulationNeedsReset_ = true;
104
105         if (isMaster && (maximumHoursToRun > 0))
106         {
107             rankCanSetSignal_ = true;
108         }
109     }
110     else if (wcycle_get_reset_counters(wcycle) > 0)
111     {
112         simulationNeedsReset_ = true;
113     }
114     else
115     {
116         // if no reset is happening, this will always be valid
117         walltime_accounting_set_valid_finish(walltime_accounting);
118     }
119 }
120
121 bool ResetHandler::setSignalImpl(gmx_walltime_accounting_t walltime_accounting)
122 {
123     const double secondsSinceStart = walltime_accounting_get_time_since_start(walltime_accounting);
124     if (secondsSinceStart > maximumHoursToRun_ * 60.0 * 60.0 * 0.495)
125     {
126         /* Set flag that will communicate the signal to all ranks in the simulation */
127         signal_.sig = static_cast<signed char>(ResetSignal::doResetCounters);
128         /* Let handler know that we did signal a reset */
129         return true;
130     }
131     /* Let handler know that we did not signal a reset */
132     return false;
133 }
134
135 bool ResetHandler::resetCountersImpl(int64_t                     step,
136                                      int64_t                     step_rel,
137                                      const MDLogger&             mdlog,
138                                      FILE*                       fplog,
139                                      const t_commrec*            cr,
140                                      nonbonded_verlet_t*         nbv,
141                                      t_nrnb*                     nrnb,
142                                      const gmx_pme_t*            pme,
143                                      const pme_load_balancing_t* pme_loadbal,
144                                      gmx_wallcycle*              wcycle,
145                                      gmx_walltime_accounting_t   walltime_accounting)
146 {
147     /* Reset either if signal has been passed, or if reset step has been reached */
148     if (convertToResetSignal(signal_.set) == ResetSignal::doResetCounters
149         || step_rel == wcycle_get_reset_counters(wcycle))
150     {
151         if (pme_loadbal_is_active(pme_loadbal))
152         {
153             /* Do not permit counter reset while PME load
154              * balancing is active. The only purpose for resetting
155              * counters is to measure reliable performance data,
156              * and that can't be done before balancing
157              * completes.
158              *
159              * TODO consider fixing this by delaying the reset
160              * until after load balancing completes,
161              * e.g. https://gerrit.gromacs.org/#/c/4964/2 */
162             gmx_fatal(FARGS,
163                       "PME tuning was still active when attempting to "
164                       "reset mdrun counters at step %" PRId64
165                       ". Try "
166                       "resetting counters later in the run, e.g. with gmx "
167                       "mdrun -resetstep.",
168                       step);
169         }
170
171         char sbuf[STEPSTRSIZE];
172
173         /* Reset all the counters related to performance over the run */
174         GMX_LOG(mdlog.warning)
175                 .asParagraph()
176                 .appendTextFormatted("step %s: resetting all time and cycle counters",
177                                      gmx_step_str(step, sbuf));
178
179         if (nbv && nbv->useGpu())
180         {
181             Nbnxm::gpu_reset_timings(nbv);
182         }
183
184         if (pme_gpu_task_enabled(pme))
185         {
186             pme_gpu_reset_timings(pme);
187         }
188
189         if ((nbv && nbv->useGpu()) || pme_gpu_task_enabled(pme))
190         {
191             resetGpuProfiler();
192         }
193
194         wallcycle_stop(wcycle, WallCycleCounter::Run);
195         wallcycle_reset_all(wcycle);
196         if (haveDDAtomOrdering(*cr))
197         {
198             reset_dd_statistics_counters(cr->dd);
199         }
200         clear_nrnb(nrnb);
201         wallcycle_start(wcycle, WallCycleCounter::Run);
202         walltime_accounting_reset_time(walltime_accounting, step);
203         print_date_and_time(fplog, cr->nodeid, "Restarted time", gmx_gettime());
204
205         wcycle_set_reset_counters(wcycle, -1);
206         if (!thisRankHasDuty(cr, DUTY_PME))
207         {
208             /* Tell our PME node to reset its counters */
209             gmx_pme_send_resetcounters(cr, step);
210         }
211         /* Reset can only happen once, so clear the triggering flag. */
212         signal_.set = static_cast<signed char>(ResetSignal::noSignal);
213         /* We have done a reset, so the finish will be valid. */
214         walltime_accounting_set_valid_finish(walltime_accounting);
215         /* Let handler know that we handled a reset */
216         return true;
217     }
218
219     /* Let handler know that we did not handle a reset */
220     return false;
221 }
222
223 } // namespace gmx