Vowpal Wabbit
main.cc
Go to the documentation of this file.
1 
2 /*
3 Copyright (c) by respective owners including Yahoo!, Microsoft, and
4 individual contributors. All rights reserved. Released under a BSD
5 license as described in the file LICENSE.
6  */
7 #ifdef _WIN32
8 #define NOMINMAX
9 #include <WinSock2.h>
10 #else
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
13 #endif
14 #include <sys/timeb.h>
15 #include "parse_args.h"
16 #include "parse_regressor.h"
17 #include "accumulate.h"
18 #include "best_constant.h"
19 #include "vw_exception.h"
20 #include <fstream>
21 
22 #include "options.h"
23 #include "options_boost_po.h"
24 
25 using namespace VW::config;
26 
27 vw* setup(options_i& options)
28 {
29  vw* all = nullptr;
30  try
31  {
32  all = VW::initialize(options);
33  }
34  catch (const std::exception& ex)
35  {
36  std::cout << ex.what() << std::endl;
37  throw;
38  }
39  catch (...)
40  {
41  std::cout << "unknown exception" << std::endl;
42  throw;
43  }
44  all->vw_is_main = true;
45 
46  if (!all->quiet && !all->bfgs && !all->searchstr && !options.was_supplied("audit_regressor"))
47  {
48  all->trace_message << std::left << std::setw(shared_data::col_avg_loss) << std::left << "average"
49  << " " << std::setw(shared_data::col_since_last) << std::left << "since"
50  << " " << std::right << std::setw(shared_data::col_example_counter) << "example"
51  << " " << std::setw(shared_data::col_example_weight) << "example"
52  << " " << std::setw(shared_data::col_current_label) << "current"
53  << " " << std::setw(shared_data::col_current_predict) << "current"
54  << " " << std::setw(shared_data::col_current_features) << "current" << std::endl;
55  all->trace_message << std::left << std::setw(shared_data::col_avg_loss) << std::left << "loss"
56  << " " << std::setw(shared_data::col_since_last) << std::left << "last"
57  << " " << std::right << std::setw(shared_data::col_example_counter) << "counter"
58  << " " << std::setw(shared_data::col_example_weight) << "weight"
59  << " " << std::setw(shared_data::col_current_label) << "label"
60  << " " << std::setw(shared_data::col_current_predict) << "predict"
61  << " " << std::setw(shared_data::col_current_features) << "features" << std::endl;
62  }
63 
64  return all;
65 }
66 
67 int main(int argc, char* argv[])
68 {
69  bool should_use_onethread = false;
70  option_group_definition driver_config("driver");
71  driver_config.add(make_option("onethread", should_use_onethread).help("Disable parse thread"));
72 
73  try
74  {
75  // support multiple vw instances for training of the same datafile for the same instance
76  std::vector<std::unique_ptr<options_boost_po>> arguments;
77  std::vector<vw*> alls;
78  if (argc == 3 && !strcmp(argv[1], "--args"))
79  {
80  std::fstream arg_file(argv[2]);
81 
82  int line_count = 1;
83  std::string line;
84  while (std::getline(arg_file, line))
85  {
86  std::stringstream sstr;
87  sstr << line << " -f model." << (line_count++);
88  sstr << " --no_stdin"; // can't use stdin with multiple models
89 
90  std::cout << sstr.str() << std::endl;
91  std::string str = sstr.str();
92  const char* new_args = str.c_str();
93 
94  int l_argc;
95  char** l_argv = VW::get_argv_from_string(new_args, l_argc);
96 
97  std::unique_ptr<options_boost_po> ptr(new options_boost_po(l_argc, l_argv));
98  ptr->add_and_parse(driver_config);
99  alls.push_back(setup(*ptr));
100  arguments.push_back(std::move(ptr));
101  }
102  }
103  else
104  {
105  std::unique_ptr<options_boost_po> ptr(new options_boost_po(argc, argv));
106  ptr->add_and_parse(driver_config);
107  alls.push_back(setup(*ptr));
108  arguments.push_back(std::move(ptr));
109  }
110 
111  vw& all = *alls[0];
112 
113  // struct timeb t_start, t_end;
114  // ftime(&t_start);
115 
116  if (should_use_onethread)
117  {
118  if (alls.size() == 1)
120  else
121  THROW("--onethread doesn't make sense with multiple learners");
122  }
123  else
124  {
125  VW::start_parser(all);
126  if (alls.size() == 1)
128  else
130  VW::end_parser(all);
131  }
132 
133  for (vw* v : alls)
134  {
135  if (v->p->exc_ptr)
136  {
137  std::rethrow_exception(v->p->exc_ptr);
138  }
139 
140  VW::sync_stats(*v);
141  VW::finish(*v);
142  }
143  }
144  catch (VW::vw_exception& e)
145  {
146  std::cerr << "vw (" << e.Filename() << ":" << e.LineNumber() << "): " << e.what() << std::endl;
147  exit(1);
148  }
149  catch (std::exception& e)
150  {
151  // vw is implemented as a library, so we use 'throw runtime_error()'
152  // error 'handling' everywhere. To reduce stderr pollution
153  // everything gets caught here & the error message is printed
154  // sans the excess exception noise, and core dump.
155  std::cerr << "vw: " << e.what() << std::endl;
156  // cin.ignore();
157  exit(1);
158  }
159  // cin.ignore();
160  return 0;
161 }
static constexpr int col_current_features
Definition: global_data.h:186
void * searchstr
Definition: global_data.h:430
vw * setup(options_i &options)
Definition: main.cc:27
void generic_driver(ready_examples_queue &examples, context_type &context)
Definition: learner.cc:253
static constexpr int col_current_label
Definition: global_data.h:182
static constexpr int col_avg_loss
Definition: global_data.h:175
static constexpr int col_since_last
Definition: global_data.h:177
const char * Filename() const
Definition: vw_exception.cc:37
char ** get_argv_from_string(std::string s, int &argc)
Definition: parse_args.cc:1646
bool quiet
Definition: global_data.h:487
void finish(vw &all, bool delete_all)
Definition: parse_args.cc:1823
const char * what() const noexcept override
Definition: vw_exception.cc:35
static constexpr int col_example_weight
Definition: global_data.h:180
void start_parser(vw &all)
Definition: parser.cc:974
bool vw_is_main
Definition: global_data.h:421
vw_ostream trace_message
Definition: global_data.h:424
bool bfgs
Definition: global_data.h:412
virtual bool was_supplied(const std::string &key)=0
void end_parser(vw &all)
Definition: parser.cc:1007
static constexpr int col_example_counter
Definition: global_data.h:179
vw * initialize(options_i &options, io_buf *model, bool skipModelLoad, trace_message_t trace_listener, void *trace_context)
Definition: parse_args.cc:1654
int main(int argc, char *argv[])
Definition: main.cc:67
void generic_driver_onethread(vw &all)
Definition: learner.cc:285
option_group_definition & add(T &&op)
Definition: options.h:90
typed_option< T > make_option(std::string name, T &location)
Definition: options.h:80
int LineNumber() const
Definition: vw_exception.cc:39
static constexpr int col_current_predict
Definition: global_data.h:184
#define THROW(args)
Definition: vw_exception.h:181
void sync_stats(vw &all)
Definition: parse_args.cc:1804