-
Notifications
You must be signed in to change notification settings - Fork 3
/
timer.cu
53 lines (40 loc) · 1017 Bytes
/
timer.cu
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
#include <cuda.h>
#include <memory>
#include <exception>
#include <stdexcept>
#include "timer.h"
static void deleteTimer(Timer* timer)
{
cudaEventDestroy(timer->started);
cudaEventDestroy(timer->stopped);
delete timer;
}
TimerPtr createTimer()
{
cudaError_t err;
Timer* timer = new Timer;
err = cudaEventCreate(&timer->started);
if (err != cudaSuccess)
{
delete timer;
throw std::runtime_error(cudaGetErrorString(err));
}
err = cudaEventCreate(&timer->stopped);
if (err != cudaSuccess)
{
cudaEventDestroy(timer->started);
delete timer;
throw std::runtime_error(cudaGetErrorString(err));
}
return TimerPtr(timer, &deleteTimer);
}
double Timer::usecs() const
{
float milliseconds = .0f;
cudaError_t err = cudaEventElapsedTime(&milliseconds, started, stopped);
if (err != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(err));
}
return (double) milliseconds * 1000;
}