This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
omxmatrix.cpp
333 lines (254 loc) · 8.7 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
/* omxmatrix.cpp
*
* HDF5/OMX Matrix helper routines
*
* @author Billy Charlton, PSRC
*/
#include "stdafx.h"
#include <cstdlib>
#include <cstring>
#include <ctime>
#include "omxmatrix.h"
using namespace std;
// ###########################################################################
// OMXMatrix: C++ Helper class to read/write TP+ 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
_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) {
if (_tableLookup.count(table)==0) {
throw NoSuchTableException();
}
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_RDONLY, 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_READ;
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::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;
}
int OMXMatrix::getCubeNumber(string tablename) {
string path = "/data/" + tablename;
hid_t leaf = H5Dopen(_h5file, path.c_str(), H5P_DEFAULT);
if (leaf<0) return -1;
herr_t exists = H5LTfind_attribute(leaf, CUBE_MAT_NUMBER);
H5Dclose(leaf);
if (exists==0) return -1;
int cubeNumber;
int status = H5LTget_attribute_int(_h5file, path.c_str(), CUBE_MAT_NUMBER, &cubeNumber);
if (status<0) cubeNumber = -1;
return cubeNumber;
}
// ---- 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 TP+ 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;
int cube_num = t+1;
H5LTset_attribute_int(_h5file, tpath.c_str(), CUBE_MAT_NUMBER, &cube_num, 1);
}
rtn = H5Pclose(plist);
rtn = H5Sclose(dataspace);
}