forked from illuhad/mandelgpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.hpp
104 lines (85 loc) · 2.13 KB
/
performance.hpp
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
/*
* This file is part of mandelgpu, a free GPU accelerated fractal viewer,
* Copyright (C) 2016 Aksel Alpay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PERFORMANCE_HPP
#define PERFORMANCE_HPP
#include <iostream>
#include <sys/time.h>
//#include <ctime>
class timer
{
timespec start_;
timespec stop_;
bool is_running_;
public:
/// Construct object
timer()
: is_running_(false)
{
}
/// start the timer
void start()
{
clock_gettime(CLOCK_MONOTONIC, &start_);
is_running_ = true;
}
/// @return whether the timer is currently running
bool is_running() const
{
return is_running_;
}
/// Stops the timer.
/// @return The duration since the call to \c timer::start() in seconds
/// with nanoseconds of precision
double stop()
{
if (!is_running_)
return 0.0;
clock_gettime(CLOCK_MONOTONIC, &stop_);
double t = stop_.tv_sec - start_.tv_sec;
t += static_cast<double> (stop_.tv_nsec - start_.tv_nsec) * 1e-9;
is_running_ = false;
return t;
}
};
class performance_estimator
{
public:
void start()
{
_timer.start();
}
struct result
{
double bandwidth;
double flops;
double time;
};
result stop(std::size_t num_bytes, std::size_t num_flops)
{
double t = _timer.stop();
//std::cout << num_flops << " " << t << std::endl;
result r;
r.bandwidth = static_cast<double>(num_bytes) * 1.e-9 / t;
r.flops = static_cast<double>(num_flops) * 1.e-9 / t;
r.time = t;
return r;
}
private:
timer _timer;
};
#endif