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