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
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "h5repack.h"
#include "h5tools.h"
#include "h5tools_utils.h"
/* number of members in an array */
#ifndef NELMTS
#define NELMTS(X) (sizeof(X) / sizeof(X[0]))
#endif
/* minimum of two values */
#undef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
/*-------------------------------------------------------------------------
* Function: aux_copy_obj
*
* Purpose: copy the object filters for object copy
*
* Return: 0 success, -1 failure
*-------------------------------------------------------------------------
*/
static int
aux_copy_obj(hid_t dcpl_id, /* dataset creation property list */
const char * name, /* object name from traverse list */
pack_info_t *objout /*OUT*/) /* info about object to filter */
{
int nfilters; /* number of filters in DCPL */
char f_objname[256]; /* filter objname */
H5D_layout_t layout;
int rank; /* rank of dataset */
hsize_t chsize[64]; /* chunk size in elements */
int i;
unsigned u;
int ret_value = 0;
/* get information about input filters */
if ((nfilters = H5Pget_nfilters(dcpl_id)) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pget_nfilters failed");
/* copy filter_info_t structure */
for (i = 0; i < nfilters; i++) {
if ((objout->filter[i].filtn = H5Pget_filter2(
dcpl_id, (unsigned)i, &objout->filter[i].filt_flag, &objout->filter[i].cd_nelmts,
objout->filter[i].cd_values, sizeof(f_objname), f_objname, NULL)) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pget_filter2 failed");
}
objout->nfilters = nfilters;
HDstrcpy(objout->path, name);
if ((layout = H5Pget_layout(dcpl_id)) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pget_layout failed");
objout->layout = layout;
if (layout == H5D_CHUNKED) {
if ((rank = H5Pget_chunk(dcpl_id, NELMTS(chsize), chsize /*out*/)) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pget_chunk failed");
objout->chunk.rank = rank;
for (u = 0; u < (unsigned)rank; u++)
objout->chunk.chunk_lengths[u] = chsize[u];
}
done:
return ret_value;
}
/*-------------------------------------------------------------------------
* Function: aux_find_obj
*
* Purpose: find the object name NAME (got from the traverse list)
* in the repack options list
*-------------------------------------------------------------------------
*/
static int
aux_find_obj(const char * name, /* object name from traverse list */
pack_opt_t * options, /* repack options */
pack_info_t *obj /*OUT*/) /* info about object to filter */
{
char * pdest = NULL;
const char * pname = NULL;
unsigned int i;
for (i = 0; i < options->op_tbl->nelems; i++) {
if (HDstrcmp(options->op_tbl->objs[i].path, name) == 0) {
*obj = options->op_tbl->objs[i];
return (int)i;
}
pdest = options->op_tbl->objs[i].path;
if (pdest[0] == '/')
pdest++;
pname = name;
if (pname[0] == '/')
pname++;
if (HDstrcmp(pdest, pname) == 0) {
*obj = options->op_tbl->objs[i];
return (int)i;
}
} /*i*/
return -1;
}
/*-------------------------------------------------------------------------
* Function: aux_assign_obj
*
* Purpose: find the object name NAME (got from the traverse list)
* in the repack options list; assign the filter information OBJ
*
* Return: 0 not found, 1 found
*-------------------------------------------------------------------------
*/
static int
aux_assign_obj(const char * name, /* object name from traverse list */
pack_opt_t * options, /* repack options */
pack_info_t *obj /*OUT*/) /* info about object to filter */
{
int idx, i;
pack_info_t tmp;
init_packobject(&tmp);
idx = aux_find_obj(name, options, &tmp);
/* name was on input */
if (idx >= 0) {
/* applying to all objects */
if (options->all_layout) {
/* assign the global layout info to the OBJ info */
tmp.layout = options->layout_g;
switch (options->layout_g) {
case H5D_CHUNKED:
tmp.chunk.rank = options->chunk_g.rank;
for (i = 0; i < tmp.chunk.rank; i++)
tmp.chunk.chunk_lengths[i] = options->chunk_g.chunk_lengths[i];
break;
case H5D_LAYOUT_ERROR:
case H5D_COMPACT:
case H5D_CONTIGUOUS:
case H5D_NLAYOUTS:
break;
default:
break;
} /*switch*/
}
else {
tmp.layout = options->op_tbl->objs[idx].layout;
switch (tmp.layout) {
case H5D_CHUNKED:
tmp.chunk.rank = options->op_tbl->objs[idx].chunk.rank;
for (i = 0; i < tmp.chunk.rank; i++)
tmp.chunk.chunk_lengths[i] = options->op_tbl->objs[idx].chunk.chunk_lengths[i];
break;
case H5D_LAYOUT_ERROR:
case H5D_COMPACT:
case H5D_CONTIGUOUS:
case H5D_NLAYOUTS:
break;
default:
break;
} /*switch*/
}
/* applying to all objects */
if (options->all_filter) {
/* assign the global filter */
tmp.nfilters = 1;
tmp.filter[0] = options->filter_g[0];
} /* if all */
else {
tmp.nfilters = options->op_tbl->objs[idx].nfilters;
for (i = 0; i < tmp.nfilters; i++) {
tmp.filter[i] = options->op_tbl->objs[idx].filter[i];
}
}
} /* if idx */
/* no input name */
else {
if (options->all_filter) {
int k;
/* assign the global filters */
tmp.nfilters = options->n_filter_g;
for (k = 0; k < options->n_filter_g; k++)
tmp.filter[k] = options->filter_g[k];
}
if (options->all_layout) {
/* assign the global layout info to the OBJ info */
tmp.layout = options->layout_g;
switch (options->layout_g) {
case H5D_CHUNKED:
tmp.chunk.rank = options->chunk_g.rank;
for (i = 0; i < tmp.chunk.rank; i++)
tmp.chunk.chunk_lengths[i] = options->chunk_g.chunk_lengths[i];
break;
case H5D_LAYOUT_ERROR:
case H5D_COMPACT:
case H5D_CONTIGUOUS:
case H5D_NLAYOUTS:
break;
default:
break;
} /*switch*/
}
}
*obj = tmp;
return 1;
}
/*-------------------------------------------------------------------------
* Function: apply_filters
*
* Purpose: apply the filters in the object to the property list;
* do extra checking in the case of SZIP; delete all filters in the case
* of H5Z_FILTER_NONE present in the PACK_INFO_T filter array
*
* Return: 0 success, -1 an error occured
*-------------------------------------------------------------------------
*/
int
apply_filters(const char * name, /* object name from traverse list */
int rank, /* rank of dataset */
const hsize_t *dims, /* dimensions of dataset */
size_t msize, /* size of type */
hid_t dcpl_id, /* dataset creation property list */
pack_opt_t * options, /* repack options */
int * has_filter) /* (OUT) object NAME has a filter */
{
int nfilters; /* number of filters in DCPL */
hsize_t chsize[64]; /* chunk size in elements */
H5D_layout_t layout;
int i;
pack_info_t obj;
pack_info_t filtobj;
int ret_value = 0;
*has_filter = 0;
if (rank == 0) /* scalar dataset, do not apply */
H5TOOLS_GOTO_DONE(0);
/*-------------------------------------------------------------------------
* initialize the assigment object
*-------------------------------------------------------------------------
*/
init_packobject(&obj);
init_packobject(&filtobj);
/*-------------------------------------------------------------------------
* find options
*-------------------------------------------------------------------------
*/
if (aux_assign_obj(name, options, &obj) == 0)
H5TOOLS_GOTO_DONE(0);
/* get information about input filters */
if ((nfilters = H5Pget_nfilters(dcpl_id)) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pget_nfilters failed");
/*-------------------------------------------------------------------------
* check if we have filters in the pipeline
* we want to replace them with the input filters
* only remove if we are inserting new ones
*-------------------------------------------------------------------------
*/
if (nfilters && obj.nfilters) {
*has_filter = 1;
if (H5Premove_filter(dcpl_id, H5Z_FILTER_ALL) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Premove_filter failed");
}
else if (nfilters) {
*has_filter = 1;
if (aux_copy_obj(dcpl_id, name, &filtobj) < 0)
H5TOOLS_GOTO_ERROR((-1), "aux_copy_obj failed");
}
/*-------------------------------------------------------------------------
* check if there is an existent chunk
* read it only if there is not a requested layout
*-------------------------------------------------------------------------
*/
if (obj.layout == -1) {
if ((layout = H5Pget_layout(dcpl_id)) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pget_layout failed");
if (layout == H5D_CHUNKED) {
if ((rank = H5Pget_chunk(dcpl_id, NELMTS(chsize), chsize /*out*/)) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pget_chunk failed");
obj.layout = H5D_CHUNKED;
obj.chunk.rank = rank;
for (i = 0; i < rank; i++)
obj.chunk.chunk_lengths[i] = chsize[i];
}
}
/*-------------------------------------------------------------------------
* the type of filter and additional parameter
* type can be one of the filters
* H5Z_FILTER_NONE 0 , uncompress if compressed
* H5Z_FILTER_DEFLATE 1 , deflation like gzip
* H5Z_FILTER_SHUFFLE 2 , shuffle the data
* H5Z_FILTER_FLETCHER32 3 , fletcher32 checksum of EDC
* H5Z_FILTER_SZIP 4 , szip compression
* H5Z_FILTER_NBIT 5 , nbit compression
* H5Z_FILTER_SCALEOFFSET 6 , scaleoffset compression
*-------------------------------------------------------------------------
*/
if (obj.nfilters) {
/*-------------------------------------------------------------------------
* filters require CHUNK layout; if we do not have one define a default
*-------------------------------------------------------------------------
*/
if (obj.layout == -1) {
/* stripmine info */
hsize_t sm_size[H5S_MAX_RANK]; /*stripmine size */
hsize_t sm_nbytes; /*bytes per stripmine */
obj.chunk.rank = rank;
/*
* determine the strip mine size. The strip mine is
* a hyperslab whose size is manageable.
*/
sm_nbytes = msize;
for (i = rank; i > 0; --i) {
hsize_t size = 0;
if (sm_nbytes == 0)
H5TOOLS_GOTO_ERROR((-1), "number of bytes per stripmine must be > 0");
size = H5TOOLS_BUFSIZE / sm_nbytes;
if (size == 0) /* datum size > H5TOOLS_BUFSIZE */
size = 1;
sm_size[i - 1] = MIN(dims[i - 1], size);
sm_nbytes *= sm_size[i - 1];
}
for (i = 0; i < rank; i++) {
obj.chunk.chunk_lengths[i] = sm_size[i];
}
}
for (i = 0; i < obj.nfilters; i++) {
if (obj.filter[i].filtn < 0)
H5TOOLS_GOTO_ERROR((-1), "invalid filter");
switch (obj.filter[i].filtn) {
/*-------------------------------------------------------------------------
* H5Z_FILTER_NONE 0 , uncompress if compressed
*-------------------------------------------------------------------------
*/
case H5Z_FILTER_NONE:
break;
/*-------------------------------------------------------------------------
* H5Z_FILTER_DEFLATE 1 , deflation like gzip
*-------------------------------------------------------------------------
*/
case H5Z_FILTER_DEFLATE: {
unsigned aggression; /* the deflate level */
aggression = obj.filter[i].cd_values[0];
/* set up for deflated data */
if (H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_chunk failed");
if (H5Pset_deflate(dcpl_id, aggression) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_deflate failed");
} break;
/*-------------------------------------------------------------------------
* H5Z_FILTER_SZIP 4 , szip compression
*-------------------------------------------------------------------------
*/
case H5Z_FILTER_SZIP: {
unsigned options_mask;
unsigned pixels_per_block;
options_mask = obj.filter[i].cd_values[0];
pixels_per_block = obj.filter[i].cd_values[1];
/* set up for szip data */
if (H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_chunk failed");
if (H5Pset_szip(dcpl_id, options_mask, pixels_per_block) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_szip failed");
} break;
/*-------------------------------------------------------------------------
* H5Z_FILTER_SHUFFLE 2 , shuffle the data
*-------------------------------------------------------------------------
*/
case H5Z_FILTER_SHUFFLE:
if (H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_chunk failed");
if (H5Pset_shuffle(dcpl_id) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_shuffle failed");
break;
/*-------------------------------------------------------------------------
* H5Z_FILTER_FLETCHER32 3 , fletcher32 checksum of EDC
*-------------------------------------------------------------------------
*/
case H5Z_FILTER_FLETCHER32:
if (H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_chunk failed");
if (H5Pset_fletcher32(dcpl_id) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_fletcher32 failed");
break;
/*----------- -------------------------------------------------------------
* H5Z_FILTER_NBIT , NBIT compression
*-------------------------------------------------------------------------
*/
case H5Z_FILTER_NBIT:
if (H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_chunk failed");
if (H5Pset_nbit(dcpl_id) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_nbit failed");
break;
/*----------- -------------------------------------------------------------
* H5Z_FILTER_SCALEOFFSET , scale+offset compression
*-------------------------------------------------------------------------
*/
case H5Z_FILTER_SCALEOFFSET: {
H5Z_SO_scale_type_t scale_type;
int scale_factor;
scale_type = (H5Z_SO_scale_type_t)obj.filter[i].cd_values[0];
scale_factor = (int)obj.filter[i].cd_values[1];
if (H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_chunk failed");
if (H5Pset_scaleoffset(dcpl_id, scale_type, scale_factor) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_scaleoffset failed");
} break;
default: {
if (H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_chunk failed");
if (H5Pset_filter(dcpl_id, obj.filter[i].filtn, obj.filter[i].filt_flag,
obj.filter[i].cd_nelmts, obj.filter[i].cd_values) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_filter failed");
} break;
} /* switch */
} /*i*/
}
/*obj.nfilters*/
if (filtobj.nfilters) {
for (i = 0; i < filtobj.nfilters; i++) {
if (filtobj.filter[i].filtn < 0)
H5TOOLS_GOTO_ERROR((-1), "invalid filter");
if (H5Zfilter_avail(filtobj.filter[i].filtn) <= 0)
H5TOOLS_GOTO_ERROR((-1), "%d filter unavailable", filtobj.filter[i].filtn);
} /* for */
} /* nfilters */
/*-------------------------------------------------------------------------
* layout
*-------------------------------------------------------------------------
*/
if (obj.layout >= 0) {
/* a layout was defined */
if (H5Pset_layout(dcpl_id, obj.layout) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_layout failed");
if (H5D_CHUNKED == obj.layout) {
if (H5Pset_chunk(dcpl_id, obj.chunk.rank, obj.chunk.chunk_lengths) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_chunk failed");
}
else if (H5D_COMPACT == obj.layout) {
if (H5Pset_alloc_time(dcpl_id, H5D_ALLOC_TIME_EARLY) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Pset_alloc_time failed");
}
/* remove filters for the H5D_CONTIGUOUS case */
else if (H5D_CONTIGUOUS == obj.layout) {
if (H5Premove_filter(dcpl_id, H5Z_FILTER_ALL) < 0)
H5TOOLS_GOTO_ERROR((-1), "H5Premove_filter failed");
}
}
done:
return ret_value;
}
|