-
Notifications
You must be signed in to change notification settings - Fork 0
/
omxmatrix.cpp
380 lines (296 loc) · 10.1 KB
/
omxmatrix.cpp
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
/* omxmatrix.cpp
*
* OMX/HDF5 Matrix helper routines
*
* @author Billy Charlton, PSRC
* @author Ben Stabler, RSG
*/
#include <cstdlib>
#include <cstring>
#include <ctime>
#include "omxmatrix.h"
using namespace std;
// ###########################################################################
// OMXMatrix: C++ Helper class to read/write TP+ (Cube) style matrix tables
// ---------------------------------------------------------------------------
OMXMatrix::OMXMatrix() {
_fileOpen = false;
_nTables = 0;
_nRows = 0;
_nCols = 0;
_memspace = -1;
}
//Destructor
OMXMatrix::~OMXMatrix()
{
if (_memspace > -1 ) {
H5Sclose(_memspace);
_memspace = -1;
}
// Close H5 file handles
if (_fileOpen==true) {
H5Fclose(_h5file);
}
_fileOpen = false;
}
//Write/Create operations ---------------------------------------------------
void OMXMatrix::createFile(int tables, int rows, int cols, vector<string> &tableNames, string fileName) {
_fileOpen = true;
_mode = MODE_CREATE;
_nRows = rows;
_nCols = cols;
_nTables = tables;
// Create the physical file - H5F_ACC_TRUNC = overwrite an existing file
_h5file = H5Fcreate(fileName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
if (0 > _h5file) {
fprintf(stderr, "ERROR: Could not create file %s.\n", fileName.c_str());
}
// Build SHAPE attribute
const int shape[2] = {rows, cols};
// Write file attributes
H5LTset_attribute_string(_h5file, "/", "OMX_VERSION", "0.2");
H5LTset_attribute_int(_h5file, "/", "SHAPE", &shape[0], 2);
// save the order that matrices are written
hid_t plist = H5Pcreate (H5P_GROUP_CREATE);
H5Pset_link_creation_order(plist, H5P_CRT_ORDER_TRACKED);
// Create folder structure
H5Gcreate(_h5file, "/data", NULL, plist, NULL);
H5Gcreate(_h5file, "/lookup", NULL, plist, NULL);
H5Pclose(plist);
// Create the datasets
init_tables(tableNames);
}
void OMXMatrix::writeRow(string table, int row, double *rowdata) {
// First see if we've opened this table already
if (_dataset.count(table) == 0) {
// Does this table exist?
if (_tableLookup.count(table) == 0) {
throw NoSuchTableException();
}
_dataset[table] = openDataset(table);
}
hsize_t count[2], offset[2];
count[0] = 1;
count[1] = _nCols;
offset[0] = row-1;
offset[1] = 0;
if (_memspace <0 ) _memspace = H5Screate_simple(2,count,NULL);
if (_dataspace.count(table)==0) {
_dataspace[table] = H5Dget_space(_dataset[table]);
}
H5Sselect_hyperslab (_dataspace[table], H5S_SELECT_SET, offset, NULL, count, NULL);
if (0 > H5Dwrite(_dataset[table], H5T_NATIVE_DOUBLE, _memspace, _dataspace[table], H5P_DEFAULT, rowdata)) {
fprintf(stderr, "ERROR: writing table %s, row %d\n", table.c_str(), row);
exit(2);
}
}
//Read/Open operations ------------------------------------------------------
void OMXMatrix::openFile(string filename) {
// Try to open the existing file
_h5file = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
if (_h5file < 0) {
fprintf(stderr, "ERROR: Can't find or open file %s",filename.c_str());
exit(2);
}
// OK, it's open and it's HDF5;
// Now query some things about the file.
_fileOpen = true;
_mode = MODE_READWRITE;
int shape[2];
herr_t status = 0;
status += H5LTget_attribute_int(_h5file, "/", "SHAPE", &shape[0]);
if (status < 0) {
fprintf(stderr, "ERROR: %s doesn't have SHAPE attribute\n", filename.c_str());
exit(2);
}
_nRows = shape[0];
_nCols = shape[1];
readTableNames();
}
int OMXMatrix::getRows() {
return _nRows;
}
int OMXMatrix::getCols() {
return _nCols;
}
int OMXMatrix::getTables() {
return _nTables;
}
string OMXMatrix::getTableName(int table) {
return _tableName[table];
}
void OMXMatrix::getRow (string table, int row, void *rowptr) {
hsize_t data_count[2], data_offset[2];
// First see if we've opened this table already
if (_dataset.count(table)==0) {
// Does this table exist?
if (_tableLookup.count(table)==0) {
throw MatrixReadException() ;
}
_dataset[table] = openDataset(table);
}
data_count[0] = 1;
data_count[1] = _nCols;
data_offset[0] = row-1;
data_offset[1] = 0;
// Create dataspace if necessary. Don't do every time or we'll run OOM.
if (_dataspace.count(table)==0) {
_dataspace[table] = H5Dget_space(_dataset[table]);
}
// Define MEMORY slab (using data_count since we don't want to read zones+1 values!)
if (_memspace < 0) {
_memspace = H5Screate_simple(2, data_count, NULL);
}
// Define DATA slab
if (0 > H5Sselect_hyperslab (_dataspace[table], H5S_SELECT_SET, data_offset, NULL, data_count, NULL)) {
fprintf(stderr, "ERROR: Couldn't select DATA subregion for table %s, subrow %d.\n",
table.c_str(),row);
exit(2);
}
// Read the data!
if (0 > H5Dread(_dataset[table], H5T_NATIVE_DOUBLE, _memspace, _dataspace[table],
H5P_DEFAULT, rowptr)) {
fprintf(stderr, "ERROR: Couldn't read table %s, subrow %d.\n",table.c_str(),row);
exit(2);
}
}
void OMXMatrix::getCol(string table, int col, void *colptr) {
hsize_t data_count[2], data_offset[2];
// First see if we've opened this table already
if (_dataset.count(table) == 0) {
// Does this table exist?
if (_tableLookup.count(table) == 0) {
throw MatrixReadException();
}
_dataset[table] = openDataset(table);
}
data_count[0] = _nRows;
data_count[1] = 1;
data_offset[0] = 0;
data_offset[1] = col - 1;
// Create dataspace if necessary. Don't do every time or we'll run OOM.
if (_dataspace.count(table) == 0) {
_dataspace[table] = H5Dget_space(_dataset[table]);
}
// Define MEMORY slab (using data_count since we don't want to read zones+1 values!)
if (_memspace < 0) {
_memspace = H5Screate_simple(2, data_count, NULL);
}
// Define DATA slab
if (0 > H5Sselect_hyperslab(_dataspace[table], H5S_SELECT_SET, data_offset, NULL, data_count, NULL)) {
fprintf(stderr, "ERROR: Couldn't select DATA subregion for table %s, subcol %d.\n",
table.c_str(), col);
exit(2);
}
// Read the data!
if (0 > H5Dread(_dataset[table], H5T_NATIVE_DOUBLE, _memspace, _dataspace[table],
H5P_DEFAULT, colptr)) {
fprintf(stderr, "ERROR: Couldn't read table %s, subcol %d.\n", table.c_str(), col);
exit(2);
}
}
void OMXMatrix::closeFile() {
for(map<string,hid_t>::iterator iterator = _dataset.begin(); iterator != _dataset.end(); iterator++) {
H5Dclose(iterator->second);
}
for(map<string,hid_t>::iterator iterator = _dataspace.begin(); iterator != _dataspace.end(); iterator++) {
H5Sclose(iterator->second);
}
if (_memspace > -1 ) {
H5Sclose(_memspace);
_memspace = -1;
}
if (_fileOpen==true) {
H5Fclose(_h5file);
}
_fileOpen = false;
}
// ---- Private functions ---------------------------------------------------
hid_t OMXMatrix::openDataset(string table) {
string tname = "/data/" + table;
hid_t dataset = H5Dopen(_h5file, tname.c_str(), H5P_DEFAULT);
if (dataset < 0) {
throw InvalidOperationException();
}
return dataset;
}
/*
* Group traversal function. Build list of tablenames from this.
*/
herr_t _leaf_info(hid_t loc_id, const char *name, const H5L_info_t *info, void *opdata)
{
OMXMatrix *m = (OMXMatrix *) opdata;
m->_nTables++;
m->_tableName[m->_nTables] = name;
m->_tableLookup[name] = m->_nTables;
return 0;
}
/* Read table names. Sets number of tables in file, too. */
void OMXMatrix::readTableNames() {
_nTables = 0;
_tableLookup.clear();
_dataset.clear();
_dataspace.clear();
unsigned flags = 0;
hid_t datagroup = H5Gopen(_h5file, "/data", H5P_DEFAULT);
// if group has creation-order index, use it
hid_t info = H5Gget_create_plist(datagroup);
H5Pget_link_creation_order(info, &flags);
H5Pclose(info);
if (flags & H5P_CRT_ORDER_TRACKED) {
// Call _leaf_info() for every child in /data:
H5Literate(datagroup, H5_INDEX_CRT_ORDER, H5_ITER_INC, NULL, _leaf_info, this);
} else {
// otherwise just use name order
H5Literate(datagroup, H5_INDEX_NAME, H5_ITER_INC, NULL, _leaf_info, this);
}
H5Gclose(datagroup);
}
void OMXMatrix::init_tables (vector<string> &tableNames) {
hsize_t dims[2]={_nRows,_nCols};
hid_t plist;
herr_t rtn;
hsize_t chunksize[2];
double fillvalue[1];
fillvalue[0] = 0.0;
chunksize[0] = 1;
chunksize[1] = _nCols;
hid_t dataspace = H5Screate_simple(2,dims, NULL);
// Use a row-chunked, zip-compressed data format:
plist = H5Pcreate(H5P_DATASET_CREATE);
rtn = H5Pset_chunk(plist, 2, chunksize);
rtn = H5Pset_deflate(plist, 7);
rtn = H5Pset_fill_value(plist, H5T_NATIVE_DOUBLE, &fillvalue);
// Loop on all tables
for (unsigned int t=0; t<tableNames.size(); t++) {
string tpath = "/data/" + tableNames[t];
string tname(tableNames[t]);
// Create a dataset for each table
_dataset[tname] = H5Dcreate2(_h5file, tpath.c_str(), H5T_NATIVE_DOUBLE,
dataspace, H5P_DEFAULT, plist, H5P_DEFAULT);
if (_dataset[tname]<0) {
fprintf(stderr, "Error creating dataset %s",tpath.c_str());
exit(2);
}
// Save the something somewhere
_tableLookup[tname] = t+1;
}
rtn = H5Pclose(plist);
rtn = H5Sclose(dataspace);
}
bool isOMX(char *filename) {
htri_t answer = H5Fis_hdf5(filename);
if (answer <= 0) return false;
// It's HDF5; is it OMX?
hid_t f = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
herr_t exists = H5LTfind_attribute(f, "OMX_VERSION");
//don't actually care what OMX version it is, yet...
//char version[255];
//int status = H5LTget_attribute_string(f,"/","OMX_VERSION", version);
H5Fclose(f);
if (exists == 0) {
fprintf(stderr, "\n** %s is HDF5, but is not a valid OMX file.\n", filename);
exit(2);
}
return true;
}