Alps
1.5.7
src
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
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
79
int
clockType_
;
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
(); }
98
~AlpsTimer
() {}
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
AlpsClockTypeWallClock
@ AlpsClockTypeWallClock
Definition:
Alps.h:45
AlpsTimer::cpu_
double cpu_
Cpu time.
Definition:
AlpsTime.h:90
AlpsTimer::start
void start()
Start to count times.
Definition:
AlpsTime.h:111
AlpsTimer::finishCpu_
double finishCpu_
Definition:
AlpsTime.h:86
AlpsTimer::setClockType
void setClockType(int ct)
Definition:
AlpsTime.h:161
AlpsTimer::setLimit
void setLimit(double lm)
Definition:
AlpsTime.h:125
AlpsCpuTime
#define AlpsCpuTime
Definition:
AlpsTime.h:42
AlpsTimer
Definition:
AlpsTime.h:75
AlpsTimer::wall_
double wall_
Wall clock time.
Definition:
AlpsTime.h:93
AlpsTimer::AlpsTimer
AlpsTimer(double lt)
Definition:
AlpsTime.h:97
AlpsTimer::clockType_
int clockType_
Definition:
AlpsTime.h:79
ALPS_DBL_MAX
#define ALPS_DBL_MAX
Definition:
Alps.h:143
AlpsWallClock
static double AlpsWallClock()
Definition:
AlpsTime.h:46
AlpsTimer::startCpu_
double startCpu_
Definition:
AlpsTime.h:84
AlpsClockTypeCpu
@ AlpsClockTypeCpu
Definition:
Alps.h:44
AlpsTimer::reachWallLimit
bool reachWallLimit()
Check if wallclock time reach limit.
Definition:
AlpsTime.h:176
AlpsTimer::getCpuTime
double getCpuTime()
Get cpu timee.
Definition:
AlpsTime.h:130
Alps.h
AlpsTimer::startWall_
double startWall_
Definition:
AlpsTime.h:85
AlpsTimer::finishWall_
double finishWall_
Definition:
AlpsTime.h:87
AlpsTimer::reachCpuLimit
bool reachCpuLimit()
Check if cpu time reach limit.
Definition:
AlpsTime.h:164
AlpsConfig.h
AlpsTimer::getLimit
double getLimit() const
Definition:
AlpsTime.h:126
AlpsTimer::getClockType
int getClockType()
Get/Set clock type.
Definition:
AlpsTime.h:160
AlpsTimer::getTime
double getTime()
Get time depends on clock type.
Definition:
AlpsTime.h:144
AlpsTimer::getWallClock
double getWallClock()
Get cpu timee.
Definition:
AlpsTime.h:137
CoinTime.hpp
AlpsTimer::stop
void stop()
Stop timer and computing times.
Definition:
AlpsTime.h:117
AlpsTimer::~AlpsTimer
~AlpsTimer()
Definition:
AlpsTime.h:98
AlpsTimer::reset
void reset()
Reset.
Definition:
AlpsTime.h:101
AlpsTimer::AlpsTimer
AlpsTimer()
Definition:
AlpsTime.h:96
AlpsTimer::limit_
double limit_
Time limit.
Definition:
AlpsTime.h:82
Generated by
1.8.17