Skip to content

Commit bf3dd8a

Browse files
authored
Merge pull request #4884 from bosilca/topic/fix_wtime
Improve the range and accuracy of MPI_Wtime.
2 parents e08e580 + 9bced03 commit bf3dd8a

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

ompi/mpi/c/wtime.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
33
* University Research and Technology
44
* Corporation. All rights reserved.
5-
* Copyright (c) 2004-2005 The University of Tennessee and The University
5+
* Copyright (c) 2004-2018 The University of Tennessee and The University
66
* of Tennessee Research Foundation. All rights
77
* reserved.
88
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
@@ -40,6 +40,22 @@
4040
#pragma weak MPI_Wtime = PMPI_Wtime
4141
#endif
4242
#define MPI_Wtime PMPI_Wtime
43+
/**
44+
* Have a base time set on the first call to wtime, to improve the range
45+
* and accuracy of the user visible timer.
46+
* More info: https://github.com/mpi-forum/mpi-issues/issues/77#issuecomment-369663119
47+
*/
48+
#if defined(__linux__) && OPAL_HAVE_CLOCK_GETTIME
49+
struct timespec ompi_wtime_time_origin = {.tv_sec = 0};
50+
#else
51+
struct timeval ompi_wtime_time_origin = {.tv_sec = 0};
52+
#endif
53+
#else /* OMPI_BUILD_MPI_PROFILING */
54+
#if defined(__linux__) && OPAL_HAVE_CLOCK_GETTIME
55+
extern struct timespec ompi_wtime_time_origin;
56+
#else
57+
extern struct timeval ompi_wtime_time_origin;
58+
#endif
4359
#endif
4460

4561
double MPI_Wtime(void)
@@ -58,16 +74,22 @@ double MPI_Wtime(void)
5874
#endif
5975
#else
6076
#if defined(__linux__) && OPAL_HAVE_CLOCK_GETTIME
61-
struct timespec tp = {.tv_sec = 0, .tv_nsec = 0};
77+
struct timespec tp;
6278
(void) clock_gettime(CLOCK_MONOTONIC, &tp);
63-
wtime = tp.tv_sec;
64-
wtime += tp.tv_nsec/1.0e+9;
79+
if( OPAL_UNLIKELY(0 == ompi_wtime_time_origin.tv_sec) ) {
80+
ompi_wtime_time_origin = tp;
81+
}
82+
wtime = (double)(tp.tv_nsec - ompi_wtime_time_origin.tv_nsec)/1.0e+9;
83+
wtime += (tp.tv_sec - ompi_wtime_time_origin.tv_sec);
6584
#else
6685
/* Fall back to gettimeofday() if we have nothing else */
6786
struct timeval tv;
6887
gettimeofday(&tv, NULL);
69-
wtime = tv.tv_sec;
70-
wtime += (double)tv.tv_usec / 1000000.0;
88+
if( OPAL_UNLIKELY(0 == ompi_wtime_time_origin.tv_sec) ) {
89+
ompi_wtime_time_origin = tv;
90+
}
91+
wtime = (double)(tv.tv_usec - ompi_wtime_time_origin.tv_usec) / 1.0e+6;
92+
wtime += (tv.tv_sec - ompi_wtime_time_origin.tv_sec);
7193
#endif
7294
#endif
7395

0 commit comments

Comments
 (0)