Actual source code: cputime.c

petsc-3.5.0 2014-06-30
Report Typos and Errors
  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: */

  7: #include <petscsys.h>                       /*I "petscsys.h" I*/
  8: #include <petsctime.h>                      /*I "petsctime.h" I*/
  9: #include <ctype.h>
 10: #include <sys/stat.h>
 11: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
 12: #include <sys/utsname.h>
 13: #endif
 14: #if defined(PETSC_HAVE_TIME_H)
 15: #include <time.h>
 16: #endif
 17: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 18: #include <sys/systeminfo.h>
 19: #endif

 21: #if defined(PETSC_HAVE_SYS_TIMES_H)

 23: #include <sys/times.h>
 26: PetscErrorCode  PetscGetCPUTime(PetscLogDouble *t)
 27: {
 28:   struct tms temp;

 31:   times(&temp);
 32:   *t = ((double)temp.tms_utime)/((double)CLOCKS_PER_SEC);
 33:   return(0);
 34: }

 36: #elif defined(PETSC_HAVE_CLOCK)

 38: #include <time.h>

 42: PetscErrorCode  PetscGetCPUTime(PetscLogDouble *t)
 43: {
 45:   *t = ((double)clock()) / ((double)CLOCKS_PER_SEC);
 46:   return(0);
 47: }

 49: #else

 51: #include <sys/time.h>
 52: #include <sys/resource.h>

 56: /*@
 57:     PetscGetCPUTime - Returns the CPU time in seconds used by the process.

 59:     Not Collective

 61:     Output Parameter:
 62: .   t - Time in seconds charged to the process.

 64:     Example:
 65: .vb
 66:     #include <petscsys.h>
 67:     ...
 68:     PetscLogDouble t1, t2;

 70:     PetscGetCPUTime(&t1);
 71:     ... code to time ...
 72:     PetscGetCPUTime(&t2);
 73:     printf("Code took %f CPU seconds\n", t2-t1);
 74: .ve

 76:     Level: intermediate

 78:     Notes:
 79:     One should use PetscTime() or the -log_summary option of
 80:     PETSc for profiling. The CPU time is NOT a realistic number to
 81:     use since it does not include the time for message passing etc.
 82:     Also on many systems the accuracy is only on the order of microseconds.
 83: @*/
 84: PetscErrorCode  PetscGetCPUTime(PetscLogDouble *t)
 85: {
 86:   static struct rusage temp;
 87:   PetscLogDouble       foo,foo1;

 90:   getrusage(RUSAGE_SELF,&temp);
 91:   foo  = temp.ru_utime.tv_sec;        /* seconds */
 92:   foo1 = temp.ru_utime.tv_usec;       /* uSecs */
 93:   *t   = foo + foo1 * 1.0e-6;
 94:   return(0);
 95: }

 97: #endif