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