Debian Codegraph @

src/SZ3/tools/H5Z-SZ3/test/dsz3FromHDF5.cpp

raw
1 /**
2 * @file dszFromHDF5.c
3 * @author Sheng Di
4 * @date July, 2017
5 * @brief This is an example of using decompression interface (HDF5)
6 * (C) 2017 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
7 * See COPYRIGHT in top-level directory.
8 */
9
10 #include <errno.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <dlfcn.h>
14 #include "hdf5.h"
15 #include "H5Z_SZ3.hpp"
16
17 #define DATASET "testdata_compressed"
18 #define MAX_CHUNK_SIZE 4294967295 //2^32-1
19
20 int main(int argc, char * argv[])
21 {
22 int dimSize = 0;
23 size_t r5=0,r4=0,r3=0,r2=0,r1=0,nbEle = 0;
24 char hdf5FilePath[640], outputFilePath[640];
25 hid_t file, dset, dcpl, space_id, dtype; /*Handles*/
26 hid_t fid, sid, cpid, idsid; /*Output Handles*/
27 H5Z_filter_t filter_id = 0;
28 herr_t status;
29 H5T_class_t type_class;
30 H5T_sign_t dsign;
31 H5T_order_t dorder;
32
33 htri_t avail;
34 char filter_name[80];
35 unsigned int flags = 0;
36 size_t nelmts = 0, dsize;
37 unsigned int values_out[7] = {0,0,0,0,0,0,0}; //at most 7 parameters
38
39 //hold dims
40 hsize_t dims[H5S_MAX_RANK], dims_used[5] = {0,0,0,0,0};
41 int ndims, dim;
42
43
44 if(argc < 2)
45 {
46 printf("Test case: dszFromHDF5 [hdf5FilePath]\n");
47 printf("Example 1: dszFromHDF5 testdata/x86/testfloat_8_8_128.dat.sz3.h5\n");
48 printf("Example 2: dszFromHDF5 testdata/x86/testint32_8x8x8.dat.sz3.h5\n");
49 exit(0);
50 }
51
52 sprintf(hdf5FilePath, "%s", argv[1]);
53 sprintf(outputFilePath, "%s.out.h5", hdf5FilePath);
54
55 /*Open the hdf5 file with SZ-compressed data*/
56 file = H5Fopen(hdf5FilePath, H5F_ACC_RDONLY, H5P_DEFAULT);
57 dset = H5Dopen(file, DATASET, H5P_DEFAULT);
58
59 /*Retrieve dataset creation property list.*/
60 dcpl = H5Dget_create_plist(dset);
61
62 /*Check that filter is not registered with the library yet*/
63 avail = H5Zfilter_avail(H5Z_FILTER_SZ3);
64 if(!avail)
65 printf("sz3 filter is not yet available after the H5Pget_filter call.\n");
66 else
67 printf("sz3 filter is available.\n");
68
69 space_id = H5Dget_space(dset);
70 nbEle = H5Sget_simple_extent_npoints(space_id);
71
72 if((dtype = H5Dget_type(dset)) < 0)
73 printf("Error: H5Dget_type(dset) < 0\n");
74
75 /*Read the data using the default properties.*/
76 printf("....Reading SZ3 compressed data .....................\n");
77
78 if((type_class = H5Tget_class(dtype)) < 0)
79 {
80 printf("Error: H5Tget_class<0\n");
81 exit(0);
82 }
83 if (0 == (dsize = H5Tget_size(dtype)))
84 {
85 printf("Error: H5Tget_size==0\n");
86 exit(0);
87 }
88
89 if((dorder = H5Tget_order(dtype)) < 0)
90 printf("Error: H5Tget_order<0\n");
91
92 //create output file /*Get Dimensions for writing*/
93
94
95 if (0 > (ndims = H5Sget_simple_extent_dims(space_id, dims, 0))) ERROR(H5Sget_simple_extent_dims);
96
97 int ndims_used = 0;
98 for(int i = 0; i < ndims; i++){
99 if(dims[i] <= 1) continue;
100 dims_used[ndims_used] = dims[i];
101 ndims_used++;
102 }
103
104 dim = ndims_used;
105
106
107 /* create HDF5 file */
108 if (0 > (fid = H5Fcreate(outputFilePath, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT))) ERROR(H5Fcreate);
109
110 /*Create dataspace. Setting maximum size */
111 if (0 > (sid = H5Screate_simple(dim, dims, NULL))) ERROR(H5Screate_simple);
112
113 /* setup dataset creation properties */
114 if (0 > (cpid = H5Pcreate(H5P_DATASET_CREATE))) ERROR(H5Pcreate);
115
116
117 switch (type_class)
118 {
119 case H5T_FLOAT:
120 if (H5Tequal(dtype, H5T_IEEE_F32BE) == 1 || H5Tequal(dtype, H5T_IEEE_F32LE) == 1
121 || H5Tequal(dtype, H5T_NATIVE_FLOAT) == 1)
122 {
123 printf("data type: float\n");
124 float* data = (float*)malloc(sizeof(float)*nbEle);
125 if(dorder==H5T_ORDER_LE)
126 status = H5Dread(dset, H5T_IEEE_F32LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
127 else //H5T_ORDER_BE
128 status = H5Dread(dset, H5T_IEEE_F32BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
129 /*Print the first 20 data values to check the correctness.*/
130 int i;
131 printf("reconstructed data = ");
132 for(i=0;i<20;i++)
133 printf("%f ", data[i]);
134 printf("\n");
135
136 //write out file data
137 if (0 > (idsid = H5Dcreate(fid, DATASET, H5T_IEEE_F32LE, sid, H5P_DEFAULT, cpid, H5P_DEFAULT))) ERROR(H5Dcreate);
138 if (0 > H5Dwrite(idsid, H5T_IEEE_F32LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data)) ERROR(H5Dwrite);
139 if (0 > H5Dclose(idsid)) ERROR(H5Dclose);
140
141
142 free(data);
143 }
144 else //64bit: double
145 {
146 printf("data type: double\n");
147 double* data = (double*)malloc(sizeof(double)*nbEle);
148 if(dorder==H5T_ORDER_LE)
149 status = H5Dread(dset, H5T_IEEE_F64LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
150 else
151 status = H5Dread(dset, H5T_IEEE_F64BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
152 /*Print the first 10 data values to check the correctness.*/
153 int i;
154 printf("reconstructed data = ");
155 for(i=0;i<20;i++)
156 printf("%f ", data[i]);
157 printf("\n");
158 free(data);
159 }
160 break;
161 case H5T_INTEGER:
162 if (0 > (dsign = H5Tget_sign(dtype)))
163 {
164 printf("Error in calling H5Tget_sign(type_id)....\n");
165 exit(0);
166 }
167 if(dsign == H5T_SGN_NONE) //unsigned
168 {
169 if(dsize==1)
170 {
171 printf("data type: unsigned char\n");
172 unsigned char* data = (unsigned char*)malloc(sizeof(unsigned char)*nbEle);
173 if(dorder==H5T_ORDER_LE)
174 status = H5Dread(dset, H5T_STD_U8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
175 else
176 status = H5Dread(dset, H5T_STD_U8BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
177 int i;
178 printf("reconstructed data = ");
179 for(i=0;i<20;i++)
180 printf("%d ", data[i]);
181 printf("\n");
182 free(data);
183 }
184 else if(dsize==2)
185 {
186 printf("data type: unsigned short\n");
187 unsigned short* data = (unsigned short*)malloc(sizeof(unsigned short)*nbEle);
188 if(dorder==H5T_ORDER_LE)
189 status = H5Dread(dset, H5T_STD_U16LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
190 else
191 status = H5Dread(dset, H5T_STD_U16BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
192 int i;
193 printf("reconstructed data = ");
194 for(i=0;i<20;i++)
195 printf("%d ", data[i]);
196 printf("\n");
197 free(data);
198 }
199 else if(dsize==4)
200 {
201 printf("data type: unsigned int\n");
202 unsigned int* data = (unsigned int*)malloc(sizeof(unsigned int)*nbEle);
203 if(dorder==H5T_ORDER_LE)
204 status = H5Dread(dset, H5T_STD_U32LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
205 else
206 status = H5Dread(dset, H5T_STD_U32BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
207 int i;
208 printf("reconstructed data = ");
209 for(i=0;i<20;i++)
210 printf("%d ", data[i]);
211 printf("\n");
212 free(data);
213 }
214 else if(dsize==8)
215 {
216 printf("data type: unsigned long\n");
217 uint64_t* data = (uint64_t*)malloc(sizeof(uint64_t)*nbEle);
218 if(dorder==H5T_ORDER_LE)
219 status = H5Dread(dset, H5T_STD_U64LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
220 else
221 status = H5Dread(dset, H5T_STD_U64BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
222 int i;
223 printf("reconstructed data = ");
224 for(i=0;i<20;i++)
225 printf("%ld ", data[i]);
226 printf("\n");
227 free(data);
228 }
229 }
230 else
231 {
232 if(dsize==1)
233 {
234 printf("data type: char\n");
235 char *data = (char*)malloc(sizeof(char)*nbEle);
236 if(dorder==H5T_ORDER_LE)
237 status = H5Dread(dset, H5T_STD_I8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
238 else
239 status = H5Dread(dset, H5T_STD_I8BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
240 int i;
241 printf("reconstructed data = ");
242 for(i=0;i<20;i++)
243 printf("%d ", data[i]);
244 printf("\n");
245 free(data);
246 }
247 else if(dsize==2)
248 {
249 printf("data type: short\n");
250 short *data = (short*)malloc(sizeof(short)*nbEle);
251 if(dorder==H5T_ORDER_LE)
252 status = H5Dread(dset, H5T_STD_I16LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
253 else
254 status = H5Dread(dset, H5T_STD_I16BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
255 int i;
256 printf("reconstructed data = ");
257 for(i=0;i<20;i++)
258 printf("%d ", data[i]);
259 printf("\n");
260 free(data);
261 }
262 else if(dsize==4)
263 {
264 printf("data type: int\n");
265 int *data = (int*)malloc(sizeof(int)*nbEle);
266 if(dorder==H5T_ORDER_LE)
267 status = H5Dread(dset, H5T_STD_I32LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
268 else
269 status = H5Dread(dset, H5T_STD_I32BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
270 int i;
271 printf("reconstructed data = ");
272 for(i=0;i<20;i++)
273 printf("%d ", data[i]);
274 printf("\n");
275 free(data);
276 }
277 else if(dsize==8)
278 {
279 printf("data type: long\n");
280 int64_t *data = (int64_t*)malloc(sizeof(int64_t)*nbEle);
281 if(dorder==H5T_ORDER_LE)
282 status = H5Dread(dset, H5T_STD_I64LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
283 else
284 status = H5Dread(dset, H5T_STD_I64BE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
285 int i;
286 printf("reconstructed data = ");
287 for(i=0;i<20;i++)
288 printf("%ld ", data[i]);
289 printf("\n");
290 free(data);
291 }
292 }
293
294 break;
295 default:
296 printf("Error: H5Z-SZ3 supports only float, double or integers.\n");
297 exit(1);
298 }
299
300 //Close reading resources
301 status = H5Pclose(dcpl);
302 status = H5Dclose(dset);
303 status = H5Fclose(file);
304
305 /*Close and release writing resources*/
306 if (0 > H5Sclose(sid)) ERROR(H5Sclose);
307 if (0 > H5Pclose(cpid)) ERROR(H5Pclose);
308 if (0 > H5Fclose(fid)) ERROR(H5Fclose);
309
310 return 0;
311 }