source: thirdparty/SZ/sz/src/utility.c @ 03ba282

Revision 03ba282, 6.5 KB checked in by Hal Finkel <hfinkel@…>, 6 years ago (diff)

add utility.c

  • Property mode set to 100644
Line 
1/**
2 *  @file utility.c
3 *  @author Sheng Di, Sihuan Li
4 *  @date Aug, 2018
5 *  @brief
6 *  (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
7 *      See COPYRIGHT in top-level directory.
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <math.h>
14#include "utility.h"
15#include "sz.h"
16#include "callZlib.h"
17#include "zstd.h"
18
19int compare_struct(const void* obj1, const void* obj2){
20        struct sort_ast_particle * srt1 = (struct sort_ast_particle*)obj1;
21        struct sort_ast_particle * srt2 = (struct sort_ast_particle*)obj2;
22        return srt1->id - srt2->id;
23}
24
25void reorder_vars(SZ_VarSet* vset){
26        SZ_Variable* v[7];
27        SZ_Variable* v_tmp;
28        int i, j;
29        //v[0]
30        for (v_tmp = vset->header->next, i = 0; i < 7; i++){
31                v[i] = v_tmp;
32                v_tmp = v_tmp->next;
33        }
34        //printf("here");
35        size_t dataLen = computeDataLength(v[0]->r5, v[0]->r4, v[0]->r3, v[0]->r2, v[0]->r1);
36        //sihuan debug
37        //printf("the data length is (in sorting): %u", dataLen);
38        struct sort_ast_particle* particle = (struct sort_ast_particle*) malloc(sizeof(struct sort_ast_particle)*dataLen);
39
40        for (i = 0; i < dataLen; i++){
41                particle[i].id = ((int64_t*)v[6]->data)[i];
42        //      printf("%llu ", particle[i].id);
43                for (j = 0; j < 6; j++)
44                        particle[i].var[j] = ((float*)v[j]->data)[i];
45        }
46
47        //sihuan debug
48        #if 0
49        printf("index before sorting: \n");
50        for (i = 0; i < 5; i++){
51                printf("%llu  ", particle[i].id);
52                printf("%.5f  ", ((float*)v[0]->data)[i]);
53        }
54        #endif
55        //printf("\n");
56        //sihuan debug
57        //for (i = 0; i < 5; i++)//{
58                //for (j = 0; j < 6; j++)
59                //      printf("%.5f  ", particle[i].var[j]);
60        //              printf("%llu  ", particle[i].id );
61        ///}
62        //printf("\n\n");
63
64
65        qsort(particle, dataLen, sizeof(struct sort_ast_particle), compare_struct);
66        for (i = 0; i < dataLen; i++){
67                ((int64_t*)v[6]->data)[i] = particle[i].id;
68                for (j = 0; j < 6; j++)
69                        ((float*)v[j]->data)[i] = particle[i].var[j];
70        }
71        free(particle);
72
73        //sihuan debug
74        #if 0
75        for (i = 0; i < 5; i++){
76                printf("%llu  ", particle[i].id);
77                printf("%.5f  ", ((float*)v[0]->data)[i]);
78        }
79        printf("\n");
80        #endif
81}
82
83size_t intersectAndsort(int64_t* preIndex, size_t preLen, SZ_VarSet* curVar, size_t dataLen, unsigned char* bitarray){
84        size_t i, j, k, m, cnt;
85        i = j = k = m = cnt = 0;
86        SZ_Variable* v[7];
87        SZ_Variable* v_tmp;
88        //v[0]
89        for (v_tmp = curVar->header->next, i = 0; i < 7; i++){
90                v[i] = v_tmp;
91                v_tmp = v_tmp->next;
92        }
93        for (i = 0; i < preLen; i++)
94                bitarray[i] = '0';
95        i = 0;
96        while(i < preLen && j < dataLen){
97                if (preIndex[i] == ((int64_t*)v[6]->data)[j]){
98                        cnt++;
99                        int64_t tmp;
100                        tmp = ((int64_t*)v[6]->data)[k];
101                        ((int64_t*)v[6]->data)[k] = ((int64_t*)v[6]->data)[j];
102                        ((int64_t*)v[6]->data)[j] = tmp;
103                        float data_tmp;
104                        for (m = 0; m < 6; m++){
105                                data_tmp = ((float*)v[m]->data)[k];
106                                ((float*)v[m]->data)[k] = ((float*)v[m]->data)[j];
107                                ((float*)v[m]->data)[j] = data_tmp;
108                        }
109                        k++; i++; j++;
110                }
111                else if (preIndex[i] < ((int64_t*)v[6]->data)[j]){
112                        bitarray[i] = '1';
113                        i++;
114                }
115                else j++;
116        }
117        printf("intersect count is: %zu, i j k pre curlen is: %zu, %zu, %zu, %zu, %zu\n\n", cnt, i, j, k, preLen, dataLen);
118        return cnt;
119}
120
121void write_reordered_tofile(SZ_VarSet* curVar, size_t dataLen){
122        int var_index; //0 for x, 1 for y...,3 for vx...5 for vz
123        int i;
124        char outputfile_name[256];
125        SZ_Variable* v[7]; SZ_Variable* v_tmp;
126        for (v_tmp = curVar->header->next, i = 0; i < 6; i++){
127                v[i] = v_tmp;
128                v_tmp = v_tmp->next;
129        }
130        for (var_index = 0; var_index < 6; var_index++){
131                sprintf(outputfile_name, "reordered_input_%d_%d.in", sz_tsc->currentStep, var_index);
132                int status_tmp;
133                writeFloatData_inBytes((float*)v[var_index]->data, dataLen, outputfile_name, &status_tmp);
134        }
135}
136
137float calculate_delta_t(size_t size){
138        SZ_Variable* v_tmp = sz_varset->header->next;
139        while(strcmp(v_tmp->varName, "x")) v_tmp = v_tmp->next;
140        float* x1 = (float*) v_tmp->data;
141        float* x0 = (float*) v_tmp->multisteps->hist_data;
142        while(strcmp(v_tmp->varName, "vx")) v_tmp = v_tmp->next;
143        float* vx0 = (float*) v_tmp->multisteps->hist_data;
144        int i, j;
145        double denom = 0.0;
146        double div = 0.0;
147        for (i = 0, j = 0; i < size; i++, j++){
148                while(sz_tsc->bit_array[j] == '1') j++;
149                denom += vx0[j] * (x1[i] - x0[j]);
150                div   += vx0[j] * vx0[j];
151        }
152        printf("the calculated delta_t is: %.10f\n", denom/div);
153        return denom/div;
154}
155
156int is_lossless_compressed_data(unsigned char* compressedBytes, size_t cmpSize)
157{
158        int frameContentSize = ZSTD_getFrameContentSize(compressedBytes, cmpSize);
159        if(frameContentSize != ZSTD_CONTENTSIZE_ERROR)
160                return ZSTD_COMPRESSOR;
161       
162        int flag = isZlibFormat(compressedBytes[0], compressedBytes[1]);
163        if(flag)
164                return GZIP_COMPRESSOR;
165
166        return -1; //fast mode (without GZIP or ZSTD)
167}
168
169unsigned long sz_lossless_compress(int losslessCompressor, int level, unsigned char* data, unsigned long dataLength, unsigned char** compressBytes)
170{
171        unsigned long outSize = 0; 
172        size_t estimatedCompressedSize = 0;
173        switch(losslessCompressor)
174        {
175        case GZIP_COMPRESSOR:
176                outSize = zlib_compress5(data, dataLength, compressBytes, level);
177                break;
178        case ZSTD_COMPRESSOR:
179                estimatedCompressedSize = dataLength*1.2;
180                *compressBytes = (unsigned char*)malloc(estimatedCompressedSize);
181                outSize = ZSTD_compress(*compressBytes, estimatedCompressedSize, data, dataLength, level); //default setting of level is 3
182                break;
183        default:
184                printf("Error: Unrecognized lossless compressor in sz_lossless_compress()\n");
185        }
186        return outSize;
187}
188
189unsigned long sz_lossless_decompress(int losslessCompressor, unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize)
190{
191        unsigned long outSize = 0;
192        switch(losslessCompressor)
193        {
194        case GZIP_COMPRESSOR:
195                outSize = zlib_uncompress5(compressBytes, cmpSize, oriData, targetOriSize);
196                break;
197        case ZSTD_COMPRESSOR:
198                *oriData = (unsigned char*)malloc(targetOriSize);
199                ZSTD_decompress(*oriData, targetOriSize, compressBytes, cmpSize);
200                outSize = targetOriSize;
201                break;
202        default:
203                printf("Error: Unrecognized lossless compressor in sz_lossless_decompress()\n");
204        }
205        return outSize;
206}
207
208unsigned long sz_lossless_decompress65536bytes(int losslessCompressor, unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData)
209{
210        unsigned long outSize = 0;
211        switch(losslessCompressor)
212        {
213        case GZIP_COMPRESSOR:
214                outSize = zlib_uncompress65536bytes(compressBytes, cmpSize, oriData);
215                break;
216        case ZSTD_COMPRESSOR:
217                *oriData = (unsigned char*)malloc(65536);
218                memset(*oriData, 0, 65536);
219                ZSTD_decompress(*oriData, 65536, compressBytes, cmpSize);       //the first 32768 bytes should be exact the same.
220                outSize = 65536;
221                break;
222        default:
223                printf("Error: Unrecognized lossless compressor\n");
224        }
225        return outSize;
226}
Note: See TracBrowser for help on using the repository browser.