xref: /petsc/include/petsctime.h (revision 8865f1ea4ea560cd84ab8db62e98b7095cdff96f)
1 /*
2        Low cost access to system time. This, in general, should not
3      be included in user programs.
4 */
5 
6 #if !defined(__PTIME_H)
7 #define __PTIME_H
8 
9 #include <petscsys.h>
10 #if defined(PETSC_HAVE_SYS_TIME_H)
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #endif
14 #if defined(PETSC_NEEDS_GETTIMEOFDAY_PROTO)
15 EXTERN_C_BEGIN
16 extern int gettimeofday(struct timeval *,struct timezone *);
17 EXTERN_C_END
18 #endif
19 
20 /* Global counters */
21 PETSC_EXTERN PetscLogDouble petsc_BaseTime;
22 
23 /*
24    PetscTime - Returns the current time of day in seconds.
25 
26    Synopsis:
27     #include "petsctime.h"
28    PetscTime(PetscLogDouble v)
29 
30    Not Collective
31 
32    Output Parameter:
33 .  v - time counter
34 
35 
36    Usage:
37      PetscLogDouble v;
38      PetscTime(v);
39      .... perform some calculation ...
40      printf("Time for operation %g\n",v);
41 
42    Notes:
43    Since the PETSc libraries incorporate timing of phases and operations,
44    PetscTime() is intended only for timing of application codes.
45    The options database commands -log, -log_summary, and -log_all activate
46    PETSc library timing. See the <A href="../../docs/manual.pdf">Users Manual</A> for more details.
47 
48 .seealso:  PetscTimeSubtract(), PetscTimeAdd()
49 
50 .keywords:  Petsc, time
51 */
52 
53 /*
54    PetscTimeSubtract - Subtracts the current time of day (in seconds) from
55    the value v.
56 
57    Synopsis:
58     #include "petsctime.h"
59    PetscTimeSubtract(PetscLogDouble v)
60 
61    Not Collective
62 
63    Input Parameter:
64 .  v - time counter
65 
66    Output Parameter:
67 .  v - time counter (v = v - current time)
68 
69 
70    Notes:
71    Since the PETSc libraries incorporate timing of phases and operations,
72    PetscTimeSubtract() is intended only for timing of application codes.
73    The options database commands -log, -log_summary, and -log_all activate
74    PETSc library timing.  See the <A href="../../docs/manual.pdf">Users Manual</A> for more details.
75 
76 .seealso:  PetscTime(), PetscTimeAdd()
77 
78 .keywords:  Petsc, time, subtract
79 */
80 
81 /*
82    PetscTimeAdd - Adds the current time of day (in seconds) to the value v.
83 
84    Synopsis:
85     #include "petsctime.h"
86    PetscTimeAdd(PetscLogDouble v)
87 
88    Not Collective
89 
90    Input Parameter:
91 .  v - time counter
92 
93    Output Parameter:
94 .  v - time counter (v = v + current time)
95 
96    Notes:
97    Since the PETSc libraries incorporate timing of phases and operations,
98    PetscTimeAdd() is intended only for timing of application codes.
99    The options database command -log_summary activate
100    PETSc library timing. See the <A href="../../docs/manual.pdf">Users Manual</A> for more details.
101 
102 .seealso:  PetscTime(), PetscTimeSubtract()
103 
104 .keywords:  Petsc, time, add
105 */
106 
107 /* ------------------------------------------------------------------
108     Some machines have very fast MPI_Wtime()
109 */
110 #if (defined(PETSC_HAVE_FAST_MPI_WTIME) && !defined(__MPIUNI_H))
111 #define PetscTime(v)         (v)=MPI_Wtime();
112 
113 #define PetscTimeSubtract(v) (v)-=MPI_Wtime();
114 
115 #define PetscTimeAdd(v)      (v)+=MPI_Wtime();
116 
117 /* ------------------------------------------------------------------
118    IBM Power and PowerPC machines have a fast clock read_real_time()
119 */
120 #elif defined(PETSC_USE_READ_REAL_TIME)
121 PETSC_EXTERN PetscLogDouble PetscReadRealTime(void);
122 #define PetscTime(v)         (v)=PetscReadRealTime();
123 
124 #define PetscTimeSubtract(v) (v)-=PetscReadRealTime();
125 
126 #define PetscTimeAdd(v)      (v)+=PetscReadRealTime();
127 
128 /* ------------------------------------------------------------------
129    Microsoft Windows has its own time routines
130 */
131 #elif defined (PETSC_USE_MICROSOFT_TIME)
132 #include <time.h>
133 EXTERN_C_BEGIN
134 PETSC_EXTERN PetscLogDouble PetscMicrosoftTime(void);
135 EXTERN_C_END
136 #define PetscTime(v)         (v)=PetscMicrosoftTime();
137 
138 #define PetscTimeSubtract(v) (v)-=PetscMicrosoftTime();
139 
140 #define PetscTimeAdd(v)      (v)+=PetscMicrosoftTime();
141 
142 /* ------------------------------------------------------------------
143     The usual Unix time routines.
144 */
145 #else
146 #define PetscTime(v)         do {                                       \
147     static struct timeval _tp;                                          \
148     gettimeofday(&_tp,(struct timezone *)0);                            \
149     (v)=((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);            \
150   } while (0)
151 
152 #define PetscTimeSubtract(v) do {                                       \
153     static struct timeval _tp;                                          \
154     gettimeofday(&_tp,(struct timezone *)0);                            \
155     (v)-=((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);           \
156   } while (0)
157 
158 #define PetscTimeAdd(v) do {                                            \
159     static struct timeval _tp;                                          \
160     gettimeofday(&_tp,(struct timezone *)0);                            \
161     (v)+=((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);           \
162   } while (0)
163 #endif
164 
165 #endif
166 
167 
168 
169 
170 
171