Actual source code: cputime.c

  1: /*$Id: cputime.c,v 1.41 2001/06/21 21:15:40 bsmith Exp $*/
  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(PARCH_win32)
 16: #include <sys/utsname.h>
 17: #endif
 18: #if defined(PARCH_win32)
 19: #include <windows.h>
 20: #include <io.h>
 21: #include <direct.h>
 22: #endif
 23: #if defined (PARCH_win32_gnu)
 24: #include <windows.h>
 25: #endif
 26: #include <time.h>  
 27: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 28: #include <sys/systeminfo.h>
 29: #endif
 30: #include "petscfix.h"

 32: #if defined (PETSC_HAVE_SYS_TIMES_H)

 34: #include <sys/times.h>
 35: #include <limits.h>
 36: int PetscGetCPUTime(PetscLogDouble *t)
 37: {
 38:   struct tms temp;

 41:   times(&temp);
 42:   *t = ((double)temp.tms_utime)/((double)CLOCKS_PER_SEC);
 43:   return(0);
 44: }

 46: #elif defined(PETSC_HAVE_CLOCK)

 48: #include <time.h>
 49: #include <sys/types.h>

 51: int PetscGetCPUTime(PetscLogDouble *t)
 52: {
 54:   *t = ((double)clock()) / ((double)CLOCKS_PER_SEC);
 55:   return(0);
 56: }

 58: #else

 60: #include <sys/types.h>
 61: #include <sys/time.h>
 62: #include <sys/resource.h>

 64: /*@C
 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 secondsn", 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: int 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