forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multisource.cc
160 lines (146 loc) · 4.12 KB
/
multisource.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "multisource.h"
#include "simple_label.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
using namespace std;
int really_read(int sock, void* in, size_t count)
{
char* buf = (char*)in;
size_t done = 0;
int r = 0;
while (done < count)
{
if ((r = read(sock,buf,count-done)) == 0)
return 0;
else
if (r < 0)
{
cerr << "argh! bad read! on message from " << sock << endl;
perror(NULL);
exit(0);
}
else
{
done += r;
buf += r;
}
}
return done;
}
bool blocking_get_prediction(int sock, prediction &p)
{
int count = really_read(sock, &p, sizeof(p));
bool ret = (count == sizeof(p));
return ret;
}
bool blocking_get_global_prediction(int sock, global_prediction &p)
{
int count = really_read(sock, &p, sizeof(p));
bool ret = (count == sizeof(p));
return ret;
}
void send_prediction(int sock, prediction &p)
{
if (write(sock, &p, sizeof(p)) < (int)sizeof(p))
{
cerr << "argh! bad write! " << endl;
perror(NULL);
exit(0);
}
}
void send_global_prediction(int sock, global_prediction p)
{
if (write(sock, &p, sizeof(p)) < (int)sizeof(p))
{
cerr << "argh! bad global write! " << sock << endl;
perror(NULL);
exit(0);
}
}
void reset(partial_example &ex)
{
ex.features.erase();
}
size_t num_finished = 0;
v_array<char> c;
int receive_features(parser* p, void* ex)
{
example* ae = (example*)ex;
io_buf* input = p->input;
fd_set fds;
FD_ZERO(&fds);
for (int* sock= input->files.begin; sock != input->files.end-num_finished; sock++)
FD_SET(*sock,&fds);
while (input->files.index() > num_finished)
{
if (select(p->max_fd,&fds,NULL, NULL, NULL) == -1)
{
cerr << "Select failed!" << endl;
perror(NULL);
exit (1);
}
for (int index = 0; index < (int)(input->files.index()-num_finished); index++)
{
int sock = input->files[index];
if (FD_ISSET(sock, &fds))
{//there is a feature or label to read
prediction pre;
if (!blocking_get_prediction(sock, pre) )
{
FD_CLR(sock, &fds);
int swap_target = input->files.index()-num_finished-1;
input->files[index]=input->files[swap_target];
input->files[swap_target]=sock;
int temp = p->ids[index];
p->ids[index]=p->ids[swap_target];
p->ids[swap_target] = temp;
temp = p->counts[index];
p->counts[index]=p->counts[swap_target];
p->counts[swap_target] = temp;
num_finished++;
index--;
}
else
{
if (pre.example_number != ++ (p->counts[index]))
cout << "count is off! " << pre.example_number << " != " << p->counts[index] <<
" for source " << index << " prediction = " << pre.p << endl;
if (pre.example_number == p->finished_count + global.ring_size)
FD_CLR(sock,&fds);//this ones to far ahead, let the buffer fill for awhile.
size_t ring_index = pre.example_number % p->pes.index();
if (p->pes[ring_index].features.index() == 0)
p->pes[ring_index].example_number = pre.example_number;
if (p->pes[ring_index].example_number != (int)pre.example_number)
cerr << "Error, example " << p->pes[ring_index].example_number << " != " << pre.example_number << endl;
feature f = {pre.p, (uint32_t)p->ids[index]};
push(p->pes[ring_index].features, f);
if (sock == p->label_sock) // The label source
{
label_data ld;
size_t len = sizeof(ld.label)+sizeof(ld.weight);
c.erase();
if (c.index() < len)
reserve(c,len);
really_read(sock,c.begin,len);
bufread_simple_label(&(p->pes[ring_index].ld), c.begin);
}
if( p->pes[ring_index].features.index() == input->count )
{
push( ae->indices, multindex );
push_many( ae->atomics[multindex], p->pes[ring_index].features.begin, p->pes[ring_index].features.index() );
label_data* ld = (label_data*)ae->ld;
*ld = p->pes[ring_index].ld;
reset(p->pes[ring_index]);
p->finished_count++;
return ae->atomics[multindex].index();
}
}
}
else if (p->counts[index] < p->finished_count + global.ring_size)
FD_SET(sock,&fds);
}
}
return 0;
}