Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / timing / walltime_accounting.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include "walltime_accounting.h"
40
41 #include "config.h"
42
43 #include <ctime>
44
45 #ifdef HAVE_UNISTD_H
46 #    include <unistd.h>
47 #endif
48 #ifdef HAVE_SYS_TIME_H
49 #    include <sys/time.h>
50 #endif
51
52 #include "gromacs/utility/basedefinitions.h"
53 #include "gromacs/utility/smalloc.h"
54
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.
59  *
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.
64  *
65  * Consider also replacing this with std::chrono. */
66
67 /*! \brief Manages caching wall-clock time measurements for
68  * simulations */
69 typedef struct gmx_walltime_accounting
70 {
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
75      * occured). */
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
82     double elapsed_time;
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
86      * MPI rank.
87      *
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. */
94     int numOpenMPThreads;
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
98     int64_t nsteps_done;
99     //! Whether the simulation has finished in a way valid for walltime reporting.
100     bool isValidFinish;
101 } t_gmx_walltime_accounting;
102
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.
106  *
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();
112
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
118 // guaranteed.
119
120 gmx_walltime_accounting_t walltime_accounting_init(int numOpenMPThreads)
121 {
122     gmx_walltime_accounting_t walltime_accounting;
123
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;
133
134     return walltime_accounting;
135 }
136
137 void walltime_accounting_destroy(gmx_walltime_accounting_t walltime_accounting)
138 {
139     sfree(walltime_accounting);
140 }
141
142 void walltime_accounting_reset_time(gmx_walltime_accounting_t walltime_accounting, int64_t step)
143 {
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;
149 }
150
151 void walltime_accounting_start_time(gmx_walltime_accounting_t walltime_accounting)
152 {
153     walltime_accounting_reset_time(walltime_accounting, 0);
154     walltime_accounting->start_time_stamp = walltime_accounting->reset_time_stamp;
155 }
156
157 void walltime_accounting_end_time(gmx_walltime_accounting_t walltime_accounting)
158 {
159     double now, now_per_thread;
160
161     now            = gmx_gettime();
162     now_per_thread = gmx_gettime_per_thread();
163
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.
173      */
174     walltime_accounting->elapsed_time_over_all_threads *= walltime_accounting->numOpenMPThreads;
175 }
176
177 double walltime_accounting_get_time_since_start(gmx_walltime_accounting_t walltime_accounting)
178 {
179     return gmx_gettime() - walltime_accounting->start_time_stamp;
180 }
181
182 double walltime_accounting_get_time_since_reset(gmx_walltime_accounting_t walltime_accounting)
183 {
184     return gmx_gettime() - walltime_accounting->reset_time_stamp;
185 }
186
187 double walltime_accounting_get_time_since_reset_over_all_threads(gmx_walltime_accounting_t walltime_accounting)
188 {
189     return walltime_accounting->elapsed_time_over_all_threads;
190 }
191
192 double walltime_accounting_get_start_time_stamp(gmx_walltime_accounting_t walltime_accounting)
193 {
194     return walltime_accounting->start_time_stamp;
195 }
196
197 int64_t walltime_accounting_get_nsteps_done_since_reset(gmx_walltime_accounting_t walltime_accounting)
198 {
199     return walltime_accounting->nsteps_done - walltime_accounting->nsteps_done_at_reset;
200 }
201
202 void walltime_accounting_set_nsteps_done(gmx_walltime_accounting_t walltime_accounting, int64_t nsteps_done)
203 {
204     walltime_accounting->nsteps_done = nsteps_done;
205 }
206
207 double gmx_gettime()
208 {
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
212        Windows). */
213 #if HAVE_CLOCK_GETTIME && defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
214     struct timespec t;
215     double          seconds;
216
217     clock_gettime(CLOCK_REALTIME, &t);
218     seconds = static_cast<double>(t.tv_sec) + 1e-9 * t.tv_nsec;
219
220     return seconds;
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.
224     struct timeval t;
225     double         seconds;
226
227     gettimeofday(&t, nullptr);
228     seconds = static_cast<double>(t.tv_sec) + 1e-6 * t.tv_usec;
229
230     return seconds;
231 #else
232     double seconds;
233
234     seconds = time(nullptr);
235
236     return seconds;
237 #endif
238 }
239
240 void walltime_accounting_set_valid_finish(gmx_walltime_accounting_t walltime_accounting)
241 {
242     walltime_accounting->isValidFinish = true;
243 }
244
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)
247 {
248     return walltime_accounting->isValidFinish;
249 }
250
251 static double gmx_gettime_per_thread()
252 {
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
256        Windows). */
257 #if HAVE_CLOCK_GETTIME && defined(_POSIX_THREAD_CPUTIME) && _POSIX_THREAD_CPUTIME > 0
258     struct timespec t;
259     double          seconds;
260
261     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);
262     seconds = static_cast<double>(t.tv_sec) + 1e-9 * t.tv_nsec;
263
264     return seconds;
265 #else
266     return gmx_gettime();
267 #endif
268 }