00001 /*===========================================================================* 00002 * This file is part of the Abstract Library for Parallel Search (ALPS). * 00003 * * 00004 * ALPS is distributed under the Common Public License as part of the * 00005 * COIN-OR repository (http://www.coin-or.org). * 00006 * * 00007 * Authors: * 00008 * * 00009 * Yan Xu, Lehigh University * 00010 * Ted Ralphs, Lehigh University * 00011 * * 00012 * Conceptual Design: * 00013 * * 00014 * Yan Xu, Lehigh University * 00015 * Ted Ralphs, Lehigh University * 00016 * Laszlo Ladanyi, IBM T.J. Watson Research Center * 00017 * Matthew Saltzman, Clemson University * 00018 * * 00019 * * 00020 * Copyright (C) 2001-2007, Lehigh University, Yan Xu, and Ted Ralphs. * 00021 *===========================================================================*/ 00022 00023 #ifndef AlpsTime_ 00024 #define AlpsTime_ 00025 00026 //############################################################################# 00027 00028 #undef SEEK_SET 00029 #undef SEEK_END 00030 #undef SEEK_CUR 00031 #include "Alps.h" 00032 #include "AlpsConfig.h" 00033 00034 #include "CoinTime.hpp" 00035 00036 #ifdef COIN_HAS_MPI 00037 # include "mpi.h" 00038 #endif 00039 00040 //############################################################################# 00041 00042 #define AlpsCpuTime CoinCpuTime 00043 00044 //############################################################################# 00045 00046 static inline double AlpsWallClock() 00047 { 00048 00049 #ifndef COIN_HAS_MPI 00050 double cpu_temp; 00051 #if defined(_MSC_VER) || defined(__MSVCRT__) 00052 unsigned int ticksnow; /* clock_t is same as int */ 00053 ticksnow = (unsigned int)clock(); 00054 cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC); 00055 double sys_temp = 0.; 00056 #else 00057 double sys_temp; 00058 struct rusage usage; 00059 getrusage(RUSAGE_SELF,&usage); 00060 cpu_temp = usage.ru_utime.tv_sec; 00061 cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec); 00062 sys_temp = (double) usage.ru_stime.tv_sec 00063 + 1.e-6 * (double) usage.ru_stime.tv_usec; 00064 #endif 00065 return cpu_temp + sys_temp; 00066 #else 00067 // COIN_HAS_MPI 00068 return MPI_Wtime(); 00069 #endif 00070 } 00071 00072 //############################################################################# 00073 00074 /* A timer used to record cpu and wallclock time. */ 00075 class AlpsTimer 00076 { 00077 public: /* Public for parallecl gather. */ 00078 00079 int clockType_; 00080 00082 double limit_; 00083 00084 double startCpu_; 00085 double startWall_; 00086 double finishCpu_; 00087 double finishWall_; 00088 00090 double cpu_; 00091 00093 double wall_; 00094 00095 public: 00096 AlpsTimer() : clockType_(AlpsClockTypeWallClock), limit_(ALPS_DBL_MAX) { reset(); } 00097 AlpsTimer(double lt) : limit_(lt) { reset(); } 00098 ~AlpsTimer() {} 00099 00101 void reset() { 00102 startCpu_ = 0.0; 00103 startWall_ = 0.0; 00104 finishCpu_ = 0.0; 00105 finishWall_ = 0.0; 00106 cpu_ = 0.0; 00107 wall_ = 0.0; 00108 } 00109 00111 void start() { 00112 startCpu_ = AlpsCpuTime(); 00113 startWall_ = AlpsWallClock(); 00114 } 00115 00117 void stop() { 00118 finishCpu_ = AlpsCpuTime(); 00119 finishWall_ = AlpsWallClock(); 00120 cpu_ = finishCpu_ - startCpu_; 00121 wall_ = finishWall_ - startWall_; 00122 } 00123 00124 //{@ 00125 void setLimit(double lm) { limit_ = lm; } 00126 double getLimit() const { return limit_; } 00128 00130 double getCpuTime() { 00131 finishCpu_ = AlpsCpuTime(); 00132 cpu_ = finishCpu_ - startCpu_; 00133 return cpu_; 00134 } 00135 00137 double getWallClock() { 00138 finishWall_ = AlpsWallClock(); 00139 wall_ = finishWall_ - startWall_; 00140 return wall_; 00141 } 00142 00144 double getTime() { 00145 assert( (clockType_ == AlpsClockTypeCpu) || 00146 (clockType_ == AlpsClockTypeWallClock) ); 00147 if (clockType_ == AlpsClockTypeCpu) { 00148 finishCpu_ = AlpsCpuTime(); 00149 cpu_ = finishCpu_ - startCpu_; 00150 return cpu_; 00151 } 00152 else { 00153 finishWall_ = AlpsWallClock(); 00154 wall_ = finishWall_ - startWall_; 00155 return wall_; 00156 } 00157 } 00158 00160 int getClockType(){ return clockType_; } 00161 void setClockType(int ct){ clockType_ = ct; } 00162 00164 bool reachCpuLimit() { 00165 finishCpu_ = AlpsCpuTime(); 00166 finishWall_ = AlpsWallClock(); 00167 if (finishCpu_ - startCpu_ > limit_) { 00168 return true; 00169 } 00170 else { 00171 return false; 00172 } 00173 } 00174 00176 bool reachWallLimit() { 00177 finishCpu_ = AlpsCpuTime(); 00178 finishWall_ = AlpsWallClock(); 00179 if (finishWall_ - startWall_ > limit_) { 00180 return true; 00181 } 00182 else { 00183 return false; 00184 } 00185 } 00186 }; 00187 00188 #endif