2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013, The GROMACS development team.
5 * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
6 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 #include "walltime_accounting.h"
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h>
52 #include "gromacs/utility/basedefinitions.h"
53 #include "gromacs/utility/smalloc.h"
55 /* TODO in future: convert gmx_walltime_accounting to a class,
56 * resolve who should have responsibility for recording the number of
57 * steps done, consider whether parts of finish_time, print_perf,
58 * wallcycle_print belong in this module.
60 * If/when any kind of task parallelism is implemented (even OpenMP
61 * regions simultaneously assigned to different tasks), consider
62 * whether this data structure (and/or cycle counters) should be
63 * maintained on a per-OpenMP-thread basis.
65 * Consider also replacing this with std::chrono. */
67 /*! \brief Manages caching wall-clock time measurements for
69 typedef struct gmx_walltime_accounting
71 //! Seconds since the epoch recorded at the start of the simulation
72 double start_time_stamp
;
73 /*! \brief Seconds since the epoch recorded at the reset of
74 * counters for the simulation (or the start, if no reset has
76 double reset_time_stamp
;
77 /*! \brief Seconds since the epoch recorded at the reset of
78 * counters for the simulation for this thread (or the start, if
79 * no reset has occured). */
80 double reset_time_stamp_per_thread
;
81 //! Total seconds elapsed over the simulation since counter reset
83 //! Total seconds elapsed over the simulation since counter reset running this thread
84 double elapsed_time_over_all_threads
;
85 /*! \brief Number of OpenMP threads that will be launched by this
88 * This is used to scale elapsed_time_over_all_threads so
89 * that any combination of real MPI, thread MPI and OpenMP (even
90 * mdrun -ntomp_pme) processes/threads would (when run at maximum
91 * efficiency) return values such that the sum of
92 * elapsed_time_over_all_threads over all threads was constant
93 * with respect to parallelism implementation. */
95 //! Numbers of steps done before reset of counters
96 int64_t nsteps_done_at_reset
;
97 //! Set by integrators to report the amount of work they did
99 //! Whether the simulation has finished in a way valid for walltime reporting.
101 } t_gmx_walltime_accounting
;
103 /*! \brief Calls system timing routines (e.g. clock_gettime) to get
104 * the (fractional) number of seconds elapsed since the epoch when
105 * this thread was executing.
107 * This can be used to measure system load. This can be unreliable if
108 * threads migrate between sockets. If thread-specific timers are not
109 * supported by the OS (e.g. if the OS is not POSIX-compliant), this
110 * function is implemented by gmx_gettime. */
111 static double gmx_gettime_per_thread();
113 // TODO In principle, all this should get protected by checks that
114 // walltime_accounting is not nullptr. In practice, that nullptr condition
115 // does not happen, and future refactoring will likely enforce it by
116 // having the gmx_walltime_accounting_t object be owned by the runner
117 // object. When these become member functions, existence will be
120 gmx_walltime_accounting_t
walltime_accounting_init(int numOpenMPThreads
)
122 gmx_walltime_accounting_t walltime_accounting
;
124 snew(walltime_accounting
, 1);
125 walltime_accounting
->start_time_stamp
= 0;
126 walltime_accounting
->reset_time_stamp
= 0;
127 walltime_accounting
->reset_time_stamp_per_thread
= 0;
128 walltime_accounting
->elapsed_time
= 0;
129 walltime_accounting
->nsteps_done_at_reset
= 0;
130 walltime_accounting
->nsteps_done
= 0;
131 walltime_accounting
->numOpenMPThreads
= numOpenMPThreads
;
132 walltime_accounting
->isValidFinish
= false;
134 return walltime_accounting
;
137 void walltime_accounting_destroy(gmx_walltime_accounting_t walltime_accounting
)
139 sfree(walltime_accounting
);
142 void walltime_accounting_reset_time(gmx_walltime_accounting_t walltime_accounting
, int64_t step
)
144 walltime_accounting
->reset_time_stamp
= gmx_gettime();
145 walltime_accounting
->reset_time_stamp_per_thread
= gmx_gettime_per_thread();
146 walltime_accounting
->elapsed_time
= 0;
147 walltime_accounting
->nsteps_done
= 0;
148 walltime_accounting
->nsteps_done_at_reset
= step
;
151 void walltime_accounting_start_time(gmx_walltime_accounting_t walltime_accounting
)
153 walltime_accounting_reset_time(walltime_accounting
, 0);
154 walltime_accounting
->start_time_stamp
= walltime_accounting
->reset_time_stamp
;
157 void walltime_accounting_end_time(gmx_walltime_accounting_t walltime_accounting
)
159 double now
, now_per_thread
;
162 now_per_thread
= gmx_gettime_per_thread();
164 walltime_accounting
->elapsed_time
= now
- walltime_accounting
->reset_time_stamp
;
165 walltime_accounting
->elapsed_time_over_all_threads
=
166 now_per_thread
- walltime_accounting
->reset_time_stamp_per_thread
;
167 /* For thread-MPI, the per-thread CPU timer makes this just
168 * work. For OpenMP threads, the per-thread CPU timer measurement
169 * needs to be multiplied by the number of OpenMP threads used,
170 * under the current assumption that all regions ever opened
171 * within a process are of the same size, and each thread should
172 * keep one core busy.
174 walltime_accounting
->elapsed_time_over_all_threads
*= walltime_accounting
->numOpenMPThreads
;
177 double walltime_accounting_get_time_since_start(gmx_walltime_accounting_t walltime_accounting
)
179 return gmx_gettime() - walltime_accounting
->start_time_stamp
;
182 double walltime_accounting_get_time_since_reset(gmx_walltime_accounting_t walltime_accounting
)
184 return gmx_gettime() - walltime_accounting
->reset_time_stamp
;
187 double walltime_accounting_get_time_since_reset_over_all_threads(gmx_walltime_accounting_t walltime_accounting
)
189 return walltime_accounting
->elapsed_time_over_all_threads
;
192 double walltime_accounting_get_start_time_stamp(gmx_walltime_accounting_t walltime_accounting
)
194 return walltime_accounting
->start_time_stamp
;
197 int64_t walltime_accounting_get_nsteps_done_since_reset(gmx_walltime_accounting_t walltime_accounting
)
199 return walltime_accounting
->nsteps_done
- walltime_accounting
->nsteps_done_at_reset
;
202 void walltime_accounting_set_nsteps_done(gmx_walltime_accounting_t walltime_accounting
, int64_t nsteps_done
)
204 walltime_accounting
->nsteps_done
= nsteps_done
;
209 /* Use clock_gettime only if we know linking the C run-time
210 library will work (which is not trivial on e.g. Crays), and its
211 headers claim sufficient support for POSIX (ie not Mac and
213 #if HAVE_CLOCK_GETTIME && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
217 clock_gettime(CLOCK_REALTIME
, &t
);
218 seconds
= static_cast<double>(t
.tv_sec
) + 1e-9 * t
.tv_nsec
;
221 #elif HAVE_GETTIMEOFDAY
222 // Note that gettimeofday() is deprecated by POSIX, but since Mac
223 // and Windows do not yet support POSIX, we are still stuck.
227 gettimeofday(&t
, nullptr);
228 seconds
= static_cast<double>(t
.tv_sec
) + 1e-6 * t
.tv_usec
;
234 seconds
= time(nullptr);
240 void walltime_accounting_set_valid_finish(gmx_walltime_accounting_t walltime_accounting
)
242 walltime_accounting
->isValidFinish
= true;
245 //! Return whether the simulation finished in a way valid for reporting walltime.
246 bool walltime_accounting_get_valid_finish(const gmx_walltime_accounting
* walltime_accounting
)
248 return walltime_accounting
->isValidFinish
;
251 static double gmx_gettime_per_thread()
253 /* Use clock_gettime only if we know linking the C run-time
254 library will work (which is not trivial on e.g. Crays), and its
255 headers claim sufficient support for POSIX (ie not Mac and
257 #if HAVE_CLOCK_GETTIME && defined(_POSIX_THREAD_CPUTIME) && _POSIX_THREAD_CPUTIME > 0
261 clock_gettime(CLOCK_THREAD_CPUTIME_ID
, &t
);
262 seconds
= static_cast<double>(t
.tv_sec
) + 1e-9 * t
.tv_nsec
;
266 return gmx_gettime();