Skip to content

Commit

Permalink
add support for matrix market (.mtx) input graphs.
Browse files Browse the repository at this point in the history
includes tests
  • Loading branch information
sbeamer committed Dec 10, 2015
1 parent 2019af8 commit d9ff8a3
Show file tree
Hide file tree
Showing 6 changed files with 600 additions and 9 deletions.
89 changes: 81 additions & 8 deletions src/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <string>
#include <type_traits>

#include "util.h"
Expand Down Expand Up @@ -83,7 +83,7 @@ class Reader {
return el;
}

// converts vertex numbering from 1..N to 0..N-1
// Note: converts vertex numbering from 1..N to 0..N-1
EdgeList ReadInMetis(std::ifstream &in, bool &needs_weights) {
EdgeList el;
NodeID_ num_nodes, num_edges;
Expand Down Expand Up @@ -123,17 +123,13 @@ class Reader {
std::istringstream edge_stream(line);
if (read_weights) {
NodeWeight<NodeID_, WeightT_> v;
while (!edge_stream.eof()) {
edge_stream >> v;
edge_stream >> std::ws;
while (edge_stream >> v >> std::ws) {
v.v -= 1;
el.push_back(Edge(u, v));
}
} else {
NodeID_ v;
while (!edge_stream.eof()) {
edge_stream >> v;
edge_stream >> std::ws;
while (edge_stream >> v >> std::ws) {
el.push_back(Edge(u, v - 1));
}
}
Expand All @@ -145,6 +141,81 @@ class Reader {
return el;
}

// Note: converts vertex numbering from 1..N to 0..N-1
// Note: weights casted to type WeightT_
EdgeList ReadInMTX(std::ifstream &in, bool &needs_weights) {
EdgeList el;
std::string start, object, format, field, symmetry, line;
in >> start >> object >> format >> field >> symmetry >> std::ws;
if (start != "%%MatrixMarket") {
std::cout << ".mtx file did not start with %%MatrixMarket" << std::endl;
std::exit(-21);
}
if ((object != "matrix") || (format != "coordinate")) {
std::cout << "only allow matrix coordinate format for .mtx" << std::endl;
std::exit(-22);
}
if (field == "complex") {
std::cout << "do not support complex weights for .mtx" << std::endl;
std::exit(-23);
}
bool read_weights;
if (field == "pattern") {
read_weights = false;
} else if ((field == "real") || (field == "double") ||
(field == "integer")) {
read_weights = true;
} else {
std::cout << "unrecognized field type for .mtx" << std::endl;
std::exit(-24);
}
bool undirected;
if (symmetry == "symmetric") {
undirected = true;
} else if ((symmetry == "general") || (symmetry == "skew-symmetric")) {
undirected = false;
} else {
std::cout << "unsupported symmetry type for .mtx" << std::endl;
std::exit(-25);
}
while (true) {
char c = in.peek();
if (c == '%') {
in.ignore(200, '\n');
} else {
break;
}
}
int64_t m, n, nonzeros;
in >> m >> n >> nonzeros >> std::ws;
if (m != n) {
std::cout << m << " " << n << " " << nonzeros << std::endl;
std::cout << "matrix must be square for .mtx" << std::endl;
std::exit(-26);
}
while (std::getline(in, line)) {
std::istringstream edge_stream(line);
NodeID_ u;
edge_stream >> u;
if (read_weights) {
NodeWeight<NodeID_, WeightT_> v;
edge_stream >> v;
v.v -= 1;
el.push_back(Edge(u - 1, v));
if (undirected)
el.push_back(Edge(v, u - 1));
} else {
NodeID_ v;
edge_stream >> v;
el.push_back(Edge(u - 1, v - 1));
if (undirected)
el.push_back(Edge(v - 1, u - 1));
}
}
needs_weights = !read_weights;
return el;
}

EdgeList ReadFile(bool &needs_weights) {
Timer t;
t.Start();
Expand All @@ -165,6 +236,8 @@ class Reader {
el = ReadInGR(file);
} else if (suffix == ".graph") {
el = ReadInMetis(file, needs_weights);
} else if (suffix == ".mtx") {
el = ReadInMTX(file, needs_weights);
} else {
std::cout << "Unrecognized suffix: " << suffix << std::endl;
std::exit(-3);
Expand Down
258 changes: 258 additions & 0 deletions test/graphs/4.mtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
%%MatrixMarket matrix coordinate pattern general
14 14 256
1 5
1 13
1 14
1 9
1 13
1 9
1 10
1 1
1 3
1 8
1 1
1 2
1 8
1 1
1 2
1 12
1 8
1 4
1 14
1 8
1 14
1 3
1 8
1 3
1 4
1 10
1 9
1 14
1 8
1 8
1 5
1 14
1 8
1 14
1 3
1 5
1 8
1 10
1 1
1 1
1 5
1 9
1 1
1 5
1 12
1 10
1 5
1 5
1 10
1 4
1 1
1 9
1 1
1 4
1 10
1 12
1 1
1 8
1 5
1 1
1 1
1 1
1 4
1 12
1 8
1 9
1 10
1 10
1 1
1 8
1 9
1 9
1 9
1 1
1 14
1 8
1 9
1 14
1 1
1 11
1 14
1 13
1 2
1 11
1 8
1 9
1 8
1 9
1 1
1 13
1 14
1 8
1 10
1 7
1 9
1 9
1 13
1 3
1 9
1 14
1 1
1 5
1 14
1 8
1 2
1 7
1 9
1 10
1 1
1 9
1 4
1 10
1 11
1 1
1 4
1 10
1 5
1 5
1 3
1 14
1 14
1 3
1 13
1 5
1 9
1 8
1 1
1 8
1 8
1 14
1 1
1 13
1 1
1 14
1 1
1 14
1 14
1 10
1 1
1 9
1 1
1 13
1 4
1 1
1 9
1 8
1 9
1 1
1 3
1 14
1 11
1 14
1 10
3 10
3 7
3 2
3 11
3 2
3 5
4 10
4 13
4 14
4 10
4 5
4 14
4 14
4 3
5 7
8 5
8 14
8 2
8 3
8 10
8 14
8 12
8 10
8 2
8 2
8 5
8 2
8 3
8 14
8 10
8 12
8 14
8 14
8 8
8 11
8 13
8 4
8 13
9 8
9 10
9 13
9 14
9 4
9 14
9 10
9 14
9 8
9 8
9 10
9 14
9 10
9 14
9 14
9 8
9 8
9 14
9 14
9 13
9 4
9 8
9 9
9 4
9 3
9 8
9 3
9 8
9 10
9 8
9 12
9 9
9 2
9 5
9 8
9 8
9 14
10 12
10 11
10 5
10 11
10 10
10 2
10 7
10 10
10 5
11 2
13 2
14 14
14 3
14 10
14 10
14 5
14 5
14 5
14 14
14 10
14 14
14 5
14 11
14 14
14 10
14 10
14 10
14 12
Loading

0 comments on commit d9ff8a3

Please sign in to comment.