Encapsulate gmx_wallclock_accounting_t into new timing module
[alexxy/gromacs.git] / src / gromacs / timing / walltime_accounting.c
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 2013, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  *
31  * And Hey:
32  * GROwing Monsters And Cloning Shrimps
33  */
34 #include "gromacs/timing/walltime_accounting.h"
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "gromacs/legacyheaders/smalloc.h"
41 #include "gromacs/legacyheaders/types/simple.h"
42
43 #include <time.h>
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 /* TODO in future: convert gmx_walltime_accounting to a class,
52  * resolve who should have responsibility for recording the number of
53  * steps done, consider whether parts of finish_time, print_perf,
54  * wallcycle_print belong in this module.
55  *
56  * If/when any kind of task parallelism is implemented (even OpenMP
57  * regions simultaneously assigned to different tasks), consider
58  * whether this data structure (and/or cycle counters) should be
59  * maintained on a per-OpenMP-thread basis. */
60
61 /*! \brief Manages caching wall-clock time measurements for
62  * simulations */
63 typedef struct gmx_walltime_accounting {
64     //! Seconds since the epoch recorded at the start of the simulation
65     double          start_time_stamp;
66     //! Seconds since the epoch recorded at the start of the simulation for this thread
67     double          start_time_stamp_per_thread;
68     //! Total seconds elapsed over the simulation
69     double          elapsed_time;
70     //! Total seconds elapsed over the simulation running this thread
71     double          elapsed_time_over_all_threads;
72     /*! \brief Number of OpenMP threads that will be launched by this
73      * MPI rank.
74      *
75      * This is used to scale elapsed_time_over_all_threads so
76      * that any combination of real MPI, thread MPI and OpenMP (even
77      * mdrun -ntomp_pme) processes/threads would (when run at maximum
78      * efficiency) return values such that the sum of
79      * elapsed_time_over_all_threads over all threads was constant
80      * with respect to parallelism implementation. */
81     int             numOpenMPThreads;
82     //! Set by integrators to report the amount of work they did
83     gmx_large_int_t nsteps_done;
84 } t_gmx_walltime_accounting;
85
86 /*! \brief Calls system timing routines (e.g. clock_gettime) to get
87  * the (fractional) number of seconds elapsed since the epoch when
88  * this thread was executing.
89  *
90  * This can be used to measure system load. This can be unreliable if
91  * threads migrate between sockets. If thread-specific timers are not
92  * supported by the OS (e.g. if the OS is not POSIX-compliant), this
93  * function is implemented by gmx_gettime. */
94 static double gmx_gettime_per_thread();
95
96 // TODO In principle, all this should get protected by checks that
97 // walltime_accounting is not null. In practice, that NULL condition
98 // does not happen, and future refactoring will likely enforce it by
99 // having the gmx_walltime_accounting_t object be owned by the runner
100 // object. When these become member functions, existence will be
101 // guaranteed.
102
103 gmx_walltime_accounting_t
104 walltime_accounting_init(int numOpenMPThreads)
105 {
106     gmx_walltime_accounting_t walltime_accounting;
107
108     snew(walltime_accounting, 1);
109     walltime_accounting->start_time_stamp            = 0;
110     walltime_accounting->start_time_stamp_per_thread = 0;
111     walltime_accounting->elapsed_time                = 0;
112     walltime_accounting->nsteps_done                 = 0;
113     walltime_accounting->numOpenMPThreads            = numOpenMPThreads;
114
115     return walltime_accounting;
116 }
117
118 void
119 walltime_accounting_destroy(gmx_walltime_accounting_t walltime_accounting)
120 {
121     sfree(walltime_accounting);
122 }
123
124 void
125 walltime_accounting_start(gmx_walltime_accounting_t walltime_accounting)
126 {
127     walltime_accounting->start_time_stamp            = gmx_gettime();
128     walltime_accounting->start_time_stamp_per_thread = gmx_gettime_per_thread();
129     walltime_accounting->elapsed_time                = 0;
130     walltime_accounting->nsteps_done                 = 0;
131 }
132
133 void
134 walltime_accounting_end(gmx_walltime_accounting_t walltime_accounting)
135 {
136     double now, now_per_thread;
137
138     now            = gmx_gettime();
139     now_per_thread = gmx_gettime_per_thread();
140
141     walltime_accounting->elapsed_time                  = now - walltime_accounting->start_time_stamp;
142     walltime_accounting->elapsed_time_over_all_threads = now_per_thread - walltime_accounting->start_time_stamp_per_thread;
143     /* For thread-MPI, the per-thread CPU timer makes this just
144      * work. For OpenMP threads, the per-thread CPU timer measurement
145      * needs to be multiplied by the number of OpenMP threads used,
146      * under the current assumption that all regions ever opened
147      * within a process are of the same size, and each thread should
148      * keep one core busy.
149      */
150     walltime_accounting->elapsed_time_over_all_threads *= walltime_accounting->numOpenMPThreads;
151 }
152
153 double
154 walltime_accounting_get_current_elapsed_time(gmx_walltime_accounting_t walltime_accounting)
155 {
156     return gmx_gettime() - walltime_accounting->start_time_stamp;
157 }
158
159 double
160 walltime_accounting_get_elapsed_time(gmx_walltime_accounting_t walltime_accounting)
161 {
162     return walltime_accounting->elapsed_time;
163 }
164
165 double
166 walltime_accounting_get_elapsed_time_over_all_threads(gmx_walltime_accounting_t walltime_accounting)
167 {
168     return walltime_accounting->elapsed_time_over_all_threads;
169 }
170
171 double
172 walltime_accounting_get_start_time_stamp(gmx_walltime_accounting_t walltime_accounting)
173 {
174     return walltime_accounting->start_time_stamp;
175 }
176
177 double
178 walltime_accounting_get_nsteps_done(gmx_walltime_accounting_t walltime_accounting)
179 {
180     return walltime_accounting->nsteps_done;
181 }
182
183 void
184 walltime_accounting_set_nsteps_done(gmx_walltime_accounting_t   walltime_accounting,
185                                     gmx_large_int_t             nsteps_done)
186 {
187     walltime_accounting->nsteps_done = nsteps_done;
188 }
189
190 double
191 gmx_gettime()
192 {
193 #if defined HAVE_CLOCK_GETTIME && _POSIX_TIMERS >= 0
194     /* Mac and Windows do not support this. For added fun, Windows
195      * defines _POSIX_TIMERS without actually providing the
196      * implementation. */
197     struct timespec t;
198     double          seconds;
199
200     clock_gettime(CLOCK_REALTIME, &t);
201     seconds = (double) t.tv_sec + 1e-9*(double)t.tv_nsec;
202
203     return seconds;
204 #elif defined HAVE_GETTIMEOFDAY
205     // Note that gettimeofday() is deprecated by POSIX, but since Mac
206     // and Windows do not yet support POSIX, we are still stuck.
207     struct timeval t;
208     double         seconds;
209
210     gettimeofday(&t, NULL);
211     seconds = (double) t.tv_sec + 1e-6*(double)t.tv_usec;
212
213     return seconds;
214 #else
215     double  seconds;
216
217     seconds = time(NULL);
218
219     return seconds;
220 #endif
221 }
222
223 static double
224 gmx_gettime_per_thread()
225 {
226 #if defined HAVE_CLOCK_GETTIME && _POSIX_THREAD_CPUTIME >= 0
227     struct timespec t;
228     double          seconds;
229
230     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);
231     seconds = (double) t.tv_sec + 1e-9*(double)t.tv_nsec;
232
233     return seconds;
234 #else
235     return gmx_gettime();
236 #endif
237 }