Skip to content

Commit

Permalink
[IO] Add experimental/freight-netl output format
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSeemaier committed Dec 6, 2023
1 parent c1e0491 commit 17c3edb
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 17 deletions.
21 changes: 9 additions & 12 deletions kagen/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@ std::unordered_map<std::string, FileFormat> GetOutputFormatMap() {
{"plain-edgelist", FileFormat::PLAIN_EDGE_LIST},
{"metis", FileFormat::METIS},
{"hmetis", FileFormat::HMETIS},
{"hmetis-directed", FileFormat::HMETIS_DIRECTED},
{"experimental/hmetis-ep", FileFormat::HMETIS_EP},
{"dot", FileFormat::DOT},
{"dot-directed", FileFormat::DOT_DIRECTED},
{"coordinates", FileFormat::COORDINATES},
{"parhip", FileFormat::PARHIP},
{"xtrapulp", FileFormat::XTRAPULP},

{"experimental/freight-netl", FileFormat::FREIGHT_NETL},
{"experimental/freight-netl-ep", FileFormat::FREIGHT_NETL_EP},
{"experimental/hmetis-ep", FileFormat::HMETIS_EP},

{"none", FileFormat::NOOP}, // @deprecated
{"edge-list", FileFormat::EDGE_LIST}, // @deprecated
{"binary-parhip", FileFormat::PARHIP}, // @deprecated
{"edge-list-undirected", FileFormat::EDGE_LIST_UNDIRECTED}, // @deprecated
{"binary-edge-list", FileFormat::BINARY_EDGE_LIST}, // @deprecated
{"binary-edge-list-undirected", FileFormat::BINARY_EDGE_LIST_UNDIRECTED}, // @deprecated
};
}

Expand Down Expand Up @@ -77,6 +71,9 @@ std::ostream& operator<<(std::ostream& out, FileFormat output_format) {
case FileFormat::HMETIS_DIRECTED:
return out << "hmetis-directed";

case FileFormat::HMETIS_EP:
return out << "experimental/hmetis-ep";

case FileFormat::DOT:
return out << "dot";

Expand All @@ -92,11 +89,11 @@ std::ostream& operator<<(std::ostream& out, FileFormat output_format) {
case FileFormat::XTRAPULP:
return out << "xtrapulp";

case FileFormat::FREIGHT_NETL:
return out << "experimental/freight-netl";

case FileFormat::FREIGHT_NETL_EP:
return out << "experimental/freight-netl-ep";

case FileFormat::HMETIS_EP:
return out << "experimental/hmetis-ep";
}

return out << "<invalid>";
Expand Down
1 change: 1 addition & 0 deletions kagen/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const std::unordered_map<FileFormat, std::unique_ptr<FileFormatFactory>>& GetGra

// Experimental formats
factories[FileFormat::FREIGHT_NETL_EP] = std::make_unique<FreightNetlEpFactory>();
factories[FileFormat::FREIGHT_NETL] = std::make_unique<FreightNetlFactory>();
factories[FileFormat::HMETIS_EP] = std::make_unique<HmetisEpFactory>();
}
return factories;
Expand Down
76 changes: 73 additions & 3 deletions kagen/io/freight-netl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,91 @@

#include "kagen/io/buffered_writer.h"

#include <unordered_map>

