xref: /petsc/src/sys/time/cputime.c (revision 62903a643c6f3b806cfd2df6dfd11354dcefb6c2)
1 #define PETSC_DLL
2 /*
3   This is to allow one to measure CPU time usage of their job,
4   NOT real time usage. Do not use this for reported timings, speedup etc.
5 */
6 
7 #include "petscsys.h"                       /*I "petsc.h" I*/
8 #include "petscfix.h"
9 #include <ctype.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #if defined(PETSC_HAVE_STDLIB_H)
13 #include <stdlib.h>
14 #endif
15 #if defined(PETSC_HAVE_SYS_UTSNAME_H)
16 #include <sys/utsname.h>
17 #endif
18 #if defined(PETSC_HAVE_TIME_H)
19 #include <time.h>
20 #endif
21 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
22 #include <sys/systeminfo.h>
23 #endif
24 #include "petscfix.h"
25 
26 #if defined (PETSC_HAVE_SYS_TIMES_H)
27 
28 #include <sys/times.h>
29 #include <limits.h>
30 #undef __FUNCT__
31 #define __FUNCT__ "PetscGetCPUTime"
32 PetscErrorCode PETSC_DLLEXPORT PetscGetCPUTime(PetscLogDouble *t)
33 {
34   struct tms temp;
35 
36   PetscFunctionBegin;
37   times(&temp);
38   *t = ((double)temp.tms_utime)/((double)CLOCKS_PER_SEC);
39   PetscFunctionReturn(0);
40 }
41 
42 #elif defined(PETSC_HAVE_CLOCK)
43 
44 #include <time.h>
45 #include <sys/types.h>
46 
47 #undef __FUNCT__
48 #define __FUNCT__ "PetscGetCPUTime"
49 PetscErrorCode PETSC_DLLEXPORT PetscGetCPUTime(PetscLogDouble *t)
50 {
51   PetscFunctionBegin;
52   *t = ((double)clock()) / ((double)CLOCKS_PER_SEC);
53   PetscFunctionReturn(0);
54 }
55 
56 #else
57 
58 #include <sys/types.h>
59 #include <sys/time.h>
60 #include <sys/resource.h>
61 
62 #undef __FUNCT__
63 #define __FUNCT__ "PetscGetCPUTime"
64 /*@
65     PetscGetCPUTime - Returns the CPU time in seconds used by the process.
66 
67     Not Collective
68 
69     Output Parameter:
70 .   t - Time in seconds charged to the process.
71 
72     Example:
73 .vb
74     #include "petsc.h"
75     ...
76     PetscLogDouble t1, t2;
77 
78     ierr = PetscGetCPUTime(&t1);CHKERRQ(ierr);
79     ... code to time ...
80     ierr = PetscGetCPUTime(&t2);CHKERRQ(ierr);
81     printf("Code took %f CPU seconds\n", t2-t1);
82 .ve
83 
84     Level: intermediate
85 
86     Notes:
87     One should use PetscGetTime() or the -log_summary option of
88     PETSc for profiling. The CPU time is NOT a realistic number to
89     use since it does not include the time for message passing etc.
90     Also on many systems the accuracy is only on the order of microseconds.
91 @*/
92 PetscErrorCode PETSC_DLLEXPORT PetscGetCPUTime(PetscLogDouble *t)
93 {
94   static struct rusage temp;
95   PetscLogDouble       foo,foo1;
96 
97   PetscFunctionBegin;
98   getrusage(RUSAGE_SELF,&temp);
99   foo     = temp.ru_utime.tv_sec;     /* seconds */
100   foo1    = temp.ru_utime.tv_usec;    /* uSecs */
101   *t      = foo + foo1 * 1.0e-6;
102   PetscFunctionReturn(0);
103 }
104 
105 #endif
106