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