Actual source code: cputime.c

  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: */

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

 26: #if defined (PETSC_HAVE_SYS_TIMES_H)

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

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

 42: #elif defined(PETSC_HAVE_CLOCK)

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

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

 56: #else

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

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

 67:     Not Collective

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

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

 84:     Level: intermediate

 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  PetscGetCPUTime(PetscLogDouble *t)
 93: {
 94:   static struct rusage temp;
 95:   PetscLogDouble       foo,foo1;

 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:   return(0);
103: }

105: #endif