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 : 5646 : explicit Watch() : 49 [ + - ]: 5646 : hrs( std::chrono::duration_cast< hours >( clock::duration::zero()) ), 50 [ + - ]: 5646 : min( std::chrono::duration_cast< minutes >( clock::duration::zero() ) ), 51 [ + - ]: 5646 : sec( std::chrono::duration_cast< seconds >( clock::duration::zero() ) ) 52 : 5646 : {} 53 : : //! Fill constructor. Initialize hours, minutes, and seconds given 54 : 622 : explicit Watch( hours&& h, minutes&& m, seconds&& s ) : 55 : 622 : hrs( std::move(h) ), min( std::move(m) ), sec( std::move(s) ) {} 56 : : }; 57 : : 58 : : //! Constructor: initialize clock to current time stamp 59 : 10524 : explicit Timer() : m_start( clock::now() ), m_prev( m_start ) {} 60 : : 61 : : //! Zero timer 62 : 2458 : 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 : 873 : real dsec() const { 67 [ + - ][ + - ]: 873 : 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 : 19413 : void pup( PUP::er& p ) { 83 : 19413 : p( reinterpret_cast<char*>(&m_start), sizeof(clock::time_point) ); 84 : 19413 : p( reinterpret_cast<char*>(&m_prev), sizeof(clock::time_point) ); 85 : 19413 : } 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 : 19413 : 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