Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelize the PrepareToRun implementation in projected fragment #2949

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 111 additions & 32 deletions analytical_engine/core/fragment/arrow_projected_fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -1117,13 +1117,13 @@ class ArrowProjectedFragment
grape::PrepareConf conf) {
if (conf.message_strategy ==
grape::MessageStrategy::kAlongEdgeToOuterVertex) {
initDestFidList(true, true, iodst_, iodoffset_);
initDestFidList(comm_spec, true, true, iodst_, iodoffset_);
} else if (conf.message_strategy ==
grape::MessageStrategy::kAlongIncomingEdgeToOuterVertex) {
initDestFidList(true, false, idst_, idoffset_);
initDestFidList(comm_spec, true, false, idst_, idoffset_);
} else if (conf.message_strategy ==
grape::MessageStrategy::kAlongOutgoingEdgeToOuterVertex) {
initDestFidList(false, true, odst_, odoffset_);
initDestFidList(comm_spec, false, true, odst_, odoffset_);
}

initOuterVertexRanges();
Expand All @@ -1140,16 +1140,19 @@ class ArrowProjectedFragment
ie_spliters_ptr_.clear();
oe_spliters_ptr_.clear();
if (directed_) {
initEdgeSpliters(ie_, ie_offsets_begin_, ie_offsets_end_, ie_spliters_);
initEdgeSpliters(oe_, oe_offsets_begin_, oe_offsets_end_, oe_spliters_);
initEdgeSpliters(comm_spec, ie_, ie_offsets_begin_, ie_offsets_end_,
ie_spliters_);
initEdgeSpliters(comm_spec, oe_, oe_offsets_begin_, oe_offsets_end_,
oe_spliters_);
for (auto& vec : ie_spliters_) {
ie_spliters_ptr_.push_back(vec.data());
}
for (auto& vec : oe_spliters_) {
oe_spliters_ptr_.push_back(vec.data());
}
} else {
initEdgeSpliters(oe_, oe_offsets_begin_, oe_offsets_end_, oe_spliters_);
initEdgeSpliters(comm_spec, oe_, oe_offsets_begin_, oe_offsets_end_,
oe_spliters_);
for (auto& vec : oe_spliters_) {
ie_spliters_ptr_.push_back(vec.data());
oe_spliters_ptr_.push_back(vec.data());
Expand Down Expand Up @@ -1690,7 +1693,7 @@ class ArrowProjectedFragment
ends[i] = range.second;
}
},
std::thread::hardware_concurrency());
std::thread::hardware_concurrency(), 1024);
return {};
}

Expand Down Expand Up @@ -1721,16 +1724,81 @@ class ArrowProjectedFragment
bends[i] = range.second.second;
}
},
std::thread::hardware_concurrency());
std::thread::hardware_concurrency(), 1024);
return {};
}

