xref: /petsc/include/petsctime.h (revision 0e9bae810fdaeb60e2713eaa8ddb89f42e079fd1)
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    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    Notes:
42    Since the PETSc libraries incorporate timing of phases and operations,
43    PetscTime() is intended only for timing of application codes.
44    The options database commands -log, -log_summary, and -log_all activate
45    PETSc library timing. See the <A href="../../docs/manual.pdf">Users Manual</A> for more details.
46 
47 .seealso:  PetscTimeSubtract(), PetscTimeAdd()
48 
49 .keywords:  Petsc, time
50 */
51 
52 /*
53    PetscTimeSubtract - Subtracts the current time of day (in seconds) from
54    the value v.
55 
56    Synopsis:
57    PetscTimeSubtract(PetscLogDouble v)
58 
59    Not Collective
60 
61    Input Parameter:
62 .  v - time counter
63 
64    Output Parameter:
65 .  v - time counter (v = v - current time)
66 
67 
68    Notes:
69    Since the PETSc libraries incorporate timing of phases and operations,
70    PetscTimeSubtract() is intended only for timing of application codes.
71    The options database commands -log, -log_summary, and -log_all activate
72    PETSc library timing.  See the <A href="../../docs/manual.pdf">Users Manual</A> for more details.
73 
74 .seealso:  PetscTime(), PetscTimeAdd()
75 
76 .keywords:  Petsc, time, subtract
77 */
78 
79 /*
80    PetscTimeAdd - Adds the current time of day (in seconds) to the value v.
81 
82    Synopsis:
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    Notes:
94    Since the PETSc libraries incorporate timing of phases and operations,
95    PetscTimeAdd() is intended only for timing of application codes.
96    The options database commands -log, -log_summary, and -log_all activate
97    PETSc library timing. See the <A href="../../docs/manual.pdf">Users Manual</A> for more details.
98 
99 .seealso:  PetscTime(), PetscTimeSubtract()
100 
101 .keywords:  Petsc, time, add
102 */
103 
104 /* ------------------------------------------------------------------
105     Some machines have very fast MPI_Wtime()
106 */
107 #if (defined(PETSC_HAVE_FAST_MPI_WTIME) && !defined(__MPIUNI_H))
108 #define PetscTime(v)         (v)=MPI_Wtime();
109 
110 #define PetscTimeSubtract(v) (v)-=MPI_Wtime();
111 
112 #define PetscTimeAdd(v)      (v)+=MPI_Wtime();
113 
114 /* ------------------------------------------------------------------
115    Power1,2,3,PC machines have a fast clock read_real_time()
116 */
117 #elif defined(PETSC_USE_READ_REAL_TIME)
118 PETSC_EXTERN PetscLogDouble rs6000_time(void);
119 #define PetscTime(v)         (v)=rs6000_time();
120 
121 #define PetscTimeSubtract(v) (v)-=rs6000_time();
122 
123 #define PetscTimeAdd(v)      (v)+=rs6000_time();
124 
125 /* ------------------------------------------------------------------
126     Dec Alpha has a very fast system clock accessible through getclock()
127     getclock() doesn't seem to have a prototype for C++
128 */
129 #elif defined(PETSC_USE_GETCLOCK)
130 EXTERN_C_BEGIN
131 extern int getclock(int clock_type,struct timespec *tp);
132 EXTERN_C_END
133 
134 
135 #define PetscTime(v)         {static struct  timespec _tp; \
136                              getclock(TIMEOFDAY,&_tp); \
137                              (v)=((PetscLogDouble)_tp.tv_sec)+(1.0e-9)*(_tp.tv_nsec);}
138 
139 #define PetscTimeSubtract(v) {static struct timespec  _tp; \
140                              getclock(TIMEOFDAY,&_tp); \
141                              (v)-=((PetscLogDouble)_tp.tv_sec)+(1.0e-9)*(_tp.tv_nsec);}
142 
143 #define PetscTimeAdd(v)      {static struct timespec  _tp; \
144                              getclock(TIMEOFDAY,&_tp); \
145                              (v)+=((PetscLogDouble)_tp.tv_sec)+(1.0e-9)*(_tp.tv_nsec);}
146 
147 /* ------------------------------------------------------------------
148    ASCI RED machine has a fast clock accessiable through dclock()
149 */
150 #elif defined (PETSC_USE_DCLOCK)
151 EXTERN_C_BEGIN
152 PETSC_EXTERN PetscLogDouble dclock();
153 EXTERN_C_END
154 
155 #define PetscTime(v)         (v)=dclock();
156 
157 #define PetscTimeSubtract(v) (v)-=dclock();
158 
159 #define PetscTimeAdd(v)      (v)+=dclock();
160 
161 
162 /* ------------------------------------------------------------------
163    Windows uses a special time code
164 */
165 #elif defined (PETSC_USE_NT_TIME)
166 #include <time.h>
167 EXTERN_C_BEGIN
168 PETSC_EXTERN PetscLogDouble nt_time(void);
169 EXTERN_C_END
170 #define PetscTime(v)         (v)=nt_time();
171 
172 #define PetscTimeSubtract(v) (v)-=nt_time();
173 
174 #define PetscTimeAdd(v)      (v)+=nt_time();
175 
176 /* ------------------------------------------------------------------
177     The usual Unix time routines.
178 */
179 #else
180 #define PetscTime(v)         do {                                       \
181     static struct timeval _tp;                                          \
182     gettimeofday(&_tp,(struct timezone *)0);                            \
183     (v)=((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);            \
184   } while (0)
185 
186 #define PetscTimeSubtract(v) do {                                       \
187     static struct timeval _tp;                                          \
188     gettimeofday(&_tp,(struct timezone *)0);                            \
189     (v)-=((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);           \
190   } while (0)
191 
192 #define PetscTimeAdd(v) do {                                            \
193     static struct timeval _tp;                                          \
194     gettimeofday(&_tp,(struct timezone *)0);                            \
195     (v)+=((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);           \
196   } while (0)
197 #endif
198 
199 #endif
200 
201 
202 
203 
204 
205