Skip to content
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
13 changes: 13 additions & 0 deletions include/gdsb/graph_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@ std::tuple<Vertex64, uint64_t> read_binary_graph_partition(std::ifstream& input,
return std::make_tuple(data.vertex_count, edge_count);
}

namespace binary
{

void read(std::ifstream&, gdsb::Edge32&);

void read(std::ifstream&, gdsb::WeightedEdge32&);

void read(std::ifstream&, gdsb::TimestampedEdge32&);

void read(std::ifstream&, gdsb::WeightedTimestampedEdge32&);

} // namespace binary

template <typename Vertex, typename Label, typename F> void read_labels(std::istream& ins, F&& emplace)
{
std::string line;
Expand Down
17 changes: 17 additions & 0 deletions include/gdsb/mpi_graph_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ std::tuple<Vertex64, uint64_t> all_read_binary_graph_partition(MPI_File const in
return std::make_tuple(data.vertex_count, edge_count);
}

namespace binary
{

// Returned boolean (of all read functions) indicate if anything went wrong
// during reading from input. Such read errors may also early return from the
// function. Thus, if the returned value is false, all written data to e (edge)
// may be invalid.
bool read(MPI_File const input, gdsb::Edge32& e);

bool read(MPI_File const input, gdsb::WeightedEdge32& e);

bool read(MPI_File const input, gdsb::TimestampedEdge32& e);

bool read(MPI_File const input, gdsb::WeightedTimestampedEdge32& e);

} // namespace binary

class MPIDataTypeAdapter
{
public:
Expand Down
33 changes: 33 additions & 0 deletions src/graph_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,37 @@ float read_float(char const* source, char** end)
return value;
}

namespace binary
{

void read(std::ifstream& input, Edge32& e)
{
input.read(reinterpret_cast<char*>(&e.source), sizeof(Vertex32));
input.read(reinterpret_cast<char*>(&e.target), sizeof(Vertex32));
}

void read(std::ifstream& input, gdsb::WeightedEdge32& e)
{
input.read(reinterpret_cast<char*>(&e.source), sizeof(Vertex32));
input.read(reinterpret_cast<char*>(&e.target.vertex), sizeof(Vertex32));
input.read(reinterpret_cast<char*>(&e.target.weight), sizeof(Weight));
}

void read(std::ifstream& input, gdsb::TimestampedEdge32& e)
{
input.read(reinterpret_cast<char*>(&e.edge.source), sizeof(Vertex32));
input.read(reinterpret_cast<char*>(&e.edge.target), sizeof(Vertex32));
input.read(reinterpret_cast<char*>(&e.timestamp), sizeof(Timestamp32));
}

void read(std::ifstream& input, gdsb::WeightedTimestampedEdge32& e)
{
input.read(reinterpret_cast<char*>(&e.edge.source), sizeof(Vertex32));
input.read(reinterpret_cast<char*>(&e.edge.target.vertex), sizeof(Vertex32));
input.read(reinterpret_cast<char*>(&e.edge.target.weight), sizeof(Weight));
input.read(reinterpret_cast<char*>(&e.timestamp), sizeof(Timestamp32));
}

} // namespace binary

} // namespace gdsb
78 changes: 78 additions & 0 deletions src/mpi_graph_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,83 @@ MPIWeightedTimestampedEdge32::MPIWeightedTimestampedEdge32()

MPI_Datatype MPIWeightedTimestampedEdge32::get() const { return m_type; }

