Actual source code: cputime.c

petsc-3.3-p7 2013-05-11
  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 <ctype.h>
  9: #include <sys/types.h>
 10: #include <sys/stat.h>
 11: #if defined(PETSC_HAVE_STDLIB_H)
 12: #include <stdlib.h>
 13: #endif
 14: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
 15: #include <sys/utsname.h>
 16: #endif
 17: #if defined(PETSC_HAVE_TIME_H)
 18: #include <time.h>
 19: #endif
 20: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 21: #include <sys/systeminfo.h>
 22: #endif

 24: #if defined (PETSC_HAVE_SYS_TIMES_H)

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

 35:   times(&temp);
 36:   *t = ((double)temp.tms_utime)/((double)CLOCKS_PER_SEC);
 37:   return(0);
 38: }

 40: #elif defined(PETSC_HAVE_CLOCK)

 42: #include <time.h>
 43: #include <sys/types.h>

 47: PetscErrorCode  PetscGetCPUTime(PetscLogDouble *t)
 48: {
 50:   *t = ((double)clock()) / ((double)CLOCKS_PER_SEC);
 51:   return(0);
 52: }

 54: #else

 56: #include <sys/types.h>
 57: #include <sys/time.h>
 58: #include <sys/resource.h>

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

 65:     Not Collective

 67:     Output Parameter:
 68: .   t - Time in seconds charged to the process.

 70:     Example:
 71: .vb
 72:     #include <petscsys.h>
 73:     ...
 74:     PetscLogDouble t1, t2;
 75:  
 76:     PetscGetCPUTime(&t1);
 77:     ... code to time ...
 78:     PetscGetCPUTime(&t2);
 79:     printf("Code took %f CPU seconds\n", t2-t1);
 80: .ve

 82:     Level: intermediate

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

 96:   getrusage(RUSAGE_SELF,&temp);
 97:   foo     = temp.ru_utime.tv_sec;     /* seconds */
 98:   foo1    = temp.ru_utime.tv_usec;    /* uSecs */
 99:   *t      = foo + foo1 * 1.0e-6;
100:   return(0);
101: }

103: #endif