Skip to content

Commit 52a9d81

Browse files
committed
use second and double precision of timing result
1 parent 93af397 commit 52a9d81

4 files changed

Lines changed: 106 additions & 54 deletions

File tree

benchmark/preconditioner/preconditioner.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,8 @@ void run_preconditioner(const char *precond_name,
171171

172172
// the timer is out of the loops to reduce calling synchronize
173173
// overhead, so the timer does not know the number of repetitions.
174-
// get_total_time() and compute_average_time() have the same result
175-
// but in different type. We use compute_average_time to eliminate
176-
// the type-casting here.
177174
auto generate_time =
178-
generate_timer->compute_average_time() / FLAGS_repetitions;
175+
generate_timer->get_total_time() / FLAGS_repetitions;
179176
add_or_set_member(this_precond_data["generate"], "time",
180177
generate_time, allocator);
181178

@@ -188,11 +185,7 @@ void run_preconditioner(const char *precond_name,
188185

189186
// the timer is out of the loops to reduce calling synchronize
190187
// overhead, so the timer does not know the number of repetitions.
191-
// get_total_time() and compute_average_time() have the same result
192-
// but in different type. We use compute_average_time to eliminate
193-
// the type-casting here.
194-
auto apply_time =
195-
apply_timer->compute_average_time() / FLAGS_repetitions;
188+
auto apply_time = apply_timer->get_total_time() / FLAGS_repetitions;
196189
add_or_set_member(this_precond_data["apply"], "time", apply_time,
197190
allocator);
198191
}

benchmark/utils/chrono_utils.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*******************************<GINKGO LICENSE>******************************
2+
Copyright (c) 2017-2020, the Ginkgo authors
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions
7+
are met:
8+
9+
1. Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright
13+
notice, this list of conditions and the following disclaimer in the
14+
documentation and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21+
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23+
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
******************************<GINKGO LICENSE>*******************************/
32+
33+
#ifndef GKO_BENCHMARK_UTILS_CHRONO_UTILS_HPP_
34+
#define GKO_BENCHMARK_UTILS_CHRONO_UTILS_HPP_
35+
36+
37+
#include <chrono>
38+
39+
40+
namespace {
41+
42+
43+
/**
44+
* Get the seconds of duration in double precision.
45+
*
46+
* @param duration the chrono::steady_clock::duration
47+
*
48+
* @return the seconds of duration in double precision.
49+
*/
50+
double get_duration_in_seconds(std::chrono::steady_clock::duration time)
51+
{
52+
auto counts =
53+
std::chrono::duration_cast<std::chrono::nanoseconds>(time).count();
54+
constexpr int sec_in_ns = 1e9;
55+
return static_cast<double>(counts) / sec_in_ns;
56+
}
57+
58+
59+
} // namespace
60+
61+
62+
#endif // GKO_BENCHMARK_UTILS_CHRONO_UTILS_HPP_

benchmark/utils/loggers.hpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4343
#include <unordered_map>
4444

4545

46+
#include "benchmark/utils/chrono_utils.hpp"
4647
#include "benchmark/utils/general.hpp"
4748

4849

@@ -109,11 +110,7 @@ struct OperationLogger : gko::log::Logger {
109110
for (const auto &entry : total) {
110111
add_or_set_member(
111112
object, entry.first.c_str(),
112-
std::chrono::duration_cast<std::chrono::nanoseconds>(
113-
entry.second)
114-
.count() /
115-
repetitions,
116-
alloc);
113+
get_duration_in_seconds(entry.second) / repetitions, alloc);
117114
}
118115
}
119116

