Branch data Line data Source code
1 : : // ***************************************************************************** 2 : : /*! 3 : : \file src/Base/Timer.cpp 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 definition 9 : : \details Timer definition. 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 Main. 13 : : */ 14 : : // ***************************************************************************** 15 : : 16 : : #include <algorithm> 17 : : #include <ratio> 18 : : #include <cmath> 19 : : 20 : : #include "Timer.hpp" 21 : : 22 : : using tk::Timer; 23 : : 24 : : tk::Timer::Watch 25 : 197 : Timer::hms() const 26 : : // ***************************************************************************** 27 : : // Return time elapsed between start and stop for timer as hours, minutes, and 28 : : // seconds. 29 : : //! \return Time elapsed between start and stop as hours, minutes, and seconds, 30 : : //! as a Watch struct. 31 : : // ***************************************************************************** 32 : : { 33 : : using std::chrono::duration_cast; 34 : : 35 : : // Compute time difference between start and now in seconds 36 : 197 : Dsec elapsed = clock::now() - m_start; 37 : : 38 : : // Put elapsed time in watch as hours:minutes:seconds 39 : : Watch watch( duration_cast< hours >( elapsed ), 40 : : duration_cast< minutes >( elapsed ) % hours(1), 41 : : duration_cast< seconds >( elapsed ) % minutes(1) ); 42 : 197 : return watch; 43 : : } 44 : : 45 : : void 46 : 1701 : Timer::eta( tk::real term, tk::real time, uint64_t nstep, uint64_t it, 47 : : Watch& elapsedWatch, Watch& estimatedWatch ) const 48 : : // ***************************************************************************** 49 : : // Estimate time for accomplishment 50 : : //! \param[in] term Time at which to terminate time stepping 51 : : //! \param[in] time Current time 52 : : //! \param[in] nstep Max number of time steps to take 53 : : //! \param[in] it Current iteration count 54 : : //! \param[out] elapsedWatch Elapsed time in h:m:s 55 : : //! \param[out] estimatedWatch Estimated time for accomplishmet in h:m:s 56 : : // ***************************************************************************** 57 : : { 58 : : using std::chrono::duration_cast; 59 : : 60 : : Dsec elapsed, estimated; 61 : : 62 [ + - ]: 1701 : if (it == 0) { 63 : : 64 : : // First iteration, just return zero 65 : : elapsed = estimated = clock::duration::zero(); 66 : : 67 : : } else { 68 : : 69 : : // Compute time difference between start and now in seconds 70 : 1701 : elapsed = clock::now() - m_start; 71 : : 72 : : // Estimate time until nstep in seconds 73 [ + - ]: 1701 : Dsec est_nstep = elapsed * static_cast<tk::real>(nstep-it) / it; 74 : : // Estimate time until term in seconds 75 : : tk::real eps = std::numeric_limits< real >::epsilon(); 76 : : tk::real large = std::numeric_limits< real >::max() - 1; 77 [ + - ][ + - ]: 1701 : Dsec est_term = std::abs(time) > eps && term < large ? 78 : 1701 : elapsed * (term-time) / time : 79 : 0 : est_nstep; 80 : : 81 : : // Time stepping will stop at term or nstep, whichever is sooner 82 : 1701 : estimated = min(est_term, est_nstep); 83 : : 84 : : } 85 : : 86 : : // Put elapsed time in watch as hours:minutes:seconds 87 : 1701 : elapsedWatch.hrs = duration_cast< hours >( elapsed ); 88 : 1701 : elapsedWatch.min = duration_cast< minutes >( elapsed ) % hours(1); 89 : 1701 : elapsedWatch.sec = duration_cast< seconds >( elapsed ) % minutes(1); 90 : : // Put estimated time in watch as hours:minutes:seconds 91 : 1701 : estimatedWatch.hrs = duration_cast< hours >( estimated ); 92 : 1701 : estimatedWatch.min = duration_cast< minutes >( estimated ) % hours(1); 93 : 1701 : estimatedWatch.sec = duration_cast< seconds >( estimated ) % minutes(1); 94 : 1701 : } 95 : : 96 : : Timer::Watch 97 : 1 : tk::hms( tk::real stamp ) 98 : : // ***************************************************************************** 99 : : //! Convert existing time stamp as a real to Watch (global-scope) 100 : : //! \param[in] stamp Time stamp as a real number 101 : : //! \return Time as hours, minutes, and seconds, as a Watch struct. 102 : : // ***************************************************************************** 103 : : { 104 : : using std::chrono::duration_cast; 105 : : const auto d = Timer::Dsec( stamp ); 106 : : return 107 : : Timer::Watch( duration_cast< Timer::hours >( d ), 108 : : duration_cast< Timer::minutes >( d ) % Timer::hours(1), 109 : 1 : duration_cast< Timer::seconds >( d ) % Timer::minutes(1) ); 110 : : }