Alps 1.5.7
Loading...
Searching...
No Matches
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-2019, 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
46static 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. */
76{
77 public: /* Public for parallecl gather. */
78
80
82 double limit_;
83
84 double startCpu_;
85 double startWall_;
86 double finishCpu_;
88
90 double cpu_;
91
93 double wall_;
94
95 public:
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() {
114 }
115
117 void stop() {
122 }
123
124 //{@
125 void setLimit(double lm) { limit_ = lm; }
126 double getLimit() const { return limit_; }
128
130 double getCpuTime() {
133 return cpu_;
134 }
135
137 double getWallClock() {
140 return wall_;
141 }
142
144 double getTime() {
145 assert( (clockType_ == AlpsClockTypeCpu) ||
150 return cpu_;
151 }
152 else {
155 return wall_;
156 }
157 }
158
160 int getClockType(){ return clockType_; }
161 void setClockType(int ct){ clockType_ = ct; }
162
167 if (finishCpu_ - startCpu_ > limit_) {
168 return true;
169 }
170 else {
171 return false;
172 }
173 }
174
179 if (finishWall_ - startWall_ > limit_) {
180 return true;
181 }
182 else {
183 return false;
184 }
185 }
186};
187
188#endif
static double AlpsWallClock()
Definition AlpsTime.h:46
#define AlpsCpuTime
Definition AlpsTime.h:42
@ AlpsClockTypeCpu
Definition Alps.h:44
@ AlpsClockTypeWallClock
Definition Alps.h:45
#define ALPS_DBL_MAX
Definition Alps.h:143
double getWallClock()
Get cpu timee.
Definition AlpsTime.h:137
double limit_
Time limit.
Definition AlpsTime.h:82
void setClockType(int ct)
Definition AlpsTime.h:161
double wall_
Wall clock time.
Definition AlpsTime.h:93
double cpu_
Cpu time.
Definition AlpsTime.h:90
~AlpsTimer()
Definition AlpsTime.h:98
int getClockType()
Get/Set clock type.
Definition AlpsTime.h:160
double finishWall_
Definition AlpsTime.h:87
double startCpu_
Definition AlpsTime.h:84
bool reachCpuLimit()
Check if cpu time reach limit.
Definition AlpsTime.h:164
double finishCpu_
Definition AlpsTime.h:86
void setLimit(double lm)
Definition AlpsTime.h:125
double startWall_
Definition AlpsTime.h:85
void reset()
Reset.
Definition AlpsTime.h:101
void stop()
Stop timer and computing times.
Definition AlpsTime.h:117
AlpsTimer(double lt)
Definition AlpsTime.h:97
AlpsTimer()
Definition AlpsTime.h:96
double getLimit() const
Definition AlpsTime.h:126
bool reachWallLimit()
Check if wallclock time reach limit.
Definition AlpsTime.h:176
int clockType_
Definition AlpsTime.h:79
void start()
Start to count times.
Definition AlpsTime.h:111
double getTime()
Get time depends on clock type.
Definition AlpsTime.h:144
double getCpuTime()
Get cpu timee.
Definition AlpsTime.h:130