-
Notifications
You must be signed in to change notification settings - Fork 3
/
timer.cpp
165 lines (142 loc) · 3.84 KB
/
timer.cpp
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
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
< BioEM software for Bayesian inference of Electron Microscopy images>
Copyright (C) 2017 Pilar Cossio, David Rohr, Fabio Baruffa, Markus Rampp,
Luka Stanisic, Volker Lindenstruth and Gerhard Hummer.
Max Planck Institute of Biophysics, Frankfurt, Germany.
Frankfurt Institute for Advanced Studies, Goethe University Frankfurt,
Germany.
Max Planck Computing and Data Facility, Garching, Germany.
Released under the GNU Public License, v3.
See license statement for terms of distribution.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include "timer.h"
#ifdef _WIN32
#include <winbase.h>
#include <windows.h>
#else
#include <time.h>
#endif
HighResTimer::HighResTimer()
{
ElapsedTime = 0;
running = 0;
}
HighResTimer::~HighResTimer() {}
void HighResTimer::Start()
{
#ifdef _WIN32
__int64 istart;
QueryPerformanceCounter((LARGE_INTEGER *) &istart);
StartTime = (double) istart;
#else
timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
StartTime = (double) tv.tv_sec * 1.0E9 + (double) tv.tv_nsec;
#endif
running = 1;
}
void HighResTimer::ResetStart()
{
ElapsedTime = 0;
Start();
}
void HighResTimer::Stop()
{
if (running == 0)
return;
running = 0;
double EndTime = 0;
#ifdef _WIN32
__int64 iend;
QueryPerformanceCounter((LARGE_INTEGER *) &iend);
EndTime = (double) iend;
#else
timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
EndTime = (double) tv.tv_sec * 1.0E9 + (double) tv.tv_nsec;
#endif
ElapsedTime += EndTime - StartTime;
}
void HighResTimer::Reset()
{
ElapsedTime = 0;
StartTime = 0;
running = 0;
}
double HighResTimer::GetElapsedTime() { return ElapsedTime / Frequency; }
double HighResTimer::GetCurrentElapsedTime()
{
if (running == 0)
return (GetElapsedTime());
double CurrentTime = 0;
#ifdef _WIN32
__int64 iend;
QueryPerformanceCounter((LARGE_INTEGER *) &iend);
CurrentTime = (double) iend;
#else
timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
CurrentTime = (double) tv.tv_sec * 1.0E9 + (double) tv.tv_nsec;
#endif
return ((CurrentTime - StartTime + ElapsedTime) / Frequency);
}
double HighResTimer::GetFrequency()
{
#ifdef _WIN32
__int64 ifreq;
QueryPerformanceFrequency((LARGE_INTEGER *) &ifreq);
return ((double) ifreq);
#else
return (1.0E9);
#endif
}
double HighResTimer::Frequency = HighResTimer::GetFrequency();
void TimeStat::InitTimeLog(int log, int size, string s)
{
tl[log].vec.reserve(size);
tl[log].name = s;
tl[log].sum = 0.;
tl[log].stdev = 0.;
}
void TimeStat::InitTimeStat(int nlogs)
{
total_logs = nlogs;
tl = new TimeLog[total_logs];
InitTimeLog(TS_TPROJECTION, angles, "Total time of projection");
InitTimeLog(TS_PROJECTION, angles, "Projection");
InitTimeLog(TS_CONVOLUTION, angles * ctfs, "Convolution");
InitTimeLog(TS_COMPARISON, angles * ctfs, "Comparison");
}
void TimeStat::EmptyTimeStat()
{
if (tl == NULL)
return;
delete[] tl;
tl = NULL;
time = 0.;
}
void TimeStat::ComputeTimeStat()
{
double mean, sq_sum;
vector<double> diff;
for (int i = 0; i < total_logs; i++)
{
tl[i].sum = std::accumulate(tl[i].vec.begin(), tl[i].vec.end(), 0.0);
mean = tl[i].sum / tl[i].vec.size();
diff.resize(tl[i].vec.size());
std::transform(tl[i].vec.begin(), tl[i].vec.end(), diff.begin(),
std::bind2nd(std::minus<double>(), mean));
sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
tl[i].stdev = std::sqrt(sq_sum / tl[i].vec.size());
}
}
void TimeStat::PrintTimeStat(int mpi_rank)
{
ComputeTimeStat();
for (int i = 0; i < total_logs; i++)
{
printf("SUMMARY -> %s: Total %f sec; Mean %f sec; Std.Dev. %f (rank %d)\n",
tl[i].name.c_str(), tl[i].sum, tl[i].sum / tl[i].vec.size(),
tl[i].stdev, mpi_rank);
}
}