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