forked from Jaiwen/GraphGrind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageRankDelta.C
174 lines (163 loc) · 5.5 KB
/
PageRankDelta.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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
//
// 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.
//#define PUSH 1
#include "ligra-numa.h"
#include "math.h"
static intT CAScounter;
template <class vertex>
struct PR_F
{
vertex* V;
double* Delta, *nghSum;
static const bool use_cache = true;
struct cache_t
{
double nghSum;
};
PR_F(vertex* _V, double* _Delta, double* _nghSum) :
V(_V), Delta(_Delta), nghSum(_nghSum) {}
inline bool update(intT s, intT d)
{
nghSum[d] += Delta[s]/V[s].getOutDegree();
return 1;
}
inline bool updateAtomic (intT s, intT d)
{
//__sync_fetch_and_add(&CAScounter,1);
writeAdd(&nghSum[d],Delta[s]/V[s].getOutDegree());
return 1;
}
inline void create_cache(cache_t &cache, intT d)
{
cache.nghSum = nghSum[d];
}
inline bool update(cache_t &cache, intT s)
{
cache.nghSum += Delta[s]/V[s].getOutDegree();
return 1;
}
inline void commit_cache(cache_t &cache, intT d)
{
nghSum[d]=cache.nghSum;
}
inline bool cond (intT d)
{
return cond_true(d);
}
};
struct PR_Vertex_F_FirstRound
{
double damping, addedConstant, one_over_n, epsilon2;
double* p, *Delta, *nghSum;
PR_Vertex_F_FirstRound(double* _p, double* _Delta, double* _nghSum, double _damping, double _one_over_n,double _epsilon2) :
p(_p),
damping(_damping), Delta(_Delta), nghSum(_nghSum), one_over_n(_one_over_n),
addedConstant((1-_damping)*_one_over_n),
epsilon2(_epsilon2) {}
inline bool operator () (intT i)
{
Delta[i] = damping*(p[i]+nghSum[i])+addedConstant-p[i];
p[i] += Delta[i];
Delta[i]-=one_over_n; //subtract off delta from initialization
return (fabs(Delta[i]) > epsilon2 * p[i]);
}
};
struct PR_Vertex_F
{
double damping, epsilon2;
double* p, *Delta, *nghSum;
PR_Vertex_F(double* _p, double* _Delta, double* _nghSum, double _damping, double _epsilon2) :
p(_p),
damping(_damping), Delta(_Delta), nghSum(_nghSum),
epsilon2(_epsilon2) {}
inline bool operator () (intT i)
{
Delta[i] = nghSum[i]*damping;
p[i]+=Delta[i];
return (fabs(Delta[i]) > epsilon2*p[i]);
}
};
struct PR_Vertex_Reset
{
double* nghSum;
PR_Vertex_Reset(double* _nghSum) :
nghSum(_nghSum) {}
inline bool operator () (intT i)
{
nghSum[i] = 0.0;
return 1;
}
};
template <class GraphType>
void Compute(GraphType &GA, long start)
{
typedef typename GraphType::vertex_type vertex; // Is determined by GraphType
const partitioner &part = GA.get_partitioner();
const int perNode = part.get_num_per_node_partitions();
const double damping = 0.85;
const double epsilon = 0.0000001;
const double epsilon2 = 0.01;
intT n = GA.n;
intT m = GA.m;
double one_over_n = 1/(double)n;
mmap_ptr<double> p;
p.part_allocate (part);
mmap_ptr<double> nghSum;
nghSum.part_allocate (part);
mmap_ptr<double> Delta;
Delta.part_allocate (part);
map_vertexL (part, [&] (intT j) { p[j] = 0.0; } );
map_vertexL (part, [&] (intT j) { Delta[j] = one_over_n; } );
map_vertexL (part, [&] (intT j) { nghSum[j] = 0.0; } );
partitioned_vertices Frontier = partitioned_vertices::bits(part,n, m);
partitioned_vertices All = partitioned_vertices::bits(part,n,m);
// CAScounter=0;
intT round = 0;
while(1)
{
round++;
partitioned_vertices output = edgeMap(GA,Frontier,PR_F<vertex>(GA.get_partition().V,Delta,nghSum),m/20);
output.del();
//vertexSubset active
partitioned_vertices active
= (round == 1) ?
vertexFilter(GA,All,PR_Vertex_F_FirstRound(p,Delta,nghSum,damping,one_over_n,epsilon2)) :
vertexFilter(GA,All,PR_Vertex_F(p,Delta,nghSum,damping,epsilon2));
//compute L1-norm (use nghSum as temp array)
{
map_vertexL (part, [&] (intT j) { nghSum[j] = fabs(Delta[j]); } );
}
double L1_norm = sequence::plusReduce(nghSum.get(),n);
if(L1_norm < epsilon) break;
//reset
vertexMap(part,All,PR_Vertex_Reset(nghSum));
Frontier.del();
Frontier = active;
}
Frontier.del();
All.del();
p.del();
Delta.del();
nghSum.del();
}