Actual source code: cputime.c

  1: /*
  2:   This is to allow one to measure CPU time usage of their job, 
  3:   NOT real time usage. Do not use this for reported timings, speedup etc.
  4: */

 6:  #include petscsys.h
  7: #include "petscfix.h"
  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
 23: #include "petscfix.h"

 25: #if defined (PETSC_HAVE_SYS_TIMES_H)

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

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

 41: #elif defined(PETSC_HAVE_CLOCK)

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

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

 55: #else

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

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

 66:     Not Collective

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

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

 83:     Level: intermediate

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

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

104: #endif