|
| 1 | +/** Copyright 2020 Alibaba Group Holding Limited. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +#ifndef EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_VC_H_ |
| 17 | +#define EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_VC_H_ |
| 18 | + |
| 19 | +#include <grape/grape.h> |
| 20 | + |
| 21 | +#include "pagerank/pagerank_vc_context.h" |
| 22 | + |
| 23 | +namespace grape { |
| 24 | + |
| 25 | +template <typename T> |
| 26 | +struct NumericSum { |
| 27 | + static T init() { return 0; } |
| 28 | + |
| 29 | + static void aggregate(T& a, const T& b) { a += b; } |
| 30 | +}; |
| 31 | + |
| 32 | +template <typename FRAG_T> |
| 33 | +class PageRankVC |
| 34 | + : public GatherScatterAppBase<FRAG_T, PageRankVCContext<FRAG_T>>, |
| 35 | + public ParallelEngine, |
| 36 | + public Communicator { |
| 37 | + using vertex_t = Vertex<typename FRAG_T::oid_t>; |
| 38 | + |
| 39 | + public: |
| 40 | + INSTALL_GATHER_SCATTER_WORKER(PageRankVC<FRAG_T>, PageRankVCContext<FRAG_T>, |
| 41 | + FRAG_T) |
| 42 | + |
| 43 | + void PEval(const fragment_t& frag, context_t& ctx, |
| 44 | + message_manager_t& messages) { |
| 45 | + if (ctx.max_round <= 0) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + ctx.step = 0; |
| 50 | + ctx.graph_vnum = frag.GetTotalVerticesNum(); |
| 51 | + |
| 52 | + { |
| 53 | +#ifdef PROFILING |
| 54 | + ctx.t0 -= GetCurrentTime(); |
| 55 | +#endif |
| 56 | + typename fragment_t::template both_vertex_array_t<int> degree( |
| 57 | + frag.Vertices(), 0); |
| 58 | +#ifdef TRACKING_MEMORY |
| 59 | + // allocate degree array for both src and dst vertices |
| 60 | + MemoryTracker::GetInstance().allocate(frag.Vertices().size() * |
| 61 | + sizeof(int)); |
| 62 | +#endif |
| 63 | + int bucket_num = frag.GetBucketNum(); |
| 64 | + int concurrency = thread_num(); |
| 65 | + if (bucket_num < (concurrency / 2)) { |
| 66 | + ForEach( |
| 67 | + frag.GetEdges().begin(), frag.GetEdges().end(), |
| 68 | + [&](int tid, const typename fragment_t::edge_t& e) { |
| 69 | + atomic_add(degree[vertex_t(e.src)], 1); |
| 70 | + atomic_add(degree[vertex_t(e.dst)], 1); |
| 71 | + }, |
| 72 | + 4096); |
| 73 | + } else { |
| 74 | + ForEach(0, bucket_num, |
| 75 | + [°ree, &frag, bucket_num](int tid, int bucket_id) { |
| 76 | + for (int i = 0; i < bucket_num; ++i) { |
| 77 | + for (auto& e : frag.GetEdgesOfBucket(i, bucket_id)) { |
| 78 | + degree[vertex_t(e.dst)] += 1; |
| 79 | + } |
| 80 | + } |
| 81 | + for (int i = 0; i < bucket_num; ++i) { |
| 82 | + for (auto& e : frag.GetEdgesOfBucket(bucket_id, i)) { |
| 83 | + degree[vertex_t(e.src)] += 1; |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | +#ifdef PROFILING |
| 89 | + ctx.t0 += GetCurrentTime(); |
| 90 | +#endif |
| 91 | + |
| 92 | + messages.GatherMasterVertices<fragment_t, int, NumericSum<int>>( |
| 93 | + frag, degree, ctx.master_degree); |
| 94 | +#ifdef TRACKING_MEMORY |
| 95 | + // deallocate degree array for both src and dst vertices |
| 96 | + MemoryTracker::GetInstance().deallocate(frag.Vertices().size() * |
| 97 | + sizeof(int)); |
| 98 | +#endif |
| 99 | + } |
| 100 | + |
| 101 | + double p = 1.0 / ctx.graph_vnum; |
| 102 | + int64_t dangling_vnum_local = 0; |
| 103 | +#ifdef PROFILING |
| 104 | + ctx.t1 -= GetCurrentTime(); |
| 105 | +#endif |
| 106 | + std::vector<int64_t> dangling_vnum_local_vec(thread_num(), 0); |
| 107 | + ForEach(frag.MasterVertices(), [&](int tid, vertex_t v) { |
| 108 | + if (ctx.master_degree[v] == 0) { |
| 109 | + ++dangling_vnum_local_vec[tid]; |
| 110 | + ctx.master_result[v] = p; |
| 111 | + } else { |
| 112 | + ctx.master_result[v] = p / ctx.master_degree[v]; |
| 113 | + } |
| 114 | + }); |
| 115 | + for (auto x : dangling_vnum_local_vec) { |
| 116 | + dangling_vnum_local += x; |
| 117 | + } |
| 118 | +#ifdef PROFILING |
| 119 | + ctx.t1 += GetCurrentTime(); |
| 120 | +#endif |
| 121 | + |
| 122 | + Sum(dangling_vnum_local, ctx.total_dangling_vnum); |
| 123 | + ctx.dangling_sum = p * ctx.total_dangling_vnum; |
| 124 | + |
| 125 | + messages.ScatterMasterVertices<fragment_t, double>(frag, ctx.master_result, |
| 126 | + ctx.curr_result); |
| 127 | + messages.ForceContinue(); |
| 128 | + } |
| 129 | + |
| 130 | + void IncEval(const fragment_t& frag, context_t& ctx, |
| 131 | + message_manager_t& messages) { |
| 132 | + if (ctx.step == 0) { |
| 133 | + messages.AllocateGatherBuffers<fragment_t, double>(frag); |
| 134 | + } |
| 135 | + ++ctx.step; |
| 136 | + |
| 137 | + double base = (1.0 - ctx.delta) / ctx.graph_vnum + |
| 138 | + ctx.delta * ctx.dangling_sum / ctx.graph_vnum; |
| 139 | + ctx.dangling_sum = base * ctx.total_dangling_vnum; |
| 140 | + |
| 141 | + ForEach(frag.Vertices(), |
| 142 | + [&ctx](int tid, vertex_t v) { ctx.next_result[v] = 0; }); |
| 143 | + |
| 144 | + int bucket_num = frag.GetBucketNum(); |
| 145 | + int concurrency = thread_num(); |
| 146 | + |
| 147 | +#ifdef PROFILING |
| 148 | + ctx.t2 -= GetCurrentTime(); |
| 149 | +#endif |
| 150 | + if (bucket_num < (concurrency / 2)) { |
| 151 | + ForEach( |
| 152 | + frag.GetEdges().begin(), frag.GetEdges().end(), |
| 153 | + [&ctx](int tid, const typename fragment_t::edge_t& e) { |
| 154 | + atomic_add(ctx.next_result[vertex_t(e.dst)], |
| 155 | + ctx.curr_result[vertex_t(e.src)]); |
| 156 | + atomic_add(ctx.next_result[vertex_t(e.src)], |
| 157 | + ctx.curr_result[vertex_t(e.dst)]); |
| 158 | + }, |
| 159 | + 4096); |
| 160 | + } else { |
| 161 | + ForEach(0, bucket_num, [&ctx, &frag, bucket_num](int tid, int bucket_id) { |
| 162 | + for (int i = 0; i < bucket_num; ++i) { |
| 163 | + for (auto& e : frag.GetEdgesOfBucket(i, bucket_id)) { |
| 164 | + ctx.next_result[vertex_t(e.dst)] += |
| 165 | + ctx.curr_result[vertex_t(e.src)]; |
| 166 | + } |
| 167 | + } |
| 168 | + for (int i = 0; i < bucket_num; ++i) { |
| 169 | + for (auto& e : frag.GetEdgesOfBucket(bucket_id, i)) { |
| 170 | + ctx.next_result[vertex_t(e.src)] += |
| 171 | + ctx.curr_result[vertex_t(e.dst)]; |
| 172 | + } |
| 173 | + } |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | +#ifdef PROFILING |
| 178 | + ctx.t2 += GetCurrentTime(); |
| 179 | +#endif |
| 180 | + |
| 181 | + messages.GatherMasterVertices<fragment_t, double, NumericSum<double>>( |
| 182 | + frag, ctx.next_result, ctx.master_result); |
| 183 | + |
| 184 | + if (ctx.step != ctx.max_round) { |
| 185 | +#ifdef PROFILING |
| 186 | + ctx.t1 -= GetCurrentTime(); |
| 187 | +#endif |
| 188 | + ForEach(frag.MasterVertices(), [&ctx, base](int tid, vertex_t v) { |
| 189 | + if (ctx.master_degree[v] > 0) { |
| 190 | + ctx.master_result[v] = |
| 191 | + (base + ctx.delta * ctx.master_result[v]) / ctx.master_degree[v]; |
| 192 | + } else { |
| 193 | + ctx.master_result[v] = base; |
| 194 | + } |
| 195 | + }); |
| 196 | +#ifdef PROFILING |
| 197 | + ctx.t1 += GetCurrentTime(); |
| 198 | +#endif |
| 199 | + |
| 200 | + messages.ScatterMasterVertices<fragment_t, double>( |
| 201 | + frag, ctx.master_result, ctx.curr_result); |
| 202 | + messages.ForceContinue(); |
| 203 | + } else { |
| 204 | +#ifdef PROFILING |
| 205 | + ctx.t1 -= GetCurrentTime(); |
| 206 | +#endif |
| 207 | + ForEach(frag.MasterVertices(), [&ctx, base](int tid, vertex_t v) { |
| 208 | + ctx.master_result[v] = ctx.master_result[v] * ctx.delta + base; |
| 209 | + }); |
| 210 | +#ifdef PROFILING |
| 211 | + ctx.t1 += GetCurrentTime(); |
| 212 | +#endif |
| 213 | + } |
| 214 | + } |
| 215 | +}; |
| 216 | + |
| 217 | +} // namespace grape |
| 218 | + |
| 219 | +#endif // EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_VC_H_ |
0 commit comments