void initDestFidList(bool in_edge, bool out_edge,
std::vector<fid_t>& fid_list,
void initDestFidList(const grape::CommSpec& comm_spec, const bool in_edge,
const bool out_edge, std::vector<fid_t>& fid_list,
std::vector<fid_t*>& fid_list_offset) {
if (!fid_list_offset.empty()) {
return;
}
fid_list_offset.resize(ivnum_ + 1, NULL);

int concurrency =
(std::thread::hardware_concurrency() + comm_spec.local_num() - 1) /
comm_spec.local_num();

// don't use std::vector<bool> due to its specialization
std::vector<uint8_t> fid_list_bitmap(ivnum_ * fnum_, 0);
std::atomic_size_t fid_list_size(0);

vineyard::parallel_for(
static_cast<vid_t>(0), static_cast<vid_t>(ivnum_),
[this, in_edge, out_edge, &fid_list_bitmap,
&fid_list_size](const vid_t& offset) {
vertex_t v = *(inner_vertices_.begin() + offset);
if (in_edge) {
auto es = GetIncomingAdjList(v);
fid_t last_fid = -1;
for (auto& e : es) {
fid_t f = GetFragId(e.neighbor());
if (f != last_fid && f != fid_ &&
!fid_list_bitmap[offset * fnum_ + f]) {
last_fid = f;
fid_list_bitmap[offset * fnum_ + f] = 1;
fid_list_size.fetch_add(1);
}
}
}
if (out_edge) {
auto es = GetOutgoingAdjList(v);
fid_t last_fid = -1;
for (auto& e : es) {
fid_t f = GetFragId(e.neighbor());
if (f != last_fid && f != fid_ &&
!fid_list_bitmap[offset * fnum_ + f]) {
last_fid = f;
fid_list_bitmap[offset * fnum_ + f] = 1;
fid_list_size.fetch_add(1);
}
}
}
},
concurrency, 1024);

fid_list.reserve(fid_list_size.load());
fid_list_offset[0] = fid_list.data();

for (vid_t i = 0; i < ivnum_; ++i) {
size_t nonzero = 0;
for (fid_t fid = 0; fid < fnum_; ++fid) {
if (fid_list_bitmap[i * fnum_ + fid]) {
nonzero += 1;
fid_list.push_back(fid);
}
}
fid_list_offset[i + 1] = fid_list_offset[i] + nonzero;
}
}

void initDestFidListSeq(const bool in_edge, const bool out_edge,
std::vector<fid_t>& fid_list,
std::vector<fid_t*>& fid_list_offset) {
if (!fid_list_offset.empty()) {
return;
}

fid_list_offset.resize(ivnum_ + 1, NULL);

Expand Down Expand Up @@ -1773,6 +1841,7 @@ class ArrowProjectedFragment
}

void initEdgeSpliters(
const grape::CommSpec& comm_spec,
const std::shared_ptr<arrow::FixedSizeBinaryArray>& edge_list,
const std::shared_ptr<arrow::Int64Array>& offsets_begin,
const std::shared_ptr<arrow::Int64Array>& offsets_end,
Expand All @@ -1784,28 +1853,38 @@ class ArrowProjectedFragment
for (auto& vec : spliters) {
vec.resize(ivnum_);
}
std::vector<int> frag_count;
for (vid_t i = 0; i < ivnum_; ++i) {
frag_count.clear();
frag_count.resize(fnum_, 0);
int64_t begin = offsets_begin->Value(i);
int64_t end = offsets_end->Value(i);
for (int64_t j = begin; j != end; ++j) {
const nbr_unit_t* nbr_ptr =
reinterpret_cast<const nbr_unit_t*>(edge_list->GetValue(j));
vertex_t u(nbr_ptr->vid);
fid_t u_fid = GetFragId(u);
++frag_count[u_fid];
}
begin += frag_count[fid_];
frag_count[fid_] = 0;
spliters[0][i] = begin;
for (fid_t j = 0; j < fnum_; ++j) {
begin += frag_count[j];
spliters[j + 1][i] = begin;
}
CHECK_EQ(begin, end);
}

int concurrency =
(std::thread::hardware_concurrency() + comm_spec.local_num() - 1) /
comm_spec.local_num();

vineyard::parallel_for(
static_cast<vid_t>(0), ivnum_,
[this, &offsets_begin, &offsets_end, &edge_list,
&spliters](const vid_t i) {
std::vector<int> frag_count(fnum_, 0);
int64_t begin = offsets_begin->Value(i);
int64_t end = offsets_end->Value(i);
for (int64_t j = begin; j != end; ++j) {
const nbr_unit_t* nbr_ptr =
reinterpret_cast<const nbr_unit_t*>(edge_list->GetValue(j));
vertex_t u(nbr_ptr->vid);
fid_t u_fid = GetFragId(u);
++frag_count[u_fid];
}
begin += frag_count[fid_];
frag_count[fid_] = 0;
spliters[0][i] = begin;
for (fid_t j = 0; j < fnum_; ++j) {
begin += frag_count[j];
spliters[j + 1][i] = begin;
}
if (begin != end) {
LOG(ERROR) << "Unexpected edge spliters for ith vertex " << i
<< ", begin: " << begin << " vs. end: " << end;
}
},
concurrency, 1024);
}

void initOuterVertexRanges() {
Expand Down
49 changes: 28 additions & 21 deletions analytical_engine/core/fragment/dynamic_fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ class DynamicFragment
if (conf.need_split_edges_by_fragment) {
LOG(ERROR) << "MutableEdgecutFragment cannot split edges by fragment";
} else if (conf.need_split_edges) {
splitEdges();
splitEdges(comm_spec);
}
}

Expand Down Expand Up @@ -905,30 +905,37 @@ class DynamicFragment
return id_parser_.max_local_id() - index - 1;
}

