38 throw std::runtime_error(
"LoopTimer started already!");
39 start_time = CLOCK::now();
47 throw std::runtime_error(
"LoopTimer didn't start but called stop!");
49 std::chrono::duration<double> elapsed = CLOCK::now() - start_time;
50 total_time += elapsed.count();
59 return total_time / nloop;
69 const std::string& stop_file_prefix,
71 : MaxCPUSecs(maxCPUSecs),
73 stop_filename_(stop_file_prefix +
".STOP"),
76 if (stop_file_prefix.empty())
77 throw std::runtime_error(
"Stop control filename prefix must not be empty!");
83 throw std::runtime_error(
"Failed to delete the existing stop control file \"" +
stop_filename_ +
84 "\", cannot continue!");
95 m_elapsed = runtimeManager.elapsed();
97 if (m_elapsed >= MaxCPUSecs)
99 stop_status_ = StopStatus::MAX_SECONDS_PASSED;
103 m_remaining = MaxCPUSecs - m_elapsed;
104 bool enough_time =
true;
105 if ((m_loop_margin * m_loop_time + m_runtime_safety_padding) > m_remaining)
108 stop_status_ = StopStatus::NOT_ENOUGH_TIME;
112 template<
class CLOCK>
115 if (std::ifstream(stop_filename_.c_str()))
117 stop_status_ = StopStatus::STOP_FILE;
124 template<
class CLOCK>
127 bool need_to_stop =
false;
128 need_to_stop |= !enough_time_for_next_iteration(loop_timer);
129 need_to_stop |= stop_file_requested();
133 template<
class CLOCK>
138 std::stringstream
log;
142 log <<
"Completed block " << std::setw(4) << block + 1 <<
" of " <<
num_blocks <<
" average " 143 << std::setprecision(4) << m_loop_time <<
" secs/block after " << std::setprecision(4) << m_elapsed <<
" secs" 149 template<
class CLOCK>
152 std::stringstream
log;
153 log <<
"RunTimeControl takes action in " << driverName <<
" driver." << std::endl;
154 if (stop_status_ == StopStatus::MAX_SECONDS_PASSED)
155 log <<
"Time limit reached. Stopping after block " << block << std::endl
156 <<
"Hard limit (seconds) " << MaxCPUSecs <<
", elapsed (seconds) " << m_elapsed << std::endl;
157 else if (stop_status_ == StopStatus::NOT_ENOUGH_TIME)
159 log <<
"Insufficient time for next block. Stopping after block " << block << std::endl;
160 log <<
" Iteration time per " << driverName <<
" block (seconds) = " << m_loop_time << std::endl;
161 log <<
" Elapsed time (seconds) = " << m_elapsed << std::endl;
162 log <<
" Remaining time (seconds) = " << m_remaining << std::endl;
164 else if (stop_status_ == StopStatus::STOP_FILE)
165 log <<
"Stop requested from the control file \"" + stop_filename_ +
"\", stopping after block " << block
168 throw std::runtime_error(
"Unidentified stop status!");
std::string generateProgressMessage(const std::string &driverName, int block, int num_blocks) const
generate terse progress messages
RunTimeControl(RunTimeManager< CLOCK > &rm, int maxCPUSecs, const std::string &stop_file_prefix, bool cleanup=true)
constructor
helper functions for EinsplineSetBuilder
RunTimeManager< ChronoClock > run_time_manager
std::string generateStopMessage(const std::string &driverName, int block) const
generate stop message explaining why
const std::string stop_filename_
the prefix of the stop file (stop_file_prefix + ".STOP")
bool enough_time_for_next_iteration(LoopTimer< CLOCK > &loop_timer)
bool stop_file_requested()
double m_runtime_safety_padding
MakeReturn< UnaryNode< FnLog, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t log(const Vector< T1, C1 > &l)
Class for determining elapsed run time enabling simulations to adjust to time limits.
bool checkStop(LoopTimer< CLOCK > &loop_timer)
check if the run needs to stop because of walltime or stop control file.
double get_time_per_iteration() const