-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.hh
189 lines (154 loc) · 4.1 KB
/
Timer.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifndef simdrt_Timer_hh
#define simdrt_Timer_hh
#if defined(__APPLE__) && defined(__POWERPC__)
#include <ppc_intrinsics.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#elif _WIN32
#include <time.h>
#else
#include <sys/time.h>
#endif
#include <algorithm>
#include <iosfwd>
using std::ostream;
namespace simdrt
{
class Timer
{
public:
Timer();
void reset();
float getElapsed() const;
float getReset();
private:
#if defined(__APPLE__) && defined(__POWERPC__)
uint64_t getTime() const;
float mach_scale;
uint64_t start;
#elif _WIN32
clock_t start;
#else
timeval start;
#endif // defined(__APPLE__) && defined(__POWERPC__)
};
class ParallelTimer
{
public:
ParallelTimer() {}
~ParallelTimer()
{
delete [] data;
}
inline void init(unsigned int nthreads_)
{
nthreads = nthreads_;
data = new Data[nthreads];
for (size_t i = 0; i < nthreads; ++i)
data[i].timer.reset();
}
inline void reset(unsigned int tid) const
{
data[tid].timer.reset();
}
inline float getElapsed(unsigned int tid) const
{
return data[tid].timer.getElapsed();
}
inline float getMaxElapsed() const
{
float max = data[0].timer.getElapsed();
for (size_t i = 0; i < nthreads; ++i)
max = std::max(max, data[i].timer.getElapsed());
return max;
}
inline float getTotalElapsed() const
{
float total = data[0].timer.getElapsed();
for (size_t i = 1; i < nthreads; ++i)
total += data[i].timer.getElapsed();
return total;
}
inline float getReset(unsigned int tid) const
{
return data[tid].timer.getReset();
}
inline float getMaxReset() const
{
float max = data[0].timer.getReset();
for (size_t i = 0; i < nthreads; ++i)
max = std::max(max, data[i].timer.getReset());
return max;
}
inline float getTotalReset() const
{
float total = data[0].timer.getReset();
for (size_t i = 0; i < nthreads; ++i)
total += data[i].timer.getReset();
return total;
}
private:
struct Data {
Timer timer;
char pad[128 - 1*sizeof(Timer)];
};
Data* data;
unsigned int nthreads;
};
inline Timer::Timer()
{
#if defined(__APPLE__) && defined(__POWERPC__)
mach_timebase_info_data_t time_info;
mach_timebase_info(&time_info);
// Scales to nanoseconds without 1e-9f
mach_scale = (1e-9*static_cast<float>(time_info.numer))/
static_cast<float>(time_info.denom);
#endif
reset();
}
inline void Timer::reset()
{
#if defined(__APPLE__) && defined(__POWERPC__)
start = getTime();
#elif _WIN32
start = clock();
#else
gettimeofday(&start, 0);
#endif // defined(__APPLE__) && defined(__POWERPC__)
}
inline float Timer::getElapsed() const
{
#if defined(__APPLE__) && defined(__POWERPC__)
uint64_t now = getTime();
return (static_cast<float>(now - start)*mach_scale);
#elif _WIN32
return (static_cast<float>(clock() - start)/
static_cast<float>(CLOCKS_PER_SEC));
#else
timeval now;
gettimeofday(&now, 0);
return (static_cast<float>(now.tv_sec - start.tv_sec) +
static_cast<float>(now.tv_usec - start.tv_usec)/1000000.);
#endif // defined(__APPLE__) && defined(__POWERPC__)
}
inline float Timer::getReset()
{
float elapsed = getElapsed();
reset();
return elapsed;
}
#if defined(__APPLE__) && defined(__POWERPC__)
inline uint64_t Timer::getTime() const
{
unsigned int tbl, tbu0, tbu1;
do {
__asm__ __volatile__ ("mftbu %0" : "=r"(tbu0));
__asm__ __volatile__ ("mftb %0" : "=r"(tbl));
__asm__ __volatile__ ("mftbu %0" : "=r"(tbu1));
} while (tbu0 != tbu1);
return ((static_cast<unsigned long long>(tbu0) << 32) | tbl);
}
#endif // defined(__APPLE__) && defined(__POWERPC__)
ostream& operator<<(ostream& os, Timer& timer);
}
#endif // simdrt_Timer_h