Fleet  0.0.9
Inference in the LOT
Fleet.h
Go to the documentation of this file.
1 
65 /* Globally shared variables and declarations */
66 
67 #pragma once
68 
69 #include <sys/resource.h> // just for setting priority defaulty
70 #include <unistd.h>
71 #include <stdlib.h>
72 #include <signal.h>
73 
74 const std::string FLEET_VERSION = "0.0.98";
75 
79 //#include "Statistics/FleetStatistics.h"
80 
81 #include "Timing.h"
82 #include "Control.h"
83 
84 // Thie one really should be included last because it depends on Primitves
85 // so we will include it here
87 
91 
92 #include "FleetArgs.h"
93 
94 unsigned long random_seed = 0;
95 
96 // This is global that checks whether CTRL_C has been pressed
97 // NOTE: this must be registered in main with signal(SIGINT, Fleet::fleet_interrupt_handler);
98 // which happens in
99 volatile sig_atomic_t CTRL_C = false;
100 
101 // apparently some OSes don't define this
102 #ifndef HOST_NAME_MAX
103  const size_t HOST_NAME_MAX = 256;
104 #endif
106 
116 class Fleet {
117 public:
118  CLI::App app;
119  timept start_time;
120  bool done; // if this is true, we don't call completed on destruction
121 
122  Fleet(std::string brief) : app{brief}, done(false) {
123 
124  app.add_option("--seed", random_seed, "Seed the rng (0 is no seed)");
125  app.add_option("--steps", FleetArgs::steps, "Number of MCMC or MCTS search steps to run");
126  app.add_option("--inner-steps", FleetArgs::inner_steps, "Steps to run inner loop of a search");
127  app.add_option("--burn", FleetArgs::burn, "Burn this many steps");
128  app.add_option("--thin", FleetArgs::thin, "Thinning on callbacks");
129  app.add_option("--print", FleetArgs::print, "Print out every this many");
130  app.add_option("--top", FleetArgs::ntop, "The number to store");
131  app.add_option("-n,--threads", FleetArgs::nthreads, "Number of threads for parallel search");
132  app.add_option("--explore", FleetArgs::explore, "Exploration parameter for MCTS");
133  app.add_option("--restart", FleetArgs::restart, "If we don't improve after this many, restart a chain");
134  app.add_option("-c,--chains", FleetArgs::nchains, "How many chains to run");
135 
136  app.add_option("--header", FleetArgs::print_header, "Set to 0 to not print header");
137 
138 
139  app.add_option("--output", FleetArgs::output_path, "Where we write output");
140  app.add_option("--input", FleetArgs::input_path, "Read standard input from here");
141  app.add_option("-T,--time", FleetArgs::timestring, "Stop (via CTRL-C) after this much time (takes smhd as seconds/minutes/hour/day units)");
142  app.add_option("--inner-time", FleetArgs::inner_timestring, "Inner time");
143  app.add_option("--tree", FleetArgs::tree_path, "Write the tree here");
144 
145 // app.add_flag( "-q,--quiet", quiet, "Don't print very much and do so on one line");
146 // app.add_flag( "-C,--checkpoint", checkpoint, "Checkpoint every this many steps");
147 
148  start_time = now();
149 
150  }
151 
155  virtual ~Fleet() {
156  if(not done) completed();
157  }
158 
162 
163 
164  static void fleet_interrupt_handler(int signum) {
165  if(signum == SIGINT) {
166  CTRL_C = true;
167  }
168  else if(signum == SIGHUP) {
169  // do nothing -- defaultly Fleet mutes SIGHUP so that commands left in the terminal will continue
170  // this is so that if we get disconnected on a terminal run, the job is maintained
171  }
172  }
173 
174  template<typename T>
175  void add_option(std::string c, T& var, std::string description ) {
176  app.add_option(c, var, description);
177  }
178  template<typename T>
179  void add_flag(std::string c, T& var, std::string description ) {
180  app.add_flag(c, var, description);
181  }
182 
183  int initialize(int argc, char** argv) {
184  CLI11_PARSE(app, argc, argv);
185 
186  // set our own handlers -- defaulty HUP won't stop
187  signal(SIGINT, Fleet::fleet_interrupt_handler);
188  signal(SIGHUP, Fleet::fleet_interrupt_handler);
189 
190  // give us a defaultly kinda nice niceness
191  setpriority(PRIO_PROCESS, 0, 5);
192 
193  // set up the random seed:
194  if(random_seed != 0) {
195  rng.seed(random_seed);
196  }
197 
198  // convert everything to ms
199  FleetArgs::runtime = convert_time(FleetArgs::timestring);
200  FleetArgs::inner_runtime = convert_time(FleetArgs::inner_timestring);
201 
202  if(FleetArgs::print_header) {
203  // Print standard fleet header
204  gethostname(hostname, HOST_NAME_MAX);
205 
206  // and build the command to get the md5 checksum of myself
207  char tmp[64]; sprintf(tmp, "md5sum /proc/%d/exe", getpid());
208 
209  COUT "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ENDL;
210  COUT "# Running Fleet on " << hostname << " with PID=" << getpid() << " by user " << getenv("USER") << " at " << datestring() ENDL;
211  COUT "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ENDL;
212  COUT "# Fleet version: " << FLEET_VERSION ENDL;
213  COUT "# Executable checksum: " << system_exec(tmp);
214  COUT "# Run options: " ENDL;
215  COUT "# \t --input=" << FleetArgs::input_path ENDL;
216  COUT "# \t --threads=" << FleetArgs::nthreads ENDL;
217  COUT "# \t --chains=" << FleetArgs::nchains ENDL;
218  COUT "# \t --steps=" << FleetArgs::steps ENDL;
219  COUT "# \t --inner_steps=" << FleetArgs::inner_steps ENDL;
220  COUT "# \t --thin=" << FleetArgs::thin ENDL;
221  COUT "# \t --print=" << FleetArgs::print ENDL;
222  COUT "# \t --time=" << FleetArgs::timestring << " (" << FleetArgs::runtime << " ms)" ENDL;
223  COUT "# \t --restart=" << FleetArgs::restart ENDL;
224  COUT "# \t --seed=" << random_seed ENDL;
225  COUT "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ENDL;
226 
227  }
228 
229  // give warning for infinite runs:
230  if(FleetArgs::steps == 0 and FleetArgs::runtime==0) {
231  CERR "# Warning: you haven not specified --time or --mcmc so this will run forever or until CTRL-C." ENDL;
232  }
233 
234  return 0;
235  }
236 
237 
238  void completed() {
239 
240  if(FleetArgs::print_header) {
241  auto elapsed_seconds = time_since(start_time) / 1000.0;
242 
243  COUT "# Elapsed time:" TAB elapsed_seconds << " seconds " ENDL;
245  COUT "# Samples per second:" TAB FleetStatistics::global_sample_count/elapsed_seconds ENDL;
246  COUT "# Global sample count:" TAB FleetStatistics::global_sample_count ENDL;
247  }
249  COUT "# Total Beam Search steps:" TAB FleetStatistics::beam_steps ENDL;
250  }
252  COUT "# Total enumeration steps:" TAB FleetStatistics::enumeration_steps ENDL;
253  }
254 
255 
256  COUT "# Total posterior calls:" TAB FleetStatistics::posterior_calls ENDL;
257  COUT "# VM ops per second:" TAB FleetStatistics::vm_ops/elapsed_seconds ENDL;
258  }
259 
260  // setting this makes sure we won't call it again
261  done = true;
262 
263  // HMM CANT DO THIS BC THERE MAY BE MORE THAN ONE TREE....
264  //COUT "# MCTS tree size:" TAB m.size() ENDL;
265  //COUT "# Max score: " TAB maxscore ENDL;
266  //COUT "# MCTS steps per second:" TAB m.statistics.N ENDL;
267  }
268 
269 };
270 
CLI::App app
Definition: Fleet.h:118
char hostname[HOST_NAME_MAX]
Definition: Fleet.h:105
std::atomic< uintmax_t > global_sample_count(0)
Fleet(std::string brief)
Definition: Fleet.h:122
virtual ~Fleet()
On destructor, we call completed unless we are already done.
Definition: Fleet.h:155
timept start_time
Definition: Fleet.h:119
#define TAB
Definition: IO.h:18
std::string system_exec(const char *cmd)
Definition: Miscellaneous.h:73
time_t convert_time(std::string &s)
Give a timepoint for the current time.
Definition: Timing.h:79
RuntimeCounter runtime(HYP &h, typename HYP::datum_t &di)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
Definition: RuntimeCounter.h:108
const std::string FLEET_VERSION
Definition: Fleet.h:74
std::atomic< uintmax_t > enumeration_steps(0)
std::atomic< uintmax_t > posterior_calls(0)
std::atomic< uintmax_t > vm_ops(0)
#define CERR
Definition: IO.h:20
std::atomic< uintmax_t > beam_steps(0)
thread_local std::mt19937 rng(rd())
#define ENDL
Definition: IO.h:19
bool done
Definition: Fleet.h:120
const size_t HOST_NAME_MAX
Definition: Fleet.h:103
unsigned long random_seed
Definition: Fleet.h:94
#define COUT
Definition: IO.h:21
volatile sig_atomic_t CTRL_C
Definition: Fleet.h:99
Definition: Fleet.h:116
This class has all the information for running MCMC or MCTS in a little package. It defaultly constru...