Vowpal Wabbit
Classes | Functions
global_data.cc File Reference
#include <stdio.h>
#include <float.h>
#include <errno.h>
#include <iostream>
#include <sstream>
#include <math.h>
#include <assert.h>
#include "global_data.h"
#include "gd.h"
#include "vw_exception.h"

Go to the source code of this file.

Classes

struct  global_prediction
 

Functions

size_t really_read (int sock, void *in, size_t count)
 
void get_prediction (int sock, float &res, float &weight)
 
void send_prediction (int sock, global_prediction p)
 
void binary_print_result (int f, float res, float weight, v_array< char >)
 
int print_tag (std::stringstream &ss, v_array< char > tag)
 
void print_result (int f, float res, float, v_array< char > tag)
 
void print_raw_text (int f, std::string s, v_array< char > tag)
 
void set_mm (shared_data *sd, float label)
 
void noop_mm (shared_data *, float)
 
void compile_gram (std::vector< std::string > grams, std::array< uint32_t, NUM_NAMESPACES > &dest, char *descriptor, bool quiet)
 
void compile_limits (std::vector< std::string > limits, std::array< uint32_t, NUM_NAMESPACES > &dest, bool quiet)
 
void trace_listener_cerr (void *, const std::string &message)
 

Function Documentation

◆ binary_print_result()

void binary_print_result ( int  f,
float  res,
float  weight,
v_array< char >   
)

Definition at line 72 of file global_data.cc.

References send_prediction().

Referenced by enable_sources(), vw::get_random_state(), and reset_source().

73 {
74  if (f >= 0)
75  {
76  global_prediction ps = {res, weight};
77  send_prediction(f, ps);
78  }
79 }
float weight
void send_prediction(int sock, global_prediction p)
Definition: global_data.cc:60
float f
Definition: cache.cc:40

◆ compile_gram()

void compile_gram ( std::vector< std::string >  grams,
std::array< uint32_t, NUM_NAMESPACES > &  dest,
char *  descriptor,
bool  quiet 
)

Definition at line 191 of file global_data.cc.

Referenced by vw::get_random_state(), and parse_feature_tweaks().

193 {
194  for (size_t i = 0; i < grams.size(); i++)
195  {
196  std::string ngram = grams[i];
197  if (isdigit(ngram[0]))
198  {
199  int n = atoi(ngram.c_str());
200  if (!quiet)
201  std::cerr << "Generating " << n << "-" << descriptor << " for all namespaces." << std::endl;
202  for (size_t j = 0; j < 256; j++) dest[j] = n;
203  }
204  else if (ngram.size() == 1)
205  std::cout << "You must specify the namespace index before the n" << std::endl;
206  else
207  {
208  int n = atoi(ngram.c_str() + 1);
209  dest[(uint32_t)(unsigned char)*ngram.c_str()] = n;
210  if (!quiet)
211  std::cerr << "Generating " << n << "-" << descriptor << " for " << ngram[0] << " namespaces." << std::endl;
212  }
213  }
214 }

◆ compile_limits()

void compile_limits ( std::vector< std::string >  limits,
std::array< uint32_t, NUM_NAMESPACES > &  dest,
bool  quiet 
)

Definition at line 216 of file global_data.cc.

Referenced by vw::get_random_state(), and parse_feature_tweaks().

217 {
218  for (size_t i = 0; i < limits.size(); i++)
219  {
220  std::string limit = limits[i];
221  if (isdigit(limit[0]))
222  {
223  int n = atoi(limit.c_str());
224  if (!quiet)
225  std::cerr << "limiting to " << n << "features for each namespace." << std::endl;
226  for (size_t j = 0; j < 256; j++) dest[j] = n;
227  }
228  else if (limit.size() == 1)
229  std::cout << "You must specify the namespace index before the n" << std::endl;
230  else
231  {
232  int n = atoi(limit.c_str() + 1);
233  dest[(uint32_t)limit[0]] = n;
234  if (!quiet)
235  std::cerr << "limiting to " << n << " for namespaces " << limit[0] << std::endl;
236  }
237  }
238 }

◆ get_prediction()

void get_prediction ( int  sock,
float &  res,
float &  weight 
)

Definition at line 52 of file global_data.cc.

References global_prediction::p, really_read(), and global_prediction::weight.

Referenced by vw::get_random_state().

53 {
55  really_read(sock, &p, sizeof(p));
56  res = p.p;
57  weight = p.weight;
58 }
float weight
size_t really_read(int sock, void *in, size_t count)
Definition: global_data.cc:24

◆ noop_mm()

void noop_mm ( shared_data ,
float   
)

◆ print_raw_text()

void print_raw_text ( int  f,
std::string  s,
v_array< char >  tag 
)

Definition at line 111 of file global_data.cc.

References print_tag(), and io_buf::write_file_or_socket().

