@@ -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
162164protected:
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
187189private:
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
285286private:
@@ -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
350352private:
0 commit comments