-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPageRank.C
158 lines (149 loc) · 4.96 KB
/
PageRank.C
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
// This code is part of the project "Ligra: A Lightweight Graph Processing
// Framework for Shared Memory", presented at Principles and Practice of
// Parallel Programming, 2013.
// Copyright (c) 2013 Julian Shun and Guy Blelloch
//
// This code has been extended in the project "GraphGrind: Addressing Load
// Imbalance of Graph Partitioning", presented at International Symposium
// on Supercomputing, 2017.
// Copyright (c) 2017 Jiawen Sun, Hans Vandierendonck, Dimitrios S. Nikolopoulos
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights (to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#if NUMA
#include "ligra-numa.h"
#else
#include "ligra.h"
#endif
#include "math.h"
int MaxIter=10;
template <class vertex>
struct PR_F
{
double* p_curr, *p_next;
vertex* V;
static const bool use_cache = true;
struct cache_t
{
double p_next;
};
PR_F(double* _p_curr, double* _p_next, vertex* _V) :
p_curr(_p_curr), p_next(_p_next), V(_V) {}
inline bool update(intT s, intT d) //update function applies PageRank equation
{
p_next[d] += p_curr[s]/V[s].getOutDegree();
return 1;
}
inline bool updateAtomic (intT s, intT d) //atomic Update
{
writeAdd(&p_next[d],p_curr[s]/V[s].getOutDegree());
return 1;
}
inline void create_cache(cache_t &cache, intT d)
{
cache.p_next = p_next[d];
}
inline bool update(cache_t &cache, intT s)
{
cache.p_next += p_curr[s]/V[s].getOutDegree();
return 1;
}
inline void commit_cache(cache_t &cache, intT d)
{
// Cache is used only in sequential mode
p_next[d] = cache.p_next;
}
inline bool cond (intT d)
{
return cond_true(d); //does nothing
}
};
//vertex map function to update its p value according to PageRank equation
struct PR_Vertex_F
{
double damping;
double addedConstant;
double* p_curr;
double* p_next;
PR_Vertex_F(double* _p_curr, double* _p_next, double _damping, intT n) :
damping(_damping), addedConstant((1-_damping)*(1/(double)n)),
p_curr(_p_curr), p_next(_p_next) {}
inline bool operator () (intT i)
{
p_next[i] = damping*p_next[i] + addedConstant;
return 1;
}
};
//resets p
struct PR_Vertex_Reset
{
double* p_curr;
PR_Vertex_Reset(double* _p_curr) :
p_curr(_p_curr) {}
inline bool operator () (intT i)
{
p_curr[i] = 0.0;
return 1;
}
};
template <class GraphType>
void Compute(GraphType &GA, long start)
{
typedef typename GraphType::vertex_type vertex; // Is determined by GraphTyp
const partitioner &part = GA.get_partitioner();
const int perNode = part.get_num_per_node_partitions();
intT n = GA.n;
intT m = GA.m;
const double damping = 0.85;
const double epsilon = 0.0000001;
//Data Array
//p_curr and p_next to do special allocation
//frontier also need special node allocation
//blocksize equal to the szie of each partitioned
double one_over_n = 1/(double)n;
mmap_ptr<double> p_curr;
p_curr.part_allocate (part);
mmap_ptr<double> p_next;
p_next.part_allocate (part);
loop(j,part,perNode,p_curr[j]=one_over_n);
loop(j,part,perNode,p_next[j]=0);
int count=0;
partitioned_vertices Frontier = partitioned_vertices::bits(part,n, m);
while(1 && count<MaxIter)
{
count++;
partitioned_vertices output = edgeMap(GA, Frontier, PR_F<vertex>(p_curr,p_next,GA.getWholeGraph().V),m/20);
vertexMap(part,Frontier,PR_Vertex_F(p_curr,p_next,damping,n));
//compute L1-norm between p_curr and p_next
{
loop(j,part,perNode,p_curr[j] = fabs(p_curr[j]-p_next[j]));
}
double L1_norm = sequence::plusReduce(p_curr.get(),n);
if(L1_norm < epsilon) break;
//reset p_curr
vertexMap(part, Frontier,PR_Vertex_Reset(p_curr));
swap(p_curr,p_next);
Frontier.del();
Frontier = output;
Frontier.bit=true;
}
Frontier.del();
p_curr.del();
p_next.del();
}