namespace kagen {
FreightNetlWriter::FreightNetlWriter(
const OutputGraphConfig& config, Graph& graph, const GraphInfo info, const PEID rank, const PEID size)
: StandardGraphWriter(config, graph, info, rank, size) {
if (size > 1) {
throw IOError("FreightNetlWriter does not support distributed/chunked output");
}
}

void FreightNetlWriter::WriteHeader(const std::string& filename, const SInt n, const SInt m) {
BufferedTextOutput<> out(tag::append, filename);
out.WriteInt(n).WriteChar(' ').WriteInt(m / 2).WriteString(" 11\n").Flush();
}

bool FreightNetlWriter::WriteBody(const std::string& filename) {
graph_.SortEdgelist();

BufferedTextOutput<> out(tag::append, filename);

std::unordered_map<SInt, SInt> nets;

SInt next_net = 0;
auto map_edge_to_net = [&](const SInt u, const SInt v) {
const SInt key = std::min(u, v) * info_.global_n + std::max(u, v);
const auto key_it = nets.find(key);
if (key_it == nets.end()) {
nets[key] = ++next_net;
return next_net;
} else {
return key_it->second;
}
};

SInt cur_edge = 0;
for (SInt from = graph_.vertex_range.first; from < graph_.vertex_range.second; ++from) {
if (info_.has_vertex_weights) {
out.WriteInt(graph_.vertex_weights[from - graph_.vertex_range.first]);
} else {
out.WriteChar('1');
}

while (cur_edge < graph_.edges.size() && std::get<0>(graph_.edges[cur_edge]) == from) {
const SInt to = std::get<1>(graph_.edges[cur_edge]);
const SInt net = map_edge_to_net(from, to);

out.WriteChar(' ').WriteInt(net).WriteChar(' ');

if (info_.has_edge_weights) {
out.WriteInt(graph_.edge_weights[cur_edge]);
} else {
out.WriteChar('1');
}
out.Flush();

++cur_edge;
}

out.WriteChar('\n').Flush();
}

return false;
}

std::unique_ptr<GraphWriter> FreightNetlFactory::CreateWriter(
const OutputGraphConfig& config, Graph& graph, const GraphInfo info, const PEID rank, const PEID size) const {
return std::make_unique<FreightNetlWriter>(config, graph, info, rank, size);
}

FreightNetlEpWriter::FreightNetlEpWriter(
const OutputGraphConfig& config, Graph& graph, const GraphInfo info, const PEID rank, const PEID size)
: StandardGraphWriter(config, graph, info, rank, size) {}

void FreightNetlEpWriter::WriteHeader(const std::string& filename, const SInt n, const SInt m) {
IgnoresEdgeWeights();
IgnoresVertexWeights();

BufferedTextOutput<> out(tag::append, filename);
out.WriteInt(m / 2).WriteChar(' ').WriteInt(n).WriteChar(' ').WriteString("11").WriteChar('\n').Flush();
out.WriteInt(m / 2).WriteChar(' ').WriteInt(n).WriteString(" 11\n").Flush();
}

bool FreightNetlEpWriter::WriteBody(const std::string& filename) {
BufferedTextOutput<> out(tag::append, filename);

// Ignores edge and node weights

for (SInt e = 0; e < graph_.edges.size(); ++e) {
const auto& [from, to] = graph_.edges[e];
if (from >= to) {
Expand Down
20 changes: 20 additions & 0 deletions kagen/io/freight-netl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
#include <string>

namespace kagen {
class FreightNetlWriter : public StandardGraphWriter {
public:
FreightNetlWriter(const OutputGraphConfig& config, Graph& graph, GraphInfo info, PEID rank, PEID size);

protected:
void WriteHeader(const std::string& filename, SInt n, SInt m) final;

bool WriteBody(const std::string& filename) final;
};

class FreightNetlFactory : public FileFormatFactory {
public:
std::vector<std::string> DefaultExtensions() const final {
return {"netl"};
}

std::unique_ptr<GraphWriter> CreateWriter(
const OutputGraphConfig& config, Graph& graph, GraphInfo info, const PEID rank, const PEID size) const final;
};

class FreightNetlEpWriter : public StandardGraphWriter {
public:
FreightNetlEpWriter(const OutputGraphConfig& config, Graph& graph, GraphInfo info, PEID rank, PEID size);
Expand Down
7 changes: 6 additions & 1 deletion kagen/io/hmetis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "kagen/io/buffered_writer.h"

#include <unordered_map>

namespace kagen {
HmetisWriter::HmetisWriter(
const bool directed, const OutputGraphConfig& config, Graph& graph, const GraphInfo info, const PEID rank,
Expand Down Expand Up @@ -67,11 +69,14 @@ HmetisEpWriter::HmetisEpWriter(
const OutputGraphConfig& config, Graph& graph, const GraphInfo info, const PEID rank, const PEID size)
: StandardGraphWriter(config, graph, info, rank, size) {
if (size > 1) {
throw IOError("HmetisEpWriter does not support distributed output");
throw IOError("HmetisEpWriter does not support distributed/chunked output");
}
}

void HmetisEpWriter::WriteHeader(const std::string& filename, const SInt n, const SInt m) {
IgnoresEdgeWeights();
IgnoresVertexWeights();

BufferedTextOutput<> out(tag::append, filename);
out.WriteInt(n).WriteChar(' ').WriteInt(m / 2).WriteChar('\n').Flush();
}
Expand Down
2 changes: 1 addition & 1 deletion kagen/io/hmetis.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class HmetisEpWriter : public StandardGraphWriter {
class HmetisEpFactory : public FileFormatFactory {
public:
std::vector<std::string> DefaultExtensions() const final {
return {"hmetis.ep"};
return {"ep.hmetis"};
}

std::unique_ptr<GraphWriter> CreateWriter(
Expand Down
1 change: 1 addition & 0 deletions kagen/kagen.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ enum class FileFormat {
COORDINATES,
PARHIP,
XTRAPULP,
FREIGHT_NETL,
FREIGHT_NETL_EP,
};

Expand Down

0 comments on commit 17c3edb

Please sign in to comment.