PFASST++
config.hpp
Go to the documentation of this file.
1 
5 #ifndef _PFASST__CONFIG_HPP_
6 #define _PFASST__CONFIG_HPP_
7 
8 #include <fstream>
9 #include <iostream>
10 #include <string>
11 #include <map>
12 using namespace std;
13 
14 #include <boost/program_options.hpp>
15 namespace po = boost::program_options;
16 
17 #ifdef WITH_MPI
18  #include <mpi.h>
19 #endif
20 
21 
22 namespace pfasst
23 {
24  static constexpr const char* VERSION = "v0.5.0-rc1-48-g4da93b0-dirty";
25 
29  namespace config
30  {
40  int get_rank()
41  {
42 #ifdef WITH_MPI
43  int initialized = 0, rank = 0;
44  // if we're not running under "mpirun/mpiexec", just assume rank 0.
45  MPI_Initialized(&initialized);
46  if (initialized) {
47  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
48  }
49  return rank;
50 #else
51  return 0;
52 #endif
53  }
54 
65  class options
66  {
67  public:
69  static const size_t LINE_WIDTH = 100;
70 
71  private:
72  po::options_description all_options;
73  map<string, po::options_description> option_groups;
74  po::variables_map variables_map;
75  vector<string> unrecognized_args;
76  bool initialized = false;
77 
79  options();
80  options(const options&) = delete;
81  void operator=(const options&) = delete;
83 
84  public:
86 
91  static options& get_instance();
92  po::variables_map& get_variables_map();
93  po::options_description& get_all_options();
94  vector<string>& get_unrecognized_args();
96 
98 
109  static void add_option(const string& group, const string& option, const string& help);
110 
125  template<typename T>
126  static void add_option(const string& group, const string& option, const string& help);
128 
134  void init();
135  };
136 
149  template<typename T>
150  inline T get_value(const string& name)
151  {
152  return options::get_instance().get_variables_map()[name].as<T>();
153  }
154 
162  template<typename T>
163  inline T get_value(const string& name, const T& default_val)
164  {
165  return options::get_instance().get_variables_map().count(name)
166  ? options::get_instance().get_variables_map()[name].as<T>() : default_val;
167  }
168 
181  static string print_help(bool if_no_params = false)
182  {
183  bool no_params_given = options::get_instance().get_variables_map().empty();
184 
185  if (!if_no_params || (if_no_params && no_params_given)) {
186  stringstream s;
187  s << options::get_instance().get_all_options() << endl;
188  s << "Logging options:" << endl
189  << " -v [ --verbose ] activates maximum verbosity" << endl
190  << " --v=arg activates verbosity upto verbose level `arg`" << endl
191  << " (valid range: 0-9)" << endl
192  << " -vmodule=arg actives verbose logging for specific module" << endl
193  << " (see [1] for details)" << endl << endl
194  << "[1]: https://github.com/easylogging/easyloggingpp#vmodule" << endl;
195  return s.str();
196  } else {
197  return string();
198  }
199  }
200 
230  static inline void read_config_file(const string& file_name)
231  {
232  ifstream ifs(file_name.c_str(), ios_base::in);
233  if (!ifs) {
234  throw invalid_argument("Config file '" + file_name + "' not found.");
235  } else {
236  po::store(po::parse_config_file(ifs, options::get_instance().get_all_options()),
237  options::get_instance().get_variables_map());
238  po::notify(options::get_instance().get_variables_map());
239  }
240  }
241 
253  static inline void read_commandline(int argc, char* argv[], bool exit_on_help = true)
254  {
255  po::parsed_options parsed = po::command_line_parser(argc, argv)
256  .options(options::get_instance().get_all_options())
257  .allow_unregistered().run();
258  options::get_instance().get_unrecognized_args() = po::collect_unrecognized(parsed.options,
259  po::exclude_positional);
260  po::store(parsed, options::get_instance().get_variables_map());
261  po::notify(options::get_instance().get_variables_map());
262 
263  if (options::get_instance().get_variables_map().count("input")) {
264  string input_file = config::get_value<string>("input");
265  read_config_file(input_file);
266  }
267 
268  if (options::get_instance().get_variables_map().count("help")) {
269  if (get_rank() == 0) {
270  cout << print_help() << endl;
271  }
272 
273  if (exit_on_help) {
274 #ifdef WITH_MPI
275  MPI_Finalize();
276 #endif
277  exit(0);
278  }
279  }
280  }
281 
300  static inline void init()
301  {
302  options::add_option ("Global", "help,h", "display this help message");
303  options::add_option ("Global", "quiet,q", "don't log to stdout");
304  options::add_option<string>("Global", "input", "INI file with configuration options");
305  options::add_option<string>("Global", "log_prefix", "a prefix for the log files");
306  options::add_option ("Global", "nocolor,c", "disable colorful logging");
307 
308  options::add_option<double>("Duration", "dt", "time step size");
309  options::add_option<double>("Duration", "tend", "final time of simulation");
310  options::add_option<size_t>("Duration", "num_steps", "number time steps");
311  options::add_option<size_t>("Duration", "num_iters", "number of iterations");
312 
313  options::add_option<size_t>("Quadrature", "num_nodes", "number of quadrature nodes");
314 
315  options::add_option<double>("Tolerances", "abs_res_tol", "absolute residual tolerance");
316  options::add_option<double>("Tolerances", "rel_res_tol", "relative residual tolerance");
317 
318  options::get_instance().init();
319  }
320  } // ::pfasst::config
321 } // ::pfasst
322 
323 #include "pfasst/config_impl.hpp"
324 
325 #endif // _PFASST__CONFIG_HPP_
po::options_description all_options
Definition: config.hpp:72
int get_rank()
Get MPI rank during initialization.
Definition: config.hpp:40
static void read_config_file(const string &file_name)
Read config parameters from file.
Definition: config.hpp:230
STL namespace.
vector< string > unrecognized_args
Definition: config.hpp:75
Runtime config options provider.
Definition: config.hpp:65
po::variables_map variables_map
Definition: config.hpp:74
static void read_commandline(int argc, char *argv[], bool exit_on_help=true)
Read and parse command line parameters.
Definition: config.hpp:253
static bool initialized
internal flag identifying whether the default logger has been initialized.
Definition: logging.hpp:247
T get_value(const string &name, const T &default_val)
Get value of specific type T with default value.
Definition: config.hpp:163
static void init()
Initialize options detection and parsing.
Definition: config.hpp:300
static constexpr const char * VERSION
PFASST++ version: closest release tag and a brief git hash.
Definition: config.hpp:24
static string print_help(bool if_no_params=false)
Compile basic help and usage information.
Definition: config.hpp:181
map< string, po::options_description > option_groups
Definition: config.hpp:73