QDP++
qdp_stopwatch.cc
Go to the documentation of this file.
1
6
7
8#include "qdp.h"
9#include<sys/time.h>
10
11namespace QDP {
12
14 {
15 stoppedP=false;
16 startedP=false;
17 sec=0;
18 usec=0;
19 state=true;
20 }
21
23
24 void StopWatch::calcDuration(long& secs, long& usecs){
25 if (!state){
26 QDPIO::cerr << "Timer in error state"<<std::endl;
27 return;}
28 secs=0;
29 usecs=0;
30 if( startedP && stoppedP )
31 {
32 if( t_end.tv_sec < t_start.tv_sec )
33 {
34 QDPIO::cerr << __func__ << ": critical timer rollover" << std::endl;
35 state=false;
36 usecs = 0;
37 }
38 else
39 {
40 secs = t_end.tv_sec - t_start.tv_sec;
41
42 if( t_end.tv_usec < t_start.tv_usec )
43 {
44 secs -= 1;
45 usecs = 1000000;
46 }
47 usecs += t_end.tv_usec - t_start.tv_usec;
48 }
49 }
50 else
51 {
52 QDPIO::cerr << __func__ << ": either stopwatch not started, or not stopped. I will return 0 instead!" << std::endl;
53 secs=0;
54 usecs=0;
55 }
56 }
57
59 {
60 startedP = false;
61 stoppedP = false;
62 sec=0;
63 usec=0;
64 state=true;
65 }
66
68 {
69 int ret_val;
70 ret_val = gettimeofday(&t_start, NULL);
71 if( ret_val != 0 )
72 {
73 QDPIO::cerr << __func__ << ": gettimeofday failed in StopWatch::start()" << std::endl;
74 state=false;
75 // QDP_abort(1);
76 }
77 else
78 state=true;
79 startedP = true;
80 stoppedP = false;
81 }
82
84 {
85 if( !startedP )
86 {
87 QDPIO::cerr << __func__ << ": attempting to stop a non running stopwatch in StopWatch::stop()" << std::endl;
88 }
89 else{
90 int ret_val;
91 ret_val = gettimeofday(&t_end, NULL);
92 if( ret_val != 0 )
93 {
94 QDPIO::cerr << __func__ << ": gettimeofday failed in StopWatch::end()" << std::endl;
95 state=false;
96 //QDP_abort(1);
97 }
98 stoppedP = true;
99
100 long usecs, secs;
101 calcDuration(secs,usecs);
102
103 usec+=usecs;
104 sec+=secs;
105
106 startedP=false;
107 stoppedP=false;
108 }
109 }
110
112 {
113 return static_cast<double>(usec)+static_cast<double>(sec*1.e6);
114 }
115
117 {
118 return static_cast<double>(sec)+static_cast<double>(usec)/1.e6;
119 }
120
121
122} // namespace QDP;
double getTimeInSeconds()
Get time in seconds.
void reset()
Reset the timer.
~StopWatch()
Destructor.
double getTimeInMicroseconds()
Get time in microseconds.
void start()
Start the timer.
StopWatch()
Constructor.
void stop()
Stop the timer.
StandardOutputStream cerr
Definition qdp_stdio.cc:22
Yet another random number generator.
Primary include file for QDP.