source: thirdparty/SZ/sz/src/TightDataPointStorageI.c @ 2c47b73

Revision 2c47b73, 12.4 KB checked in by Hal Finkel <hfinkel@…>, 6 years ago (diff)

more work on adding SZ (latest version)

  • Property mode set to 100644
Line 
1/**
2 *  @file TightPointDataStorageI.c
3 *  @author Sheng Di and Dingwen Tao
4 *  @date Aug, 2016
5 *  @brief The functions used to construct the tightPointDataStorage element for storing compressed bytes.
6 *  (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
7 *      See COPYRIGHT in top-level directory.
8 */
9
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13#include <math.h>
14#include "TightDataPointStorageI.h"
15#include "sz.h"
16#include "Huffman.h"
17//#include "rw.h"
18
19int computeRightShiftBits(int exactByteSize, int dataType)
20{
21        int rightShift = 0; 
22        switch(dataType)
23        {
24        case SZ_INT8:
25        case SZ_UINT8:
26                rightShift = 8 - exactByteSize*8;
27                break;
28        case SZ_INT16:
29        case SZ_UINT16:
30                rightShift = 16 - exactByteSize*8;
31                break;
32        case SZ_INT32:
33        case SZ_UINT32:
34                rightShift = 32 - exactByteSize*8;
35                break;
36        case SZ_INT64:
37        case SZ_UINT64:
38                rightShift = 64 - exactByteSize*8;
39                break;
40        }
41        return rightShift;     
42}
43
44int convertDataTypeSizeCode(int dataTypeSizeCode)
45{
46        int result = 0;
47        switch(dataTypeSizeCode)
48        {
49        case 0:
50                result = 1;
51                break;
52        case 1:
53                result = 2;
54                break;
55        case 2:
56                result = 4;
57                break;
58        case 3:
59                result = 8;
60                break;
61        }
62        return result; 
63}
64
65int convertDataTypeSize(int dataTypeSize)
66{
67        int result = 0;
68        switch(dataTypeSize)
69        {
70        case 1:
71                result = 0; //0000
72                break;
73        case 2:
74                result = 4; //0100
75                break;
76        case 4:
77                result = 8; //1000
78                break;
79        case 8:
80                result = 12; //1100
81                break;
82        }
83        return result;
84}
85
86void new_TightDataPointStorageI_Empty(TightDataPointStorageI **this)
87{
88        *this = (TightDataPointStorageI*)malloc(sizeof(TightDataPointStorageI));
89
90        (*this)->dataSeriesLength = 0;
91        (*this)->allSameData = 0;
92        (*this)->exactDataNum = 0;
93        (*this)->realPrecision = 0;
94        (*this)->minValue = 0;
95        (*this)->exactByteSize = 0;
96
97        (*this)->typeArray = NULL; //its size is dataSeriesLength/4 (or xxx/4+1)
98        (*this)->typeArray_size = 0;
99       
100        (*this)->exactDataBytes = NULL;
101        (*this)->exactDataBytes_size = 0;
102
103        (*this)->intervals = 0;
104        (*this)->isLossless = 0;       
105}
106
107int new_TightDataPointStorageI_fromFlatBytes(TightDataPointStorageI **this, unsigned char* flatBytes, size_t flatBytesLength)
108{
109        new_TightDataPointStorageI_Empty(this);
110        size_t i, index = 0;
111        char version[3];
112        for (i = 0; i < 3; i++)
113                version[i] = flatBytes[index++]; //3
114        unsigned char sameRByte = flatBytes[index++]; //1
115        if(checkVersion(version)!=1)
116        {
117                //wrong version
118                printf("Wrong version: \nCompressed-data version (%d.%d.%d)\n",version[0], version[1], version[2]);
119                printf("Current sz version: (%d.%d.%d)\n", versionNumber[0], versionNumber[1], versionNumber[2]);
120                printf("Please double-check if the compressed data (or file) is correct.\n");
121                exit(0);
122        }
123        int same = sameRByte & 0x01;
124        //conf_params->szMode = (sameRByte & 0x06)>>1;
125        int dataByteSizeCode = (sameRByte & 0x0C)>>2;
126        convertDataTypeSizeCode(dataByteSizeCode); //in bytes
127        (*this)->isLossless = (sameRByte & 0x10)>>4;
128
129        exe_params->SZ_SIZE_TYPE = ((sameRByte & 0x40)>>6)==1?8:4;
130        int errorBoundMode = ABS;
131       
132        sz_params* params = convertBytesToSZParams(&(flatBytes[index]));
133        if(confparams_dec!=NULL)
134                free(confparams_dec);
135        confparams_dec = params;
136        index += MetaDataByteLength; //20       
137       
138        if(same==0)
139                (*this)->exactByteSize = flatBytes[index++]; //1
140       
141        unsigned char dsLengthBytes[8];
142        for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++)
143                dsLengthBytes[i] = flatBytes[index++];
144        (*this)->dataSeriesLength = bytesToSize(dsLengthBytes);// ST
145        if((*this)->isLossless==1)
146        {
147                //(*this)->exactMidBytes = flatBytes+8;
148                return errorBoundMode;
149        }
150        else if(same==1)
151        {
152                (*this)->allSameData = 1;
153                size_t exactDataBytesLength = flatBytesLength - 32;//32=3 + 1 + MetaDataByteLength - 8 (initialized SZ_TYPE_LENGTH);
154                if(exactDataBytesLength>0)
155                        (*this)->exactDataBytes = (unsigned char*)malloc(sizeof(unsigned char)*exactDataBytesLength);
156                else
157                        (*this)->exactDataBytes = NULL;
158                       
159                for(i = 0;i<exactDataBytesLength;i++)
160                        (*this)->exactDataBytes[i] = flatBytes[index++];
161                return errorBoundMode;
162        }
163        else
164                (*this)->allSameData = 0;
165
166        unsigned char byteBuf[8];
167
168        for (i = 0; i < 4; i++)
169                byteBuf[i] = flatBytes[index++];
170        int max_quant_intervals = bytesToInt_bigEndian(byteBuf);// 4   
171
172        confparams_dec->maxRangeRadius = max_quant_intervals/2;
173
174        if(errorBoundMode>=PW_REL)
175        {
176                printf("Error: errorBoundMode>=PW_REL in new_TightDataPointStorageI_fromFlatBytes!! Wrong...\n");
177                exit(0);
178        }
179
180        for (i = 0; i < 4; i++)
181                byteBuf[i] = flatBytes[index++];
182        (*this)->intervals = bytesToInt_bigEndian(byteBuf);// 4
183
184        for (i = 0; i < 8; i++)
185                byteBuf[i] = flatBytes[index++];
186        (*this)->minValue = bytesToLong_bigEndian(byteBuf); //8
187               
188        for (i = 0; i < 8; i++)
189                byteBuf[i] = flatBytes[index++];
190        (*this)->realPrecision = bytesToDouble(byteBuf);//8
191       
192        for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++)
193                byteBuf[i] = flatBytes[index++];
194        (*this)->typeArray_size = bytesToSize(byteBuf);// ST           
195
196        for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++)
197                byteBuf[i] = flatBytes[index++];
198        (*this)->exactDataNum = bytesToSize(byteBuf);// ST
199       
200        for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++)
201                byteBuf[i] = flatBytes[index++];
202        (*this)->exactDataBytes_size = bytesToSize(byteBuf);// ST               
203
204
205        (*this)->typeArray = &flatBytes[index];
206        //retrieve the number of states (i.e., stateNum)
207        (*this)->allNodes = bytesToInt_bigEndian((*this)->typeArray); //the first 4 bytes store the stateNum
208        (*this)->stateNum = ((*this)->allNodes+1)/2;           
209
210        index+=(*this)->typeArray_size;
211       
212        if((*this)->exactDataBytes_size > 0)
213        {       
214                (*this)->exactDataBytes = &flatBytes[index];
215                index+=(*this)->exactDataBytes_size*sizeof(char);       
216        }
217        else
218                (*this)->exactDataBytes = NULL; 
219        return errorBoundMode;
220}
221
222/**
223 *
224 * type's length == dataSeriesLength
225 * exactDataBytes's length == exactDataBytes_size
226 * */
227void new_TightDataPointStorageI(TightDataPointStorageI **this,
228                size_t dataSeriesLength, size_t exactDataNum, int byteSize, 
229                int* type, unsigned char* exactDataBytes, size_t exactDataBytes_size,
230                double realPrecision, long minValue, int intervals, int dataType) 
231{
232        //int i = 0;
233        *this = (TightDataPointStorageI *)malloc(sizeof(TightDataPointStorageI));
234        (*this)->allSameData = 0;
235        (*this)->realPrecision = realPrecision;
236        (*this)->minValue = minValue;
237        switch(dataType)
238        {
239        case SZ_INT8:
240        case SZ_UINT8:
241                (*this)->dataTypeSize = 1;
242                break;
243        case SZ_INT16:
244        case SZ_UINT16:
245                (*this)->dataTypeSize = 2;
246                break;
247        case SZ_INT32:
248        case SZ_UINT32:
249                (*this)->dataTypeSize = 4;
250                break;
251        case SZ_INT64:
252        case SZ_UINT64:
253                (*this)->dataTypeSize = 8;
254                break;
255        }
256
257        (*this)->dataSeriesLength = dataSeriesLength;
258        (*this)->exactDataNum = exactDataNum;
259        (*this)->exactByteSize = byteSize;
260
261
262        int stateNum = 2*intervals;
263        HuffmanTree* huffmanTree = createHuffmanTree(stateNum);
264        encode_withTree(huffmanTree, type, dataSeriesLength, &(*this)->typeArray, &(*this)->typeArray_size);
265        SZ_ReleaseHuffman(huffmanTree);
266               
267        (*this)->exactDataBytes = exactDataBytes;
268        (*this)->exactDataBytes_size = exactDataBytes_size;
269       
270        (*this)->intervals = intervals;
271       
272        (*this)->isLossless = 0;
273}
274
275void convertTDPStoBytes_int(TightDataPointStorageI* tdps, unsigned char* bytes, unsigned char* dsLengthBytes, unsigned char sameByte)
276{
277        size_t i, k = 0;
278       
279        unsigned char byteBuffer[8] = {0,0,0,0,0,0,0,0};
280       
281        for(i = 0;i<3;i++)//3 bytes
282                bytes[k++] = versionNumber[i];
283        bytes[k++] = sameByte;  //1     byte
284       
285        convertSZParamsToBytes(confparams_cpr, &(bytes[k]));
286        k = k + MetaDataByteLength;     
287               
288        bytes[k++] = tdps->exactByteSize; //1 byte
289
290        sizeToBytes(byteBuffer, tdps->dataSeriesLength);
291        for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST: 4 or 8 bytes
292                bytes[k++] = byteBuffer[i];     
293       
294        intToBytes_bigEndian(byteBuffer, confparams_cpr->max_quant_intervals);
295        for(i = 0;i<4;i++)//4
296                bytes[k++] = byteBuffer[i];
297       
298        intToBytes_bigEndian(byteBuffer, tdps->intervals);
299        for(i = 0;i<4;i++)//4
300                bytes[k++] = byteBuffer[i];                     
301       
302        longToBytes_bigEndian(byteBuffer, tdps->minValue);
303        for (i = 0; i < 8; i++)// 8
304                bytes[k++] = byteBuffer[i];
305
306        doubleToBytes(byteBuffer, tdps->realPrecision);
307        for (i = 0; i < 8; i++)// 8
308                bytes[k++] = byteBuffer[i];                     
309
310        sizeToBytes(byteBuffer, tdps->typeArray_size);
311        for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST
312                bytes[k++] = byteBuffer[i];
313
314        sizeToBytes(byteBuffer, tdps->exactDataNum);
315        for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST
316                bytes[k++] = byteBuffer[i];
317
318        sizeToBytes(byteBuffer, tdps->exactDataBytes_size);
319        for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST
320                bytes[k++] = byteBuffer[i];
321
322        memcpy(&(bytes[k]), tdps->typeArray, tdps->typeArray_size);
323        k += tdps->typeArray_size;
324
325        memcpy(&(bytes[k]), tdps->exactDataBytes, tdps->exactDataBytes_size);
326        k += tdps->exactDataBytes_size;
327}
328
329//convert TightDataPointStorageI to bytes...
330void convertTDPStoFlatBytes_int(TightDataPointStorageI *tdps, unsigned char** bytes, size_t *size)
331{
332        size_t i, k = 0; 
333        unsigned char dsLengthBytes[8];
334       
335        if(exe_params->SZ_SIZE_TYPE==4)
336                intToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//4
337        else
338                longToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//8
339
340        unsigned char sameByte = tdps->allSameData==1?(unsigned char)1:(unsigned char)0;
341        sameByte = sameByte | (confparams_cpr->szMode << 1);
342        if(tdps->isLossless)
343                sameByte = (unsigned char) (sameByte | 0x10);
344       
345        int dataTypeSizeCode = convertDataTypeSize(tdps->dataTypeSize);
346        sameByte = (unsigned char) (sameByte | dataTypeSizeCode);
347       
348        if(exe_params->SZ_SIZE_TYPE==8)
349                sameByte = (unsigned char) (sameByte | 0x40); // 01000000, the 6th bit
350       
351        if(tdps->allSameData==1)
352        {
353                size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + tdps->exactDataBytes_size;
354                *bytes = (unsigned char *)malloc(sizeof(unsigned char)*totalByteLength);
355
356                for (i = 0; i < 3; i++)//3
357                        (*bytes)[k++] = versionNumber[i];
358                (*bytes)[k++] = sameByte;//1
359               
360                convertSZParamsToBytes(confparams_cpr, &((*bytes)[k]));
361                k = k + MetaDataByteLength;                     
362               
363                for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++)
364                        (*bytes)[k++] = dsLengthBytes[i];
365               
366                for (i = 0; i < tdps->exactDataBytes_size; i++)
367                        (*bytes)[k++] = tdps->exactDataBytes[i];
368
369                *size = totalByteLength;
370        }
371        else 
372        {
373                if(confparams_cpr->errorBoundMode>=PW_REL)
374                {                       
375                        printf("Error: errorBoundMode >= PW_REL!! can't be...\n");
376                        exit(0);
377                }
378
379                size_t totalByteLength = 3 + 1 + MetaDataByteLength + 1 + exe_params->SZ_SIZE_TYPE + 4 + 4 + 8 + 8
380                                + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE
381                                + tdps->typeArray_size + tdps->exactDataBytes_size;
382
383                *bytes = (unsigned char *)malloc(sizeof(unsigned char)*totalByteLength);
384
385                convertTDPStoBytes_int(tdps, *bytes, dsLengthBytes, sameByte);
386               
387                *size = totalByteLength;
388        }
389}
390
391void convertTDPStoFlatBytes_int_args(TightDataPointStorageI *tdps, unsigned char* bytes, size_t *size)
392{
393        size_t i, k = 0; 
394        unsigned char dsLengthBytes[8];
395       
396        if(exe_params->SZ_SIZE_TYPE==4)
397                intToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//4
398        else
399                longToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//8
400               
401        unsigned char sameByte = tdps->allSameData==1?(unsigned char)1:(unsigned char)0;
402        sameByte = sameByte | (confparams_cpr->szMode << 1);
403        if(tdps->isLossless)
404                sameByte = (unsigned char) (sameByte | 0x10);
405        if(exe_params->SZ_SIZE_TYPE==8)
406                sameByte = (unsigned char) (sameByte | 0x40); // 01000000, the 6th bit
407               
408        if(tdps->allSameData==1)
409        {
410                size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + tdps->exactDataBytes_size;
411                //*bytes = (unsigned char *)malloc(sizeof(unsigned char)*totalByteLength);
412
413                for (i = 0; i < 3; i++)//3
414                        bytes[k++] = versionNumber[i];
415                bytes[k++] = sameByte;//1
416               
417                convertSZParamsToBytes(confparams_cpr, &(bytes[k]));
418                k = k + MetaDataByteLength;     
419                               
420                for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++)//ST
421                        bytes[k++] = dsLengthBytes[i];         
422                for (i = 0; i < tdps->exactDataBytes_size; i++)
423                        bytes[k++] = tdps->exactDataBytes[i];
424
425                *size = totalByteLength;
426        }
427        else
428        {
429                if(confparams_cpr->errorBoundMode>=PW_REL)
430                {                       
431                        printf("Error: errorBoundMode>=PW_REL!! can't be....\n");
432                        exit(0);
433                }
434
435                size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + 1 + 4 + 4 + 8 + 8
436                                + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE 
437                                + tdps->typeArray_size + tdps->exactDataBytes_size;
438
439                convertTDPStoBytes_int(tdps, bytes, dsLengthBytes, sameByte);
440               
441                *size = totalByteLength;
442        }
443}
444
445void free_TightDataPointStorageI(TightDataPointStorageI *tdps)
446{
447        if(tdps->typeArray!=NULL)
448                free(tdps->typeArray);
449        if(tdps->exactDataBytes!=NULL)
450                free(tdps->exactDataBytes);
451        free(tdps);
452}
453
454void free_TightDataPointStorageI2(TightDataPointStorageI *tdps)
455{
456        free(tdps);
457}
458
459
Note: See TracBrowser for help on using the repository browser.