Skip to content

Commit

Permalink
Move code to read graphs from FileGraphGenerator to io.cpp, keep the …
Browse files Browse the repository at this point in the history
…generated graph as graph_ member in Generator
  • Loading branch information
DanielSeemaier committed Nov 30, 2023
1 parent fc17adc commit 669e1ff
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 132 deletions.
2 changes: 1 addition & 1 deletion kagen/generators/barabassi/barabassi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Barabassi::Barabassi(const PGeneratorConfig& config, const PEID rank, const PEID

void Barabassi::FinalizeEdgeList(MPI_Comm comm) {
if (!config_.directed) {
AddNonlocalReverseEdges(edges_, vertex_range_, comm);
AddNonlocalReverseEdges(graph_.edges, graph_.vertex_range, comm);
}
}

Expand Down
90 changes: 11 additions & 79 deletions kagen/generators/file/file_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,106 +30,38 @@ bool FileGraphGenerator::CheckDeficit(const ReaderDeficits deficit) const {
void FileGraphGenerator::GenerateImpl(const GraphRepresentation representation) {
auto reader = CreateGraphReader(config_.input_graph.format, config_.input_graph, rank_, size_);
deficits_ = reader->Deficits();

const auto [n, m] = [&] {
if (CheckDeficit(ReaderDeficits::UNKNOWN_NUM_VERTICES) && CheckDeficit(ReaderDeficits::UNKNOWN_NUM_EDGES)) {
return std::pair<SInt, SInt>{size_, size_};
}

auto [n, m] = reader->ReadSize();
if (CheckDeficit(ReaderDeficits::UNKNOWN_NUM_VERTICES)) {
n = size_;
}
if (CheckDeficit(ReaderDeficits::UNKNOWN_NUM_EDGES)) {
m = size_;
}
return std::pair<SInt, SInt>{n, m};
}();

if (CheckDeficit(ReaderDeficits::REQUIRES_REDISTRIBUTION)
&& config_.input_graph.distribution == GraphDistribution::BALANCE_EDGES) {
throw std::invalid_argument("not implemented");
}

// If we need postprocessing, always generate an edge list because postprocessing is not implemented for CSR
actual_representation_ =
CheckDeficit(ReaderDeficits::REQUIRES_REDISTRIBUTION) ? GraphRepresentation::EDGE_LIST : representation;

SInt from = 0;
SInt to_node = std::numeric_limits<SInt>::max();
SInt to_edge = std::numeric_limits<SInt>::max();

switch (config_.input_graph.distribution) {
case GraphDistribution::BALANCE_VERTICES:
std::tie(from, to_node) = ComputeRange(n, size_, rank_);
break;

case GraphDistribution::BALANCE_EDGES: {
const auto edge_range = ComputeRange(m, size_, rank_);
from = reader->FindNodeByEdge(edge_range.first);
to_edge = edge_range.second;
break;
}
}

auto graph = reader->Read(from, to_node, to_edge, actual_representation_);

vertex_range_ = graph.vertex_range;
xadj_ = std::move(graph.xadj);
adjncy_ = std::move(graph.adjncy);
edges_ = std::move(graph.edges);
edge_weights_ = std::move(graph.edge_weights);
vertex_weights_ = std::move(graph.vertex_weights);
coordinates_ = std::move(graph.coordinates);
graph_ = ReadGraph(*reader, representation, config_.input_graph, rank_, size_);
}

void FileGraphGenerator::FinalizeEdgeList(MPI_Comm comm) {
if (actual_representation_ == GraphRepresentation::CSR) {
if (graph_.representation == GraphRepresentation::CSR) {
FinalizeCSR(comm);

if (Output()) {
std::cout << "converting to edge list ... " << std::flush;
}

edges_ = BuildEdgeListFromCSR(vertex_range_, xadj_, adjncy_);
{
[[maybe_unused]] auto free_xadj = std::move(xadj_);
[[maybe_unused]] auto free_adjncy = std::move(adjncy_);
}
graph_.edges = BuildEdgeListFromCSR(graph_.vertex_range, graph_.xadj, graph_.adjncy);
graph_.FreeCSR();
}

if (CheckDeficit(ReaderDeficits::REQUIRES_REDISTRIBUTION)) {
if (Output()) {
std::cout << "redistributing edges ... " << std::flush;
}

const SInt n = [&] {
SInt n = 0;
if (CheckDeficit(ReaderDeficits::UNKNOWN_NUM_VERTICES)) {
n = FindNumberOfVerticesInEdgelist(edges_, comm);
} else {
n = vertex_range_.second;
MPI_Bcast(&n, 1, KAGEN_MPI_SINT, size_ - 1, comm);
}
return n;
}();

std::tie(vertex_range_.first, vertex_range_.second) = ComputeRange(n, size_, rank_);
RedistributeEdgesByVertexRange(edges_, vertex_range_, comm);
}
graph_ = FinalizeReadGraph(deficits_, std::move(graph_), Output(), comm);
}

void FileGraphGenerator::FinalizeCSR(MPI_Comm comm) {
if (actual_representation_ == GraphRepresentation::EDGE_LIST) {
if (graph_.representation == GraphRepresentation::EDGE_LIST) {
FinalizeEdgeList(comm);

if (Output()) {
std::cout << "converting to CSR ... " << std::flush;
}

std::tie(xadj_, adjncy_) = BuildCSRFromEdgeList(vertex_range_, edges_, edge_weights_);
{ [[maybe_unused]] auto free_edges = std::move(edges_); }
std::tie(graph_.xadj, graph_.adjncy) =
BuildCSRFromEdgeList(graph_.vertex_range, graph_.edges, graph_.edge_weights);
graph_.FreeEdgelist();
}

graph_ = FinalizeReadGraph(deficits_, std::move(graph_), Output(), comm);
}

bool FileGraphGenerator::Output() const {
Expand Down
3 changes: 1 addition & 2 deletions kagen/generators/file/file_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class FileGraphGenerator : public Generator {
PEID rank_;
PEID size_;

int deficits_;
GraphRepresentation actual_representation_;
int deficits_;
};
} // namespace kagen
48 changes: 17 additions & 31 deletions kagen/generators/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ Generator::~Generator() = default;
void Generator::Generate(const GraphRepresentation representation) {
Reset();

representation_ = representation;
graph_.representation = representation;

switch (representation_) {
switch (graph_.representation) {
case GraphRepresentation::EDGE_LIST:
GenerateEdgeList();
break;
Expand All @@ -29,7 +29,7 @@ void Generator::Generate(const GraphRepresentation representation) {
}

void Generator::Finalize(MPI_Comm comm) {
switch (representation_) {
switch (graph_.representation) {
case GraphRepresentation::EDGE_LIST:
FinalizeEdgeList(comm);
break;
Expand All @@ -49,21 +49,21 @@ void CSROnlyGenerator::GenerateEdgeList() {
}

void CSROnlyGenerator::FinalizeEdgeList(MPI_Comm comm) {
if (xadj_.empty()) {
if (graph_.xadj.empty()) {
return;
}

// Otherwise, we have generated the graph in CSR representation, but
// actually want edge list representation -> transform graph
FinalizeCSR(comm);
edges_ = BuildEdgeListFromCSR(vertex_range_, xadj_, adjncy_);
graph_.edges = BuildEdgeListFromCSR(graph_.vertex_range, graph_.xadj, graph_.adjncy);
{
XadjArray tmp;
std::swap(xadj_, tmp);
std::swap(graph_.xadj, tmp);
}
{
AdjncyArray tmp;
std::swap(adjncy_, tmp);
std::swap(graph_.adjncy, tmp);
}
}

Expand All @@ -72,54 +72,40 @@ void EdgeListOnlyGenerator::GenerateCSR() {
}

void EdgeListOnlyGenerator::FinalizeCSR(MPI_Comm comm) {
if (!xadj_.empty()) {
if (!graph_.xadj.empty()) {
return;
}

// Otherwise, we have generated the graph in edge list representation, but
// actually want CSR format --> transform graph
FinalizeEdgeList(comm);
std::tie(xadj_, adjncy_) = BuildCSRFromEdgeList(vertex_range_, edges_, edge_weights_);
std::tie(graph_.xadj, graph_.adjncy) = BuildCSRFromEdgeList(graph_.vertex_range, graph_.edges, graph_.edge_weights);
{
Edgelist tmp;
std::swap(edges_, tmp);
std::swap(graph_.edges, tmp);
}
}

SInt Generator::GetNumberOfEdges() const {
return std::max(adjncy_.size(), edges_.size());
return std::max(graph_.adjncy.size(), graph_.edges.size());
}

Graph Generator::Take() {
return {
vertex_range_,
representation_,
std::move(edges_),
std::move(xadj_),
std::move(adjncy_),
std::move(vertex_weights_),
std::move(edge_weights_),
std::move(coordinates_)};
return std::move(graph_);
}

void Generator::SetVertexRange(const VertexRange vertex_range) {
vertex_range_ = vertex_range;
graph_.vertex_range = vertex_range;
}

void Generator::FilterDuplicateEdges() {
std::sort(edges_.begin(), edges_.end());
auto it = std::unique(edges_.begin(), edges_.end());
edges_.erase(it, edges_.end());
std::sort(graph_.edges.begin(), graph_.edges.end());
auto it = std::unique(graph_.edges.begin(), graph_.edges.end());
graph_.edges.erase(it, graph_.edges.end());
}

void Generator::Reset() {
edges_.clear();
xadj_.clear();
adjncy_.clear();
vertex_weights_.clear();
edge_weights_.clear();
coordinates_.first.clear();
coordinates_.second.clear();
graph_.Clear();
}

GeneratorFactory::~GeneratorFactory() = default;
Expand Down
22 changes: 7 additions & 15 deletions kagen/generators/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,35 @@ class Generator {
void SetVertexRange(VertexRange vetrex_range);

inline void PushCoordinate(const HPFloat x, const HPFloat y) {
coordinates_.first.emplace_back(x, y);
graph_.coordinates.first.emplace_back(x, y);
}

inline void PushCoordinate(const HPFloat x, const HPFloat y, const HPFloat z) {
coordinates_.second.emplace_back(x, y, z);
graph_.coordinates.second.emplace_back(x, y, z);
}

inline void PushVertexWeight(const SSInt weight) {
vertex_weights_.push_back(weight);
graph_.vertex_weights.push_back(weight);
}

inline void PushEdge(const SInt from, const SInt to) {
edges_.emplace_back(from, to);
graph_.edges.emplace_back(from, to);
}

inline void PushEdgeWeight(const SSInt weight) {
edge_weights_.push_back(weight);
graph_.edge_weights.push_back(weight);
}

inline void SetVertexRange(const SInt first_vertex, const SInt first_invalid_vertex) {
vertex_range_ = std::make_pair(first_vertex, first_invalid_vertex);
graph_.vertex_range = std::make_pair(first_vertex, first_invalid_vertex);
}

void FilterDuplicateEdges();

VertexRange vertex_range_;
Edgelist edges_;
XadjArray xadj_;
AdjncyArray adjncy_;
Coordinates coordinates_;
VertexWeights vertex_weights_;
EdgeWeights edge_weights_;
Graph graph_;

private:
void Reset();

GraphRepresentation representation_;
};

class ConfigurationError : public std::exception {
Expand Down
6 changes: 3 additions & 3 deletions kagen/generators/graph500_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace kagen {
Graph500Generator::Graph500Generator(const PGeneratorConfig& config) : config_(config) {}

void Graph500Generator::FinalizeEdgeList(MPI_Comm comm) {
const SInt log_n = std::log2(config_.n);
const SInt n = 1ull << log_n;
vertex_range_ = RedistributeEdgesRoundRobin(local_edges_, edges_, n, comm);
const SInt log_n = std::log2(config_.n);
const SInt n = 1ull << log_n;
graph_.vertex_range = RedistributeEdgesRoundRobin(local_edges_, graph_.edges, n, comm);
}
} // namespace kagen
2 changes: 1 addition & 1 deletion kagen/generators/hyperbolic/hyperbolic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Hyperbolic<Double>::Hyperbolic(const PGeneratorConfig& config, const PEID rank,

template <typename Double>
void Hyperbolic<Double>::FinalizeEdgeList(MPI_Comm comm) {
AddNonlocalReverseEdges(edges_, vertex_range_, comm);
AddNonlocalReverseEdges(graph_.edges, graph_.vertex_range, comm);
}

template <typename Double>
Expand Down
Loading

0 comments on commit 669e1ff

Please sign in to comment.