xref: /petsc/src/sys/mpiuni/mpitime.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1 #include <petscsys.h>
2 #if !defined(MPIUNI_H)
3 #error "Wrong mpi.h included! require mpi.h from MPIUNI"
4 #endif
5 
6 #if defined(__cplusplus)
7 extern "C" {
8 #endif
9 /* ------------------------------------------------------------------
10    Microsoft Windows has its own time routines
11 */
12 #if defined(PETSC_USE_MICROSOFT_TIME)
13 #include <windows.h>
14 #define FACTOR 4294967296.0 /* pow(2,32) */
15 
16 double MPI_Wtime(void) {
17   static int           flag = 1;
18   static LARGE_INTEGER StartTime, PerfFreq, CurTime;
19   static double        SecInTick = 0.0;
20 
21   DWORD  dwStartHigh, dwCurHigh;
22   double dTime, dHigh;
23   double ptime;
24 
25   if (flag) {
26     if (!QueryPerformanceCounter(&StartTime)) PETSCABORT(MPI_COMM_WORLD, PETSC_ERR_LIB);
27     if (!QueryPerformanceFrequency(&PerfFreq)) PETSCABORT(MPI_COMM_WORLD, PETSC_ERR_LIB);
28       /* Explicitly convert the higher 32 bits, and add the lower 32 bits from the counter */
29       /* works on non-pentium CPUs ? */
30 #if defined(PETSC_HAVE_LARGE_INTEGER_U)
31     SecInTick = 1.0 / ((double)PerfFreq.u.HighPart * FACTOR + (double)PerfFreq.u.LowPart);
32 #else
33     SecInTick = 1.0 / ((double)PerfFreq.HighPart * FACTOR + (double)PerfFreq.LowPart);
34 #endif
35     flag = 0;
36   }
37 
38   if (!QueryPerformanceCounter(&CurTime)) PETSCABORT(MPI_COMM_WORLD, PETSC_ERR_LIB);
39 #if defined(PETSC_HAVE_LARGE_INTEGER_U)
40   dwCurHigh   = (DWORD)CurTime.u.HighPart;
41   dwStartHigh = (DWORD)StartTime.u.HighPart;
42 #else
43   dwCurHigh = (DWORD)CurTime.HighPart;
44   dwStartHigh = (DWORD)StartTime.HighPart;
45 #endif
46   dHigh = (signed)(dwCurHigh - dwStartHigh);
47 
48 #if defined(PETSC_HAVE_LARGE_INTEGER_U)
49   dTime = dHigh * (double)FACTOR + (double)CurTime.u.LowPart - (double)StartTime.u.LowPart;
50 #else
51   dTime = dHigh * (double)FACTOR + (double)CurTime.LowPart - (double)StartTime.LowPart;
52 #endif
53   /* Use the following with older versions of the Borland compiler
54   dTime = dHigh*(double)FACTOR + (double)CurTime.u.LowPart - (double)StartTime.u.LowPart;
55   */
56   ptime = (double)SecInTick * dTime;
57   return (ptime);
58 }
59 
60 /* ------------------------------------------------------------------
61     The usual Unix time routines.
62 */
63 #else
64 
65 #if defined(PETSC_HAVE_SYS_TIME_H)
66 #include <sys/time.h>
67 #endif
68 
69 #if defined(PETSC_NEEDS_GETTIMEOFDAY_PROTO)
70 extern int gettimeofday(struct timeval *, struct timezone *);
71 #endif
72 
73 double MPI_Wtime(void) {
74   static struct timeval _tp;
75   gettimeofday(&_tp, (struct timezone *)0);
76   return ((double)_tp.tv_sec) + (1.0e-6) * (_tp.tv_usec);
77 }
78 #endif
79 
80 #if defined(__cplusplus)
81 }
82 #endif
83