XGC1
command_line_inputs.hpp
Go to the documentation of this file.
1 #ifndef COMMAND_LINE_INPUTS_HPP
2 #define COMMAND_LINE_INPUTS_HPP
3 
4 #include <stdexcept>
5 #include <functional>
6 #include <vector>
7 #include <string>
8 
9 template<typename T>
10 static T read_arg_after(int argc, char* argv[], int i, int* ierr);
11 
12 template<>
13 int read_arg_after(int argc, char* argv[], int i, int* ierr){
14  *ierr = 0;
15  if (i + 1 < argc) { // Make sure we aren't at the end of argv!
16  std::string arg = argv[i+1];
17  try {
18  std::size_t pos;
19  int x = std::stoi(arg, &pos);
20  if (pos >= arg.size()) {
21  return x;
22  }
23  } catch(...){
24  //
25  }
26  }
27  *ierr = 1;
28  return -1;
29 }
30 
32  public:
37  bool do_dryrun;
41  bool debug;
42 
43  private:
44  struct Option{
45  std::string name;
46  std::string explanation;
47  Option(std::string name, std::string explanation) : name(name), explanation(explanation) {}
48  };
49  std::vector<Option> options;
50 
52  bool found;
53 
54  bool check(std::string input, std::string name, std::string explanation){
55  options.push_back(Option(name, explanation));
56  if (input==name){
57  found=true;
58  return true;
59  }else{
60  return false;
61  }
62  };
63 
64  void print_options(){
65  std::string msg = "Available options are:\n";
66  for(int j = 0; j < options.size(); j++)
67  msg += "\t" + options[j].name + "\t" + options[j].explanation + "\n";
68  printf("%s", msg.c_str());
69  }
70 
71  void error(std::string msg){
72  printf("ERROR: %s\n", msg.c_str());
73  print_options();
74  exit(1);
75  }
76 
77  public:
78 
79  CommandLineInputs(int argc, char* argv[])
86  max_walltime(-1),
87  debug(false)
88  {
89  int ierr;
90  first_input = true;
91  // Loop through inputs
92  for (int i = 1; i < argc; ++i) {
93  std::string input = std::string(argv[i]);
94  found = false;
95 
96  if(check(input,"--test","Checks particles against reference data")){
97  run_particle_test=true;
98  }
99  if(check(input,"--update-test","Updates reference data used in particle test")){
101  }
102  if(check(input,"--moments-test","Checks moments against reference data")){
103  run_moments_test=true;
104  }
105  if(check(input,"--update-moments-test","Updates reference data used in moments test")){
106  update_moments_test=true;
107  }
108  if(check(input,"--debug","Activates extra barriered checkpoints and validity checks (not intended for production)")){
109  debug=true;
110  }
111  if(check(input,"--dryrun","Partially runs XGC setup to check for some common input errors")){
112  do_dryrun=true;
113  dryrun_n_mpi_ranks = -1; // default
114  }
115  if(check(input,"-n_ranks","Optional input if using --dryrun: Specify the number of MPI ranks you plan to use")){
116  if(!do_dryrun){
117  error("-n_ranks can only be used with --dryrun");
118  }
119  dryrun_n_mpi_ranks = read_arg_after<int>(argc, argv, i, &ierr);
120  if(ierr!=0) error("Expected integer after -n_ranks");
121  i++;
122  }
123  if(check(input,"-max_walltime","Specifies a maximum walltime in minutes after which XGC will write a checkpoint and end the simulation")){
124  max_walltime = read_arg_after<int>(argc, argv, i, &ierr);
125  if(ierr!=0) error("Expected integer after -max_walltime");
126  i++;
127  }
128  if(check(input,"--perf-barriers","Adds MPI barriers to top-level timers to help identify load imbalance")){
129  perf_barriers=true;
130  }
131  if(!found){
132  error("Unknown command line argument " + input);
133  }
134  first_input = false;
135  }
136  }
137 };
138 
139 #endif
static T read_arg_after(int argc, char *argv[], int i, int *ierr)
Definition: command_line_inputs.hpp:13
Option
Definition: streamed_parallel_for.hpp:13
logical false
Definition: module.F90:102
Definition: command_line_inputs.hpp:44
std::string name
Definition: command_line_inputs.hpp:45
std::string explanation
Definition: command_line_inputs.hpp:46
Option(std::string name, std::string explanation)
Definition: command_line_inputs.hpp:47
Definition: command_line_inputs.hpp:31
void print_options()
Definition: command_line_inputs.hpp:64
bool debug
Definition: command_line_inputs.hpp:41
bool update_moments_test
Definition: command_line_inputs.hpp:36
CommandLineInputs(int argc, char *argv[])
Definition: command_line_inputs.hpp:79
std::vector< Option > options
Definition: command_line_inputs.hpp:49
int max_walltime
Definition: command_line_inputs.hpp:40
bool run_particle_test
Definition: command_line_inputs.hpp:33
bool found
Definition: command_line_inputs.hpp:52
bool do_dryrun
Definition: command_line_inputs.hpp:37
bool first_input
Definition: command_line_inputs.hpp:51
bool check(std::string input, std::string name, std::string explanation)
Definition: command_line_inputs.hpp:54
void error(std::string msg)
Definition: command_line_inputs.hpp:71
int dryrun_n_mpi_ranks
Definition: command_line_inputs.hpp:38
bool perf_barriers
Definition: command_line_inputs.hpp:39
bool run_moments_test
Definition: command_line_inputs.hpp:35
bool update_particle_test
Definition: command_line_inputs.hpp:34