moab
|
00001 #ifndef CPUTIMER_HPP 00002 #define CPUTIMER_HPP 00003 00004 #ifdef USE_MPI 00005 # include "moab_mpi.h" 00006 #else 00007 # include <sys/resource.h> 00008 #endif 00009 00010 namespace moab 00011 { 00012 00013 class CpuTimer { 00014 private: 00015 double tAtBirth, tAtLast; 00016 double mAtBirth, mAtLast; 00017 long rssAtBirth, rssAtLast; 00018 00019 double runtime(); 00020 long runmem(); 00021 00022 public: 00023 CpuTimer() : tAtBirth(runtime()), tAtLast(tAtBirth) {} 00024 double time_since_birth() { return (tAtLast = runtime()) - tAtBirth; }; 00025 double time_elapsed() { double tmp = tAtLast; return (tAtLast = runtime()) - tmp; } 00026 long mem_since_birth() {return (mAtLast=runmem()) - mAtBirth;} 00027 long mem_elapsed() {long tmp = mAtLast; return (mAtLast=runmem()) - tmp;} 00028 }; 00029 00030 inline double CpuTimer::runtime() 00031 { 00032 #if defined(_MSC_VER) || defined(__MINGW32__) 00033 return (double)clock() / CLOCKS_PER_SEC; 00034 #elif defined(USE_MPI) 00035 return MPI_Wtime(); 00036 #else 00037 struct rusage r_usage; 00038 getrusage(RUSAGE_SELF, &r_usage); 00039 double utime = (double)r_usage.ru_utime.tv_sec + 00040 ((double)r_usage.ru_utime.tv_usec/1.e6); 00041 double stime = (double)r_usage.ru_stime.tv_sec + 00042 ((double)r_usage.ru_stime.tv_usec/1.e6); 00043 return utime + stime; 00044 #endif 00045 } 00046 00047 inline long CpuTimer::runmem() 00048 { 00049 #if defined(_MSC_VER) || defined(__MINGW32__) 00050 return 0; 00051 #elif defined(USE_MPI) 00052 return 0; 00053 #else 00054 struct rusage r_usage; 00055 getrusage(RUSAGE_SELF, &r_usage); 00056 mAtLast = r_usage.ru_maxrss; 00057 return mAtLast; 00058 #endif 00059 } 00060 00061 } 00062 00063 #endif