-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathTiming.h
More file actions
150 lines (132 loc) · 4.34 KB
/
Copy pathTiming.h
File metadata and controls
150 lines (132 loc) · 4.34 KB
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
#ifndef __TIMING_H__
#define __TIMING_H__
#if defined(WIN32) || defined(_WIN32) || defined(WIN64)
#define FORCE_INLINE __forceinline
#else
#define FORCE_INLINE __attribute__((always_inline))
#endif
#include <iostream>
#include <stack>
#include <unordered_map>
//#include "Common/Common.h"
#include <chrono>
#include "IDFactory.h"
#include "cuNSearch_export.h"
namespace cuNSearch
{
struct TimingHelper
{
std::chrono::time_point<std::chrono::high_resolution_clock> start;
std::string name;
};
struct AverageTime
{
double totalTime;
unsigned int counter;
std::string name;
};
class Timing
{
public:
static cuNSearch_EXPORT bool m_dontPrintTimes;
static cuNSearch_EXPORT unsigned int m_startCounter;
static cuNSearch_EXPORT unsigned int m_stopCounter;
static cuNSearch_EXPORT std::stack<TimingHelper> m_timingStack;
static cuNSearch_EXPORT std::unordered_map<int, AverageTime> m_averageTimes;
static void reset()
{
while (!m_timingStack.empty())
m_timingStack.pop();
m_averageTimes.clear();
m_startCounter = 0;
m_stopCounter = 0;
}
FORCE_INLINE static void startTiming(const std::string& name = std::string(""))
{
TimingHelper h;
h.start = std::chrono::high_resolution_clock::now();
h.name = name;
Timing::m_timingStack.push(h);
Timing::m_startCounter++;
}
FORCE_INLINE static double stopTiming(bool print = true)
{
if (!Timing::m_timingStack.empty())
{
Timing::m_stopCounter++;
std::chrono::time_point<std::chrono::high_resolution_clock> stop = std::chrono::high_resolution_clock::now();
TimingHelper h = Timing::m_timingStack.top();
Timing::m_timingStack.pop();
std::chrono::duration<double> elapsed_seconds = stop - h.start;
double t = elapsed_seconds.count() * 1000.0;
if (print)
std::cout << "time " << h.name.c_str() << ": " << t << " ms\n" << std::flush;
return t;
}
return 0;
}
FORCE_INLINE static double stopTiming(bool print, int &id)
{
if (id == -1)
id = IDFactory::getId();
if (!Timing::m_timingStack.empty())
{
Timing::m_stopCounter++;
std::chrono::time_point<std::chrono::high_resolution_clock> stop = std::chrono::high_resolution_clock::now();
TimingHelper h = Timing::m_timingStack.top();
Timing::m_timingStack.pop();
std::chrono::duration<double> elapsed_seconds = stop - h.start;
double t = elapsed_seconds.count() * 1000.0;
if (print && !Timing::m_dontPrintTimes)
std::cout << "time " << h.name.c_str() << ": " << t << " ms\n" << std::flush;
if (id >= 0)
{
std::unordered_map<int, AverageTime>::iterator iter;
iter = Timing::m_averageTimes.find(id);
if (iter != Timing::m_averageTimes.end())
{
Timing::m_averageTimes[id].totalTime += t;
Timing::m_averageTimes[id].counter++;
}
else
{
AverageTime at;
at.counter = 1;
at.totalTime = t;
at.name = h.name;
Timing::m_averageTimes[id] = at;
}
}
return t;
}
return 0;
}
FORCE_INLINE static void printAverageTimes()
{
std::unordered_map<int, AverageTime>::iterator iter;
for (iter = Timing::m_averageTimes.begin(); iter != Timing::m_averageTimes.end(); iter++)
{
AverageTime &at = iter->second;
const double avgTime = at.totalTime / at.counter;
std::cout << "Average time " << at.name.c_str() << ": " << avgTime << " ms\n" << std::flush;
}
if (Timing::m_startCounter != Timing::m_stopCounter)
std::cout << "Problem: " << Timing::m_startCounter << " calls of startTiming and " << Timing::m_stopCounter << " calls of stopTiming.\n " << std::flush;
std::cout << "---------------------------------------------------------------------------\n\n";
}
FORCE_INLINE static void printTimeSums()
{
std::unordered_map<int, AverageTime>::iterator iter;
for (iter = Timing::m_averageTimes.begin(); iter != Timing::m_averageTimes.end(); iter++)
{
AverageTime &at = iter->second;
const double timeSum = at.totalTime;
std::cout << "Time sum " << at.name.c_str() << ": " << timeSum << " ms\n" << std::flush;
}
if (Timing::m_startCounter != Timing::m_stopCounter)
std::cout << "Problem: " << Timing::m_startCounter << " calls of startTiming and " << Timing::m_stopCounter << " calls of stopTiming.\n " << std::flush;
std::cout << "---------------------------------------------------------------------------\n\n";
}
};
}
#endif