-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.h
More file actions
84 lines (55 loc) · 2.13 KB
/
timer.h
File metadata and controls
84 lines (55 loc) · 2.13 KB
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
#ifndef TIMER_H
#define TIMER_H
#define USE_MPIWTIME 1
#define USE_NANOPRECISION 1 // NOTE: If this breaks the build then set it to 0
#if USE_MPIWTIME
#include <mpi.h>
#define TIMER_INIT_COUNTERS(_start_,_stop_) double _start_, _stop_;
#define TIMER_GET_CLOCK(_counter_) {\
MPI_Barrier(MPI_COMM_WORLD); \
_counter_= MPI_Wtime();\
}
#define TIMER_GET_DIFF(_start_,_stop_,_diff_){ \
_diff_ =((_stop_) - (_start_))*(1000000000); }
#elif USE_NANOPRECISION
/*
Nanosecond Precision Timer
http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/
timespec diff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
NOTE: need to link with -lrt
https://stackoverflow.com/questions/3875197/gcc-with-std-c99-complains-about-not-knowing-struct-timespec
*/
#include <time.h>
//TODO: fix diff to match what was in blog
#define TIMER_INIT_COUNTERS(_start_,_stop_) struct timespec _start_, _stop_;
#define TIMER_GET_CLOCK(_counter_) clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &_counter_);
#define TIMER_GET_DIFF(_start_,_stop_,_diff_){ \
_diff_ = (_stop_.tv_sec-_start_.tv_sec) * 1000000000 + _stop_.tv_nsec - _start_.tv_nsec;}
#else
/*
Microsecond Precision Timer.
*/
#include <sys/time.h>
#define TIMER_INIT_COUNTERS(_start_,_stop_) struct timeval _start_; struct timeval _stop_;
#define TIMER_GET_CLOCK(_counter_) gettimeofday(&_counter_, NULL);
#define TIMER_GET_DIFF(_start_,_stop_,_diff_) _diff_ = ((_stop_.tv_sec - _start_.tv_sec) * 1e6 + _stop_.tv_usec - _start_.tv_usec)* 1000; // <-- TODO: this 1000 should actually be derived like it is in memory mountatin
#endif // TIMERS
#define TIMER_WARMUP(_start_,_stop_)\
{\
TIMER_GET_CLOCK(_start_); TIMER_GET_CLOCK(_stop_); \
TIMER_GET_CLOCK(_start_); TIMER_GET_CLOCK(_stop_); \
TIMER_GET_CLOCK(_start_); TIMER_GET_CLOCK(_stop_); \
TIMER_GET_CLOCK(_start_); TIMER_GET_CLOCK(_stop_); \
}
#endif /* TIMER_H */