Fleet  0.0.9
Inference in the LOT
Fleet.h
Go to the documentation of this file.
1 
361 #pragma once
362 
363 #include <sys/resource.h> // just for setting priority defaulty
364 #include <unistd.h>
365 #include <stdlib.h>
366 #include <signal.h>
367 #include <string>
368 
369 const std::string FLEET_VERSION = "0.1.09";
370 
371 #include "Random.h"
372 #include "Timing.h"
373 #include "Control.h"
374 
378 
379 #include "FleetArgs.h"
380 
381 unsigned long random_seed = 0;
382 
383 // This is global that checks whether CTRL_C has been pressed
384 // NOTE: this must be registered in main with signal(SIGINT, Fleet::fleet_interrupt_handler);
385 // which happens in
386 volatile sig_atomic_t CTRL_C = false;
387 
388 // apparently some OSes don't define this
389 #ifndef HOST_NAME_MAX
390  const size_t HOST_NAME_MAX = 256;
391 #endif
392 char hostname[HOST_NAME_MAX];
393 
394 // store the main thread id if we want it
395 std::thread::id main_thread_id = std::this_thread::get_id();
396 
397 #include "FleetStatistics.h"
398 
408 class Fleet {
409 public:
410  CLI::App app;
411  timept start_time;
412  bool done; // if this is true, we don't call completed on destruction
413 
414  Fleet(std::string brief) : app{brief}, done(false) {
415 
416  app.add_option("--seed", random_seed, "Seed the rng (0 is no seed)");
417  app.add_option("--steps", FleetArgs::steps, "Number of MCMC or MCTS search steps to run");
418  app.add_option("--inner-steps", FleetArgs::inner_steps, "Steps to run inner loop of a search");
419  app.add_option("--burn", FleetArgs::burn, "Burn this many steps");
420  app.add_option("--thin", FleetArgs::thin, "Thinning on callbacks");
421  app.add_option("--print", FleetArgs::print, "Print out every this many");
422  app.add_option("--top", FleetArgs::ntop, "The number to store");
423  app.add_option("-n,--threads", FleetArgs::nthreads, "Number of threads for parallel search");
424  app.add_option("--explore", FleetArgs::explore, "Exploration parameter for MCTS");
425  app.add_option("--restart", FleetArgs::restart, "If we don't improve after this many, restart a chain");
426  app.add_option("-c,--chains", FleetArgs::nchains, "How many chains to run");
427  app.add_option("--partition-depth", FleetArgs::partition_depth, "How deep do we recurse when we do partition-mcmc?");
428 
429  app.add_option("--header", FleetArgs::print_header, "Set to 0 to not print header");
430 
431  app.add_option("--top-print-best", FleetArgs::top_print_best, "Should all tops defaultly print their best?");
432 
433  app.add_option("--output", FleetArgs::output_path, "Where we write output");
434  app.add_option("--input", FleetArgs::input_path, "Read standard input from here");
435  app.add_option("-T,--time", FleetArgs::timestring, "Stop (via CTRL-C) after this much time (takes smhd as seconds/minutes/hour/day units)");
436  app.add_option("--inner-restart", FleetArgs::inner_restart, "Inner restart");
437  app.add_option("--inner-time", FleetArgs::inner_timestring, "Inner time");
438  app.add_option("--inner-thin", FleetArgs::inner_thin, "Inner thinning");
439  app.add_option("--tree", FleetArgs::tree_path, "Write the tree here");
440 
441 // app.add_flag( "-q,--quiet", quiet, "Don't print very much and do so on one line");
442 // app.add_flag( "-C,--checkpoint", checkpoint, "Checkpoint every this many steps");
443 
444  start_time = now();
445 
446  // now if we are running MPI
447 // #ifdef FLEET_MPI
448 
449 // #endif
450 
451  }
452 
456  virtual ~Fleet() {
457  if(not done) completed();
458  }
459 
463 
464 
465  static void fleet_interrupt_handler(int signum) {
466  if(signum == SIGINT) {
467  CTRL_C = true;
468  }
469  else if(signum == SIGHUP) {
470  // do nothing -- defaultly Fleet mutes SIGHUP so that commands left in the terminal will continue
471  // this is so that if we get disconnected on a terminal run, the job is maintained
472  }
473  }
474 
475 
476  template<typename T>
477  void add_option(std::string c, T& var, std::string description ) {
478  app.add_option(c, var, description);
479  }
480  template<typename T>
481  void add_flag(std::string c, T& var, std::string description ) {
482  app.add_flag(c, var, description);
483  }
484 
485  int initialize(int argc, char** argv) {
486 
487  //CLI11_PARSE(app, argc, argv);
488  app.parse(argc, argv); // parse here -- and DO NOT catch exit codes (unlike the macro above)
489  // if we catch exit codes, we silently keep running without parsing...
490 
491  // set our own handlers -- defaulty HUP won't stop
492  signal(SIGINT, Fleet::fleet_interrupt_handler);
493  signal(SIGHUP, Fleet::fleet_interrupt_handler);
494 
495  // give us a defaultly kinda nice niceness
496  setpriority(PRIO_PROCESS, 0, 5);
497 
498  // convert everything to ms
501 
502  // seed the rng
503  rng.seed(random_seed);
504 
506  // Print standard fleet header
507  gethostname(hostname, HOST_NAME_MAX);
508 
509  // and build the command to get the md5 checksum of myself
510  char tmp[64]; sprintf(tmp, "md5sum /proc/%d/exe", getpid());
511 
512  COUT "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ENDL;
513  COUT "# Running Fleet on " << hostname << " with PID=" << getpid() << " by user " << getenv("USER") << " at " << datestring() ENDL;
514  COUT "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ENDL;
515  COUT "# Fleet version: " << FLEET_VERSION ENDL;
516  COUT "# Executable checksum: " << system_exec(tmp);
517  COUT "# Run options: " ENDL;
518  COUT "# \t --input=" << FleetArgs::input_path ENDL;
519  COUT "# \t --threads=" << FleetArgs::nthreads ENDL;
520  COUT "# \t --chains=" << FleetArgs::nchains ENDL;
521  COUT "# \t --steps=" << FleetArgs::steps ENDL;
522  COUT "# \t --inner_steps=" << FleetArgs::inner_steps ENDL;
523  COUT "# \t --thin=" << FleetArgs::thin ENDL;
524  COUT "# \t --print=" << FleetArgs::print ENDL;
525  COUT "# \t --time=" << FleetArgs::timestring << " (" << FleetArgs::runtime << " ms)" ENDL;
526  COUT "# \t --restart=" << FleetArgs::restart ENDL;
527  COUT "# \t --seed=" << random_seed ENDL;
528  COUT "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ENDL;
529 
530  }
531 
532  // give warning for infinite runs:
533  if(FleetArgs::steps == 0 and FleetArgs::runtime==0) {
534  CERR "# Warning: you haven not specified --time or --steps so this will run forever or until CTRL-C." ENDL;
535  }
536 
537  return 0;
538  }
539 
540 
541  void completed() {
542 
544  auto elapsed_seconds = time_since(start_time) / 1000.0;
545 
546  COUT "# Elapsed time:" TAB elapsed_seconds << " seconds " ENDL;
548  COUT "# Samples per second:" TAB FleetStatistics::global_sample_count/elapsed_seconds ENDL;
549  COUT "# Global sample count:" TAB FleetStatistics::global_sample_count ENDL;
550  }
552  COUT "# Total Beam Search steps:" TAB FleetStatistics::beam_steps ENDL;
553  }
555  COUT "# Total enumeration steps:" TAB FleetStatistics::enumeration_steps ENDL;
556  }
558  COUT "# Warning: " TAB FleetStatistics::depth_exceptions TAB " grammar depth exceptions." ENDL;
559  }
560 
561  COUT "# Total posterior calls:" TAB FleetStatistics::posterior_calls ENDL;
562  COUT "# VM ops per second:" TAB FleetStatistics::vm_ops/elapsed_seconds ENDL;
563 
564  COUT "# Fleet completed at " << datestring() ENDL;
565 
566  }
567 
568  // setting this makes sure we won't call it again
569  done = true;
570 
571  // HMM CANT DO THIS BC THERE MAY BE MORE THAN ONE TREE....
572  //COUT "# MCTS tree size:" TAB m.size() ENDL;
573  //COUT "# Max score: " TAB maxscore ENDL;
574  //COUT "# MCTS steps per second:" TAB m.statistics.N ENDL;
575  }
576 
577 };
578 
unsigned long burn
Definition: FleetArgs.h:14
unsigned long inner_runtime
Definition: FleetArgs.h:25
unsigned long print
Definition: FleetArgs.h:33
std::atomic< uintmax_t > global_sample_count(0)
std::string tree_path
Definition: FleetArgs.h:38
unsigned long inner_thin
Definition: FleetArgs.h:27
#define TAB
Definition: IO.h:19
std::atomic< uintmax_t > depth_exceptions(0)
Rng rng
double explore
Definition: FleetArgs.h:19
std::string system_exec(const char *cmd)
Definition: Miscellaneous.h:11
const std::string FLEET_VERSION
Definition: Fleet.h:369
unsigned long ntop
Definition: FleetArgs.h:15
std::atomic< uintmax_t > enumeration_steps(0)
std::string inner_timestring
Definition: FleetArgs.h:29
std::string timestring
Definition: FleetArgs.h:28
volatile sig_atomic_t CTRL_C
size_t partition_depth
Definition: FleetArgs.h:22
int print_header
Definition: FleetArgs.h:17
std::atomic< uintmax_t > posterior_calls(0)
std::atomic< uintmax_t > vm_ops(0)
unsigned long inner_restart
Definition: FleetArgs.h:26
#define CERR
Definition: IO.h:23
std::atomic< uintmax_t > beam_steps(0)
size_t nchains
Definition: FleetArgs.h:21
#define ENDL
Definition: IO.h:21
void seed(unsigned long s)
Seed only if s is nonzero.
Definition: Rng.h:56
This is a thread_local rng whose first object is used to see others (in other threads). This way, we can have thread_local rngs that all are seeded deterministcally in Fleet via –seed=X.
std::string output_path
Definition: FleetArgs.h:39
std::string input_path
Definition: FleetArgs.h:37
unsigned long inner_steps
Definition: FleetArgs.h:13
size_t nthreads
Definition: FleetArgs.h:20
unsigned long top_print_best
Definition: FleetArgs.h:35
unsigned long runtime
Definition: FleetArgs.h:24
#define COUT
Definition: IO.h:24
unsigned long steps
Definition: FleetArgs.h:12
time_t convert_time(const std::string &s)
Give a timepoint for the current time.
Definition: Timing.h:79
unsigned long restart
Definition: FleetArgs.h:31
This class has all the information for running MCMC or MCTS in a little package. It defaultly constru...
unsigned long thin
Definition: FleetArgs.h:32