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