namespace binary
{

// For every call to MPI_File_read() we pass MPI_STATUS_IGNORE since we do not
// investigate any issues using the status but the returned error codes.
bool read(MPI_File const input, gdsb::Edge32& e)
{
int ec = MPI_File_read(input, &e.source, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
if (ec != MPI_SUCCESS)
{
return false;
}

ec = MPI_File_read(input, &e.target, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
return ec == MPI_SUCCESS;
}

bool read(MPI_File const input, gdsb::WeightedEdge32& e)
{
int ec = MPI_File_read(input, &e.source, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
if (ec != MPI_SUCCESS)
{
return false;
}

ec = MPI_File_read(input, &e.target.vertex, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
if (ec != MPI_SUCCESS)
{
return false;
}

ec = MPI_File_read(input, &e.target.weight, 1, MPI_FLOAT, MPI_STATUS_IGNORE);
return ec == MPI_SUCCESS;
}

bool read(MPI_File const input, gdsb::TimestampedEdge32& e)
{
int ec = MPI_File_read(input, &e.edge.source, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
if (ec != MPI_SUCCESS)
{
return false;
}

ec = MPI_File_read(input, &e.edge.target, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
if (ec != MPI_SUCCESS)
{
return false;
}

ec = MPI_File_read(input, &e.timestamp, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
return ec == MPI_SUCCESS;
}

bool read(MPI_File const input, gdsb::WeightedTimestampedEdge32& e)
{
int ec = MPI_File_read(input, &e.edge.source, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
if (ec != MPI_SUCCESS)
{
return false;
}

ec = MPI_File_read(input, &e.edge.target.vertex, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
if (ec != MPI_SUCCESS)
{
return false;
}

ec = MPI_File_read(input, &e.edge.target.weight, 1, MPI_FLOAT, MPI_STATUS_IGNORE);
if (ec != MPI_SUCCESS)
{
return false;
}

ec = MPI_File_read(input, &e.timestamp, 1, MPI_INT32_T, MPI_STATUS_IGNORE);
return ec == MPI_SUCCESS;
}
} // namespace binary

} // namespace mpi
} // namespace gdsb
2 changes: 1 addition & 1 deletion test/batcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TEST_CASE("Batcher")

TEST_CASE("partition_batch_count, on enzymes graph")
{
std::ifstream binary_graph(graph_path + unweighted_directed_graph_enzymes_bin);
std::ifstream binary_graph(graph_path + directed_unweighted_graph_enzymes_bin);
BinaryGraphHeader header = read_binary_graph_header(binary_graph);

SECTION("partition size 2")
Expand Down
70 changes: 68 additions & 2 deletions test/graph_input_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ TEST_CASE("read_graph, floating point weights")
}
}

TEST_CASE("read_graph, loops")
TEST_CASE("read_graph", "loops, ia southernwoman graph")
{
Edges32 edges;
auto emplace = [&](Vertex32 const u, Vertex32 const v) { edges.push_back({ u, v }); };
Expand Down Expand Up @@ -507,6 +507,72 @@ TEST_CASE("read_graph, market_matrix")
}
}

TEST_CASE("read", "binary")
{
SECTION("Edge32, enzymes graph")
{
std::filesystem::path file_path(graph_path + directed_unweighted_graph_enzymes_bin);
std::ifstream binary_graph{ file_path };

BinaryGraphHeader header = read_binary_graph_header(binary_graph);

gdsb::Edge32 e;
binary::read(binary_graph, e);

CHECK(e.source == 2u);
CHECK(e.target == 1u);
}

SECTION("WeightedEdge32, songbird social graph")
{
std::filesystem::path file_path(graph_path + undirected_weighted_aves_songbird_social_bin);
std::ifstream binary_graph{ file_path };

BinaryGraphHeader header = read_binary_graph_header(binary_graph);

gdsb::WeightedEdge32 e;
binary::read(binary_graph, e);

// First edge should be {1, 2, 0.0735930735931}
CHECK(e.source == 1u);
CHECK(e.target.vertex == 2u);
CHECK(e.target.weight == float(0.0735930735931));
}

SECTION("TimestampedEdge32, reptilia tortoise graph")
{
std::filesystem::path file_path(graph_path + undirected_unweighted_temporal_reptilia_tortoise_bin);
std::ifstream binary_graph{ file_path };

BinaryGraphHeader header = read_binary_graph_header(binary_graph);

gdsb::TimestampedEdge32 e;
binary::read(binary_graph, e);

// First edge should be {1, 2}, {2005}
CHECK(e.edge.source == 1u);
CHECK(e.edge.target == 2u);
CHECK(e.timestamp == 2005u);
}

SECTION("WeightedTimestampedEdge32, small weighted temporal graph")
{
std::filesystem::path file_path(graph_path + small_weighted_temporal_graph_bin);
std::ifstream binary_graph{ file_path };

BinaryGraphHeader header = read_binary_graph_header(binary_graph);

gdsb::WeightedTimestampedEdge32 e;
binary::read(binary_graph, e);

// First edge should be {0, 1, 1}, {1}
CHECK(e.edge.source == 0u);
CHECK(e.edge.target.vertex == 1u);
CHECK(e.edge.target.weight == float(1.));
CHECK(e.timestamp == 1u);
}
}

TEST_CASE("read_binary_graph, small weighted temporal")
{
WeightedTimestampedEdges32 timestamped_edges;
Expand Down Expand Up @@ -607,7 +673,7 @@ TEST_CASE("read_binary_graph, undirected, unweighted, static")
return true;
};

std::ifstream binary_graph(graph_path + unweighted_directed_graph_enzymes_bin);
std::ifstream binary_graph(graph_path + directed_unweighted_graph_enzymes_bin);

BinaryGraphHeader header = read_binary_graph_header(binary_graph);
REQUIRE(header.vertex_id_byte_size == sizeof(Vertex32));
Expand Down
Loading