-
Notifications
You must be signed in to change notification settings - Fork 0
/
blogel_app_pagerank1.h
396 lines (355 loc) · 12.4 KB
/
blogel_app_pagerank1.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "utils/communication.h"
#include "blogel/BVertex.h"
#include "blogel/Block.h"
#include "blogel/BType.h"
#include "blogel/BWorker.h"
#include "blogel/BGlobal.h"
#include <iostream>
#include "math.h"
using namespace std;
#define EPS 0.01
struct PRValue {
double pr;
vector<triplet> edges;
int split;
};
//------------------------------------
class PRVertex : public BVertex<VertexID, PRValue, char> {
public:
virtual void compute(MessageContainer& messages)
{
} //not used
};
//====================================
struct doublepair {
bool converge;
double accum; //pr sum for nodes with out-degree 0
};
ibinstream& operator<<(ibinstream& m, const doublepair& v)
{
m << v.converge;
m << v.accum;
return m;
}
obinstream& operator>>(obinstream& m, doublepair& v)
{
m >> v.converge;
m >> v.accum;
return m;
}
//====================================
struct tuple {
int block;
double weight;
int worker;
};
ibinstream& operator<<(ibinstream& m, const tuple& v)
{
m << v.block;
m << v.weight;
m << v.worker;
return m;
}
obinstream& operator>>(obinstream& m, tuple& v)
{
m >> v.block;
m >> v.weight;
m >> v.worker;
return m;
}
//------------------------------------
struct PRBlockValue {
double pr;
double delta;
vector<tuple> edges;
};
//------------------------------------
class PRBlock : public Block<PRBlockValue, PRVertex, double> //msg = weight * pr
{
public:
virtual void compute(MessageContainer& messages, VertexContainer& vertexes)
{
if (step_num() == 1) {
value().pr = 1.0 / get_bnum();
value().delta = EPS / get_bnum() + 1; //init delta>EPS/|B|
} else {
double sum = 0;
for (MessageIter it = messages.begin(); it != messages.end(); it++) {
sum += *it;
}
doublepair* agg = ((doublepair*)getAgg());
if (agg->converge) {
vote_to_halt();
return;
}
double residual = agg->accum / get_bnum();
double newVal = 0.15 / get_bnum() + 0.85 * (sum + residual);
value().delta = fabs(newVal - value().pr);
value().pr = newVal;
}
for (vector<tuple>::iterator it = value().edges.begin(); it != value().edges.end(); it++) {
send_message(it->block, it->worker, it->weight * value().pr);
}
}
};
//====================================
class PRSum : public BAggregator<PRVertex, PRBlock, doublepair, doublepair> {
private:
doublepair pair;
public:
virtual void init()
{
pair.converge = true;
pair.accum = 0;
}
virtual void stepPartialV(PRVertex* v) {}; //not used
virtual void stepPartialB(PRBlock* b)
{
if (b->value().edges.size() == 0)
pair.accum += b->value().pr;
if (b->value().delta > EPS / get_bnum())
pair.converge = false;
}
virtual void stepFinal(doublepair* part)
{
pair.accum += part->accum;
if (part->converge == false)
pair.converge = false;
}
virtual doublepair* finishPartial()
{
return &pair;
}
virtual doublepair* finishFinal()
{
return &pair;
}
};
//====================================
class PRWorker : public BWorker<PRBlock, PRSum> {
public:
void localPR(PRBlock* block, VertexContainer& vertexes)
{
bool converge = false;
double accum = 0;
int num = block->size;
double* pr_buf;
int round = 1;
double threshold = EPS / num;
while (converge == false) {
double oldaccum;
double* old_pr_buf;
if (round > 1) {
converge = true;
oldaccum = accum;
accum = 0;
old_pr_buf = pr_buf;
}
pr_buf = new double[num];
for (int i = 0; i < num; i++)
pr_buf[i] = 0;
for (int i = block->begin; i < block->begin + block->size; i++) {
PRVertex* vertex = vertexes[i];
if (round == 1) {
vertex->value().pr = 1.0 / num;
} else {
int logID = i - block->begin;
double new_pr = 0.15 / num + 0.85 * (old_pr_buf[logID] + oldaccum / num);
double delta = new_pr - vertex->value().pr;
if (fabs(delta) > threshold)
converge = false;
vertex->value().pr = new_pr;
}
//------
vector<triplet>& edges = vertex->value().edges;
int split = vertex->value().split;
if (split == -1)
accum += vertex->value().pr;
else {
double msg = vertex->value().pr / (split + 1);
for (int j = 0; j <= split; j++) {
triplet nb = edges[j];
int phyID = nb.wid;
int logID = phyID - block->begin;
pr_buf[logID] += msg;
}
}
}
if (round > 1)
delete old_pr_buf;
round++;
}
// cout<<"Worker "<<_my_rank<<": Block "<<block->blockID<<" local-pr for "<<round-1<<" rounds"<<endl;//DEGUG !!!!!!
delete pr_buf;
}
virtual void blockInit(VertexContainer& vertexes, BlockContainer& blocks)
{
ResetTimer(4);
hash_map<int, int> map;
for (int i = 0; i < vertexes.size(); i++)
map[vertexes[i]->id] = i;
//////
if (_my_rank == MASTER_RANK)
cout << "Splitting in/out-block edges ..." << endl;
for (BlockIter it = blocks.begin(); it != blocks.end(); it++) {
PRBlock* block = *it;
for (int i = block->begin; i < block->begin + block->size; i++) {
PRVertex* vertex = vertexes[i];
vector<triplet>& edges = vertex->value().edges;
vector<triplet> tmp;
vector<triplet> tmp1;
for (int j = 0; j < edges.size(); j++) {
if (edges[j].bid == block->bid) {
edges[j].wid = map[edges[j].vid]; //workerID->array index
tmp.push_back(edges[j]);
} else
tmp1.push_back(edges[j]);
}
edges.swap(tmp);
vertex->value().split = edges.size() - 1;
edges.insert(edges.end(), tmp1.begin(), tmp1.end());
}
}
StopTimer(4);
if (_my_rank == MASTER_RANK)
cout << "In/out-block edges split. Time elapsed: " << get_timer(4) << " seconds" << endl;
//----------------------------------------------
ResetTimer(4);
if (_my_rank == MASTER_RANK)
cout << "Local PageRank computing ..." << endl;
for (BlockIter it = blocks.begin(); it != blocks.end(); it++) {
PRBlock* block = *it;
localPR(block, vertexes);
}
StopTimer(4);
if (_my_rank == MASTER_RANK)
cout << "Local PageRank computed. Time elapsed: " << get_timer(4) << " seconds" << endl;
//----------------------------------------------
ResetTimer(4);
if (_my_rank == MASTER_RANK)
cout << "Restoring from local-array-index to worker-id ..." << endl;
for (BlockIter it = blocks.begin(); it != blocks.end(); it++) {
PRBlock* block = *it;
for (int i = block->begin; i < block->begin + block->size; i++) {
PRVertex* vertex = vertexes[i];
vector<triplet>& edges = vertex->value().edges;
int split = vertex->value().split;
for (int j = 0; j <= split; j++) {
edges[j].wid = _my_rank;
}
}
}
StopTimer(4);
if (_my_rank == MASTER_RANK)
cout << "Worker-id restored. Time elapsed: " << get_timer(4) << " seconds" << endl;
//----------------------------------------------
ResetTimer(4);
cout << "Worker " << _my_rank << ": initializing block edges ..." << endl;
for (BlockIter it = blocks.begin(); it != blocks.end(); it++) {
PRBlock* block = *it;
hash_map<int, tuple> bmap; //bmap[BJ]=current weight of (BI->BJ)
for (int i = block->begin; i < block->begin + block->size; i++) {
PRVertex* vertex = vertexes[i];
vector<triplet>& vedges = vertex->value().edges;
int degree = vedges.size();
//group vertex's neighbors by blockID
hash_map<int, tuple> count; //count[BJ]=# of neighbors belonging to BJ
for (int j = 0; j < degree; j++) {
int blockID = vedges[j].bid;
int workerID = vedges[j].wid;
hash_map<int, tuple>::iterator cit = count.find(blockID);
if (cit == count.end()) {
tuple cur = { blockID, 1, workerID };
count[blockID] = cur;
} else
cit->second.weight++;
}
//accumulate to the sums
for (hash_map<int, tuple>::iterator cit = count.begin(); cit != count.end(); cit++) {
int blockID = cit->first;
double cnt = cit->second.weight;
int workerID = cit->second.worker;
double val = vertex->value().pr * cnt / degree;
hash_map<int, tuple>::iterator bit = bmap.find(blockID);
if (bit == bmap.end()) {
tuple cur = { blockID, val, workerID };
bmap[blockID] = cur;
} else
bit->second.weight += val;
}
}
//bmap -> block's adj-list
vector<tuple>& adj_list = block->value().edges;
double wsum = 0;
for (hash_map<int, tuple>::iterator bit = bmap.begin(); bit != bmap.end(); bit++) {
adj_list.push_back(bit->second);
wsum += bit->second.weight;
}
//normalize weights
for (int i = 0; i < adj_list.size(); i++)
adj_list[i].weight /= wsum;
}
StopTimer(4);
cout << "Worker " << _my_rank << ": block edges initialized. Time elapsed: " << get_timer(4) << " seconds" << endl;
}
//input line format: me \t nb1 nb2 ...
//each item is of format "vertexID blockID workerID"
virtual PRVertex* toVertex(char *line) {
char *pch;
PRVertex* v = new PRVertex;
pch = strtok(line, " ");
v->id = atoi(pch);
pch = strtok(NULL, " ");
pch = strtok(NULL, " ");
v->bid = atoi(pch);
pch = strtok(NULL, " ");
v->wid = atoi(pch);
pch = strtok(NULL, " ");
int num = atoi(pch);
while (num--) {
triplet trip;
pch = strtok(NULL, " ");
trip.vid = atoi(pch);
pch = strtok(NULL, " ");
trip.bid = atoi(pch);
pch = strtok(NULL, " ");
trip.wid = atoi(pch);
v->value().edges.push_back(trip);
}
return v;
}
//append v.local-pr block(v).pr after '\t'
virtual void toline(PRBlock* b, PRVertex* v, BufferedWriter& writer)
{
char buf[1024];
int bsize = b->size;
sprintf(buf, "%d %d %d\t%e %e %d", v->id, v->bid, v->wid, v->value().pr, b->value().pr, bsize);
writer.write(buf);
vector<triplet>& nbs = v->value().edges;
for (int i = 0; i < nbs.size(); i++) {
sprintf(buf, " %d %d %d", nbs[i].vid, nbs[i].bid, nbs[i].wid);
writer.write(buf);
}
writer.write("\n");
}
};
class PRCombiner : public Combiner<double> {
public:
virtual void combine(double& old, const double& new_msg)
{
old += new_msg;
}
};
void blogel_app_pagerank1(string in_path, string out_path)
{
WorkerParams param;
param.input_path = in_path;
param.output_path = out_path;
param.force_write = true;
PRWorker worker;
PRCombiner combiner;
worker.setBCombiner(&combiner);
PRSum agg;
worker.setAggregator(&agg);
worker.run(param);
}