Vowpal Wabbit
Classes | Functions
sender.cc File Reference
#include <vector>
#include <netdb.h>
#include "io_buf.h"
#include "cache.h"
#include "network.h"
#include "reductions.h"

Go to the source code of this file.

Classes

struct  sender
 

Functions

void open_sockets (sender &s, std::string host)
 
void send_features (io_buf *b, example &ec, uint32_t mask)
 
void receive_result (sender &s)
 
void learn (sender &s, LEARNER::single_learner &, example &ec)
 
void finish_example (vw &, sender &, example &)
 
void end_examples (sender &s)
 
LEARNER::base_learnersender_setup (options_i &options, vw &all)
 

Function Documentation

◆ end_examples()

void end_examples ( sender s)

Definition at line 93 of file sender.cc.

References sender::buf, io_buf::files, receive_result(), sender::received_index, and sender::sent_index.

Referenced by sender_setup().

94 {
95  // close our outputs to signal finishing.
96  while (s.received_index != s.sent_index) receive_result(s);
97  shutdown(s.buf->files[0], SHUT_WR);
98 }
void receive_result(sender &s)
Definition: sender.cc:65
v_array< int > files
Definition: io_buf.h:64
io_buf * buf
Definition: sender.cc:28
size_t received_index
Definition: sender.cc:33
size_t sent_index
Definition: sender.cc:32

◆ finish_example()

void finish_example ( vw ,
sender ,
example  
)

Definition at line 91 of file sender.cc.

91 {}

◆ learn()

void learn ( sender s,
LEARNER::single_learner ,
example ec 
)

Definition at line 79 of file sender.cc.

References sender::all, sender::buf, label_parser::cache_label, cache_tag(), sender::delay_ring, example::l, label_data::label, parser::lp, vw::p, vw::parse_mask, receive_result(), sender::received_index, parser::ring_size, vw::sd, send_features(), sender::sent_index, vw::set_minmax, polylabel::simple, and example::tag.

Referenced by sender_setup().

80 {
81  if (s.received_index + s.all->p->ring_size / 2 - 1 == s.sent_index)
82  receive_result(s);
83 
84  s.all->set_minmax(s.all->sd, ec.l.simple.label);
85  s.all->p->lp.cache_label(&ec.l, *s.buf); // send label information.
86  cache_tag(*s.buf, ec.tag);
87  send_features(s.buf, ec, (uint32_t)s.all->parse_mask);
88  s.delay_ring[s.sent_index++ % s.all->p->ring_size] = &ec;
89 }
v_array< char > tag
Definition: example.h:63
float label
Definition: simple_label.h:14
label_data simple
Definition: example.h:28
parser * p
Definition: global_data.h:377
void receive_result(sender &s)
Definition: sender.cc:65
void(* set_minmax)(shared_data *sd, float label)
Definition: global_data.h:394
shared_data * sd
Definition: global_data.h:375
example ** delay_ring
Definition: sender.cc:31
io_buf * buf
Definition: sender.cc:28
void(* cache_label)(void *, io_buf &cache)
Definition: label_parser.h:14
const size_t ring_size
Definition: parser.h:80
size_t received_index
Definition: sender.cc:33
void send_features(io_buf *b, example &ec, uint32_t mask)
Definition: sender.cc:51
vw * all
Definition: sender.cc:30
polylabel l
Definition: example.h:57
uint64_t parse_mask
Definition: global_data.h:453
size_t sent_index
Definition: sender.cc:32
void cache_tag(io_buf &cache, v_array< char > tag)
Definition: cache.cc:192
label_parser lp
Definition: parser.h:102

◆ open_sockets()

void open_sockets ( sender s,
std::string  host 
)

Definition at line 44 of file sender.cc.

References sender::buf, io_buf::files, open_socket(), v_array< T >::push_back(), and sender::sd.

Referenced by sender_setup().

45 {
46  s.sd = open_socket(host.c_str());
47  s.buf = new io_buf();
48  s.buf->files.push_back(s.sd);
49 }
void push_back(const T &new_ele)
Definition: v_array.h:107
v_array< int > files
Definition: io_buf.h:64
int sd
Definition: sender.cc:29
int open_socket(const char *host, unsigned short port)
io_buf * buf
Definition: sender.cc:28
Definition: io_buf.h:54

◆ receive_result()

void receive_result ( sender s)

Definition at line 65 of file sender.cc.

References sender::all, sender::delay_ring, VW::get_prediction(), loss_function::getLoss(), example::l, label_data::label, example::loss, vw::loss, vw::p, example::pred, sender::received_index, return_simple_example(), parser::ring_size, polyprediction::scalar, sender::sd, vw::sd, polylabel::simple, and example::weight.

