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: #undef __FUNCT__  
 38: int PetscGetCPUTime(PetscLogDouble *t)
 39: {
 40:   struct tms temp;

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

 48: #elif defined(PETSC_HAVE_CLOCK)

 50: #include <time.h>
 51: #include <sys/types.h>

 53: #undef __FUNCT__  
 55: int PetscGetCPUTime(PetscLogDouble *t)
 56: {
 58:   *t = ((double)clock()) / ((double)CLOCKS_PER_SEC);
 59:   return(0);
 60: }

 62: #else

 64: #include <sys/types.h>
 65: #include <sys/time.h>
 66: #include <sys/resource.h>

 68: #undef __FUNCT__  
 70: /*@C
 71:     PetscGetCPUTime - Returns the CPU time in seconds used by the process.

 73:     Not Collective

 75:     Output Parameter:
 76: .   t - Time in seconds charged to the process.

 78:     Example:
 79: .vb
 80:     #include "petsc.h"
 81:     ...
 82:     PetscLogDouble t1, t2;
 83:  
 84:     PetscGetCPUTime(&t1);
 85:     ... code to time ...
 86:     PetscGetCPUTime(&t2);
 87:     printf("Code took %f CPU secondsn", t2-t1);
 88: .ve

 90:     Level: intermediate

 92:     Notes:
 93:     One should use PetscGetTime() or the -log_summary option of 
 94:     PETSc for profiling. The CPU time is NOT a realistic number to
 95:     use since it does not include the time for message passing etc.
 96:     Also on many systems the accuracy is only on the order of microseconds.
 97: @*/
 98: int PetscGetCPUTime(PetscLogDouble *t)
 99: {
100:   static struct rusage temp;
101:   PetscLogDouble       foo,foo1;

104:   getrusage(RUSAGE_SELF,&temp);
105:   foo     = temp.ru_utime.tv_sec;     /* seconds */
106:   foo1    = temp.ru_utime.tv_usec;    /* uSecs */
107:   *t      = foo + foo1 * 1.0e-6;
108:   return(0);
109: }

111: #endif