2
2
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
3
3
* University Research and Technology
4
4
* 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
6
6
* of Tennessee Research Foundation. All rights
7
7
* reserved.
8
8
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
40
40
#pragma weak MPI_Wtime = PMPI_Wtime
41
41
#endif
42
42
#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
43
59
#endif
44
60
45
61
double MPI_Wtime (void )
@@ -58,16 +74,22 @@ double MPI_Wtime(void)
58
74
#endif
59
75
#else
60
76
#if defined(__linux__ ) && OPAL_HAVE_CLOCK_GETTIME
61
- struct timespec tp = {. tv_sec = 0 , . tv_nsec = 0 } ;
77
+ struct timespec tp ;
62
78
(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 );
65
84
#else
66
85
/* Fall back to gettimeofday() if we have nothing else */
67
86
struct timeval tv ;
68
87
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 );
71
93
#endif
72
94
#endif
73
95
0 commit comments