1 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 */ 6 7 #include <petscsys.h> /*I "petscsys.h" I*/ 8 #include <petsctime.h> /*I "petsctime.h" I*/ 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 20 21 #if defined(PETSC_HAVE_SYS_TIMES_H) 22 23 #include <sys/times.h> 24 #undef __FUNCT__ 25 #define __FUNCT__ "PetscGetCPUTime" 26 PetscErrorCode PetscGetCPUTime(PetscLogDouble *t) 27 { 28 struct tms temp; 29 30 PetscFunctionBegin; 31 times(&temp); 32 *t = ((double)temp.tms_utime)/((double)CLOCKS_PER_SEC); 33 PetscFunctionReturn(0); 34 } 35 36 #elif defined(PETSC_HAVE_CLOCK) 37 38 #include <time.h> 39 40 #undef __FUNCT__ 41 #define __FUNCT__ "PetscGetCPUTime" 42 PetscErrorCode PetscGetCPUTime(PetscLogDouble *t) 43 { 44 PetscFunctionBegin; 45 *t = ((double)clock()) / ((double)CLOCKS_PER_SEC); 46 PetscFunctionReturn(0); 47 } 48 49 #else 50 51 #include <sys/time.h> 52 #include <sys/resource.h> 53 54 #undef __FUNCT__ 55 #define __FUNCT__ "PetscGetCPUTime" 56 /*@ 57 PetscGetCPUTime - Returns the CPU time in seconds used by the process. 58 59 Not Collective 60 61 Output Parameter: 62 . t - Time in seconds charged to the process. 63 64 Example: 65 .vb 66 #include <petscsys.h> 67 ... 68 PetscLogDouble t1, t2; 69 70 ierr = PetscGetCPUTime(&t1);CHKERRQ(ierr); 71 ... code to time ... 72 ierr = PetscGetCPUTime(&t2);CHKERRQ(ierr); 73 printf("Code took %f CPU seconds\n", t2-t1); 74 .ve 75 76 Level: intermediate 77 78 Notes: 79 One should use PetscTime() or the -log_summary option of 80 PETSc for profiling. The CPU time is NOT a realistic number to 81 use since it does not include the time for message passing etc. 82 Also on many systems the accuracy is only on the order of microseconds. 83 @*/ 84 PetscErrorCode PetscGetCPUTime(PetscLogDouble *t) 85 { 86 static struct rusage temp; 87 PetscLogDouble foo,foo1; 88 89 PetscFunctionBegin; 90 getrusage(RUSAGE_SELF,&temp); 91 foo = temp.ru_utime.tv_sec; /* seconds */ 92 foo1 = temp.ru_utime.tv_usec; /* uSecs */ 93 *t = foo + foo1 * 1.0e-6; 94 PetscFunctionReturn(0); 95 } 96 97 #endif 98