xref: /petsc/include/petsctime.h (revision 0700a8246d308f50502909ba325e6169d3ee27eb)
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 extern PetscLogDouble PETSC_DLLEXPORT BaseTime;
22 
23 /*
24    PetscTime - Returns the current time of day in seconds.
25 
26    Output Parameter:
27 .  v - time counter
28 
29    Synopsis:
30    PetscTime(PetscLogDouble v)
31 
32    Usage:
33      PetscLogDouble v;
34      PetscTime(v);
35      .... perform some calculation ...
36      printf("Time for operation %g\n",v);
37 
38    Notes:
39    Since the PETSc libraries incorporate timing of phases and operations,
40    PetscTime() is intended only for timing of application codes.
41    The options database commands -log, -log_summary, and -log_all activate
42    PETSc library timing.  See the users manual for further details.
43 
44 .seealso:  PetscTimeSubtract(), PetscTimeAdd()
45 
46 .keywords:  Petsc, time
47 */
48 
49 /*
50    PetscTimeSubtract - Subtracts the current time of day (in seconds) from
51    the value v.
52 
53    Input Parameter:
54 .  v - time counter
55 
56    Output Parameter:
57 .  v - time counter (v = v - current time)
58 
59    Synopsis:
60    PetscTimeSubtract(PetscLogDouble v)
61 
62    Notes:
63    Since the PETSc libraries incorporate timing of phases and operations,
64    PetscTimeSubtract() is intended only for timing of application codes.
65    The options database commands -log, -log_summary, and -log_all activate
66    PETSc library timing.  See the users manual for further details.
67 
68 .seealso:  PetscTime(), PetscTimeAdd()
69 
70 .keywords:  Petsc, time, subtract
71 */
72 
73 /*
74    PetscTimeAdd - Adds the current time of day (in seconds) to the value v.
75 
76    Input Parameter:
77 .  v - time counter
78 
79    Output Parameter:
80 .  v - time counter (v = v + current time)
81 
82    Synopsis:
83    PetscTimeAdd(PetscLogDouble v)
84 
85    Notes:
86    Since the PETSc libraries incorporate timing of phases and operations,
87    PetscTimeAdd() is intended only for timing of application codes.
88    The options database commands -log, -log_summary, and -log_all activate
89    PETSc library timing.  See the users manual for further details.
90 
91 .seealso:  PetscTime(), PetscTimeSubtract()
92 
93 .keywords:  Petsc, time, add
94 */
95 
96 /* ------------------------------------------------------------------
97     Some machines have very fast MPI_Wtime()
98 */
99 #if (defined(PETSC_HAVE_FAST_MPI_WTIME) && !defined(__MPIUNI_H))
100 #define PetscTime(v)         (v)=MPI_Wtime();
101 
102 #define PetscTimeSubtract(v) (v)-=MPI_Wtime();
103 
104 #define PetscTimeAdd(v)      (v)+=MPI_Wtime();
105 
106 /* ------------------------------------------------------------------
107    Power1,2,3,PC machines have a fast clock read_real_time()
108 */
109 #elif defined(PETSC_USE_READ_REAL_TIME)
110 EXTERN PetscLogDouble rs6000_time(void);
111 #define PetscTime(v)         (v)=rs6000_time();
112 
113 #define PetscTimeSubtract(v) (v)-=rs6000_time();
114 
115 #define PetscTimeAdd(v)      (v)+=rs6000_time();
116 
117 /* ------------------------------------------------------------------
118     Dec Alpha has a very fast system clock accessible through getclock()
119     getclock() doesn't seem to have a prototype for C++
120 */
121 #elif defined(PETSC_USE_GETCLOCK)
122 EXTERN_C_BEGIN
123 EXTERN int getclock(int clock_type,struct timespec *tp);
124 EXTERN_C_END
125 
126 
127 #define PetscTime(v)         {static struct  timespec _tp; \
128                              getclock(TIMEOFDAY,&_tp); \
129                              (v)=((PetscLogDouble)_tp.tv_sec)+(1.0e-9)*(_tp.tv_nsec);}
130 
131 #define PetscTimeSubtract(v) {static struct timespec  _tp; \
132                              getclock(TIMEOFDAY,&_tp); \
133                              (v)-=((PetscLogDouble)_tp.tv_sec)+(1.0e-9)*(_tp.tv_nsec);}
134 
135 #define PetscTimeAdd(v)      {static struct timespec  _tp; \
136                              getclock(TIMEOFDAY,&_tp); \
137                              (v)+=((PetscLogDouble)_tp.tv_sec)+(1.0e-9)*(_tp.tv_nsec);}
138 
139 /* ------------------------------------------------------------------
140    ASCI RED machine has a fast clock accessiable through dclock()
141 */
142 #elif defined (PETSC_USE_DCLOCK)
143 EXTERN_C_BEGIN
144 EXTERN PetscLogDouble dclock();
145 EXTERN_C_END
146 
147 #define PetscTime(v)         (v)=dclock();
148 
149 #define PetscTimeSubtract(v) (v)-=dclock();
150 
151 #define PetscTimeAdd(v)      (v)+=dclock();
152 
153 
154 /* ------------------------------------------------------------------
155    Windows uses a special time code
156 */
157 #elif defined (PETSC_USE_NT_TIME)
158 #include <time.h>
159 EXTERN_C_BEGIN
160 EXTERN PetscLogDouble nt_time(void);
161 EXTERN_C_END
162 #define PetscTime(v)         (v)=nt_time();
163 
164 #define PetscTimeSubtract(v) (v)-=nt_time();
165 
166 #define PetscTimeAdd(v)      (v)+=nt_time();
167 
168 /* ------------------------------------------------------------------
169     The usual Unix time routines.
170 */
171 #else
172 #define PetscTime(v)         {static struct timeval _tp; \
173                              gettimeofday(&_tp,(struct timezone *)0);\
174                              (v)=((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);}
175 
176 #define PetscTimeSubtract(v) {static struct timeval _tp; \
177                              gettimeofday(&_tp,(struct timezone *)0);\
178                              (v)-=((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);}
179 
180 #define PetscTimeAdd(v)      {static struct timeval _tp; \
181                              gettimeofday(&_tp,(struct timezone *)0);\
182                              (v)+=((PetscLogDouble)_tp.tv_sec)+(1.0e-6)*(_tp.tv_usec);}
183 #endif
184 
185 #endif
186 
187 
188 
189 
190 
191