Fleet  0.0.9
Inference in the LOT
Control.h
Go to the documentation of this file.
1 #pragma once
2 
3 //#define DEBUG_CONTROL
4 
5 #include <signal.h>
6 
7 #include "FleetArgs.h"
8 #include "Timing.h"
9 #include "IO.h"
10 
11 extern std::atomic<bool> CTRL_C; //volatile sig_atomic_t CTRL_C;
12 
23 struct Control {
24  unsigned long steps;
25  time_ms runtime;
26  size_t nthreads;
27  unsigned long restart;
28 
29  timept start_time;
30 
31  // NOTE TODO: THE BELOW SHOULD BE UPDATED TO BE ATOMIC SINCE ITS ACCESSED BY MULTPLE THREADS
32  std::atomic<unsigned long> done_steps; // how many have we done -- updated by multiple threads
33 
34  bool break_CTRLC; // should we break on ctrl_c?
35 
36 
37 
38  Control(unsigned long st=FleetArgs::steps,
39  unsigned long t=FleetArgs::runtime,
40  size_t thr=FleetArgs::nthreads,
41  unsigned long re=FleetArgs::restart) :
42  steps(st), runtime(t), nthreads(thr), restart(re), break_CTRLC(true) {
43  // We defaultly read arguments from FleetArgs
44 
45  start(); // just defaultly because it's easier
46  }
47 
48  Control(const Control& c) : steps(c.steps), runtime(c.runtime), nthreads(c.nthreads), restart(c.restart), break_CTRLC(true) {
49 
50  // NOTE: this is weird if using multiple threads
51  done_steps = c.done_steps.load();
52  }
53 
54  void start() {
58  done_steps = 0;
59  start_time = now();
60  }
61 
62 
63  bool running() {
69  ++done_steps;
70 
71  if(break_CTRLC and CTRL_C) {
72  #ifdef DEBUG_CONTROL
73  std::cerr << "Control break on CTRL_C" << std::endl;
74  #endif
75  return false;
76  }
77 
78  if(steps > 0 and done_steps >= steps+1) {
79  #ifdef DEBUG_CONTROL
80  std::cerr << "Control break on steps" << std::endl;
81  #endif
82  return false;
83  }
84 
85  if(runtime > 0 and time_since(start_time) >= runtime) {
86  #ifdef DEBUG_CONTROL
87  std::cerr << "Control break on runtime\t"<< runtime << std::endl;
88  #endif
89  return false;
90  }
91 
92  return true;
93  }
94 
95 };
96 
103  unsigned long t=FleetArgs::inner_runtime,
104  size_t thr=1,
105  unsigned long re=FleetArgs::inner_restart) {
106  return {st,t,thr,re};
107 }
unsigned long inner_runtime
Definition: FleetArgs.h:26
unsigned long steps
Definition: Control.h:24
Control(const Control &c)
Definition: Control.h:48
bool break_CTRLC
Definition: Control.h:34
time_ms runtime
Definition: Control.h:25
void start()
Definition: Control.h:54
Definition: Control.h:23
unsigned long inner_restart
Definition: FleetArgs.h:27
size_t nthreads
Definition: Control.h:26
Control(unsigned long st=FleetArgs::steps, unsigned long t=FleetArgs::runtime, size_t thr=FleetArgs::nthreads, unsigned long re=FleetArgs::restart)
Definition: Control.h:38
Control InnerControl(unsigned long st=FleetArgs::inner_steps, unsigned long t=FleetArgs::inner_runtime, size_t thr=1, unsigned long re=FleetArgs::inner_restart)
Make a Control object (NOTE it&#39;s a Control object not an InnerControl one) that has default parameter...
Definition: Control.h:102
std::atomic< unsigned long > done_steps
Definition: Control.h:32
bool running()
Definition: Control.h:63
timept start_time
Definition: Control.h:29
unsigned long inner_steps
Definition: FleetArgs.h:13
size_t nthreads
Definition: FleetArgs.h:20
unsigned long runtime
Definition: FleetArgs.h:25
unsigned long restart
Definition: Control.h:27
unsigned long steps
Definition: FleetArgs.h:12
unsigned long restart
Definition: FleetArgs.h:32
std::atomic< bool > CTRL_C