XGCa
clock_check.hpp
Go to the documentation of this file.
1 #ifndef CLOCK_CHECK_HPP
2 #define CLOCK_CHECK_HPP
3 
4 #include <chrono>
5 #include "my_mpi.hpp"
6 #include "timer_macro.hpp"
7 
8 class ClockCheck{
9 
10  std::chrono::steady_clock::time_point start_time_;
11  std::chrono::minutes duration_;
13  bool on;
14 
15  public:
16 
17  // Constructor takes the number of minutes
18  ClockCheck(int minutes)
19  : start_time_(std::chrono::steady_clock::now()),
20  duration_int_(minutes),
21  on(false)
22  {
23  if(minutes>0){
24  duration_ = std::chrono::minutes(minutes);
25  on = true;
26  }
27  }
28 
29  // Returns true if that many minutes have passed since initialization
30  bool hasElapsed() const {
31  if(!on) return false;
32 
33  GPTLstart("JOB_TIME_CHECK");
34  auto now = std::chrono::steady_clock::now();
35  auto time_elapsed = now - start_time_;
36  bool time_has_elapsed = (time_elapsed > duration_);
37 
38  // Use time on root rank to ensure all ranks agree
39  // If MPI_Bcast is expensive, could rely on local time until close to end.
40  //if(time_remaining < 20){
41 #ifdef USE_MPI
42  MPI_Bcast( &time_has_elapsed, 1, MPI_C_BOOL, 0, SML_COMM_WORLD );
43 #endif
44  //}
45  GPTLstop("JOB_TIME_CHECK");
46 
47  if(is_rank_zero() && time_has_elapsed) printf("\n\n*** Exiting XGC early due to command line walltime limit (max_walltime = %d minutes) ***\n\n\n", duration_int_);
48 
49  return time_has_elapsed;
50  }
51 };
52 
53 #endif
Definition: clock_check.hpp:8
std::chrono::minutes duration_
Definition: clock_check.hpp:11
std::chrono::steady_clock::time_point start_time_
Definition: clock_check.hpp:10
ClockCheck(int minutes)
Definition: clock_check.hpp:18
int duration_int_
Definition: clock_check.hpp:12
bool on
Definition: clock_check.hpp:13
bool hasElapsed() const
Definition: clock_check.hpp:30
bool is_rank_zero()
Definition: globals.hpp:27
MPI_Comm SML_COMM_WORLD
Definition: my_mpi.cpp:4
logical false
Definition: module.F90:102
static int GPTLstart(const char *name)
Definition: timer_macro.hpp:9
static int GPTLstop(const char *name)
Definition: timer_macro.hpp:10