Referenced by vw::vw().

112 {
113  if (f < 0)
114  return;
115 
116  std::stringstream ss;
117  ss << s;
118  print_tag(ss, tag);
119  ss << '\n';
120  ssize_t len = ss.str().size();
121  ssize_t t = io_buf::write_file_or_socket(f, ss.str().c_str(), (unsigned int)len);
122  if (t != len)
123  {
124  std::cerr << "write error: " << strerror(errno) << std::endl;
125  }
126 }
static ssize_t write_file_or_socket(int f, const void *buf, size_t nbytes)
Definition: io_buf.cc:140
int print_tag(std::stringstream &ss, v_array< char > tag)
Definition: global_data.cc:81
float f
Definition: cache.cc:40

◆ print_result()

void print_result ( int  f,
float  res,
float  ,
v_array< char >  tag 
)

Definition at line 91 of file global_data.cc.

References print_tag(), and io_buf::write_file_or_socket().

Referenced by vw::get_random_state(), and vw::vw().

92 {
93  if (f >= 0)
94  {
95  std::stringstream ss;
96  auto saved_precision = ss.precision();
97  if (floorf(res) == res)
98  ss << std::setprecision(0);
99  ss << std::fixed << res << std::setprecision(saved_precision);
100  print_tag(ss, tag);
101  ss << '\n';
102  ssize_t len = ss.str().size();
103  ssize_t t = io_buf::write_file_or_socket(f, ss.str().c_str(), (unsigned int)len);
104  if (t != len)
105  {
106  std::cerr << "write error: " << strerror(errno) << std::endl;
107  }
108  }
109 }
static ssize_t write_file_or_socket(int f, const void *buf, size_t nbytes)
Definition: io_buf.cc:140
int print_tag(std::stringstream &ss, v_array< char > tag)
Definition: global_data.cc:81
float f
Definition: cache.cc:40

◆ print_tag()

int print_tag ( std::stringstream &  ss,
v_array< char >  tag 
)

Definition at line 81 of file global_data.cc.

References v_array< T >::begin(), v_array< T >::end(), and v_array< T >::size().

Referenced by active_print_result(), confidence_print_result(), vw::get_random_state(), ACTION_SCORE::print_action_score(), print_raw_text(), and print_result().

82 {
83  if (tag.begin() != tag.end())
84  {
85  ss << ' ';
86  ss.write(tag.begin(), sizeof(char) * tag.size());
87  }
88  return tag.begin() != tag.end();
89 }
T *& begin()
Definition: v_array.h:42
size_t size() const
Definition: v_array.h:68
T *& end()
Definition: v_array.h:43

◆ really_read()

size_t really_read ( int  sock,
void *  in,
size_t  count 
)

Definition at line 24 of file global_data.cc.

References THROWERRNO.

Referenced by get_prediction().

25 {
26  char* buf = (char*)in;
27  size_t done = 0;
28  int r = 0;
29  while (done < count)
30  {
31  if ((r =
32 #ifdef _WIN32
33  recv(sock, buf, (unsigned int)(count - done), 0)
34 #else
35  read(sock, buf, (unsigned int)(count - done))
36 #endif
37  ) == 0)
38  return 0;
39  else if (r < 0)
40  {
41  THROWERRNO("read(" << sock << "," << count << "-" << done << ")");
42  }
43  else
44  {
45  done += r;
46  buf += r;
47  }
48  }
49  return done;
50 }
#define THROWERRNO(args)
Definition: vw_exception.h:167

◆ send_prediction()

void send_prediction ( int  sock,
global_prediction  p 
)

Definition at line 60 of file global_data.cc.

References THROWERRNO.

Referenced by binary_print_result().

61 {
62  if (
63 #ifdef _WIN32
64  send(sock, reinterpret_cast<const char*>(&p), sizeof(p), 0)
65 #else
66  write(sock, &p, sizeof(p))
67 #endif
68  < (int)sizeof(p))
69  THROWERRNO("send_prediction write(" << sock << ")");
70 }
#define THROWERRNO(args)
Definition: vw_exception.h:167

◆ set_mm()

void set_mm ( shared_data sd,
float  label 
)

Definition at line 128 of file global_data.cc.

References shared_data::max_label, and shared_data::min_label.

Referenced by vw::vw().

129 {
130  sd->min_label = std::min(sd->min_label, label);
131  if (label != FLT_MAX)
132  sd->max_label = std::max(sd->max_label, label);
133 }
float min_label
Definition: global_data.h:150
float max_label
Definition: global_data.h:151

◆ trace_listener_cerr()

void trace_listener_cerr ( void *  ,
const std::string &  message 
)

Definition at line 240 of file global_data.cc.

Referenced by vw_ostream::vw_ostream().

241 {
242  std::cerr << message;
243  std::cerr.flush();
244 }