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