@@ -208,9 +205,7 @@ struct ResidualLogger : gko::log::Logger {
208205
const gko::LinOp *residual_norm) const override
209206
{
210207
timestamps.PushBack(
211-
std::chrono::duration_cast<std::chrono::nanoseconds>(
212-
std::chrono::steady_clock::now() - start)
213-
.count(),
208+
get_duration_in_seconds(std::chrono::steady_clock::now() - start),
214209
alloc);
215210
if (residual_norm) {
216211
rec_res_norms.PushBack(

benchmark/utils/timer.hpp

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4040
#include <gflags/gflags.h>
4141

4242

43+
#include "benchmark/utils/chrono_utils.hpp"
44+
45+
4346
#ifdef HAS_CUDA
4447

4548

@@ -98,47 +101,46 @@ class Timer {
98101
}
99102

100103
/**
101-
* Get the summation of each time in nanoseconds.
104+
* Get the summation of each time in seconds.
102105
*
103-
* @return the nanoseconds of total time
106+
* @return the seconds of total time
104107
*/
105-
std::int64_t get_total_time() const { return total_duration_ns_; }
108+
double get_total_time() const { return total_duration_sec_; }
106109

107110
/**
108111
* Get the number of repetitions.
109112
*
110113
* @return the number of repetitions
111114
*/
112-
std::int64_t get_num_repetitions() const { return duration_ns_.size(); }
115+
std::int64_t get_num_repetitions() const { return duration_sec_.size(); }
113116

114117
/**
115-
* Compute the average time of repetitions in nanoseconds
118+
* Compute the average time of repetitions in seconds
116119
*
117-
* @return the average time in nanoseconds
120+
* @return the average time in seconds
118121
*/
119122
double compute_average_time() const
120123
{
121-
return static_cast<double>(this->get_total_time()) /
122-
this->get_num_repetitions();
124+
return this->get_total_time() / this->get_num_repetitions();
123125
}
124126

125127
/**
126-
* Get the vector containing the time of each repetition in nanoseconds.
128+
* Get the vector containing the time of each repetition in seconds.
127129
*
128-
* @return the vector of time for each repetition in nanoseconds
130+
* @return the vector of time for each repetition in seconds
129131
*/
130-
std::vector<std::int64_t> get_time_detail() const { return duration_ns_; }
132+
std::vector<double> get_time_detail() const { return duration_sec_; }
131133

132134
/**
133-
* Get the latest result in nanoseconds. If there is no result yet, return
135+
* Get the latest result in seconds. If there is no result yet, return
134136
* 0.
135137
*
136-
* @return the latest result in nanoseconds
138+
* @return the latest result in seconds
137139
*/
138-
std::int64_t get_latest_time() const
140+
double get_latest_time() const
139141
{
140-
if (duration_ns_.size() >= 1) {
141-
return duration_ns_.back();
142+
if (duration_sec_.size() >= 1) {
143+
return duration_sec_.back();
142144
} else {
143145
return 0;
144146
}
@@ -149,27 +151,27 @@ class Timer {
149151
*/
150152
void clear()
151153
{
152-
duration_ns_.clear();
154+
duration_sec_.clear();
153155
tic_called_ = false;
154-
total_duration_ns_ = 0;
156+
total_duration_sec_ = 0;
155157
}
156158

157159
/**
158160
* Create a timer
159161
*/
160-
Timer() : tic_called_(false), total_duration_ns_(0) {}
162+
Timer() : tic_called_(false), total_duration_sec_(0) {}
161163

162164
protected:
163165
/**
164-
* Put the nanosecond result into vector
166+
* Put the second result into vector
165167
*
166-
* @param ns the nanosecond result to insert
168+
* @param ns the second result to insert
167169
*/
168-
void add_record(std::int64_t ns)
170+
void add_record(double ns)
169171
{
170172
// add the result;
171-
duration_ns_.emplace_back(ns);
172-
total_duration_ns_ += ns;
173+
duration_sec_.emplace_back(ns);
174+
total_duration_sec_ += ns;
173175
}
174176

175177
/**
@@ -178,16 +180,16 @@ class Timer {
178180
virtual void tic_impl() = 0;
179181

180182
/**
181-
* The implementation of toc. Return the nanoseconds result.
183+
* The implementation of toc. Return the seconds result.
182184
*
183-
* @return the nanoseconds result
185+
* @return the seconds result
184186
*/
185-
virtual std::int64_t toc_impl() = 0;
187+
virtual double toc_impl() = 0;
186188

187189
private:
188-
std::vector<std::int64_t> duration_ns_;
190+
std::vector<double> duration_sec_;
189191
bool tic_called_;
190-
std::int64_t total_duration_ns_;
192+
double total_duration_sec_;
191193
};
192194

193195

@@ -212,13 +214,11 @@ class CpuTimer : public Timer {
212214
start_ = std::chrono::steady_clock::now();
213215
}
214216

215-
std::int64_t toc_impl() override
217+
double toc_impl() override
216218
{
217219
exec_->synchronize();
218220
auto stop = std::chrono::steady_clock::now();
219-
auto duration_time =
220-
std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start_)
221-
.count();
221+
auto duration_time = get_duration_in_seconds(stop - start_);
222222
return duration_time;
223223
}
224224

@@ -268,7 +268,7 @@ class CudaTimer : public Timer {
268268
GKO_ASSERT_NO_CUDA_ERRORS(cudaEventRecord(start_));
269269
}
270270

271-
std::int64_t toc_impl() override
271+
double toc_impl() override
272272
{
273273
gko::cuda::device_guard g{id_};
274274
// Currently, gko::CudaExecutor always use default stream.
@@ -279,7 +279,8 @@ class CudaTimer : public Timer {
279279
// resolution of around 0.5 microseconds
280280
GKO_ASSERT_NO_CUDA_ERRORS(
281281
cudaEventElapsedTime(&duration_time, start_, stop_));
282-
return static_cast<std::int64_t>(duration_time * 1e6);
282+
constexpr int sec_in_ms = 1e3;
283+
return static_cast<double>(duration_time) / sec_in_ms;
283284
}
284285

285286
private:
@@ -333,7 +334,7 @@ class HipTimer : public Timer {
333334
GKO_ASSERT_NO_HIP_ERRORS(hipEventRecord(start_));
334335
}
335336

336-
std::int64_t toc_impl() override
337+
double toc_impl() override
337338
{
338339
gko::hip::device_guard g{id_};
339340
// Currently, gko::HipExecutor always use default stream.
@@ -344,7 +345,8 @@ class HipTimer : public Timer {
344345
// resolution of around 0.5 microseconds
345346
GKO_ASSERT_NO_HIP_ERRORS(
346347
hipEventElapsedTime(&duration_time, start_, stop_));
347-
return static_cast<std::int64_t>(duration_time * 1e6);
348+
constexpr int sec_in_ms = 1e3;
349+
return static_cast<double>(duration_time) / sec_in_ms;
348350
}
349351

350352
private:

0 commit comments

Comments
 (0)