-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathevent.h
54 lines (41 loc) · 1.15 KB
/
event.h
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
#ifndef __BENCHMARK_EVENT_H__
#define __BENCHMARK_EVENT_H__
// #ifndef __CUDACC__
// #define __device__
// #define __host__
// #endif
#include "cuda.h"
#include <string>
#include <stdexcept>
struct Event
{
cudaEvent_t event;
inline Event(cudaStream_t stream = 0)
{
auto err = cudaEventCreateWithFlags(&event, cudaEventDefault);
if (err != cudaSuccess)
{
throw std::runtime_error(std::string("Failed to create event: ") + cudaGetErrorString(err));
}
err = cudaEventRecord(event, stream);
if (err != cudaSuccess)
{
throw std::runtime_error(std::string("Failed to record event on stream: ") + cudaGetErrorString(err));
}
}
inline ~Event()
{
cudaEventDestroy(event);
}
inline double operator-(const Event& other) const
{
float msecs = 0;
auto err = cudaEventElapsedTime(&msecs, other.event, event);
if (err != cudaSuccess)
{
throw std::runtime_error(std::string("Could not calculate elapsed time: ") + cudaGetErrorString(err));
}
return ((double) msecs) * 1e3;
}
};
#endif