Actual source code: cputime.c


  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 <petsctime.h>
  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>
 24: PetscErrorCode PetscGetCPUTime(PetscLogDouble *t)
 25: {
 26:   struct tms temp;

 28:   times(&temp);
 29:   *t = ((double)temp.tms_utime) / ((double)CLOCKS_PER_SEC);
 30:   return 0;
 31: }

 33: #elif defined(PETSC_HAVE_CLOCK)

 35:   #include <time.h>

 37: PetscErrorCode PetscGetCPUTime(PetscLogDouble *t)
 38: {
 39:   *t = ((double)clock()) / ((double)CLOCKS_PER_SEC);
 40:   return 0;
 41: }

 43: #else

 45:   #include <sys/time.h>
 46:   #include <sys/resource.h>

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

 51:     Not Collective

 53:     Output Parameter:
 54: .   t - Time in seconds charged to the process.

 56:     Example:
 57: .vb
 58: #include <petscsys.h>
 59:     ...
 60:     PetscLogDouble t1, t2;

 62:     PetscGetCPUTime(&t1);
 63:     ... code to time ...
 64:     PetscGetCPUTime(&t2);
 65:     printf("Code took %f CPU seconds\n", t2-t1);
 66: .ve

 68:     Level: intermediate

 70:     Note:
 71:     One should use the -log_view option of
 72:     PETSc for profiling. The CPU time is NOT a realistic number to
 73:     use since it does not include the time for message passing etc.
 74:     Also on many systems the accuracy is only on the order of microseconds.

 76: .seealso: `PetscTime()`, `PetscLogView()`
 77: @*/
 78: PetscErrorCode PetscGetCPUTime(PetscLogDouble *t)
 79: {
 80:   static struct rusage temp;
 81:   PetscLogDouble       foo, foo1;

 83:   getrusage(RUSAGE_SELF, &temp);
 84:   foo  = temp.ru_utime.tv_sec;  /* seconds */
 85:   foo1 = temp.ru_utime.tv_usec; /* uSecs */
 86:   *t   = foo + foo1 * 1.0e-6;
 87:   return 0;
 88: }

 90: #endif