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