xref: /petsc/include/petsctime.h (revision 53df731d4a58a13d32dc697c3d2b74491914e802)
1 /*
2        Low cost access to a system time. This, in general, should not be included in user programs.
3 */
4 #pragma once
5 
6 #include <petscsys.h>
7 
8 /* SUBMANSEC = Sys */
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 from some base time in the past in seconds.
17 
18    Synopsis:
19     #include <petsctime.h>
20     PetscErrorCode PetscTime(PetscLogDouble *v)
21 
22    Not Collective
23 
24    Output Parameter:
25 .  v - time counter
26 
27    Usage:
28 .vb
29      PetscLogDouble v;
30      PetscTime(&v);
31      .... perform some calculation ...
32      printf("Time for operation %g\n",v);
33 .ve
34 
35    Level: developer
36 
37    Note:
38    Since the PETSc libraries incorporate timing of phases and operations, we do not recommend ever using `PetscTime()`.
39    The options database command  `-log_view` activates PETSc library timing.
40    See `PetscLogStageRegister()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()` for how to register
41    stages and events in application codes.
42 
43 .seealso: `PetscTimeSubtract()`, `PetscTimeAdd()`, `PetscLogStageRegister()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`
44 M*/
45 
46 /*MC
47    PetscTimeSubtract - Subtracts the current time (in seconds) from the value `v`.
48 
49    Synopsis:
50     #include <petsctime.h>
51     PetscErrorCode PetscTimeSubtract(PetscLogDouble *v)
52 
53    Not Collective
54 
55    Input Parameter:
56 .  v - time counter
57 
58    Output Parameter:
59 .  v - time counter (`v` = `v` - current time)
60 
61    Level: developer
62 
63    Note:
64    Since the PETSc libraries incorporate timing of phases and operations, we do not always recommend using `PetscTimeSubtract()`.
65    The options database command  `-log_view` activates PETSc library timing.
66    See `PetscLogStageRegister()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()` for how to register
67    stages and events in application codes.
68 
69 .seealso: `PetscTime()`, `PetscTimeAdd()`, `PetscLogStageRegister()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`
70 M*/
71 
72 /*MC
73    PetscTimeAdd - Adds the current time (in seconds) to the value `v`.
74 
75    Synopsis:
76     #include <petsctime.h>
77     PetscErrorCode PetscTimeAdd(PetscLogDouble *v)
78 
79    Not Collective
80 
81    Input Parameter:
82 .  v - time counter
83 
84    Output Parameter:
85 .  v - time counter (`v` = `v` + current time)
86 
87    Level: developer
88 
89    Note:
90    Since the PETSc libraries incorporate timing of phases and operations,  we do not ever recommend using `PetscTimeAdd()`.
91    The options database command `-log_view` activates PETSc library timing.
92 
93 .seealso: `PetscTime()`, `PetscTimeSubtract()`, `PetscLogStageRegister()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`
94 M*/
95 
96 static inline PetscErrorCode PetscTime(PetscLogDouble *v)
97 {
98   *v = MPI_Wtime();
99   return PETSC_SUCCESS;
100 }
101 
102 static inline PetscErrorCode PetscTimeSubtract(PetscLogDouble *v)
103 {
104   *v -= MPI_Wtime();
105   return PETSC_SUCCESS;
106 }
107 
108 static inline PetscErrorCode PetscTimeAdd(PetscLogDouble *v)
109 {
110   *v += MPI_Wtime();
111   return PETSC_SUCCESS;
112 }
113