|
20 | 20 |
|
21 | 21 | ************************************************************/ |
22 | 22 |
|
23 | | -#include "hdf5.h" |
24 | 23 | #include <stdio.h> |
25 | 24 | #include <stdlib.h> |
| 25 | +#include "hdf5.h" |
26 | 26 |
|
27 | 27 | #define FILENAME "h5ex_d_lz4.h5" |
| 28 | +#define FILENAME_ENC "h5ex_d_lz4_enc.h5" |
28 | 29 | #define DATASET "DS1" |
29 | 30 | #define DIM0 32 |
30 | 31 | #define DIM1 64 |
31 | 32 | #define CHUNK0 4 |
32 | 33 | #define CHUNK1 8 |
33 | 34 | #define H5Z_FILTER_LZ4 32004 |
34 | 35 |
|
| 36 | +static int |
| 37 | +write_lz4_dataset(hid_t file_id, hid_t space_id, const hsize_t chunk[2], const char *name, size_t nelmts, |
| 38 | + const unsigned int *cd_values, const int *wdata) |
| 39 | +{ |
| 40 | + hid_t dcpl_id = H5I_INVALID_HID; |
| 41 | + hid_t dset_id = H5I_INVALID_HID; |
| 42 | + int ret = -1; |
| 43 | + |
| 44 | + if ((dcpl_id = H5Pcreate(H5P_DATASET_CREATE)) < 0) |
| 45 | + goto done; |
| 46 | + if (H5Pset_filter(dcpl_id, H5Z_FILTER_LZ4, H5Z_FLAG_MANDATORY, nelmts, cd_values) < 0) |
| 47 | + goto done; |
| 48 | + if (H5Pset_chunk(dcpl_id, 2, chunk) < 0) |
| 49 | + goto done; |
| 50 | + if ((dset_id = H5Dcreate(file_id, name, H5T_STD_I32LE, space_id, H5P_DEFAULT, dcpl_id, H5P_DEFAULT)) < 0) |
| 51 | + goto done; |
| 52 | + if (H5Dwrite(dset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata) < 0) |
| 53 | + goto done; |
| 54 | + |
| 55 | + ret = 0; |
| 56 | +done: |
| 57 | + if (dset_id >= 0) |
| 58 | + H5Dclose(dset_id); |
| 59 | + if (dcpl_id >= 0) |
| 60 | + H5Pclose(dcpl_id); |
| 61 | + return ret; |
| 62 | +} |
| 63 | + |
| 64 | +static int |
| 65 | +read_and_check(hid_t file_id, const char *name, int expected_max, hsize_t *out_storage) |
| 66 | +{ |
| 67 | + hid_t dset_id = H5I_INVALID_HID; |
| 68 | + int rdata[DIM0][DIM1]; |
| 69 | + int i, j, max_seen; |
| 70 | + hsize_t storage = 0; |
| 71 | + int ret = -1; |
| 72 | + |
| 73 | + if ((dset_id = H5Dopen(file_id, name, H5P_DEFAULT)) < 0) |
| 74 | + goto done; |
| 75 | + if (H5Dread(dset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]) < 0) |
| 76 | + goto done; |
| 77 | + storage = H5Dget_storage_size(dset_id); |
| 78 | + max_seen = rdata[0][0]; |
| 79 | + for (i = 0; i < DIM0; i++) |
| 80 | + for (j = 0; j < DIM1; j++) |
| 81 | + if (max_seen < rdata[i][j]) |
| 82 | + max_seen = rdata[i][j]; |
| 83 | + if (max_seen != expected_max) |
| 84 | + goto done; |
| 85 | + if (out_storage) |
| 86 | + *out_storage = storage; |
| 87 | + ret = 0; |
| 88 | +done: |
| 89 | + if (dset_id >= 0) |
| 90 | + H5Dclose(dset_id); |
| 91 | + return ret; |
| 92 | +} |
| 93 | + |
| 94 | +static int |
| 95 | +run_encoder_check(const int *wdata, int expected_max, const hsize_t dims[2], const hsize_t chunk[2]) |
| 96 | +{ |
| 97 | + hid_t file_id = H5I_INVALID_HID; |
| 98 | + hid_t space_id = H5I_INVALID_HID; |
| 99 | + hsize_t size_hc9 = 0, size_hc12 = 0, size_hc99 = 0; |
| 100 | + const unsigned int cd_hc9[2] = {0, 9}; |
| 101 | + const unsigned int cd_hc12[2] = {0, 12}; |
| 102 | + const unsigned int cd_hc99[2] = {0, 99}; |
| 103 | + const unsigned int cd_fast[2] = {0, (unsigned int)(-8)}; /* fast, acceleration 9 */ |
| 104 | + int ret = -1; |
| 105 | + |
| 106 | + if ((file_id = H5Fcreate(FILENAME_ENC, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) |
| 107 | + goto done; |
| 108 | + if ((space_id = H5Screate_simple(2, dims, NULL)) < 0) |
| 109 | + goto done; |
| 110 | + |
| 111 | + if (write_lz4_dataset(file_id, space_id, chunk, "DS_HC9", 2, cd_hc9, wdata) < 0) |
| 112 | + goto done; |
| 113 | + if (write_lz4_dataset(file_id, space_id, chunk, "DS_HC12", 2, cd_hc12, wdata) < 0) |
| 114 | + goto done; |
| 115 | + if (write_lz4_dataset(file_id, space_id, chunk, "DS_HC99", 2, cd_hc99, wdata) < 0) |
| 116 | + goto done; |
| 117 | + if (write_lz4_dataset(file_id, space_id, chunk, "DS_FAST", 2, cd_fast, wdata) < 0) |
| 118 | + goto done; |
| 119 | + |
| 120 | + H5Sclose(space_id); |
| 121 | + space_id = H5I_INVALID_HID; |
| 122 | + H5Fclose(file_id); |
| 123 | + file_id = H5I_INVALID_HID; |
| 124 | + |
| 125 | + if ((file_id = H5Fopen(FILENAME_ENC, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) |
| 126 | + goto done; |
| 127 | + |
| 128 | + if (read_and_check(file_id, "DS_HC9", expected_max, &size_hc9) < 0) |
| 129 | + goto done; |
| 130 | + if (read_and_check(file_id, "DS_HC12", expected_max, &size_hc12) < 0) |
| 131 | + goto done; |
| 132 | + if (read_and_check(file_id, "DS_HC99", expected_max, &size_hc99) < 0) |
| 133 | + goto done; |
| 134 | + if (read_and_check(file_id, "DS_FAST", expected_max, NULL) < 0) |
| 135 | + goto done; |
| 136 | + |
| 137 | + printf("....Encoder selector check ........\n"); |
| 138 | + printf(" DS_HC9: round-trip OK\n"); |
| 139 | + printf(" DS_HC12: round-trip OK\n"); |
| 140 | + printf(" DS_HC99: round-trip OK; storage equals DS_HC12: %s\n", size_hc99 == size_hc12 ? "yes" : "no"); |
| 141 | + printf(" DS_FAST: round-trip OK\n"); |
| 142 | + |
| 143 | + if (size_hc99 == size_hc12) |
| 144 | + ret = 0; |
| 145 | +done: |
| 146 | + if (space_id >= 0) |
| 147 | + H5Sclose(space_id); |
| 148 | + if (file_id >= 0) |
| 149 | + H5Fclose(file_id); |
| 150 | + return ret; |
| 151 | +} |
| 152 | + |
35 | 153 | int |
36 | 154 | main(void) |
37 | 155 | { |
@@ -215,6 +333,11 @@ main(void) |
215 | 333 | if (avail) |
216 | 334 | printf("lz4 filter is available now since H5Dread triggered loading of the filter.\n"); |
217 | 335 |
|
| 336 | + if (run_encoder_check(wdata[0], max, dims, chunk) < 0) { |
| 337 | + printf("Encoder selector check FAILED\n"); |
| 338 | + goto done; |
| 339 | + } |
| 340 | + |
218 | 341 | ret_value = 0; |
219 | 342 |
|
220 | 343 | done: |
|
0 commit comments