-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpregel_app_pagerank.h
executable file
·151 lines (132 loc) · 3.3 KB
/
pregel_app_pagerank.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
#include "basic/pregel-dev.h"
using namespace std;
//input line format: vertexID \t numOfNeighbors neighbor1 neighbor2 ...
//output line format: v \t PageRank(v) ...
//an aggregator collects PageRank(v) for all dangling vertices, which is then redistributed to all vertices in the next superstep
//equivalent to adding edges from a dangling vertex to all vertices in the graph
struct PRValue_pregel
{
double pr;
vector<VertexID> edges;
};
ibinstream & operator<<(ibinstream & m, const PRValue_pregel & v){
m<<v.pr;
m<<v.edges;
return m;
}
obinstream & operator>>(obinstream & m, PRValue_pregel & v){
m>>v.pr;
m>>v.edges;
return m;
}
//====================================
class PRVertex_pregel:public Vertex<VertexID, PRValue_pregel, double>
{
public:
virtual void compute(MessageContainer & messages)
{
if(step_num()==1)
{
value().pr=1.0/get_vnum();
}
else
{
double sum=0;
for(MessageIter it=messages.begin(); it!=messages.end(); it++)
{
sum+=*it;
}
double* agg=(double*)getAgg();
double residual=*agg/get_vnum();
value().pr=0.15/get_vnum()+0.85*(sum+residual);
}
if(step_num()<ROUND)
{
double msg=value().pr/value().edges.size();
for(vector<VertexID>::iterator it=value().edges.begin(); it!=value().edges.end(); it++)
{
send_message(*it, msg);
}
}
else vote_to_halt();
}
};
//====================================
class PRAgg_pregel:public Aggregator<PRVertex_pregel, double, double>
{
private:
double sum;
public:
virtual void init(){
sum=0;
}
virtual void stepPartial(PRVertex_pregel* v)
{
if(v->value().edges.size()==0) sum+=v->value().pr;
}
virtual void stepFinal(double* part)
{
sum+=*part;
}
virtual double* finishPartial(){ return ∑ }
virtual double* finishFinal(){ return ∑ }
};
class PRWorker_pregel:public Worker<PRVertex_pregel, PRAgg_pregel>
{
char buf[100];
public:
virtual PRVertex_pregel* toVertex(char* line)
{
char * pch;
pch=strtok(line, "\t");
PRVertex_pregel* v=new PRVertex_pregel;
v->id=atoi(pch);
pch=strtok(NULL, " ");
int num=atoi(pch);
for(int i=0; i<num; i++)
{
pch=strtok(NULL, " ");
v->value().edges.push_back(atoi(pch));
}
return v;
}
virtual void toline(PRVertex_pregel* v, BufferedWriter & writer)
{
sprintf(buf, "%d\t%f\n", v->id, v->value().pr);
writer.write(buf);
}
};
class PRCombiner_pregel:public Combiner<double>
{
public:
virtual void combine(double & old, const double & new_msg)
{
old+=new_msg;
}
};
void pregel_pagerank(string in_path, string out_path, bool use_combiner){
WorkerParams param;
param.input_path=in_path;
param.output_path=out_path;
param.force_write=true;
param.native_dispatcher=false;
PRWorker_pregel worker;
PRCombiner_pregel combiner;
if(use_combiner) worker.setCombiner(&combiner);
PRAgg_pregel agg;
worker.setAggregator(&agg);
worker.run(param);
}
void pregel_pagerank_report(string in_path, string out_path, string report_path, bool use_combiner){
WorkerParams param;
param.input_path=in_path;
param.output_path=out_path;
param.force_write=true;
param.native_dispatcher=false;
PRWorker_pregel worker;
PRCombiner_pregel combiner;
if(use_combiner) worker.setCombiner(&combiner);
PRAgg_pregel agg;
worker.setAggregator(&agg);
worker.run_report(param, report_path);
}