8dc9ac17be581e06ce8cdfd7d45613bc0d83eb74
[alexxy/gromacs.git] / src / gromacs / timing / walltime_accounting.c
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, 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 "gromacs/timing/walltime_accounting.h"
39
40 #include "config.h"
41
42 #include <time.h>
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 #ifdef HAVE_SYS_TIME_H
47 #include <sys/time.h>
48 #endif
49
50 #include "gromacs/utility/basedefinitions.h"
51 #include "gromacs/utility/smalloc.h"
52
53 /* TODO in future: convert gmx_walltime_accounting to a class,
54  * resolve who should have responsibility for recording the number of
55  * steps done, consider whether parts of finish_time, print_perf,
56  * wallcycle_print belong in this module.
57  *
58  * If/when any kind of task parallelism is implemented (even OpenMP
59  * regions simultaneously assigned to different tasks), consider
60  * whether this data structure (and/or cycle counters) should be
61  * maintained on a per-OpenMP-thread basis. */
62
63 /*! \brief Manages caching wall-clock time measurements for
64  * simulations */
65 typedef struct gmx_walltime_accounting {
66     //! Seconds since the epoch recorded at the start of the simulation
67     double          start_time_stamp;
68     //! Seconds since the epoch recorded at the start of the simulation for this thread
69     double          start_time_stamp_per_thread;
70     //! Total seconds elapsed over the simulation
71     double          elapsed_time;
72     //! Total seconds elapsed over the simulation running this thread
73     double          elapsed_time_over_all_threads;
74     /*! \brief Number of OpenMP threads that will be launched by this
75      * MPI rank.
76      *
77      * This is used to scale elapsed_time_over_all_threads so
78      * that any combination of real MPI, thread MPI and OpenMP (even
79      * mdrun -ntomp_pme) processes/threads would (when run at maximum
80      * efficiency) return values such that the sum of
81      * elapsed_time_over_all_threads over all threads was constant
82      * with respect to parallelism implementation. */
83     int             numOpenMPThreads;
84     //! Set by integrators to report the amount of work they did
85     gmx_int64_t     nsteps_done;
86 } t_gmx_walltime_accounting;
87
88 /*! \brief Calls system timing routines (e.g. clock_gettime) to get
89  * the (fractional) number of seconds elapsed since the epoch when
90  * this thread was executing.
91  *
92  * This can be used to measure system load. This can be unreliable if
93  * threads migrate between sockets. If thread-specific timers are not
94  * supported by the OS (e.g. if the OS is not POSIX-compliant), this
95  * function is implemented by gmx_gettime. */
96 static double gmx_gettime_per_thread();
97
98 // TODO In principle, all this should get protected by checks that
99 // walltime_accounting is not null. In practice, that NULL condition
100 // does not happen, and future refactoring will likely enforce it by
101 // having the gmx_walltime_accounting_t object be owned by the runner
102 // object. When these become member functions, existence will be
103 // guaranteed.
104
105 gmx_walltime_accounting_t
106 walltime_accounting_init(int numOpenMPThreads)
107 {
108     gmx_walltime_accounting_t walltime_accounting;
109
110     snew(walltime_accounting, 1);
111     walltime_accounting->start_time_stamp            = 0;
112     walltime_accounting->start_time_stamp_per_thread = 0;
113     walltime_accounting->elapsed_time                = 0;
114     walltime_accounting->nsteps_done                 = 0;
115     walltime_accounting->numOpenMPThreads            = numOpenMPThreads;
116
117     return walltime_accounting;
118 }
119
120 void
121 walltime_accounting_destroy(gmx_walltime_accounting_t walltime_accounting)
122 {
123     sfree(walltime_accounting);
124 }
125
126 void
127 walltime_accounting_start(gmx_walltime_accounting_t walltime_accounting)
128 {
129     walltime_accounting->start_time_stamp            = gmx_gettime();
130     walltime_accounting->start_time_stamp_per_thread = gmx_gettime_per_thread();
131     walltime_accounting->elapsed_time                = 0;
132     walltime_accounting->nsteps_done                 = 0;
133 }
134
135 void
136 walltime_accounting_end(gmx_walltime_accounting_t walltime_accounting)
137 {
138     double now, now_per_thread;
139
140     now            = gmx_gettime();
141     now_per_thread = gmx_gettime_per_thread();
142
143     walltime_accounting->elapsed_time                  = now - walltime_accounting->start_time_stamp;
144     walltime_accounting->elapsed_time_over_all_threads = now_per_thread - walltime_accounting->start_time_stamp_per_thread;
145     /* For thread-MPI, the per-thread CPU timer makes this just
146      * work. For OpenMP threads, the per-thread CPU timer measurement
147      * needs to be multiplied by the number of OpenMP threads used,
148      * under the current assumption that all regions ever opened
149      * within a process are of the same size, and each thread should
150      * keep one core busy.
151      */
152     walltime_accounting->elapsed_time_over_all_threads *= walltime_accounting->numOpenMPThreads;
153 }
154
155 double
156 walltime_accounting_get_current_elapsed_time(gmx_walltime_accounting_t walltime_accounting)
157 {
158     return gmx_gettime() - walltime_accounting->start_time_stamp;
159 }
160
161 double
162 walltime_accounting_get_elapsed_time(gmx_walltime_accounting_t walltime_accounting)
163 {
164     return walltime_accounting->elapsed_time;
165 }
166
167 double
168 walltime_accounting_get_elapsed_time_over_all_threads(gmx_walltime_accounting_t walltime_accounting)
169 {
170     return walltime_accounting->elapsed_time_over_all_threads;
171 }
172
173 double
174 walltime_accounting_get_start_time_stamp(gmx_walltime_accounting_t walltime_accounting)
175 {
176     return walltime_accounting->start_time_stamp;
177 }
178
179 double
180 walltime_accounting_get_nsteps_done(gmx_walltime_accounting_t walltime_accounting)
181 {
182     return walltime_accounting->nsteps_done;
183 }
184
185 void
186 walltime_accounting_set_nsteps_done(gmx_walltime_accounting_t   walltime_accounting,
187                                     gmx_int64_t                 nsteps_done)
188 {
189     walltime_accounting->nsteps_done = nsteps_done;
190 }
191
192 double
193 gmx_gettime()
194 {
195 #if defined HAVE_CLOCK_GETTIME && _POSIX_TIMERS >= 0
196     /* Mac and Windows do not support this. For added fun, Windows
197      * defines _POSIX_TIMERS without actually providing the
198      * implementation. */
199     struct timespec t;
200     double          seconds;
201
202     clock_gettime(CLOCK_REALTIME, &t);
203     seconds = (double) t.tv_sec + 1e-9*(double)t.tv_nsec;
204
205     return seconds;
206 #elif defined HAVE_GETTIMEOFDAY
207     // Note that gettimeofday() is deprecated by POSIX, but since Mac
208     // and Windows do not yet support POSIX, we are still stuck.
209     struct timeval t;
210     double         seconds;
211
212     gettimeofday(&t, NULL);
213     seconds = (double) t.tv_sec + 1e-6*(double)t.tv_usec;
214
215     return seconds;
216 #else
217     double  seconds;
218
219     seconds = time(NULL);
220
221     return seconds;
222 #endif
223 }
224
225 static double
226 gmx_gettime_per_thread()
227 {
228 #if defined HAVE_CLOCK_GETTIME && _POSIX_THREAD_CPUTIME >= 0
229     struct timespec t;
230     double          seconds;
231
232     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);
233     seconds = (double) t.tv_sec + 1e-9*(double)t.tv_nsec;
234
235     return seconds;
236 #else
237     return gmx_gettime();
238 #endif
239 }