forked from alibaba/libgrape-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagerank_local.h
224 lines (203 loc) Β· 6.97 KB
/
pagerank_local.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
/** Copyright 2020 Alibaba Group Holding Limited.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_LOCAL_H_
#define EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_LOCAL_H_
#include <grape/grape.h>
#include "pagerank/pagerank_local_context.h"
namespace grape {
/**
* @brief An implementation of PageRankLocal, which can work
* on both directed and undirected graphs.
*
* @tparam FRAG_T
*/
template <typename FRAG_T>
class PageRankLocal
: public BatchShuffleAppBase<FRAG_T, PageRankLocalContext<FRAG_T>>,
public ParallelEngine {
public:
INSTALL_BATCH_SHUFFLE_WORKER(PageRankLocal<FRAG_T>,
PageRankLocalContext<FRAG_T>, FRAG_T)
using vertex_t = typename FRAG_T::vertex_t;
static constexpr bool need_split_edges = true;
static constexpr MessageStrategy message_strategy =
MessageStrategy::kAlongOutgoingEdgeToOuterVertex;
static constexpr LoadStrategy load_strategy = LoadStrategy::kOnlyOut;
PageRankLocal() = default;
void PEval(const fragment_t& frag, context_t& ctx,
message_manager_t& messages) {
auto inner_vertices = frag.InnerVertices();
#ifdef PROFILING
ctx.exec_time -= GetCurrentTime();
#endif
ctx.step = 0;
ForEach(inner_vertices, [&ctx, &frag](int tid, vertex_t u) {
int EdgeNum = frag.GetLocalOutDegree(u);
ctx.result[u] = EdgeNum > 0 ? 1.0 / EdgeNum : 1.0;
});
#ifdef PROFILING
ctx.exec_time += GetCurrentTime();
ctx.postprocess_time -= GetCurrentTime();
#endif
messages.SyncInnerVertices<fragment_t, double>(frag, ctx.result,
thread_num());
#ifdef PROFILING
ctx.postprocess_time += GetCurrentTime();
#endif
}
void IncEval(const fragment_t& frag, context_t& ctx,
message_manager_t& messages) {
auto inner_vertices = frag.InnerVertices();
++ctx.step;
bool last_step = (ctx.step == ctx.max_round);
if (ctx.avg_degree > 10) {
#ifdef PROFILING
ctx.exec_time -= GetCurrentTime();
#endif
if (frag.fnum() > 1) {
ForEach(inner_vertices, [&ctx, &frag](int tid, vertex_t u) {
double cur = 0;
auto es = frag.GetOutgoingInnerVertexAdjList(u);
for (auto& e : es) {
cur += ctx.result[e.get_neighbor()];
}
ctx.next_result[u] = cur;
});
} else {
ForEach(inner_vertices, [&ctx, &frag](int tid, vertex_t u) {
double cur = 0;
auto es = frag.GetOutgoingInnerVertexAdjList(u);
for (auto& e : es) {
cur += ctx.result[e.get_neighbor()];
}
ctx.next_result[u] = 1 - ctx.delta + ctx.delta * cur;
});
}
#ifdef PROFILING
ctx.exec_time += GetCurrentTime();
#endif
for (fid_t i = 2; i < frag.fnum(); ++i) {
#ifdef PROFILING
ctx.preprocess_time -= GetCurrentTime();
#endif
fid_t src_fid = messages.UpdatePartialOuterVertices();
#ifdef PROFILING
ctx.preprocess_time += GetCurrentTime();
ctx.exec_time -= GetCurrentTime();
#endif
ForEach(inner_vertices, [src_fid, &frag, &ctx](int tid, vertex_t u) {
double cur = ctx.next_result[u];
auto es = frag.GetOutgoingAdjList(u, src_fid);
for (auto& e : es) {
cur += ctx.result[e.get_neighbor()];
}
ctx.next_result[u] = cur;
});
#ifdef PROFILING
ctx.exec_time += GetCurrentTime();
#endif
}
if (frag.fnum() > 1) {
#ifdef PROFILING
ctx.preprocess_time -= GetCurrentTime();
#endif
fid_t src_fid = messages.UpdatePartialOuterVertices();
#ifdef PROFILING
ctx.preprocess_time += GetCurrentTime();
ctx.exec_time -= GetCurrentTime();
#endif
if (last_step) {
ForEach(inner_vertices, [src_fid, &frag, &ctx](int tid, vertex_t u) {
double cur = ctx.next_result[u];
auto es = frag.GetOutgoingAdjList(u, src_fid);
for (auto& e : es) {
cur += ctx.result[e.get_neighbor()];
}
ctx.next_result[u] = 1 - ctx.delta + ctx.delta * cur;
});
} else {
ForEach(inner_vertices, [src_fid, &frag, &ctx](int tid, vertex_t u) {
double cur = ctx.next_result[u];
auto es = frag.GetOutgoingAdjList(u, src_fid);
for (auto& e : es) {
cur += ctx.result[e.get_neighbor()];
}
ctx.next_result[u] = 1 - ctx.delta + ctx.delta * cur;
int en = frag.GetLocalOutDegree(u);
ctx.next_result[u] =
en > 0 ? ctx.next_result[u] / en : ctx.next_result[u];
});
}
#ifdef PROFILING
ctx.exec_time += GetCurrentTime();
#endif
}
} else {
#ifdef PROFILING
ctx.exec_time -= GetCurrentTime();
#endif
ForEach(inner_vertices, [&ctx, &frag](int tid, vertex_t u) {
double cur = 0;
auto es = frag.GetOutgoingInnerVertexAdjList(u);
for (auto& e : es) {
cur += ctx.result[e.get_neighbor()];
}
ctx.next_result[u] = 1 - ctx.delta + ctx.delta * cur;
});
#ifdef PROFILING
ctx.exec_time += GetCurrentTime();
#endif
for (fid_t i = 1; i < frag.fnum(); ++i) {
#ifdef PROFILING
ctx.preprocess_time -= GetCurrentTime();
#endif
fid_t src_fid = messages.UpdatePartialOuterVertices();
#ifdef PROFILING
ctx.preprocess_time += GetCurrentTime();
ctx.exec_time -= GetCurrentTime();
#endif
ForEach(frag.OuterVertices(src_fid),
[&frag, &ctx](int tid, vertex_t u) {
double cur = ctx.result[u] * ctx.delta;
auto es = frag.GetIncomingAdjList(u);
for (auto& e : es) {
atomic_add(ctx.next_result[e.get_neighbor()], cur);
}
});
#ifdef PROFILING
ctx.exec_time += GetCurrentTime();
#endif
}
if (!last_step) {
ForEach(inner_vertices, [&frag, &ctx](int tid, Vertex<uint32_t> u) {
int en = frag.GetLocalOutDegree(u);
if (en > 0) {
ctx.next_result[u] /= en;
}
});
}
}
#ifdef PROFILING
ctx.postprocess_time -= GetCurrentTime();
#endif
if (!last_step) {
messages.SyncInnerVertices<fragment_t, double>(frag, ctx.next_result,
thread_num());
}
ctx.result.Swap(ctx.next_result);
#ifdef PROFILING
ctx.postprocess_time += GetCurrentTime();
#endif
}
};
} // namespace grape
#endif // EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_LOCAL_H_