From aa71a8399fbdcd1b71463cfe96f5a428bf3f9f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=93=E7=AE=80?= Date: Wed, 11 Sep 2024 14:41:30 +0800 Subject: [PATCH 01/15] vc init --- examples/analytical_apps/flags.cc | 2 + examples/analytical_apps/flags.h | 2 + .../analytical_apps/pagerank/pagerank_vc.h | 126 +++++++++ .../pagerank/pagerank_vc_context.h | 68 +++++ examples/analytical_apps/run_app.cc | 3 + examples/analytical_apps/run_app_vc.h | 81 ++++++ grape/app/vc_app_base.h | 58 +++++ grape/fragment/basic_efile_fragment_loader.h | 1 - grape/fragment/basic_vc_fragment_loader.h | 115 +++++++++ grape/fragment/immutable_edgecut_fragment.h | 2 +- grape/fragment/immutable_vertexcut_fragment.h | 170 ++++++++++++ grape/fragment/loader.h | 13 + grape/fragment/vc_fragment_loader.h | 119 +++++++++ grape/grape.h | 3 + grape/parallel/vc_message_manager.h | 244 ++++++++++++++++++ grape/utils/vertex_array.h | 36 +++ grape/vertex_map/partitioner.h | 76 ++++++ grape/worker/vc_worker.h | 117 +++++++++ 18 files changed, 1234 insertions(+), 2 deletions(-) create mode 100644 examples/analytical_apps/pagerank/pagerank_vc.h create mode 100644 examples/analytical_apps/pagerank/pagerank_vc_context.h create mode 100644 examples/analytical_apps/run_app_vc.h create mode 100644 grape/app/vc_app_base.h create mode 100644 grape/fragment/basic_vc_fragment_loader.h create mode 100644 grape/fragment/immutable_vertexcut_fragment.h create mode 100644 grape/fragment/vc_fragment_loader.h create mode 100644 grape/parallel/vc_message_manager.h create mode 100644 grape/worker/vc_worker.h diff --git a/examples/analytical_apps/flags.cc b/examples/analytical_apps/flags.cc index e2872d08..e6d9a93c 100644 --- a/examples/analytical_apps/flags.cc +++ b/examples/analytical_apps/flags.cc @@ -61,6 +61,8 @@ DEFINE_string(serialization_prefix, "", DEFINE_int32(app_concurrency, -1, "concurrency of application"); DEFINE_int32(load_concurrency, 1, "concurrency of loading graph"); +DEFINE_bool(vc, false, "whether to use vertex-cut storage."); + DEFINE_string(lb, "cta", "Load balancing policy, these options can be used: " " none, cta, cm, wm, strict"); diff --git a/examples/analytical_apps/flags.h b/examples/analytical_apps/flags.h index 59a55e6e..6f60d0b7 100644 --- a/examples/analytical_apps/flags.h +++ b/examples/analytical_apps/flags.h @@ -53,5 +53,7 @@ DECLARE_string(serialization_prefix); DECLARE_int32(app_concurrency); DECLARE_int32(load_concurrency); +DECLARE_bool(vc); + DECLARE_string(lb); #endif // EXAMPLES_ANALYTICAL_APPS_FLAGS_H_ diff --git a/examples/analytical_apps/pagerank/pagerank_vc.h b/examples/analytical_apps/pagerank/pagerank_vc.h new file mode 100644 index 00000000..b5520993 --- /dev/null +++ b/examples/analytical_apps/pagerank/pagerank_vc.h @@ -0,0 +1,126 @@ +/** 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_VC_H_ +#define EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_VC_H_ + +#include + +#include "pagerank/pagerank_vc_context.h" + +namespace grape { + +template +struct NumericSum { + static T init() { return 0; } + + static void aggregate(T& a, const T& b) { a += b; } +}; + +template +class PageRankVC : public VCAppBase>, + public ParallelEngine, + public Communicator { + using vertex_t = Vertex; + + public: + INSTALL_VC_WORKER(PageRankVC, PageRankVCContext, FRAG_T) + + void PEval(const fragment_t& frag, context_t& ctx, + message_manager_t& messages) { + if (ctx.max_round <= 0) { + return; + } + + ctx.step = 0; + ctx.graph_vnum = frag.GetTotalVerticesNum(); + + { + typename fragment_t::template both_vertex_array_t degree( + frag.GetVertices(), 0); + for (auto& e : frag.GetEdges()) { + ++degree[vertex_t(e.src)]; + ++degree[vertex_t(e.dst)]; + } + + messages.GatherMasterVertices>( + frag, degree, ctx.master_degree); + } + + int64_t dangling_vnum_local = 0; + double p = 1.0 / ctx.graph_vnum; + for (auto v : frag.GetMasterVertices()) { + if (ctx.master_degree[v] == 0) { + ++dangling_vnum_local; + ctx.master_result[v] = p; + } else { + ctx.master_result[v] = p / ctx.master_degree[v]; + } + } + + Sum(dangling_vnum_local, ctx.total_dangling_vnum); + ctx.dangling_sum = p * ctx.total_dangling_vnum; + + messages.ScatterMasterVertices(frag, ctx.master_result, + ctx.curr_result); + } + + void IncEval(const fragment_t& frag, context_t& ctx, + message_manager_t& messages) { + ++ctx.step; + + double base = (1.0 - ctx.delta) / ctx.graph_vnum + + ctx.delta * ctx.dangling_sum / ctx.graph_vnum; + ctx.dangling_sum = base * ctx.total_dangling_vnum; + + if (ctx.step != ctx.max_round) { + for (auto& e : frag.GetEdges()) { + ctx.next_result[vertex_t(e.dst)] += ctx.curr_result[vertex_t(e.src)]; + ctx.next_result[vertex_t(e.src)] += ctx.curr_result[vertex_t(e.dst)]; + } + + messages.GatherMasterVertices>( + frag, ctx.next_result, ctx.master_result); + for (auto v : frag.GetMasterVertices()) { + if (ctx.master_degree[v] > 0) { + ctx.master_result[v] = + (base + ctx.delta * ctx.master_result[v]) / ctx.master_degree[v]; + } else { + ctx.master_result[v] = base; + } + } + + messages.ScatterMasterVertices( + frag, ctx.master_result, ctx.curr_result); + } else { + for (auto& e : frag.GetEdges()) { + ctx.next_result[vertex_t(e.dst)] += ctx.curr_result[vertex_t(e.src)]; + ctx.next_result[vertex_t(e.src)] += ctx.curr_result[vertex_t(e.dst)]; + } + + messages.GatherMasterVertices>( + frag, ctx.next_result, ctx.master_result); + for (auto v : frag.GetMasterVertices()) { + ctx.master_result[v] = ctx.master_result[v] * ctx.delta + base; + } + + messages.ForceTerminate(); + } + } +}; + +} // namespace grape + +#endif // EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_VC_H_ diff --git a/examples/analytical_apps/pagerank/pagerank_vc_context.h b/examples/analytical_apps/pagerank/pagerank_vc_context.h new file mode 100644 index 00000000..41ad6737 --- /dev/null +++ b/examples/analytical_apps/pagerank/pagerank_vc_context.h @@ -0,0 +1,68 @@ +/** 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_VC_CONTEXT_H_ +#define EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_VC_CONTEXT_H_ + +namespace grape { + +template +class PageRankVCContext { + using oid_t = typename FRAG_T::oid_t; + + public: + const FRAG_T& fragment() { return fragment_; } + + explicit PageRankVCContext(const FRAG_T& fragment) : fragment_(fragment) { + curr_result.Init(fragment.GetVertices()); + next_result.Init(fragment.GetVertices()); + master_result.Init(fragment.GetMasterVertices()); + master_degree.Init(fragment.GetMasterVertices()); + } + + void Init(VCMessageManager& messages, double delta, int max_round) { + this->delta = delta; + this->max_round = max_round; + step = 0; + } + + void Output(std::ostream& os) { + auto& frag = this->fragment(); + auto master_vertices = frag.GetMasterVertices(); + for (auto v : master_vertices) { + os << v.GetValue() << " " << std::scientific << std::setprecision(15) + << master_result[v] << std::endl; + } + } + + typename FRAG_T::template both_vertex_array_t curr_result; + typename FRAG_T::template both_vertex_array_t next_result; + typename FRAG_T::template vertex_array_t master_result; + typename FRAG_T::template vertex_array_t master_degree; + + int64_t total_dangling_vnum = 0; + int64_t graph_vnum; + int step = 0; + int max_round = 0; + double delta = 0; + + double dangling_sum = 0.0; + + const FRAG_T& fragment_; +}; + +} // namespace grape + +#endif // EXAMPLES_ANALYTICAL_APPS_PAGERANK_PAGERANK_VC_CONTEXT_H_ diff --git a/examples/analytical_apps/run_app.cc b/examples/analytical_apps/run_app.cc index 7bdaa19d..7af79bd8 100644 --- a/examples/analytical_apps/run_app.cc +++ b/examples/analytical_apps/run_app.cc @@ -20,6 +20,7 @@ limitations under the License. #include #include "run_app_opt.h" +#include "run_app_vc.h" int main(int argc, char* argv[]) { FLAGS_stderrthreshold = 0; @@ -41,6 +42,8 @@ int main(int argc, char* argv[]) { std::string name = FLAGS_application; if (FLAGS_opt) { grape::RunOpt(); + } else if (FLAGS_vc) { + grape::RunVC(); } else { if (name.find("sssp") != std::string::npos) { grape::Run(); diff --git a/examples/analytical_apps/run_app_vc.h b/examples/analytical_apps/run_app_vc.h new file mode 100644 index 00000000..77439a7b --- /dev/null +++ b/examples/analytical_apps/run_app_vc.h @@ -0,0 +1,81 @@ +/** 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_RUN_APP_VC_H_ +#define EXAMPLES_ANALYTICAL_APPS_RUN_APP_VC_H_ + +#include "grape/fragment/immutable_vertexcut_fragment.h" +#include "grape/fragment/loader.h" +#include "pagerank/pagerank_vc.h" +#include "run_app.h" + +namespace grape { + +template