OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
timer.hpp
1//
2// Timer.h
3//
4//
5
6#ifndef TIMER_HPP
7#define TIMER_HPP
8
9#include <time.h>
10#include <sys/time.h>
11
12#ifdef __MACH__
13#include <mach/clock.h>
14#include <mach/mach.h>
15#endif
16
17#include "util/cuda_util.hpp"
18
27class timer
28{
30 bool running;
31
33 struct timespec tsstart;
34
36 clock_t cstart;
37
38 // stop time
39 struct timespec tsstop;
40
42 clock_t cstop;
43
44#if defined(__NVCC__) && !defined(CUDA_ON_CPU)
45 #ifdef __HIP__
46 hipEvent_t start_g, stop_g;
47 #else
48 cudaEvent_t start_g, stop_g;
49 #endif
50#endif
51
52 // Fill the stop point
53 void check()
54 {
55#if defined(SYNC_BEFORE_TAKE_TIME) && defined(__NVCC__)
56 #ifdef __HIP__
57 hipDeviceSynchronize();
58 #else
59 cudaDeviceSynchronize();
60 #endif
61#endif
62
63#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
64 clock_serv_t cclock;
65 mach_timespec_t mts;
66 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
67 clock_get_time(cclock, &mts);
68 mach_port_deallocate(mach_task_self(), cclock);
69 tsstop.tv_sec = mts.tv_sec;
70 tsstop.tv_nsec = mts.tv_nsec;
71#else
72 clock_gettime(CLOCK_REALTIME, &tsstop);
73#endif
74 cstop = clock();
75 }
76
77public:
78
81 :running(false),cstart(0),cstop(0)
82 {
83 tsstart = timespec();
84 tsstop = timespec();
85 }
86
90 void start()
91 {
92 // time is running
93 running = true;
94
95#if defined(SYNC_BEFORE_TAKE_TIME) && defined(__NVCC__)
96 cudaDeviceSynchronize();
97#endif
98
99#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
100 clock_serv_t cclock;
101 mach_timespec_t mts;
102 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
103 clock_get_time(cclock, &mts);
104 mach_port_deallocate(mach_task_self(), cclock);
105 tsstart.tv_sec = mts.tv_sec;
106 tsstart.tv_nsec = mts.tv_nsec;
107#else
108 clock_gettime(CLOCK_REALTIME, &tsstart);
109#endif
110
111 cstart = clock();
112
113 }
114
119 void stop()
120 {
121 if (running == false) return;
122 running = false;
123 check();
124 }
125
130 double getwct()
131 {
132 if (running == true)
133 check();
134
135 return ((double)(tsstop.tv_sec - tsstart.tv_sec)) + ((1e-9) * ((double)(tsstop.tv_nsec - tsstart.tv_nsec)));
136 }
137
142 double getcputime()
143 {
144 if (running == true)
145 check();
146
147 return (((double)(cstop - cstart)) / CLOCKS_PER_SEC);
148 }
149
154 void reset()
155 {
156 tsstart = tsstop;
157 cstart = cstop;
158 }
159
160 #if defined(__NVCC__) && !defined(CUDA_ON_CPU)
161
162 void startGPU()
163 {
164 #ifdef __HIP__
165 hipEventCreate(&start_g);
166 hipEventRecord(start_g,0);
167 #else
168 cudaEventCreate(&start_g);
169 cudaEventRecord(start_g,0);
170 #endif
171 }
172
173 void stopGPU()
174 {
175 #ifdef __HIP__
176 hipEventCreate(&stop_g);
177 hipEventRecord(stop_g,0);
178 hipEventSynchronize(stop_g);
179 #else
180 cudaEventCreate(&stop_g);
181 cudaEventRecord(stop_g,0);
182 cudaEventSynchronize(stop_g);
183 #endif
184 }
185
186 double getwctGPU()
187 {
188 float elapsedTime;
189
190 #ifdef __HIP__
191 hipEventElapsedTime(&elapsedTime, start_g,stop_g);
192 #else
193 cudaEventElapsedTime(&elapsedTime, start_g,stop_g);
194 #endif
195
196 return elapsedTime;
197 }
198
199 #endif
200};
201
202#endif
203
204
Class for cpu time benchmarking.
Definition timer.hpp:28
bool running
Flag that indicate if the timer is running or not.
Definition timer.hpp:30
clock_t cstart
start time from epoch
Definition timer.hpp:36
void stop()
Stop the timer.
Definition timer.hpp:119
double getcputime()
Return the cpu time.
Definition timer.hpp:142
void reset()
Reset the timer.
Definition timer.hpp:154
clock_t cstop
stop time from epoch
Definition timer.hpp:42
void start()
Start the timer.
Definition timer.hpp:90
double getwct()
Return the elapsed real time.
Definition timer.hpp:130
timer()
Default constructor.
Definition timer.hpp:80
struct timespec tsstart
starting time
Definition timer.hpp:33