| 1 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 2 |
* Copyright by The HDF Group. * |
| 3 |
* All rights reserved. * |
| 4 |
* * |
| 5 |
* This file is part of the HDF5 LZ4 filter plugin source. The full * |
| 6 |
* copyright notice, including terms governing use, modification, and * |
| 7 |
* terms governing use, modification, and redistribution, is contained in * |
| 8 |
* the file COPYING, which can be found at the root of the LZ4 source code * |
| 9 |
* distribution tree. If you do not have access to this file, you may * |
| 10 |
* request a copy from [email protected]. * |
| 11 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 12 |
|
| 13 |
/************************************************************ |
| 14 |
|
| 15 |
This example shows how to write data and read it from a dataset |
| 16 |
using lz4 compression. |
| 17 |
lz4 filter is not available in HDF5. |
| 18 |
The example uses a new feature available in HDF5 version 1.8.11 |
| 19 |
to discover, load and register filters at run time. |
| 20 |
|
| 21 |
************************************************************/ |
| 22 |
|
| 23 |
#include <hdf5.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
|
| 27 |
#define FILE "h5ex_d_lz4.h5" |
| 28 |
#define DATASET "DS1" |
| 29 |
#define DIM0 32 |
| 30 |
#define DIM1 64 |
| 31 |
#define CHUNK0 4 |
| 32 |
#define CHUNK1 8 |
| 33 |
#define H5Z_FILTER_LZ4 32004 |
| 34 |
|
| 35 |
int |
| 36 |
main (void) |
| 37 |
{ |
| 38 |
hid_t file_id = -1; /* Handles */ |
| 39 |
hid_t space_id = -1; /* Handles */ |
| 40 |
hid_t dset_id = -1; /* Handles */ |
| 41 |
hid_t dcpl_id = -1; /* Handles */ |
| 42 |
herr_t status; |
| 43 |
htri_t avail; |
| 44 |
H5Z_filter_t filter_id = 0; |
| 45 |
char filter_name[80]; |
| 46 |
hsize_t dims[2] = {DIM0, DIM1}, |
| 47 |
chunk[2] = {CHUNK0, CHUNK1}; |
| 48 |
size_t nelmts = 1; /* number of elements in cd_values */ |
| 49 |
unsigned int flags; |
| 50 |
unsigned filter_config; |
| 51 |
const unsigned int cd_values[1] = {3}; /* lz4 default is 3 */ |
| 52 |
unsigned int values_out[1] = {99}; |
| 53 |
int wdata[DIM0][DIM1], /* Write buffer */ |
| 54 |
rdata[DIM0][DIM1], /* Read buffer */ |
| 55 |
max; |
| 56 |
hsize_t i, j; |
| 57 |
int ret_value = 1; |
| 58 |
|
| 59 |
/* |
| 60 |
* Initialize data. |
| 61 |
*/ |
| 62 |
for (i=0; i<DIM0; i++) |
| 63 |
for (j=0; j<DIM1; j++) |
| 64 |
wdata[i][j] = i * j - j; |
| 65 |
|
| 66 |
/* |
| 67 |
* Create a new file using the default properties. |
| 68 |
*/ |
| 69 |
file_id = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
| 70 |
if (file_id < 0) goto done; |
| 71 |
|
| 72 |
/* |
| 73 |
* Create dataspace. Setting maximum size to NULL sets the maximum |
| 74 |
* size to be the current size. |
| 75 |
*/ |
| 76 |
space_id = H5Screate_simple (2, dims, NULL); |
| 77 |
if (space_id < 0) goto done; |
| 78 |
|
| 79 |
/* |
| 80 |
* Create the dataset creation property list, add the gzip |
| 81 |
* compression filter and set the chunk size. |
| 82 |
*/ |
| 83 |
dcpl_id = H5Pcreate (H5P_DATASET_CREATE); |
| 84 |
if (dcpl_id < 0) goto done; |
| 85 |
|
| 86 |
status = H5Pset_filter (dcpl_id, H5Z_FILTER_LZ4, H5Z_FLAG_MANDATORY, (size_t)1, cd_values); |
| 87 |
if (status < 0) goto done; |
| 88 |
|
| 89 |
/* |
| 90 |
* Check that filter is registered with the library now. |
| 91 |
* If it is registered, retrieve filter's configuration. |
| 92 |
*/ |
| 93 |
avail = H5Zfilter_avail(H5Z_FILTER_LZ4); |
| 94 |
if (avail) { |
| 95 |
status = H5Zget_filter_info (H5Z_FILTER_LZ4, &filter_config); |
| 96 |
if ( (filter_config & H5Z_FILTER_CONFIG_ENCODE_ENABLED) && |
| 97 |
(filter_config & H5Z_FILTER_CONFIG_DECODE_ENABLED) ) |
| 98 |
printf ("lz4 filter is available for encoding and decoding.\n"); |
| 99 |
} else { |
| 100 |
printf ("failed to set filter.\n"); |
| 101 |
goto done; |
| 102 |
} |
| 103 |
|
| 104 |
status = H5Pset_chunk (dcpl_id, 2, chunk); |
| 105 |
if (status < 0) printf ("failed to set chunk.\n"); |
| 106 |
|
| 107 |
/* |
| 108 |
* Create the dataset. |
| 109 |
*/ |
| 110 |
printf ("....Writing lz4 compressed data ................\n"); |
| 111 |
dset_id = H5Dcreate (file_id, DATASET, H5T_STD_I32LE, space_id, H5P_DEFAULT, dcpl_id, H5P_DEFAULT); |
| 112 |
if (dset_id < 0) goto done; |
| 113 |
|
| 114 |
/* |
| 115 |
* Write the data to the dataset. |
| 116 |
*/ |
| 117 |
status = H5Dwrite (dset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0]); |
| 118 |
if (status < 0) printf ("failed to write data.\n"); |
| 119 |
|
| 120 |
/* |
| 121 |
* Close and release resources. |
| 122 |
*/ |
| 123 |
H5Dclose (dset_id); |
| 124 |
dset_id = -1; |
| 125 |
H5Pclose (dcpl_id); |
| 126 |
dcpl_id = -1; |
| 127 |
H5Sclose (space_id); |
| 128 |
space_id = -1; |
| 129 |
H5Fclose (file_id); |
| 130 |
file_id = -1; |
| 131 |
status = H5close(); |
| 132 |
if (status < 0) { |
| 133 |
printf ("/nFAILED to close library/n"); |
| 134 |
goto done; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
printf ("....Close the file and reopen for reading ........\n"); |
| 139 |
/* |
| 140 |
* Now we begin the read section of this example. |
| 141 |
*/ |
| 142 |
|
| 143 |
/* |
| 144 |
* Open file and dataset using the default properties. |
| 145 |
*/ |
| 146 |
file_id = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); |
| 147 |
if (file_id < 0) goto done; |
| 148 |
|
| 149 |
dset_id = H5Dopen (file_id, DATASET, H5P_DEFAULT); |
| 150 |
if (dset_id < 0) goto done; |
| 151 |
|
| 152 |
/* |
| 153 |
* Retrieve dataset creation property list. |
| 154 |
*/ |
| 155 |
dcpl_id = H5Dget_create_plist (dset_id); |
| 156 |
if (dcpl_id < 0) goto done; |
| 157 |
|
| 158 |
/* |
| 159 |
* Retrieve and print the filter id, compression level and filter's name for lz4. |
| 160 |
*/ |
| 161 |
filter_id = H5Pget_filter2 (dcpl_id, (unsigned) 0, &flags, &nelmts, values_out, sizeof(filter_name), filter_name, NULL); |
| 162 |
printf ("Filter info is available from the dataset creation property \n "); |
| 163 |
printf (" Filter identifier is "); |
| 164 |
switch (filter_id) { |
| 165 |
case H5Z_FILTER_LZ4: |
| 166 |
printf ("%d\n", filter_id); |
| 167 |
printf (" Number of parameters is %d with the value %u\n", nelmts, values_out[0]); |
| 168 |
printf (" To find more about the filter check %s\n", filter_name); |
| 169 |
break; |
| 170 |
default: |
| 171 |
printf ("Not expected filter\n"); |
| 172 |
break; |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* Read the data using the default properties. |
| 177 |
*/ |
| 178 |
printf ("....Reading lz4 compressed data ................\n"); |
| 179 |
status = H5Dread (dset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]); |
| 180 |
if (status < 0) printf ("failed to read data.\n"); |
| 181 |
|
| 182 |
/* |
| 183 |
* Find the maximum value in the dataset, to verify that it was |
| 184 |
* read correctly. |
| 185 |
*/ |
| 186 |
max = rdata[0][0]; |
| 187 |
for (i=0; i<DIM0; i++) |
| 188 |
for (j=0; j<DIM1; j++) { |
| 189 |
/*printf("%d \n", rdata[i][j]); */ |
| 190 |
if (max < rdata[i][j]) |
| 191 |
max = rdata[i][j]; |
| 192 |
} |
| 193 |
/* |
| 194 |
* Print the maximum value. |
| 195 |
*/ |
| 196 |
printf ("Maximum value in %s is %d\n", DATASET, max); |
| 197 |
/* |
| 198 |
* Check that filter is registered with the library now. |
| 199 |
*/ |
| 200 |
avail = H5Zfilter_avail(H5Z_FILTER_LZ4); |
| 201 |
if (avail) |
| 202 |
printf ("lz4 filter is available now since H5Dread triggered loading of the filter.\n"); |
| 203 |
|
| 204 |
ret_value = 0; |
| 205 |
|
| 206 |
done: |
| 207 |
/* |
| 208 |
* Close and release resources. |
| 209 |
*/ |
| 210 |
if (dcpl_id >= 0) H5Pclose (dcpl_id); |
| 211 |
if (dset_id >= 0) H5Dclose (dset_id); |
| 212 |
if (space_id >= 0) H5Sclose (space_id); |
| 213 |
if (file_id >= 0) H5Fclose (file_id); |
| 214 |
|
| 215 |
return ret_value; |
| 216 |
} |