xref: /petsc/include/petsctime.h (revision fb8e56e08d4d0bfe9fc63603ca1f7fddd68abbdb)
1 /*
2        Low cost access to system time. This, in general, should not
3      be included in user programs.
4 */
5 
6 #if !defined(__PETSCTIME_H)
7 #define __PETSCTIME_H
8 #include <petscsys.h>
9 
10 PETSC_EXTERN PetscErrorCode PetscGetCPUTime(PetscLogDouble*);
11 
12 /* Global counters */
13 PETSC_EXTERN PetscLogDouble petsc_BaseTime;
14 
15 /*MC
16    PetscTime - Returns the current time of day in seconds.
17 
18    Synopsis:
19     #include "petsctime.h"
20    PetscTime(PetscLogDouble *v)
21 
22    Not Collective
23 
24    Output Parameter:
25 .  v - time counter
26 
27 
28    Usage:
29      PetscLogDouble v;
30      PetscTime(&v);
31      .... perform some calculation ...
32      printf("Time for operation %g\n",v);
33 
34    Level: developer
35 
36    Notes:
37    Since the PETSc libraries incorporate timing of phases and operations,
38    we do not recomment every using PetscTime()
39    The options database command  -log_summary activate
40    PETSc library timing. See the <A href="../../docs/manual.pdf">Users Manual</A> for more details.
41 
42 .seealso:  PetscTimeSubtract(), PetscTimeAdd(), PetscLogStageRegister(), PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd()
43 
44 .keywords:  Petsc, time
45 M*/
46 
47 /*MC
48    PetscTimeSubtract - Subtracts the current time of day (in seconds) from
49    the value v.
50 
51    Synopsis:
52     #include "petsctime.h"
53    PetscTimeSubtract(&PetscLogDouble *v)
54 
55    Not Collective
56 
57    Input Parameter:
58 .  v - time counter
59 
60    Output Parameter:
61 .  v - time counter (v = v - current time)
62 
63    Level: developer
64 
65    Notes:
66    Since the PETSc libraries incorporate timing of phases and operations,
67    we do not every recommend using PetscTimeSubtract()
68    The options database command  -log_summary activates
69    PETSc library timing.  See the <A href="../../docs/manual.pdf">Users Manual</A> for more details, also
70    see PetscLogStageRegister(), PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd() for how to register
71    stages and events in application codes.
72 
73 .seealso:  PetscTime(), PetscTimeAdd(), PetscLogStageRegister(), PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd()
74 
75 .keywords:  Petsc, time, subtract
76 M*/
77 
78 /*MC
79    PetscTimeAdd - Adds the current time of day (in seconds) to the value v.
80 
81    Synopsis:
82     #include "petsctime.h"
83    PetscTimeAdd(PetscLogDouble *v)
84 
85    Not Collective
86 
87    Input Parameter:
88 .  v - time counter
89 
90    Output Parameter:
91 .  v - time counter (v = v + current time)
92 
93    Level: developer
94 
95    Notes:
96    Since the PETSc libraries incorporate timing of phases and operations,
97    we do not ever recommend using PetscTimeAdd().
98    The options database command -log_summary activate
99    PETSc library timing. See the <A href="../../docs/manual.pdf">Users Manual</A> for more details.
100 
101 .seealso:  PetscTime(), PetscTimeSubtract(), PetscLogStageRegister(), PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd()
102 
103 .keywords:  Petsc, time, add
104 M*/
105 
106 /* ------------------------------------------------------------------
107     Some machines have very fast MPI_Wtime()
108 */
109 #if (defined(PETSC_HAVE_FAST_MPI_WTIME) && !defined(__MPIUNI_H))
110 PETSC_STATIC_INLINE PetscErrorCode PetscTime(PetscLogDouble *v)
111 {
112   *v = MPI_Wtime();
113   return 0;
114 }
115 
116 PETSC_STATIC_INLINE PetscErrorCode PetscTimeSubtract(PetscLogDouble *v)
117 {
118   *v -= MPI_Wtime();
119   return 0;
120 }
121 
122 PETSC_STATIC_INLINE PetscErrorCode PetscTimeAdd(PetscLogDouble *v)
123 {
124   *v += MPI_Wtime();
125   return 0;
126 }
127 
128 /* ------------------------------------------------------------------
129    IBM Power and PowerPC machines have a fast clock read_real_time()
130 */
131 #elif defined(PETSC_USE_READ_REAL_TIME)
132 PETSC_EXTERN PetscLogDouble PetscReadRealTime(void);
133 
134 PETSC_STATIC_INLINE PetscErrorCode PetscTime(PetscLogDouble *v)
135 {
136   *v = PetscReadRealTime();
137   return 0;
138 }
139 
140 PETSC_STATIC_INLINE PetscErrorCode PetscTimeSubtract(PetscLogDouble *v)
141 {
142   *v -= PetscReadRealTime();
143   return 0;
144 }
145 
146 PETSC_STATIC_INLINE PetscErrorCode PetscTimeAdd(PetscLogDouble *v)
147 {
148   *v += PetscReadRealTime();
149   return 0;
150 }
151 
152 /* ------------------------------------------------------------------
153    Microsoft Windows has its own time routines
154 */
155 #elif defined (PETSC_USE_MICROSOFT_TIME)
156 #include <time.h>
157 PETSC_EXTERN PetscLogDouble PetscMicrosoftTime(void);
158 
159 PETSC_STATIC_INLINE PetscErrorCode PetscTime(PetscLogDouble *v)
160 {
161   *v = PetscMicrosoftTime();
162   return 0;
163 }
164 
165 PETSC_STATIC_INLINE PetscErrorCode PetscTimeSubtract(PetscLogDouble *v)
166 {
167   *v -= PetscMicrosoftTime();
168   return 0;
169 }
170 
171 PETSC_STATIC_INLINE PetscErrorCode PetscTimeAdd(PetscLogDouble *v)
172 {
173   *v += PetscMicrosoftTime();
174   return 0;
175 }
176 
177 /* ------------------------------------------------------------------
178     The usual Unix time routines.
179 */
180 #else
181 
182 #if defined(PETSC_HAVE_SYS_TIME_H)
183 #include <sys/time.h>
184 #endif
185 
186 #if defined(PETSC_NEEDS_GETTIMEOFDAY_PROTO)
187 PETSC_EXTERN int gettimeofday(struct timeval *,struct timezone *);
188 #endif
189 
190 PETSC_STATIC_INLINE PetscErrorCode PetscTime(PetscLogDouble *v)
191 {
192   static struct timeval _tp;
193   gettimeofday(&_tp,(struct timezone *)0);
194   *v = ((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);
195   return 0;
196 }
197 
198 PETSC_STATIC_INLINE PetscErrorCode PetscTimeSubtract(PetscLogDouble *v)
199 {
200   static struct timeval _tp;
201   gettimeofday(&_tp,(struct timezone *)0);
202   *v -= ((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);
203   return 0;
204 }
205 
206 PETSC_STATIC_INLINE PetscErrorCode PetscTimeAdd(PetscLogDouble *v)
207 {
208   static struct timeval _tp;
209   gettimeofday(&_tp,(struct timezone *)0);
210   *v += ((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);
211   return 0;
212 }
213 
214 #endif
215 
216 #endif
217 
218 
219 
220