-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiming.cpp
65 lines (51 loc) · 1.42 KB
/
timing.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
#include <cuda_runtime.h>
#include "timing.h"
#ifdef _WIN32
#define WINDOWS_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
static double freq;
void InitTime() {
LARGE_INTEGER timerFreq;
QueryPerformanceFrequency(&timerFreq);
freq = (double) timerFreq.QuadPart;
}
uint64_t GetTime() {
LARGE_INTEGER t1;
QueryPerformanceCounter(&t1);
return t1.QuadPart;
}
// returns milliseconds
float TimeDelta(uint64_t start, uint64_t stop) {
return float(((stop - start) * 1000) / freq);
}
#else
#include <time.h>
void InitTime() {}
uint64_t GetTime() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return (uint64_t) ts.tv_nsec + (uint64_t) ts.tv_sec * 1000000000ull;
}
// returns milliseconds
float TimeDelta(uint64_t start, uint64_t stop) {
return ((stop - start) / 1000000.0);
}
#endif
cuda_timer StartCudaTimer() {
cuda_timer t;
cudaEventCreate(&t.start);
cudaEventCreate(&t.stop, cudaEventBlockingSync);
cudaEventRecord(t.start);
return t;
}
// blocks until gpu is finished, returns milliseconds
float StopCudaTimer(cuda_timer t) {
float result = 0.0f;
cudaEventRecord(t.stop);
cudaEventSynchronize(t.stop);
cudaEventElapsedTime(&result, t.start, t.stop);
cudaEventDestroy(t.start);
cudaEventDestroy(t.stop);
return result;
}