-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreateh5.cpp
593 lines (521 loc) · 18.9 KB
/
createh5.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
* HDF5-UDF: User-Defined Functions for HDF5
*
* File: createh5.cpp
*
* Creates HDF5 files that can be used for testing purposes.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hdf5.h>
#include <string>
#include <sstream>
#include <fstream>
#include <functional>
#include <map>
// https://github.com/lucasvr/snappy-cuda
#ifndef SNAPPY_CUDA_FILTER_ID
#define SNAPPY_CUDA_FILTER_ID 32003
#endif
// https://github.com/lucasvr/nvcomp-iofilter
#ifndef NVCOMP_FILTER_ID
#define NVCOMP_FILTER_ID 32004
#endif
hsize_t NATIVE_DIM0 = 100;
hsize_t NATIVE_DIM1 = 50;
hsize_t CHUNK_DIM0 = NATIVE_DIM0;
hsize_t CHUNK_DIM1 = 25;
const std::map<std::string, H5Z_filter_t> filter_map {
{"deflate", H5Z_FILTER_DEFLATE},
{"snappy-cuda", SNAPPY_CUDA_FILTER_ID},
{"nvcomp", NVCOMP_FILTER_ID},
};
int create_regular_dataset(hid_t file_id, std::string compression, int count);
int create_compound_dataset_nostring(hid_t file_id, bool simple_layout, std::string compression, int count);
int create_compound_dataset_string(hid_t file_id, bool simple_layout, std::string compression, int count);
int create_compound_dataset_varstring(hid_t file_id, bool simple_layout, std::string compression, int count);
int create_native_int32(hid_t file_id, std::string compression, int count);
int create_native_string(hid_t file_id, std::string compression, int count);
int create_native_varstring(hid_t file_id, std::string compression, int count);
std::string getOptionValue(int argc, char **argv, const char *option, const char *default_value)
{
for (int i=1; i<argc; ++i)
{
char *start = strstr(argv[i], option);
if (start == argv[i] && strlen(argv[i]) == strlen(option))
{
// No-argument option (e.g., --help). Return "1"
return "1";
}
else if (start == argv[i])
{
char *value = &start[strlen(option) + strlen("=")];
return std::string(value);
}
}
return std::string(default_value);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
std::stringstream ss;
for (auto const &entry: filter_map)
ss << entry.first << ", ";
auto supported_filters = ss.str().substr(0, ss.str().length()-2);
fprintf(stdout, "Syntax: %s <options>\n", argv[0]);
fprintf(stdout, "Available options are:\n"
" --compound=TYPE Create a compound dataset with a predefined structure\n"
" Valid options for TYPE are:\n"
" NOSTRING_SIMPLE (simple layout, no string members)\n"
" NOSTRING_MIXED (mixed layout, no string members)\n"
" STRING_SIMPLE (simple layout, including a fixed-sized string member)\n"
" STRING_MIXED (mixed layout, including a fixed-sized string member)\n"
" VARSTRING_SIMPLE (simple layout, including a variable-sized string member)\n"
" VARSTRING_MIXED (mixed layout, including a variable-sized string member)\n"
" --datatype=TYPE Create a dataset with a predefined native type (default: INT32)\n"
" Valid options for TYPE are:\n"
" INT32 (an integer-based dataset)\n"
" STRING (a fixed-sized string dataset)\n"
" VARSTRING (a variable-sized string dataset)\n"
" --count=N Create this many datasets in the output file (default: 0)\n"
" --dims=X,Y X and Y dimensions (default: %lu,%lu)\n"
" --cdims=X,Y Chunked X and Y dimensions (default: %lu,%lu)\n"
" --compress=FILTER Compress the dataset(s) with one of the supported filters:\n"
" %s\n"
" --out=FILE Output file name (truncates FILE if it already exists)\n\n",
NATIVE_DIM0, NATIVE_DIM1, CHUNK_DIM0, CHUNK_DIM1, supported_filters.c_str());
return 1;
}
// Argument processing
std::stringstream default_dims, default_cdims;
default_dims << NATIVE_DIM0 << "," << NATIVE_DIM1;
default_cdims << CHUNK_DIM0 << "," << CHUNK_DIM1;
std::string compound = getOptionValue(argc, argv, "--compound", "");
std::string datatype = getOptionValue(argc, argv, "--datatype", "INT32");
int dataset_count = atoi(getOptionValue(argc, argv, "--count", "0").c_str());
std::string compression = getOptionValue(argc, argv, "--compress", "");
std::string hdf5_file = getOptionValue(argc, argv, "--out", "");
if (hdf5_file.size() == 0)
{
fprintf(stderr, "Error: missing output file (--out=FILE)\n");
return 1;
}
std::string dims = getOptionValue(argc, argv, "--dims", default_dims.str().c_str());
sscanf(dims.c_str(), "%lu,%lu", &NATIVE_DIM0, &NATIVE_DIM1);
std::string cdims = getOptionValue(argc, argv, "--cdims", default_cdims.str().c_str());
sscanf(cdims.c_str(), "%lu,%lu", &CHUNK_DIM0, &CHUNK_DIM1);
// Create (or truncate an existing) output file
hid_t file_id = H5Fcreate(hdf5_file.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
if (file_id < 0)
{
fprintf(stderr, "Failed to create file %s\n", hdf5_file.c_str());
return 1;
}
struct {
const char *type;
bool simple_layout;
int (*func)(hid_t, bool, std::string, int);
} compound_functions[] = {
{"NOSTRING_SIMPLE", true, create_compound_dataset_nostring},
{"NOSTRING_MIXED", false, create_compound_dataset_nostring},
{"STRING_SIMPLE", true, create_compound_dataset_string},
{"STRING_MIXED", false, create_compound_dataset_string},
{"VARSTRING_SIMPLE", true, create_compound_dataset_varstring},
{"VARSTRING_MIXED", false, create_compound_dataset_varstring},
{NULL, false, NULL}
};
struct {
const char *type;
int (*func)(hid_t, std::string, int);
} native_functions[] = {
{"INT32", create_native_int32},
{"STRING", create_native_string},
{"VARSTRING", create_native_varstring},
{NULL, NULL}
};
int ret = 0, count = 1;
while (true)
{
if (compound.size())
{
ret = -1;
for (int i=0; compound_functions[i].type != NULL; ++i)
{
auto entry = &compound_functions[i];
if (compound.compare(entry->type) == 0)
ret = entry->func(file_id, entry->simple_layout, compression, count);
}
if (ret == -1)
{
fprintf(stderr, "Invalid compound type '%s' requested\n", compound.c_str());
break;
}
count++;
}
if (datatype.size())
{
ret = -1;
for (int i=0; native_functions[i].type != NULL; ++i)
{
auto entry = &native_functions[i];
if (datatype.compare(entry->type) == 0)
ret = entry->func(file_id, compression, count);
}
if (ret == -1)
{
fprintf(stderr, "Invalid datatype '%s' requested\n", datatype.c_str());
break;
}
count++;
}
if (ret != 0 || count > dataset_count)
break;
}
H5Fclose(file_id);
return ret;
}
// Files with native data types
hid_t __make_compression_plist(std::string compression)
{
auto filter_info = filter_map.find(compression);
if (filter_info == filter_map.end())
{
fprintf(stderr, "Requested compression filter not supported\n");
return -1;
}
hid_t plist_id = H5Pcreate(H5P_DATASET_CREATE);
if (plist_id < 0)
{
fprintf(stderr, "Failed to create property list\n");
return -1;
}
size_t cd_nvalues = 0;
unsigned int cd_values[4];
if (filter_info->second == H5Z_FILTER_DEFLATE)
{
// Set compression level
cd_nvalues = 1;
cd_values[0] = 9;
}
herr_t ret = H5Pset_filter(plist_id, filter_info->second, H5Z_FLAG_MANDATORY, cd_nvalues, cd_values);
if (ret < 0)
{
fprintf(stderr, "Failed to configure compression filter. Please check that $HDF5_PLUGIN_PATH is set\n");
H5Pclose(plist_id);
return -1;
}
hsize_t cdims[2] = {CHUNK_DIM0, CHUNK_DIM1};
ret = H5Pset_chunk(plist_id, 2, cdims);
if (ret < 0)
{
fprintf(stderr, "Failed to set chunk size\n");
H5Pclose(plist_id);
return -1;
}
return plist_id;
}
int __create_native_dataset(
hid_t file_id, hid_t space_id, hid_t type_id, hid_t mem_type, int count, void *data, std::string compression)
{
int retval = 0;
char name[64];
snprintf(name, sizeof(name)-1, "Dataset%d", count);
hid_t plist_id = H5P_DEFAULT;
if (compression.size())
{
plist_id = __make_compression_plist(compression);
if (plist_id < 0)
return 1;
}
hid_t dset_id = H5Dcreate(file_id, name, type_id, space_id, H5P_DEFAULT, plist_id, H5P_DEFAULT);
if (dset_id < 0)
{
fprintf(stderr, "Failed to create dataset\n");
retval = 1;
}
else
{
herr_t ret = H5Dwrite(dset_id, mem_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
if (ret < 0)
{
fprintf(stderr, "Error writing data to file\n");
retval = 1;
}
H5Dclose(dset_id);
}
if (plist_id != H5P_DEFAULT)
H5Pclose(plist_id);
return retval;
}
int create_native_int32(hid_t file_id, std::string compression, int count)
{
int32_t *data = new int[NATIVE_DIM0 * NATIVE_DIM1];
for (hsize_t i=0; i<NATIVE_DIM0; ++i)
for (hsize_t j=0; j<NATIVE_DIM1; ++j)
data[i * NATIVE_DIM1 + j] = count * 10 * i + j;
hsize_t dims[2] = {NATIVE_DIM0, NATIVE_DIM1};
hid_t space_id = H5Screate_simple(2, dims, NULL);
if (space_id < 0)
{
fprintf(stderr, "Failed to create dataspace\n");
delete[] data;
return 1;
}
int ret = __create_native_dataset(
file_id, space_id, H5T_STD_I32LE, H5T_NATIVE_INT, count, (void *) data, compression);
H5Sclose(space_id);
delete[] data;
return ret;
}
int __populate_string_data(
int dims,
int ¤t_dim,
std::function<void(int, const char *)> write_fn)
{
// We use the LICENSE file as input to string-based datasets
std::ifstream textfile("../LICENSE");
if (! textfile.is_open())
{
fprintf(stderr, "Failed to open '../LICENSE'\n");
return 1;
}
std::string inputFileBuffer(
(std::istreambuf_iterator<char>(textfile)),
(std::istreambuf_iterator<char>()));
std::string word;
std::istringstream iss(inputFileBuffer);
while (iss >> word && current_dim < dims)
{
write_fn(current_dim, word.c_str());
current_dim++;
}
// Fill up the remaining dimensions with a static string, if needed
while (current_dim < dims)
{
write_fn(current_dim, "<dummy>");
current_dim++;
}
return 0;
}
int create_native_varstring(hid_t file_id, std::string compression, int count)
{
// Prepare output data
int current_dim = 0;
char *data[NATIVE_DIM0];
auto _callback = [&](int dim, const char *word)
{
data[dim] = strdup(word);
};
int ret = __populate_string_data(NATIVE_DIM0, current_dim, _callback);
if (ret != 0)
return 1;
// Prepare HDF5 handles
hsize_t dims[1] = {NATIVE_DIM0};
hid_t space_id = H5Screate_simple(1, dims, NULL);
if (space_id < 0)
{
fprintf(stderr, "Failed to create dataspace\n");
return 1;
}
// Variable-length string type
hid_t stringtype_id = H5Tcopy(H5T_C_S1);
H5Tset_size(stringtype_id, H5T_VARIABLE);
ret = __create_native_dataset(
file_id, space_id, stringtype_id, stringtype_id, count, (void *) data, compression);
H5Sclose(space_id);
for (hsize_t dim=0; dim<NATIVE_DIM0; ++dim)
free(data[dim]);
return ret;
}
int create_native_string(hid_t file_id, std::string compression, int count)
{
// Prepare output data
const int len = 32;
int current_dim = 0;
char data[NATIVE_DIM0][len];
memset(data, 0, sizeof(data));
auto _callback = [&](int dim, const char *word)
{
snprintf(data[dim], len-1, "%s", word);
};
int ret = __populate_string_data(NATIVE_DIM0, current_dim, _callback);
if (ret != 0)
return 1;
// Prepare HDF5 handles
hsize_t dims[1] = {NATIVE_DIM0};
hid_t space_id = H5Screate_simple(1, dims, NULL);
if (space_id < 0)
{
fprintf(stderr, "Failed to create dataspace\n");
return 1;
}
// Fixed-length string type
hid_t stringtype_id = H5Tcopy(H5T_C_S1);
H5Tset_size(stringtype_id, len);
ret = __create_native_dataset(
file_id, space_id, stringtype_id, stringtype_id, count, (void *) data[0], compression);
H5Sclose(space_id);
return ret;
}
// Files with compound data types
const int COMPOUND_DIM0 = 1000;
struct compound_varstring_t {
int serial_no;
char *location;
double temperature;
double pressure;
};
struct compound_string_t {
int serial_no;
char location[20];
double temperature;
double pressure;
};
struct compound_nostring_t {
int serial_no;
double temperature;
double pressure;
};
template <typename T>
int __create_compound_dataset(
hid_t file_id,
bool simple_layout,
size_t varstr_size,
int count,
T data,
std::string compression,
std::function<void(hid_t, const char *, int)> string_fn)
{
char name[64];
snprintf(name, sizeof(name)-1, "Dataset%d", count);
hsize_t dims[1] = {COMPOUND_DIM0};
hid_t space_id = H5Screate_simple(1, dims, NULL);
if (space_id < 0)
{
fprintf(stderr, "Failed to create dataspace\n");
return 1;
}
hid_t plist_id = H5P_DEFAULT;
if (compression.size())
{
plist_id = __make_compression_plist(compression);
if (plist_id < 0)
{
H5Sclose(space_id);
return 1;
}
}
// The dataset layout is based on the example provided by the HDF Group at
// https://support.hdfgroup.org/ftp/HDF5/examples/examples-by-api/hdf5-examples/1_8/C/H5T/h5ex_t_cmpd.c
hid_t memtype_id = H5Tcreate(H5T_COMPOUND, sizeof(*data));
H5Tinsert(memtype_id, "Serial number", HOFFSET(__typeof(*data), serial_no), H5T_NATIVE_INT);
string_fn(memtype_id, "Location", -1);
H5Tinsert(memtype_id, "Temperature (F)", HOFFSET(__typeof(*data), temperature), H5T_NATIVE_DOUBLE);
H5Tinsert(memtype_id, "Pressure (inHg)", HOFFSET(__typeof(*data), pressure), H5T_NATIVE_DOUBLE);
// If simple_layout==false, then we use a different disk layout to exercise HDF5-UDF's
// ability to pad the compound structure. Note that the example at the URL above includes
// a 'char *' to a string element that we don't include here at all times.
hid_t filetype_id = H5Tcreate(H5T_COMPOUND, 8 + varstr_size + 8 + 8);
H5Tinsert(filetype_id, "Serial number", 0, H5T_STD_I64LE);
string_fn(filetype_id, "Location", 8);
H5Tinsert(filetype_id, "Temperature (F)", 8 + varstr_size, H5T_IEEE_F64LE);
H5Tinsert(filetype_id, "Pressure (inHg)", 8 + varstr_size + 8, H5T_IEEE_F64LE);
// Create the compound dataset using either a simple approach (in which we write data
// according to the memory layout) or a more complex one in which the memory and disk
// layouts differ.
int retval = 0;
hid_t dset_id = H5Dcreate(file_id, name, simple_layout ? memtype_id : filetype_id, space_id,
H5P_DEFAULT, plist_id, H5P_DEFAULT);
if (dset_id < 0)
{
fprintf(stderr, "Failed to create dataset\n");
retval = 1;
}
else
{
herr_t ret = H5Dwrite(dset_id, memtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
if (ret < 0)
{
fprintf(stderr, "Error writing data to file\n");
retval = 1;
}
}
if (plist_id != H5P_DEFAULT)
H5Pclose(plist_id);
H5Tclose(filetype_id);
H5Tclose(memtype_id);
H5Dclose(dset_id);
H5Sclose(space_id);
return retval;
}
int create_compound_dataset_varstring(hid_t file_id, bool simple_layout, std::string compression, int count)
{
compound_varstring_t data[COMPOUND_DIM0];
memset(data, 0, sizeof(data));
for (int i=0; i<COMPOUND_DIM0; ++i) {
char loc[64];
snprintf(loc, sizeof(loc)-1, "Location_%d", i);
data[i].serial_no = i;
// This is a short-lived program. Just let this one leak.
data[i].location = strdup(loc);
data[i].temperature = COMPOUND_DIM0/(i+1.0);
data[i].pressure = COMPOUND_DIM0/((i+1)*2.0);
}
// Variable-length string type
hid_t stringtype_id = H5Tcopy(H5T_C_S1);
H5Tset_size(stringtype_id, H5T_VARIABLE);
auto _callback = [&](hid_t type_id, const char *name, int offset)
{
H5Tinsert(
type_id,
name,
offset != -1 ? offset : HOFFSET(compound_varstring_t, location),
stringtype_id);
};
return __create_compound_dataset<compound_varstring_t *>(
file_id, simple_layout, sizeof(hvl_t), count, data, compression, _callback);
}
int create_compound_dataset_string(hid_t file_id, bool simple_layout, std::string compression, int count)
{
compound_string_t data[COMPOUND_DIM0];
memset(data, 0, sizeof(data));
for (int i=0; i<COMPOUND_DIM0; ++i) {
data[i].serial_no = i;
sprintf(data[i].location, "Location_%d", i);
data[i].temperature = COMPOUND_DIM0/(i+1.0);
data[i].pressure = COMPOUND_DIM0/((i+1)*2.0);
}
// Fixed-length string type
hid_t stringtype_id = H5Tcopy(H5T_C_S1);
size_t varstr_size = sizeof(data[0].location);
H5Tset_size(stringtype_id, varstr_size);
auto _callback = [&](hid_t type_id, const char *name, int offset)
{
H5Tinsert(
type_id,
name,
offset != -1 ? offset : HOFFSET(compound_string_t, location),
stringtype_id);
};
return __create_compound_dataset<compound_string_t *>(
file_id, simple_layout, varstr_size, count, data, compression, _callback);
}
int create_compound_dataset_nostring(hid_t file_id, bool simple_layout, std::string compression, int count)
{
compound_nostring_t data[COMPOUND_DIM0];
memset(data, 0, sizeof(data));
for (int i=0; i<COMPOUND_DIM0; ++i) {
data[i].serial_no = i;
data[i].temperature = COMPOUND_DIM0/(i+1.0);
data[i].pressure = COMPOUND_DIM0/((i+1)*2.0);
}
auto _callback = [&](hid_t type_id, const char *name, int offset)
{
};
// Provide a sizeof(hvl_t) to ensure the structure is padded when
// simple_layout == false.
return __create_compound_dataset<compound_nostring_t *>(
file_id, simple_layout, sizeof(hvl_t), count, data, compression, _callback);
}