-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcuda_timer.h
More file actions
119 lines (96 loc) · 2.91 KB
/
cuda_timer.h
File metadata and controls
119 lines (96 loc) · 2.91 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
#ifndef __CUDA_TIMER_H__
#define __CUDA_TIMER_H__
/*
CudaTimer - make it easier to use CUDA events to track the time
spent during some operations. Rather than creating a start event,
recording it in the current stream, then creating an end event and
recording it, then deallocating both, just make a CudaTime object
and call start() and end(). If multiple calls to start() and end()
are made, the time between each pair will be added up.
*/
#include <vector>
#include <string>
#include <cstdio>
#include "cuda.h"
#include "cucheck.h"
using namespace std;
class CudaTimer {
string name;
vector<cudaEvent_t> events;
bool isStarted;
cudaStream_t currentStream;
// add an event
void add(cudaStream_t stream = 0) {
cudaEvent_t event;
CUCHECK(cudaEventCreate(&event));
CUCHECK(cudaEventRecord(event, stream));
events.push_back(event);
}
public:
CudaTimer(const char *name_ = "") {
name = name_;
isStarted = false;
}
~CudaTimer() {
for (size_t i=0; i < events.size(); i++)
cudaEventDestroy(events[i]);
}
const char *getName() {return name.c_str();}
void print() {
if (count() == 1) {
printf("%s: %.3f ms\n", getName(), time());
} else {
printf("%s: %d calls in %.3f ms\n", getName(), count(), time());
}
}
// start timer
void start(cudaStream_t stream = 0) {
if (isStarted) {
fprintf(stderr, "ERROR: CudaTimer::start() (%s) called with timer "
"already started.\n", getName());
return;
}
isStarted = true;
currentStream = stream;
add(stream);
}
// end timer
void end(cudaStream_t stream = 0) {
if (!isStarted) {
fprintf(stderr, "ERROR: CudaTimer::end() (%s) called with timer not "
"started.\n", getName());
return;
}
if (stream != currentStream) {
fprintf(stderr, "ERROR: CudaTimer::end() called on a different stream "
"than the one on which the timer was started.\n");
return;
}
currentStream = 0;
isStarted = false;
add(stream);
}
// return the number of start/end pairs
int count() {return (int)(events.size() / 2);}
void sync() {CUCHECK(cudaThreadSynchronize());}
// Compute the total time in milliseconds.
float time() {
float total = 0;
for (size_t i = 0; i < events.size(); i += 2) {
float ms;
CUCHECK(cudaEventElapsedTime(&ms, events[i], events[i+1]));
total += ms;
}
return total;
}
// return the number of CUDA events
cudaEvent_t getEvent(size_t i) {return events[i];}
cudaEvent_t getLastEvent() {return events[events.size()-1];}
// return the time between event 2*i and 2*i+1
float getEventTime(int i) {
float ms;
CUCHECK(cudaEventElapsedTime(&ms, events[i*2], events[i*2+1]));
return ms;
}
};
#endif // __CUDA_TIMER_H__