-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.h
111 lines (82 loc) · 2.43 KB
/
timer.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
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
/*
* Copyright 2008-2010 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __TIMER_H__
#define __TIMER_H__
#include <cuda.h>
#ifdef _WIN32
#include <windows.h>
struct cpu_timer
{
LARGE_INTEGER m_startTime, m_endTime;
cpu_timer() { }
~cpu_timer() { }
void start() { QueryPerformanceCounter(&m_startTime); }
void stop() { cudaThreadSynchronize(); QueryPerformanceCounter(&m_endTime);}
float elapsed_sec() {
LARGE_INTEGER diff;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
diff.QuadPart = m_endTime.QuadPart - m_startTime.QuadPart;
return (float)diff.QuadPart / (float)freq.QuadPart;
}
float elapsed_ms()
{
return elapsed_sec() * 1000.0f;
}
};
#else
#include <sys/time.h>
struct cpu_timer
{
double _start_time, _end_time;
cpu_timer() { }
~cpu_timer() { }
void start() {
struct timeval t;
gettimeofday(&t, 0);
_start_time = t.tv_sec + (1e-6 * t.tv_usec);
}
void stop() {
struct timeval t;
gettimeofday(&t, 0);
_end_time = t.tv_sec + (1e-6 * t.tv_usec);
}
float elapsed_sec() {
return (float) (_end_time - _start_time);
}
float elapsed_ms()
{
return (float) 1000 * (_end_time - _start_time);
}
};
#endif
struct gpu_timer
{
cudaEvent_t e_start, e_stop;
gpu_timer() { cudaEventCreate(&e_start); cudaEventCreate(&e_stop); }
~gpu_timer() { cudaEventDestroy(e_start); cudaEventDestroy(e_stop); }
void start() { cudaEventRecord(e_start, 0); }
void stop() { cudaEventRecord(e_stop, 0); }
float elapsed_ms()
{
cudaEventSynchronize(e_stop);
float ms;
cudaEventElapsedTime(&ms, e_start, e_stop);
return ms;
}
float elapsed_sec() { return elapsed_ms() / 1000.0f; }
};
#endif