-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbmt.h
393 lines (368 loc) · 11.8 KB
/
bmt.h
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
////////////////////////////////////////////////////////////////////////////////
// Copyright 2014 Chris Fougner. //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see http://www.gnu.org/licenses/. //
////////////////////////////////////////////////////////////////////////////////
// Binary Matrix (BMT) is a binary file format storing matrices.
// It supports the following matrix types
//
// - Sparse and dense matrices.
// - Row major (resp. CSR) and column major (resp. CSC) matrices.
// - Double or single precision entries.
// - 32- or 64-bit integer indices.
//
// This file format is much more efficient than MatrixMarket files and similar
// at storing matrices. The layout of the file is
//
// Sparse: (int_prec, float_prec, endian, mattype, m, n, nnz, val, ind, ptr)
// Dense: (int_prec, float_prec, endian, mattype, m, n, data)
//
// - int_prec : Integer precision (either 32 or 64).
// - float_prec : Floatint point precision (either 32 or 64).
// - endian : Endianness (either big or small).
// - mattype : Matrix type (either sparse or dense).
// - (m, n) : Matrix dimensions.
// - nnz : [sparse only] Number of non-zeros in matrix.
// - val : [sparse only] Numeric entries in matrix.
// - ind : [sparse only] Column, resp. row indices (for CSR resp.
// CSC sparse matrices).
// - ptr : [sparse only] Row, resp. column pointers (for CSR resp.
// CSC sparse matrices).
// - data : [dense only] Matrix data.
//
#ifndef BMT_H_
#define BMT_H_
#include <stdint.h>
#include <cstdio>
#include <typeinfo>
// Checks if a specified matrix is sparse or dense.
//
// Input arguments:
// - fname : Filename of matrix to check.
//
// Returns:
// 0 : The matrix is not sparse.
// 1 : The matrix is sparse.
// 2 : The matrix could not be found.
// 3 : The matrix could not be read.
int IsSparse(const char fname[]) {
FILE *fid = fopen(fname, "rb");
if (fid != 0)
return 2;
char buf[4];
int count = fread(buf, sizeof(buf), 1, fid);
if (count != 0)
return 3;
fclose(fid);
return buf[3] == 2 || buf[3] == 3;
}
// Reads a specified sparse matrix.
//
// Template arguments:
// - T : Floating point type of matrix.
// - I : Integer type of matrix indices.
//
// Input arguments:
// - fname : Filename of matrix to load.
//
// Output arguments
// - order : 'r' for CSR format or 'c' for CSC.
// - (m, n) : Dimensions of matrix.
// - nnz : Number of non-zeros in matrix.
// - val : Pointer to array of numeric data.
// - ind : Pointer to array of column (CSR) or row (CSC) indices.
// - ptr : Pointer to array of row (CSR) or column (CSC) pointers.
//
// Returns:
// 0 : Matrix successfully read.
// 1 : Could not open file.
// 2 : File length incorrect (or header corrupt).
// 3 : Integer precision invalid.
// 4 : Floating point precision invalid.
// 5 : Endianness invalid.
// 6 : Matrix type invald.
// 7 : Unsupported system, sizeof(float) != 4.
// 8 : Unsupported system, sizeof(double) != 8.
template <typename T, typename I>
int ReadBmtSparse(const char *fname, char *order, I *m, I *n, I *nnz, T **val,
I **ind, I **ptr) {
int flag = 0;
// Read info (int precision, float precision, endianness, matrix type).
FILE *fid = fopen(fname, "rb");
if (!fid)
return 1;
char info_buf[4];
size_t count = fread(info_buf, sizeof(info_buf), 1, fid);
if (count == 0)
flag = 2;
const int kIPrec = info_buf[0];
const int kFPrec = info_buf[1];
const int kEndian = info_buf[2];
const int kMatTyp = info_buf[3];
// Check info is valid.
if (!flag) {
if (kIPrec != 32 && kIPrec != 64)
flag = 3;
else if (kFPrec != 32 && kFPrec != 64)
flag = 4;
else if (kEndian != 0 && kEndian != 1)
flag = 5;
else if (kMatTyp != 2 && kMatTyp != 3)
flag = 6;
else if (kFPrec == 32 && sizeof(float) != 4)
flag = 7;
else if (kFPrec == 64 && sizeof(double) != 8)
flag = 8;
}
// Read m, n and nnz.
size_t m_, n_, nnz_;
if (!flag && kIPrec == 32) {
int32_t size_buf[3];
count = fread(size_buf, sizeof(size_buf), 1, fid);
if (count == 0)
flag = 2;
m_ = static_cast<size_t>(size_buf[0]);
n_ = static_cast<size_t>(size_buf[1]);
nnz_ = static_cast<size_t>(size_buf[2]);
} else if (!flag) {
int64_t size_buf[3];
count = fread(size_buf, sizeof(size_buf), 1, fid);
if (count == 0)
flag = 2;
m_ = static_cast<size_t>(size_buf[0]);
n_ = static_cast<size_t>(size_buf[1]);
nnz_ = static_cast<size_t>(size_buf[2]);
}
// Set output info
*m = static_cast<I>(m_);
*n = static_cast<I>(n_);
*nnz = static_cast<I>(nnz_);
*order = kMatTyp == 2 ? 'c' : 'r';
*val = 0;
*ptr = *ind = 0;
// Read val.
if (!flag && kFPrec == 32) {
float *val_ = new float[nnz_];
count = fread(val_, nnz_ * sizeof(float), 1, fid);
if (count == 0)
flag = 2;
if (!flag && typeid(float) == typeid(T)) {
*val = reinterpret_cast<T*>(val_);
} else if (!flag) {
*val = new T[nnz_];
for (size_t i = 0; i < nnz_; ++i)
(*val)[i] = static_cast<T>(val_[i]);
delete [] val_;
} else {
delete [] val_;
}
} else if (!flag) {
double *val_ = new double[nnz_];
count = fread(val_, nnz_ * sizeof(double), 1, fid);
if (count == 0)
flag = 2;
if (!flag && typeid(double) == typeid(T)) {
*val = reinterpret_cast<T*>(val_);
} else if (!flag) {
*val = new T[nnz_];
for (size_t i = 0; i < nnz_; ++i)
(*val)[i] = static_cast<T>(val_[i]);
delete [] val_;
} else {
delete [] val_;
}
}
// Read ind and ptr.
size_t num_ptr_;
if (kMatTyp == 2)
num_ptr_ = n_ + 1;
else
num_ptr_ = m_ + 1;
if (!flag && kIPrec == 4) {
int32_t *ind_ = new int32_t[nnz_];
int32_t *ptr_ = new int32_t[num_ptr_];
count = fread(ind_, nnz_ * sizeof(int32_t), 1, fid);
if (count == 0)
flag = 2;
count = fread(ptr_, num_ptr_ * sizeof(int32_t), 1, fid);
if (!flag && count == 0)
flag = 2;
if (!flag && typeid(I) == typeid(int32_t)) {
*ind = reinterpret_cast<I*>(ind_);
*ptr = reinterpret_cast<I*>(ptr_);
} else if (!flag) {
*ind = new I[nnz_];
*ptr = new I[num_ptr_];
for (size_t i = 0; i < nnz_; ++i)
(*ind)[i] = static_cast<I>(ind_[i]);
for (size_t i = 0; i < num_ptr_; ++i)
(*ptr)[i] = static_cast<I>(ptr_[i]);
delete [] ind_;
delete [] ptr_;
} else {
delete [] ind_;
delete [] ptr_;
}
} else if (!flag) {
int64_t *ind_ = new int64_t[nnz_];
int64_t *ptr_ = new int64_t[num_ptr_];
count = fread(ind_, nnz_ * sizeof(int64_t), 1, fid);
if (count == 0)
flag = 2;
count = fread(ptr_, num_ptr_ * sizeof(int64_t), 1, fid);
if (!flag && count == 0)
flag = 2;
if (!flag && typeid(I) == typeid(int64_t)) {
*ind = reinterpret_cast<I*>(ind_);
*ptr = reinterpret_cast<I*>(ptr_);
} else if (!flag) {
*ind = new I[nnz_];
*ptr = new I[num_ptr_];
for (size_t i = 0; i < nnz_; ++i)
(*ind)[i] = static_cast<I>(ind_[i]);
for (size_t i = 0; i < num_ptr_; ++i)
(*ptr)[i] = static_cast<I>(ptr_[i]);
delete [] ind_;
delete [] ptr_;
} else {
delete [] ind_;
delete [] ptr_;
}
}
// Delete all data if there was an error.
if (flag) {
if (*val != 0)
delete [] val;
*val = 0;
*ind = *ptr = 0;
}
// Close and finish.
fclose(fid);
return flag;
}
// Reads a specified dense matrix.
//
// Template arguments:
// - T : Floating point type of matrix.
// - I : Integer type of matrix dimensions.
//
// Input arguments:
// - fname : Filename of matrix to load.
//
// Output arguments
// - order : 'r' for row major format or 'c' for column major format.
// - (m, n) : Dimensions of matrix.
// - data : Pointer to array of numeric data.
//
// Returns:
// 0 : Matrix successfully read.
// 1 : Could not open file (or header corrupt).
// 2 : File length incorrect.
// 3 : Integer precision invalid.
// 4 : Floating point precision invalid.
// 5 : Endianness invalid.
// 6 : Matrix type invald.
// 7 : Unsupported system, sizeof(float) != 4.
// 8 : Unsupported system, sizeof(double) != 8.
template <typename T, typename I>
int ReadBmtDense(const char *fname, char *order, I *m, I *n, T **data) {
int flag = 0;
// Read info (int precision, float precision, endianness, matrix type).
FILE *fid = fopen(fname, "rb");
if (!fid)
return 1;
char info_buf[4];
size_t count = fread(info_buf, sizeof(info_buf), 1, fid);
if (count == 0)
flag = 2;
const int kIPrec = info_buf[0];
const int kFPrec = info_buf[1];
const int kEndian = info_buf[2];
const int kMatTyp = info_buf[3];
// Check info is valid.
if (!flag) {
if (kIPrec != 32 && kIPrec != 64)
flag = 3;
else if (kFPrec != 32 && kFPrec != 64)
flag = 4;
else if (kEndian != 0 && kEndian != 1)
flag = 5;
else if (kMatTyp != 0 && kMatTyp != 1)
flag = 6;
else if (kFPrec == 32 && sizeof(float) != 4)
flag = 7;
else if (kFPrec == 64 && sizeof(double) != 8)
flag = 8;
}
// Read m, n and nnz.
size_t m_, n_;
if (!flag && kIPrec == 32) {
int32_t size_buf[2];
count = fread(size_buf, sizeof(size_buf), 1, fid);
if (count == 0)
flag = 2;
m_ = static_cast<size_t>(size_buf[0]);
n_ = static_cast<size_t>(size_buf[1]);
} else if (!flag) {
int64_t size_buf[2];
count = fread(size_buf, sizeof(size_buf), 1, fid);
if (count == 0)
flag = 2;
m_ = static_cast<size_t>(size_buf[0]);
n_ = static_cast<size_t>(size_buf[1]);
}
// Set output info
*m = static_cast<I>(m_);
*n = static_cast<I>(n_);
*order = kMatTyp == 0 ? 'c' : 'r';
// Read val.
if (!flag && kFPrec == 32) {
float *data_ = new float[m_ * n_];
count = fread(data_, m_ * n_ * sizeof(float), 1, fid);
if (count == 0)
flag = 2;
if (!flag && typeid(float) == typeid(T)) {
*data = reinterpret_cast<T*>(data_);
} else if (!flag) {
*data = new T[m_ * n_];
for (size_t i = 0; i < m_ * n_; ++i)
(*data)[i] = static_cast<T>(data_[i]);
delete [] data_;
} else {
delete [] data_;
}
} else if (!flag) {
double *data_ = new double[m_ * n_];
count = fread(data_, m_ * n_ * sizeof(double), 1, fid);
if (count == 0)
flag = 2;
if (!flag && typeid(double) == typeid(T)) {
*data = reinterpret_cast<T*>(data_);
} else if (!flag) {
*data = new T[m_ * n_];
for (size_t i = 0; i < m_ * n_; ++i)
(*data)[i] = static_cast<T>(data_[i]);
delete [] data_;
} else {
delete [] data_;
}
}
if (flag)
*data = 0;
// Close and return.
fclose(fid);
return flag;
}
// TODO(chris): BMT-write functions.
#endif // BMT_H_