-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixmarket.hpp
377 lines (305 loc) · 11.3 KB
/
matrixmarket.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#pragma once
#include <utility>
#include <string>
#include <cstdio>
#include <cassert>
#include <vector>
#include <tuple>
#include <fstream>
#include <sstream>
#include <deque>
#include <algorithm>
namespace MatrixMarket {
///////////////////////////////////////////////////////////////////////////////
// Public API
///////////////////////////////////////////////////////////////////////////////
template<typename CoordType, typename ValueType>
struct CSRMatrix {
CoordType num_rows;
CoordType num_cols;
CoordType num_nonzeros;
std::vector<CoordType> row_offsets;
std::vector<CoordType> col_indices;
std::vector<ValueType> values;
};
template<typename CoordType, typename ValueType>
CSRMatrix<CoordType,ValueType> read_csr(const char* filename);
template<typename CoordType, typename ValueType>
struct CSCMatrix {
CoordType num_rows;
CoordType num_cols;
CoordType num_nonzeros;
std::vector<CoordType> col_offsets;
std::vector<CoordType> row_indices;
std::vector<ValueType> values;
};
template<typename CoordType, typename ValueType>
CSCMatrix<CoordType,ValueType> read_csc(const char* filename);
///////////////////////////////////////////////////////////////////////////////
// Utility struct for the header
///////////////////////////////////////////////////////////////////////////////
enum class SymmetryType { GENERAL, SYMMETRIC };
enum class ValueFormat { REAL, INTEGER, PATTERN };
template<typename CoordType>
struct Header {
SymmetryType symmetry;
ValueFormat value_type;
CoordType num_rows;
CoordType num_cols;
CoordType num_nonzeros;
};
///////////////////////////////////////////////////////////////////////////////
// Utility Token class
///////////////////////////////////////////////////////////////////////////////
class Tokens {
public:
Tokens(std::string& str, char sep) {
std::istringstream iss(str);
std::string token;
while (std::getline(iss, token, sep)) {
tokens.push_back(token);
}
}
std::string pop() {
assert(!tokens.empty());
auto token = tokens.front();
tokens.pop_front();
return token;
}
std::string& peek() {
assert(!tokens.empty());
return tokens.front();
}
size_t size() {
return tokens.size();
}
private:
std::deque<std::string> tokens;
};
///////////////////////////////////////////////////////////////////////////////
// Utility functions
///////////////////////////////////////////////////////////////////////////////
inline ValueFormat parse_value_fmt(std::string value_fmt_string) {
if (value_fmt_string == "real") {
return ValueFormat::REAL;
} else if (value_fmt_string == "integer") {
return ValueFormat::INTEGER;
} else if (value_fmt_string == "pattern") {
return ValueFormat::PATTERN;
} else {
throw std::invalid_argument("Bad Header: unknown value format");
}
}
inline SymmetryType parse_symmetry(std::string symmetry_string) {
if (symmetry_string == "general") {
return SymmetryType::GENERAL;
} else if (symmetry_string == "symmetric") {
return SymmetryType::SYMMETRIC;
} else {
throw std::invalid_argument("Bad Header: unknown symmetry");
}
}
template<typename IntType>
IntType parse_int(std::string int_string) {
std::istringstream iss(int_string);
IntType int_val;
iss >> int_val;
return int_val;
}
template<typename NumType>
NumType parse_num(std::string num_string) {
std::istringstream iss(num_string);
NumType num_val;
iss >> num_val;
return num_val;
}
template<typename CoordType>
Header<CoordType> read_header(std::ifstream& f) {
assert(f.is_open());
std::string line;
std::getline(f, line);
auto format_tokens = Tokens(line, ' ');
if (format_tokens.size() != 5) {
throw std::invalid_argument("Bad Header: ill-shaped format line");
}
if (format_tokens.pop() != "%%MatrixMarket") {
throw std::invalid_argument("Bad Header: missing %%%%MatrixMarket");
}
if (format_tokens.pop() != "matrix") {
throw std::invalid_argument("Bad Header: only matrix supported");
}
if (format_tokens.pop() != "coordinate") {
throw std::invalid_argument("Bad Header: only coordinate supported");
}
auto value_fmt = parse_value_fmt(format_tokens.pop());
auto symmetry = parse_symmetry(format_tokens.pop());
do {
std::getline(f, line);
} while (line[0] == '%');
auto mtx_size_tokens = Tokens(line, ' ');
if (mtx_size_tokens.size() != 3) {
throw std::invalid_argument("Bad Header: missing matrix size");
}
auto num_rows = parse_int<CoordType>(mtx_size_tokens.pop());
auto num_cols = parse_int<CoordType>(mtx_size_tokens.pop());
auto num_nonzeros = parse_int<CoordType>(mtx_size_tokens.pop());
return Header<CoordType>{
symmetry,
value_fmt,
num_rows,
num_cols,
num_nonzeros
};
}
///////////////////////////////////////////////////////////////////////////////
// Read CSR
///////////////////////////////////////////////////////////////////////////////
template <typename CoordType, typename ValueType>
struct Nonzero {
CoordType row;
CoordType col;
ValueType value;
};
template<typename CoordType, typename ValueType>
CSRMatrix<CoordType,ValueType> read_csr(const char* filename) {
static_assert(std::is_integral<CoordType>::value, "CoordType must be integral");
static_assert(std::is_arithmetic<ValueType>::value, "ValueType must be arithmetic");
std::ifstream infile(filename);
if (!infile.is_open()) {
throw new std::invalid_argument("Could not open file for reading");
}
auto header = read_header<CoordType>(infile);
// Read in and populate the non-zeros in "COO format"
using NonzeroType = Nonzero<CoordType,ValueType>;
std::vector<NonzeroType> nonzeros;
for (CoordType i = 0; i < header.num_nonzeros; i++) {
std::string line;
std::getline(infile, line);
auto tokens = Tokens(line, ' ');
if (header.value_type == ValueFormat::PATTERN && tokens.size() != 2) {
throw std::invalid_argument("Bad Matrix: ill-shaped pattern line");
} else if (header.value_type != ValueFormat::PATTERN && tokens.size() != 3) {
throw std::invalid_argument("Bad Matrix: ill-shaped value line");
}
auto row = parse_int<CoordType>(tokens.pop());
auto col = parse_int<CoordType>(tokens.pop());
auto value = header.value_type == ValueFormat::PATTERN ? 1 : parse_num<ValueType>(tokens.pop());
if (row < 1 || row > header.num_rows) {
throw std::invalid_argument("Bad Matrix: row out of bounds");
}
if (col < 1 || col > header.num_cols) {
throw std::invalid_argument("Bad Matrix: col out of bounds");
}
// Fix the 1-indexing
row--;
col--;
nonzeros.push_back(Nonzero<CoordType,ValueType>{row, col, value});
if (header.symmetry == SymmetryType::SYMMETRIC && row != col) {
nonzeros.push_back(Nonzero<CoordType,ValueType>{col, row, value});
}
}
// nonzeros is now a COO representation of the matrix
// the remaining code converts it to CSR
std::sort(nonzeros.begin(), nonzeros.end(),
[](const NonzeroType& a, const NonzeroType& b) {
return std::tie(a.row, a.col) < std::tie(b.row, b.col);
});
std::vector<CoordType> row_offsets;
std::vector<CoordType> col_indices;
std::vector<ValueType> values;
row_offsets.push_back(0);
CoordType current_row = 0;
for (const auto& nz : nonzeros) {
while (current_row < nz.row) {
row_offsets.push_back(static_cast<CoordType>(col_indices.size()));
++current_row;
}
col_indices.push_back(nz.col);
values.push_back(nz.value);
}
while (current_row < header.num_rows) {
row_offsets.push_back(static_cast<CoordType>(col_indices.size()));
current_row++;
}
return CSRMatrix<CoordType, ValueType>{
header.num_rows,
header.num_cols,
static_cast<CoordType>(values.size()),
std::move(row_offsets),
std::move(col_indices),
std::move(values)
};
}
///////////////////////////////////////////////////////////////////////////////
template<typename CoordType, typename ValueType>
CSCMatrix<CoordType,ValueType> read_csc(const char* filename) {
static_assert(std::is_integral<CoordType>::value, "CoordType must be integral");
static_assert(std::is_arithmetic<ValueType>::value, "ValueType must be arithmetic");
std::ifstream infile(filename);
if (!infile.is_open()) {
throw new std::invalid_argument("Could not open file for reading");
}
auto header = read_header<CoordType>(infile);
// Read in and populate the non-zeros in "COO format"
using NonzeroType = Nonzero<CoordType,ValueType>;
std::vector<NonzeroType> nonzeros;
for (CoordType i = 0; i < header.num_nonzeros; i++) {
std::string line;
std::getline(infile, line);
auto tokens = Tokens(line, ' ');
if (header.value_type == ValueFormat::PATTERN && tokens.size() != 2) {
throw std::invalid_argument("Bad Matrix: ill-shaped pattern line");
} else if (header.value_type != ValueFormat::PATTERN && tokens.size() != 3) {
throw std::invalid_argument("Bad Matrix: ill-shaped value line");
}
auto row = parse_int<CoordType>(tokens.pop());
auto col = parse_int<CoordType>(tokens.pop());
auto value = header.value_type == ValueFormat::PATTERN ? 1 : parse_num<ValueType>(tokens.pop());
if (row < 1 || row > header.num_rows) {
throw std::invalid_argument("Bad Matrix: row out of bounds");
}
if (col < 1 || col > header.num_cols) {
throw std::invalid_argument("Bad Matrix: col out of bounds");
}
// Fix the 1-indexing
row--;
col--;
nonzeros.push_back(Nonzero<CoordType,ValueType>{row, col, value});
if (header.symmetry == SymmetryType::SYMMETRIC && row != col) {
nonzeros.push_back(Nonzero<CoordType,ValueType>{col, row, value});
}
}
// nonzeros is now a COO representation of the matrix
// the remaining code converts it to CSR
std::sort(nonzeros.begin(), nonzeros.end(),
[](const NonzeroType& a, const
NonzeroType& b) {
return std::tie(a.col, a.row) < std::tie(b.col, b.row);
});
std::vector<CoordType> col_offsets;
std::vector<CoordType> row_indices;
std::vector<ValueType> values;
col_offsets.push_back(0);
CoordType current_col = 0;
for (const auto& nz : nonzeros) {
while (current_col < nz.col) {
col_offsets.push_back(static_cast<CoordType>(row_indices.size()));
++current_col;
}
row_indices.push_back(nz.row);
values.push_back(nz.value);
}
while (current_col < header.num_cols) {
col_offsets.push_back(static_cast<CoordType>(row_indices.size()));
current_col++;
}
return CSCMatrix<CoordType, ValueType>{
header.num_rows,
header.num_cols,
static_cast<CoordType>(values.size()),
std::move(col_offsets),
std::move(row_indices),
std::move(values)
};
}
} // namespace MatrixMarket