XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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;
40  bool debug;
41 
42  private:
43  struct Option{
44  std::string name;
45  std::string explanation;
46  Option(std::string name, std::string explanation) : name(name), explanation(explanation) {}
47  };
48  std::vector<Option> options;
49 
51  bool found;
52 
53  bool check(std::string input, std::string name, std::string explanation){
54  options.push_back(Option(name, explanation));
55  if (input==name){
56  found=true;
57  return true;
58  }else{
59  return false;
60  }
61  };
62 
63  void print_options(){
64  std::string msg = "Available options are:\n";
65  for(int j = 0; j < options.size(); j++)
66  msg += "\t" + options[j].name + "\t" + options[j].explanation + "\n";
67  printf("%s", msg.c_str());
68  }
69 
70  void error(std::string msg){
71  printf("ERROR: %s\n", msg.c_str());
72  print_options();
73  exit(1);
74  }
75 
76  public:
77 
78  CommandLineInputs(int argc, char* argv[])
79  : run_particle_test(false),
80  update_particle_test(false),
81  run_moments_test(false),
82  update_moments_test(false),
83  do_dryrun(false),
84  perf_barriers(false),
85  debug(false)
86  {
87  int ierr;
88  first_input = true;
89  // Loop through inputs
90  for (int i = 1; i < argc; ++i) {
91  std::string input = std::string(argv[i]);
92  found = false;
93 
94  if(check(input,"--test","Checks particles against reference data")){
95  run_particle_test=true;
96  }
97  if(check(input,"--update-test","Updates reference data used in particle test")){
99  }
100  if(check(input,"--moments-test","Checks moments against reference data")){
101  run_moments_test=true;
102  }
103  if(check(input,"--update-moments-test","Updates reference data used in moments test")){
104  update_moments_test=true;
105  }
106  if(check(input,"--debug","Activates extra barriered checkpoints and validity checks (not intended for production)")){
107  debug=true;
108  }
109  if(check(input,"--dryrun","Partially runs XGC setup to check for some common input errors")){
110  do_dryrun=true;
111  dryrun_n_mpi_ranks = -1; // default
112  }
113  if(check(input,"-n_ranks","Optional input if using --dryrun: Specify the number of MPI ranks you plan to use")){
114  if(!do_dryrun){
115  error("-n_ranks can only be used with --dryrun");
116  }
117  dryrun_n_mpi_ranks = read_arg_after<int>(argc, argv, i, &ierr);
118  if(ierr!=0) error("Expected integer after -n_ranks");
119  i++;
120  }
121  if(check(input,"--perf-barriers","Adds MPI barriers to top-level timers to help identify load imbalance")){
122  perf_barriers=true;
123  }
124  if(!found){
125  error("Unknown command line argument " + input);
126  }
127  first_input = false;
128  }
129  }
130 };
131 
132 #endif
std::string name
Definition: command_line_inputs.hpp:44
static T read_arg_after(int argc, char *argv[], int i, int *ierr)
Definition: command_line_inputs.hpp:13
bool run_particle_test
Definition: command_line_inputs.hpp:33
bool first_input
Definition: command_line_inputs.hpp:50
bool update_particle_test
Definition: command_line_inputs.hpp:34
Option(std::string name, std::string explanation)
Definition: command_line_inputs.hpp:46
Definition: command_line_inputs.hpp:31
void print_options()
Definition: command_line_inputs.hpp:63
bool debug
Definition: command_line_inputs.hpp:40
CommandLineInputs(int argc, char *argv[])
Definition: command_line_inputs.hpp:78
bool update_moments_test
Definition: command_line_inputs.hpp:36
std::string explanation
Definition: command_line_inputs.hpp:45
Option
Definition: streamed_parallel_for.hpp:13
bool check(std::string input, std::string name, std::string explanation)
Definition: command_line_inputs.hpp:53
bool perf_barriers
Definition: command_line_inputs.hpp:39
void error(std::string msg)
Definition: command_line_inputs.hpp:70
bool do_dryrun
Definition: command_line_inputs.hpp:37
bool run_moments_test
Definition: command_line_inputs.hpp:35
std::vector< Option > options
Definition: command_line_inputs.hpp:48
int dryrun_n_mpi_ranks
Definition: command_line_inputs.hpp:38
bool found
Definition: command_line_inputs.hpp:51
Definition: command_line_inputs.hpp:43