GraphChi  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros
cmdopts.hpp
Go to the documentation of this file.
1 
29 #ifndef GRAPHCHI_CMDOPTS_DEF
30 #define GRAPHCHI_CMDOPTS_DEF
31 
32 
33 #include <string>
34 #include <iostream>
35 
36 #include "api/chifilenames.hpp"
37 #include "util/configfile.hpp"
38 
39 namespace graphchi {
40 
43 #ifdef __GNUC__
44 #define VARIABLE_IS_NOT_USED __attribute__ ((unused))
45 #else
46 #define VARIABLE_IS_NOT_USED
47 #endif
48 
49  static bool _cmd_configured = false;
50 
51  static int _argc;
52  static char **_argv;
53  static std::map<std::string, std::string> conf;
54 
55 
56  // Config file
57  static std::string VARIABLE_IS_NOT_USED get_config_option_string(const char *option_name) {
58  if (conf.find(option_name) != conf.end()) {
59  return conf[option_name];
60  } else {
61  std::cout << "ERROR: could not find option " << option_name << " from config.";
62  assert(false);
63  }
64  }
65 
66  static std::string VARIABLE_IS_NOT_USED get_config_option_string(const char *option_name,
67  std::string default_value) {
68  if (conf.find(option_name) != conf.end()) {
69  return conf[option_name];
70  } else {
71  return default_value;
72  }
73 
74  }
75  static int VARIABLE_IS_NOT_USED get_config_option_int(const char *option_name, int default_value) {
76  if (conf.find(option_name) != conf.end()) {
77  return atoi(conf[option_name].c_str());
78  } else {
79  return default_value;
80  }
81  }
82 
83  static uint64_t VARIABLE_IS_NOT_USED get_config_option_long(const char *option_name, uint64_t default_value) {
84  if (conf.find(option_name) != conf.end()) {
85  return atol(conf[option_name].c_str());
86  } else {
87  return default_value;
88  }
89  }
90  static double VARIABLE_IS_NOT_USED get_config_option_double(const char *option_name, double default_value) {
91  if (conf.find(option_name) != conf.end()) {
92  return atof(conf[option_name].c_str());
93  } else {
94  return default_value;
95  }
96  }
97 
98  static void set_argc(int argc, const char ** argv);
99  static void set_argc(int argc, const char ** argv) {
100  _argc = argc;
101  _argv = (char**)argv;
102  _cmd_configured = true;
103  conf = loadconfig(filename_config());
104  }
105 
106  static void graphchi_init(int argc, const char ** argv);
107  static void graphchi_init(int argc, const char ** argv) {
108  set_argc(argc, argv);
109  }
110 
111  static void check_cmd_init() {
112  if (!_cmd_configured) {
113  std::cout << "ERROR: command line options not initialized." << std::endl;
114  std::cout << " You need to call set_argc() in the beginning of the program." << std::endl;
115  }
116  }
117 
118  static std::string VARIABLE_IS_NOT_USED get_option_string(const char *option_name,
119  std::string default_value)
120  {
121  check_cmd_init();
122  int i;
123 
124  for (i = _argc - 2; i >= 0; i -= 2)
125  if (strcmp(_argv[i], option_name) == 0)
126  return std::string(_argv[i + 1]);
127  return get_config_option_string(option_name, default_value);
128  }
129 
130  static std::string VARIABLE_IS_NOT_USED get_option_string(const char *option_name)
131  {
132  int i;
133  check_cmd_init();
134 
135  for (i = _argc - 2; i >= 0; i -= 2)
136  if (strcmp(_argv[i], option_name) == 0)
137  return std::string(_argv[i + 1]);
138 
139  std::cout << "Error: command line argument [" << std::string(option_name) << "] is required!" << std::endl;
140  assert(false);
141  }
142 
143  static std::string VARIABLE_IS_NOT_USED get_option_string_interactive(const char *option_name, std::string options)
144  {
145  int i;
146  check_cmd_init();
147 
148  for (i = _argc - 2; i >= 0; i -= 2)
149  if (strcmp(_argv[i], option_name) == 0)
150  return std::string(_argv[i + 1]);
151 
152  std::cout << "Please enter value for command-line argument [" << std::string(option_name) << "]"<< std::endl;
153  std::cout << " (Options are: " << options << ")" << std::endl;
154 
155  std::string val;
156  std::cin >> val;
157 
158  return val;
159  }
160 
161 
162 
163 
164 
165  static int VARIABLE_IS_NOT_USED get_option_int(const char *option_name, int default_value)
166  {
167  int i;
168  check_cmd_init();
169 
170  for (i = _argc - 2; i >= 0; i -= 2)
171  if (strcmp(_argv[i], option_name) == 0)
172  return atoi(_argv[i + 1]);
173 
174  return get_config_option_int(option_name, default_value);
175  }
176 
177  static int VARIABLE_IS_NOT_USED get_option_int(const char *option_name)
178  {
179  int i;
180  check_cmd_init();
181 
182  for (i = _argc - 2; i >= 0; i -= 2)
183  if (strcmp(_argv[i], option_name) == 0)
184  return atoi(_argv[i + 1]);
185 
186  std::cout << "Error: command line argument [" << std::string(option_name) << "] is required!" << std::endl;
187  assert(false);
188  }
189 
190 
191 
192  static uint64_t VARIABLE_IS_NOT_USED get_option_long(const char *option_name, uint64_t default_value)
193  {
194  int i;
195  check_cmd_init();
196 
197  for (i = _argc - 2; i >= 0; i -= 2)
198  if (strcmp(_argv[i], option_name) == 0)
199  return atol(_argv[i + 1]);
200  return get_config_option_long(option_name, default_value);
201  }
202 
203  static float VARIABLE_IS_NOT_USED get_option_float(const char *option_name, float default_value)
204  {
205  int i;
206  check_cmd_init();
207 
208  for (i = _argc - 2; i >= 0; i -= 2)
209  if (strcmp(_argv[i], option_name) == 0)
210  return (float)atof(_argv[i + 1]);
211  return (float) get_config_option_double(option_name, default_value);
212  }
213 
214 } // End namespace
215 
216 
217 #endif
218 
219