void splitEdges() {
void splitEdges(const grape::CommSpec& comm_spec) {
auto& inner_vertices = InnerVertices();
iespliter_.Init(inner_vertices);
oespliter_.Init(inner_vertices);
int inner_neighbor_count = 0;
for (auto& v : inner_vertices) {
inner_neighbor_count = 0;
auto ie = GetIncomingAdjList(v);
for (auto& e : ie) {
if (IsInnerVertex(e.neighbor)) {
++inner_neighbor_count;
}
}
iespliter_[v] = get_ie_begin(v) + inner_neighbor_count;

inner_neighbor_count = 0;
auto oe = GetOutgoingAdjList(v);
for (auto& e : oe) {
if (IsInnerVertex(e.neighbor)) {
++inner_neighbor_count;
}
}
oespliter_[v] = get_oe_begin(v) + inner_neighbor_count;
}
int concurrency =
(std::thread::hardware_concurrency() + comm_spec.local_num() - 1) /
comm_spec.local_num();
vineyard::parallel_for(
static_cast<vid_t>(0), static_cast<vid_t>(inner_vertices.size()),
[this, &inner_vertices](const vid_t& offset) {
vertex_t v = *(inner_vertices.begin() + offset);
size_t inner_neighbor_count = 0;
auto ie = GetIncomingAdjList(v);
for (auto& e : ie) {
if (IsInnerVertex(e.neighbor)) {
++inner_neighbor_count;
}
}
iespliter_[v] = get_ie_begin(v) + inner_neighbor_count;

inner_neighbor_count = 0;
auto oe = GetOutgoingAdjList(v);
for (auto& e : oe) {
if (IsInnerVertex(e.neighbor)) {
++inner_neighbor_count;
}
}
oespliter_[v] = get_oe_begin(v) + inner_neighbor_count;
},
concurrency, 1024);
}

vid_t parseOrAddOuterVertexGid(vid_t gid) {
Expand Down
3 changes: 1 addition & 2 deletions analytical_engine/core/fragment/fragment_reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#ifdef NETWORKX

#include <glog/logging.h>

#include <cstddef>
#include <cstdint>
#include <memory>
Expand All @@ -29,6 +27,7 @@

#include "boost/leaf/error.hpp"
#include "boost/leaf/result.hpp"
#include "glog/logging.h"
#include "grape/communication/communicator.h"
#include "grape/serialization/in_archive.h"
#include "grape/worker/comm_spec.h"
Expand Down
11 changes: 11 additions & 0 deletions analytical_engine/core/server/rpc_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ class GSParams {

const rpc::LargeAttrValue& GetLargeAttr() const { return large_attr_; }

const std::string DebugString() const {
std::ostringstream ss;
ss << "GSParams: {";
for (auto const& kv : params_) {
ss << rpc::ParamKey_Name(kv.first) << ": " << kv.second.DebugString()
<< ", ";
}
ss << "}";
return ss.str();
}

private:
const std::map<int, rpc::AttrValue> params_;
const rpc::LargeAttrValue& large_attr_;
Expand Down
Loading