Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/Base/Timer.hpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.,
7 : : 2022-2024 J. Bakosi
8 : : All rights reserved. See the LICENSE file for details.
9 : : \brief Timer declaration
10 : : \details Timer declaration. Timer is a simple class to do timing various
11 : : parts of the code in a portable way. The functionality is intended to be
12 : : very minimal and simple, but still convenient to use, with as little state
13 : : as possible. For an example client code, see walker::Main in Main/Walker.C.
14 : : */
15 : : // *****************************************************************************
16 : : #ifndef Timer_h
17 : : #define Timer_h
18 : :
19 : : #include <cstdint>
20 : : #include <chrono>
21 : :
22 : : #include "NoWarning/pup.hpp" // for er
23 : : #include "Types.hpp"
24 : :
25 : : namespace tk {
26 : :
27 : : //! \brief Simple class to do timing
28 : : //! \details Timing use at various parts of the code in a portable way. The
29 : : //! functionality is intended to be very minimal and simple, but still
30 : : //! convenient to use, with as little state as possible.
31 : : class Timer {
32 : :
33 : : public:
34 : : // Shorthands for seconds duration, hours, minutes, seconds
35 : : using Dsec = std::chrono::duration< real >;
36 : : using hours = std::chrono::hours;
37 : : using minutes = std::chrono::minutes;
38 : : using seconds = std::chrono::seconds;
39 : : // Shorthand for clock, setting clock type
40 : : using clock = std::chrono::high_resolution_clock;
41 : :
42 : : //! Watch stores time in hours:minutes:seconds
43 : : struct Watch {
44 : : hours hrs;
45 : : minutes min;
46 : : seconds sec;
47 : : //! Zero constructor. Zeros hours, minutes, and seconds
48 : 2883 : explicit Watch() :
49 : 2883 : hrs( std::chrono::duration_cast< hours >( clock::duration::zero()) ),
50 : 2883 : min( std::chrono::duration_cast< minutes >( clock::duration::zero() ) ),
51 [ + - ]: 2883 : sec( std::chrono::duration_cast< seconds >( clock::duration::zero() ) )
52 : : {}
53 : : //! Fill constructor. Initialize hours, minutes, and seconds given
54 : 632 : explicit Watch( hours&& h, minutes&& m, seconds&& s ) :
55 : 632 : hrs( std::move(h) ), min( std::move(m) ), sec( std::move(s) ) {}
56 : : };
57 : :
58 : : //! Constructor: initialize clock to current time stamp
59 [ + + ]: 10860 : explicit Timer() : m_start( clock::now() ), m_prev( m_start ) {}
60 : :
61 : : //! Zero timer
62 [ + - ]: 2479 : void zero() { m_start = clock::now(); m_prev = m_start; }
63 : :
64 : : //! Query time in second since the constructor call
65 : : //! \return Time elapsed between start and stop as a real number
66 : : real dsec() const {
67 [ + - ][ + - ]: 888 : return std::chrono::duration_cast< Dsec >(clock::now() - m_start).count();
[ + - ][ + - ]
68 : : }
69 : :
70 : : //! Query time in second since the constructor call
71 : : Watch hms() const;
72 : :
73 : : //! Estimate time for accomplishment
74 : : void eta( real term, real time, uint64_t nstep, uint64_t it,
75 : : tk::real res0, tk::real res, tk::real rest,
76 : : Watch& elapsedWatch, Watch& estimatedWatch );
77 : :
78 : : /** @name Pack/Unpack: Serialize Timer object for Charm++ */
79 : : ///@{
80 : : //! Pack/Unpack serialize member function
81 : : //! \param[in,out] p Charm++'s PUP::er serializer object reference
82 : 20303 : void pup( PUP::er& p ) {
83 : 20303 : p( reinterpret_cast<char*>(&m_start), sizeof(clock::time_point) );
84 : 20303 : p( reinterpret_cast<char*>(&m_prev), sizeof(clock::time_point) );
85 : 20303 : }
86 : : //! \brief Pack/Unpack serialize operator|
87 : : //! \param[in,out] p Charm++'s PUP::er serializer object reference
88 : : //! \param[in,out] t Timer object reference
89 [ - - ][ - - ]: 20303 : friend void operator|( PUP::er& p, Timer& t ) { t.pup(p); }
[ - - ][ + - ]
[ - - ][ - - ]
[ + - ][ + - ]
90 : : ///@}
91 : :
92 : : private:
93 : : clock::time_point m_start; //!< Time stamp at Timer() or zero()
94 : : clock::time_point m_prev; //!< Time stamp at previous update in eta()
95 : : };
96 : :
97 : : //! Convert existing time stamp as a real to Watch (global scope)
98 : : Timer::Watch
99 : : hms( tk::real stamp );
100 : :
101 : : } // tk::
102 : :
103 : : #endif // Timer_h
|