Referenced by end_examples(), and learn().

66 {
67  float res, weight;
68 
69  get_prediction(s.sd, res, weight);
70  example& ec = *s.delay_ring[s.received_index++ % s.all->p->ring_size];
71  ec.pred.scalar = res;
72 
73  label_data& ld = ec.l.simple;
74  ec.loss = s.all->loss->getLoss(s.all->sd, ec.pred.scalar, ld.label) * ec.weight;
75 
76  return_simple_example(*(s.all), nullptr, ec);
77 }
loss_function * loss
Definition: global_data.h:523
void get_prediction(int sock, float &res, float &weight)
Definition: global_data.cc:52
float scalar
Definition: example.h:45
float label
Definition: simple_label.h:14
label_data simple
Definition: example.h:28
parser * p
Definition: global_data.h:377
virtual float getLoss(shared_data *, float prediction, float label)=0
shared_data * sd
Definition: global_data.h:375
example ** delay_ring
Definition: sender.cc:31
int sd
Definition: sender.cc:29
const size_t ring_size
Definition: parser.h:80
size_t received_index
Definition: sender.cc:33
float loss
Definition: example.h:70
float weight
vw * all
Definition: sender.cc:30
polylabel l
Definition: example.h:57
polyprediction pred
Definition: example.h:60
float weight
Definition: example.h:62
void return_simple_example(vw &all, void *, example &ec)

◆ send_features()

void send_features ( io_buf b,
example ec,
uint32_t  mask 
)

Definition at line 51 of file sender.cc.

References constant_namespace, example_predict::feature_space, io_buf::flush(), example_predict::indices, output_byte(), output_features(), and v_array< T >::size().

Referenced by learn().

52 {
53  // note: subtracting 1 b/c not sending constant
54  output_byte(*b, (unsigned char)(ec.indices.size() - 1));
55 
56  for (namespace_index ns : ec.indices)
57  {
58  if (ns == constant_namespace)
59  continue;
60  output_features(*b, ns, ec.feature_space[ns], mask);
61  }
62  b->flush();
63 }
v_array< namespace_index > indices
void output_byte(io_buf &cache, unsigned char s)
Definition: cache.cc:144
size_t size() const
Definition: v_array.h:68
std::array< features, NUM_NAMESPACES > feature_space
void output_features(io_buf &cache, unsigned char index, features &fs, uint64_t mask)
Definition: cache.cc:153
virtual void flush()
Definition: io_buf.h:194
unsigned char namespace_index
constexpr unsigned char constant_namespace
Definition: constant.h:22

◆ sender_setup()

LEARNER::base_learner* sender_setup ( options_i options,
vw all 
)

Definition at line 100 of file sender.cc.

References VW::config::option_group_definition::add(), VW::config::options_i::add_and_parse(), end_examples(), VW::finish_example(), LEARNER::init_learner(), learn(), LEARNER::make_base(), VW::config::make_option(), open_sockets(), vw::p, parser::ring_size, and VW::config::options_i::was_supplied().

Referenced by parse_reductions().

101 {
102  std::string host;
103 
104  option_group_definition sender_options("Network sending");
105  sender_options.add(make_option("sendto", host).keep().help("send examples to <host>"));
106  options.add_and_parse(sender_options);
107 
108  if (!options.was_supplied("sendto"))
109  {
110  return nullptr;
111  }
112 
113  auto s = scoped_calloc_or_throw<sender>();
114  s->sd = -1;
115  open_sockets(*s.get(), host);
116 
117  s->all = &all;
118  s->delay_ring = calloc_or_throw<example*>(all.p->ring_size);
119 
121  l.set_finish_example(finish_example);
122  l.set_end_examples(end_examples);
123  return make_base(l);
124 }
void open_sockets(sender &s, std::string host)
Definition: sender.cc:44
base_learner * make_base(learner< T, E > &base)
Definition: learner.h:462
virtual void add_and_parse(const option_group_definition &group)=0
void finish_example(vw &, sender &, example &)
Definition: sender.cc:91
parser * p
Definition: global_data.h:377
learner< T, E > & init_learner(free_ptr< T > &dat, L *base, void(*learn)(T &, L &, E &), void(*predict)(T &, L &, E &), size_t ws, prediction_type::prediction_type_t pred_type)
Definition: learner.h:369
void learn(sender &s, LEARNER::single_learner &, example &ec)
Definition: sender.cc:79
virtual bool was_supplied(const std::string &key)=0
const size_t ring_size
Definition: parser.h:80
typed_option< T > make_option(std::string name, T &location)
Definition: options.h:80
void end_examples(sender &s)
Definition: sender.cc:93