Alps  1.5.3
AlpsTime.h
Go to the documentation of this file.
1 /*===========================================================================*
2  * This file is part of the Abstract Library for Parallel Search (ALPS). *
3  * *
4  * ALPS is distributed under the Eclipse Public License as part of the *
5  * COIN-OR repository (http://www.coin-or.org). *
6  * *
7  * Authors: *
8  * *
9  * Yan Xu, Lehigh University *
10  * Ted Ralphs, Lehigh University *
11  * *
12  * Conceptual Design: *
13  * *
14  * Yan Xu, Lehigh University *
15  * Ted Ralphs, Lehigh University *
16  * Laszlo Ladanyi, IBM T.J. Watson Research Center *
17  * Matthew Saltzman, Clemson University *
18  * *
19  * *
20  * Copyright (C) 2001-2013, Lehigh University, Yan Xu, and Ted Ralphs. *
21  *===========================================================================*/
22 
23 #ifndef AlpsTime_
24 #define AlpsTime_
25 
26 //#############################################################################
27 
28 #undef SEEK_SET
29 #undef SEEK_END
30 #undef SEEK_CUR
31 #include "Alps.h"
32 #include "AlpsConfig.h"
33 
34 #include "CoinTime.hpp"
35 
36 #ifdef COIN_HAS_MPI
37 # include "mpi.h"
38 #endif
39 
40 //#############################################################################
41 
42 #define AlpsCpuTime CoinCpuTime
43 
44 //#############################################################################
45 
46 static inline double AlpsWallClock()
47 {
48 
49 #ifndef COIN_HAS_MPI
50  double cpu_temp;
51 #if defined(_MSC_VER) || defined(__MSVCRT__)
52  unsigned int ticksnow; /* clock_t is same as int */
53  ticksnow = (unsigned int)clock();
54  cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
55  double sys_temp = 0.;
56 #else
57  double sys_temp;
58  struct rusage usage;
59  getrusage(RUSAGE_SELF,&usage);
60  cpu_temp = (double) usage.ru_utime.tv_sec;
61  cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
62  sys_temp = (double) usage.ru_stime.tv_sec
63  + 1.e-6 * (double) usage.ru_stime.tv_usec;
64 #endif
65  return cpu_temp + sys_temp;
66 #else
67  // COIN_HAS_MPI
68  return MPI_Wtime();
69 #endif
70 }
71 
72 //#############################################################################
73 
74 /* A timer used to record cpu and wallclock time. */
75 class AlpsTimer
76 {
77  public: /* Public for parallecl gather. */
78 
80 
82  double limit_;
83 
84  double startCpu_;
85  double startWall_;
86  double finishCpu_;
87  double finishWall_;
88 
90  double cpu_;
91 
93  double wall_;
94 
95  public:
96  AlpsTimer() : clockType_(AlpsClockTypeWallClock), limit_(ALPS_DBL_MAX) { reset(); }
97  AlpsTimer(double lt) : limit_(lt) { reset(); }
99 
101  void reset() {
102  startCpu_ = 0.0;
103  startWall_ = 0.0;
104  finishCpu_ = 0.0;
105  finishWall_ = 0.0;
106  cpu_ = 0.0;
107  wall_ = 0.0;
108  }
109 
111  void start() {
112  startCpu_ = AlpsCpuTime();
113  startWall_ = AlpsWallClock();
114  }
115 
117  void stop() {
118  finishCpu_ = AlpsCpuTime();
119  finishWall_ = AlpsWallClock();
120  cpu_ = finishCpu_ - startCpu_;
121  wall_ = finishWall_ - startWall_;
122  }
123 
124  //{@
125  void setLimit(double lm) { limit_ = lm; }
126  double getLimit() const { return limit_; }
128 
130  double getCpuTime() {
131  finishCpu_ = AlpsCpuTime();
132  cpu_ = finishCpu_ - startCpu_;
133  return cpu_;
134  }
135 
137  double getWallClock() {
138  finishWall_ = AlpsWallClock();
139  wall_ = finishWall_ - startWall_;
140  return wall_;
141  }
142 
144  double getTime() {
145  assert( (clockType_ == AlpsClockTypeCpu) ||
146  (clockType_ == AlpsClockTypeWallClock) );
147  if (clockType_ == AlpsClockTypeCpu) {
148  finishCpu_ = AlpsCpuTime();
149  cpu_ = finishCpu_ - startCpu_;
150  return cpu_;
151  }
152  else {
153  finishWall_ = AlpsWallClock();
154  wall_ = finishWall_ - startWall_;
155  return wall_;
156  }
157  }
158 
160  int getClockType(){ return clockType_; }
161  void setClockType(int ct){ clockType_ = ct; }
162 
164  bool reachCpuLimit() {
165  finishCpu_ = AlpsCpuTime();
166  finishWall_ = AlpsWallClock();
167  if (finishCpu_ - startCpu_ > limit_) {
168  return true;
169  }
170  else {
171  return false;
172  }
173  }
174 
176  bool reachWallLimit() {
177  finishCpu_ = AlpsCpuTime();
178  finishWall_ = AlpsWallClock();
179  if (finishWall_ - startWall_ > limit_) {
180  return true;
181  }
182  else {
183  return false;
184  }
185  }
186 };
187 
188 #endif
double wall_
Wall clock time.
Definition: AlpsTime.h:93
AlpsTimer(double lt)
Definition: AlpsTime.h:97
void stop()
Stop timer and computing times.
Definition: AlpsTime.h:117
double cpu_
Cpu time.
Definition: AlpsTime.h:90
bool reachWallLimit()
Check if wallclock time reach limit.
Definition: AlpsTime.h:176
double getCpuTime()
Get cpu timee.
Definition: AlpsTime.h:130
int getClockType()
Get/Set clock type.
Definition: AlpsTime.h:160
double getTime()
Get time depends on clock type.
Definition: AlpsTime.h:144
#define AlpsCpuTime
Definition: AlpsTime.h:42
double startCpu_
Definition: AlpsTime.h:84
~AlpsTimer()
Definition: AlpsTime.h:98
bool reachCpuLimit()
Check if cpu time reach limit.
Definition: AlpsTime.h:164
static double AlpsWallClock()
Definition: AlpsTime.h:46
AlpsTimer()
Definition: AlpsTime.h:96
#define ALPS_DBL_MAX
Definition: Alps.h:143
double limit_
Time limit.
Definition: AlpsTime.h:82
double getLimit() const
Definition: AlpsTime.h:126
void reset()
Reset.
Definition: AlpsTime.h:101
void start()
Start to count times.
Definition: AlpsTime.h:111
double finishCpu_
Definition: AlpsTime.h:86
double startWall_
Definition: AlpsTime.h:85
void setClockType(int ct)
Definition: AlpsTime.h:161
double finishWall_
Definition: AlpsTime.h:87
int clockType_
Definition: AlpsTime.h:79
void setLimit(double lm)
Definition: AlpsTime.h:125
double getWallClock()
Get cpu timee.
Definition: AlpsTime.h:137