GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
command_line_options.cpp
1 /*
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 #include <graphlab/options/command_line_options.hpp>
23 #include <graphlab/scheduler/scheduler_list.hpp>
24 
25 
26 namespace boost {
27 
28  template<>
29  std::string lexical_cast< std::string>(const std::vector<int>& vec) {
30  return graphlab_vec_to_string(vec);
31  }
32 
33  template<>
34  std::string lexical_cast<std::string>(const std::vector<uint32_t>& vec) {
35  return graphlab_vec_to_string(vec);
36  }
37 
38  template<>
39  std::string lexical_cast<std::string>(const std::vector<uint64_t>& vec) {
40  return graphlab_vec_to_string(vec);
41  }
42 
43  template<>
44  std::string lexical_cast< std::string >(const std::vector<double>& vec) {
45  return graphlab_vec_to_string(vec);
46  }
47 
48  template<>
49  std::string lexical_cast< std::string>(const std::vector<float>& vec) {
50  return graphlab_vec_to_string(vec);
51  }
52 
53  template<>
54  std::string lexical_cast< std::string>(const std::vector<std::string>& vec) {
55  return graphlab_vec_to_string(vec);
56  }
57 };
58 
59 
60 namespace graphlab {
61 
62 static const char* engine_help_string =
63 #include <graphlab/options/engine_help.txt>
64 ;
65 
66 static const char* graph_help_string =
67 #include <graphlab/options/graph_help.txt>
68 ;
69 
70 
71 
72  bool command_line_options::parse(int argc, const char* const* argv,
73  bool allow_unregistered) {
74  namespace boost_po = boost::program_options;
75 
76  size_t ncpus(get_ncpus());
77  std::string engine_opts_string;
78  std::string schedulertype(get_scheduler_type());
79  std::string scheduler_opts_string = "";
80  std::string graph_opts_string = "";
81 
82  if(!suppress_graphlab_options) {
83  // Set the program options
84  desc.add_options()
85  ("ncpus",
86  boost_po::value<size_t>(&(ncpus))->
87  default_value(ncpus),
88  "Number of cpus to use per machine. Defaults to (#cores - 2)")
89  ("scheduler",
90  boost_po::value<std::string>(&(schedulertype))->
91  default_value(schedulertype),
92  (std::string("Supported schedulers are: "
94  ". Too see options for each scheduler, run the program with the option"
95  " ---schedhelp=[scheduler_name]").c_str()))
96  ("engine_opts",
97  boost_po::value<std::string>(&(engine_opts_string))->
98  default_value(engine_opts_string),
99  "string of engine options i.e., \"timeout=100\"")
100  ("graph_opts",
101  boost_po::value<std::string>(&(graph_opts_string))->
102  default_value(graph_opts_string),
103  "String of graph options i.e., \"ingress=random\"")
104  ("scheduler_opts",
105  boost_po::value<std::string>(&(scheduler_opts_string))->
106  default_value(scheduler_opts_string),
107  "String of scheduler options i.e., \"strict=true\"")
108  ("engine_help",
109  boost_po::value<std::string>()->implicit_value(""),
110  "Display help for engine options.")
111  ("graph_help",
112  boost_po::value<std::string>()->implicit_value(""),
113  "Display help for the distributed graph.")
114  ("scheduler_help",
115  boost_po::value<std::string>()->implicit_value(""),
116  "Display help for schedulers.");
117  }
118  // Parse the arguments
119  try {
120  std::vector<std::string> arguments;
121  std::copy(argv + 1, argv + argc + !argc,
122  std::inserter(arguments, arguments.end()));
123 
124  boost_po::command_line_parser parser(arguments);
125  parser.options(desc);
126  if (allow_unregistered) parser.allow_unregistered();
127  if (num_positional) parser.positional(pos_opts);
128  boost_po::parsed_options parsed = parser.run();
129  if (allow_unregistered) {
130  unrecognized_options =
131  boost_po::collect_unrecognized(parsed.options,
132  boost_po::include_positional);
133  } else {
134  unrecognized_options.clear();
135  }
136  boost_po::store(parsed, vm);
137  boost_po::notify(vm);
138  } catch( boost_po::error error) {
139  std::cout << "Invalid syntax:\n"
140  << "\t" << error.what()
141  << "\n\n" << std::endl
142  << "Description:"
143  << std::endl;
145  return false;
146  }
147  if(vm.count("help")) {
149  return false;
150  }
151  if (vm.count("scheduler_help")) {
152  std::string schedname = vm["scheduler_help"].as<std::string>();
153  if (schedname != "") {
154  print_scheduler_info(schedname, std::cout);
155  } else {
156  std::vector<std::string> schednames = get_scheduler_names();
157  for(size_t i = 0;i < schednames.size(); ++i) {
158  print_scheduler_info(schednames[i], std::cout);
159  }
160  }
161  return false;
162  }
163  if (vm.count("engine_help")) {
164  std::cout << engine_help_string;
165  return false;
166  }
167  if (vm.count("graph_help")) {
168  std::cout << graph_help_string;
169  return false;
170  }
171  set_ncpus(ncpus);
172 
173  set_scheduler_type(schedulertype);
174 
175  get_scheduler_args().parse_string(scheduler_opts_string);
176  get_engine_args().parse_string(engine_opts_string);
177  get_graph_args().parse_string(graph_opts_string);
178  return true;
179  } // end of parse
180 
181 
182  bool command_line_options::is_set(const std::string& option) {
183  return vm.count(option);
184  }
185 
186  void command_line_options::add_positional(const std::string& str) {
187  num_positional++;
188  pos_opts.add(str.c_str(), 1);
189  }
190 }
191