QMCPACK
RunTimeManager.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2017 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 /** @file RunTimeManager.h
14  * @brief Class for determining elapsed run time enabling simulations to adjust to time limits.
15  */
16 #ifndef QMCPLUSPLUS_RUNTIME_MANAGER_H
17 #define QMCPLUSPLUS_RUNTIME_MANAGER_H
18 
19 #include "Utilities/Clock.h"
20 #include <string>
21 
22 
23 namespace qmcplusplus
24 {
25 template<class CLOCK = ChronoClock>
27 {
28 public:
29  inline double elapsed()
30  {
31  std::chrono::duration<double> elapsed = CLOCK::now() - start_time;
32  return elapsed.count();
33  }
34  // Initialize the start time at static class initialization time
35  RunTimeManager() : start_time(CLOCK::now()) {}
36 
37  bool isStopNeeded() const { return need_to_stop_; }
38  void markStop() { need_to_stop_ = true; }
39 
40 private:
41  const typename CLOCK::time_point start_time;
43 };
44 
46 
47 extern template class RunTimeManager<ChronoClock>;
48 extern template class RunTimeManager<FakeChronoClock>;
49 
50 template<class CLOCK = ChronoClock>
51 class LoopTimer
52 {
53 public:
54  LoopTimer();
55  void start();
56  void stop();
57  double get_time_per_iteration() const;
58 
59 private:
60  int nloop;
61  bool ticking;
62  typename CLOCK::time_point start_time;
63  double total_time;
64 };
65 
66 extern template class LoopTimer<ChronoClock>;
67 extern template class LoopTimer<FakeChronoClock>;
68 
69 template<class CLOCK = ChronoClock>
71 {
72  const int MaxCPUSecs;
74  double m_loop_margin;
75  double m_loop_time;
76  double m_elapsed;
77  double m_remaining;
79  /// the prefix of the stop file (stop_file_prefix + ".STOP")
80  const std::string stop_filename_;
81 
82  enum class StopStatus
83  {
84  CONTINUE, // keep running
85  MAX_SECONDS_PASSED, // all already passed max_seconds
86  NOT_ENOUGH_TIME, // not enough time for next iteration
87  STOP_FILE, // reqsuted stop from a file
88  } stop_status_;
89 
90  bool enough_time_for_next_iteration(LoopTimer<CLOCK>& loop_timer);
91  bool stop_file_requested();
92 
93 public:
94  /** constructor
95  * @param rm the RunTimeManager attached to
96  * @param maxCPUSecs maxmimal allowed seconds from the beginning of the simulation
97  * @param stop_file_prefix the prefix of the stop file
98  * @param cleanup if true, clean up stop files left from previous simulations. Rank 0 handle this.
99  */
100  RunTimeControl(RunTimeManager<CLOCK>& rm, int maxCPUSecs, const std::string& stop_file_prefix, bool cleanup = true);
101 
102  /** check if the run needs to stop because of walltime or stop control file.
103  * it should be called at the end of each block in a driver.
104  */
105  bool checkStop(LoopTimer<CLOCK>& loop_timer);
106 
107  /// generate terse progress messages
108  std::string generateProgressMessage(const std::string& driverName, int block, int num_blocks) const;
109  /// generate stop message explaining why
110  std::string generateStopMessage(const std::string& driverName, int block) const;
111 
112  // for testing
113  void runtime_padding(int runtime_padding) { m_runtime_safety_padding = runtime_padding; }
114  void loop_margin(int loopMargin) { m_loop_margin = loopMargin; }
115 };
116 
117 extern template class RunTimeControl<ChronoClock>;
118 extern template class RunTimeControl<FakeChronoClock>;
119 
120 } // namespace qmcplusplus
121 #endif
CLOCK::time_point start_time
void runtime_padding(int runtime_padding)
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
RunTimeManager< ChronoClock > run_time_manager
const char num_blocks[]
Definition: HDFVersion.h:44
const CLOCK::time_point start_time
const std::string stop_filename_
the prefix of the stop file (stop_file_prefix + ".STOP")
RunTimeManager< CLOCK > & runtimeManager
void loop_margin(int loopMargin)