xref: /petsc/src/sys/mpiuni/mpitime.c (revision 6dd63270497ad23dcf16ae500a87ff2b2a0b7474)
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   dTime = dHigh * (double)FACTOR;
50   #if defined(PETSC_HAVE_LARGE_INTEGER_U)
51   dTime += (double)CurTime.u.LowPart - (double)StartTime.u.LowPart;
52   #else
53   dTime += (double)CurTime.LowPart - (double)StartTime.LowPart;
54   #endif
55   /* Use the following with older versions of the Borland compiler
56   dTime = dHigh*(double)FACTOR + (double)CurTime.u.LowPart - (double)StartTime.u.LowPart;
57   */
58   ptime = (double)SecInTick * dTime;
59   return ptime;
60 }
61 
62 /* ------------------------------------------------------------------
63     The usual Unix time routines.
64 */
65 #else
66 
67   #if defined(PETSC_HAVE_SYS_TIME_H)
68     #include <sys/time.h>
69   #endif
70 
71   #if defined(PETSC_NEEDS_GETTIMEOFDAY_PROTO)
72 extern int gettimeofday(struct timeval *, struct timezone *);
73   #endif
74 
75 double MPI_Wtime(void)
76 {
77   static struct timeval _tp;
78   gettimeofday(&_tp, NULL);
79   return ((double)_tp.tv_sec) + (1.0e-6) * (_tp.tv_usec);
80 }
81 #endif
82 
83 #if defined(__cplusplus)
84 }
85 #endif
86