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 | |
---|
19 | int 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 | |
---|
25 | void 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 | |
---|
83 | size_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 | |
---|
121 | void 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 | |
---|
137 | float 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 | |
---|
156 | int is_lossless_compressed_data(unsigned char* compressedBytes, size_t cmpSize) |
---|
157 | { |
---|
158 | #if ZSTD_VERSION_NUMBER >= 10300 |
---|
159 | int frameContentSize = ZSTD_getFrameContentSize(compressedBytes, cmpSize); |
---|
160 | if(frameContentSize != ZSTD_CONTENTSIZE_ERROR) |
---|
161 | return ZSTD_COMPRESSOR; |
---|
162 | #else |
---|
163 | int frameContentSize = ZSTD_getDecompressedSize(compressedBytes, cmpSize); |
---|
164 | if(frameContentSize != 0) |
---|
165 | return ZSTD_COMPRESSOR; |
---|
166 | #endif |
---|
167 | |
---|
168 | int flag = isZlibFormat(compressedBytes[0], compressedBytes[1]); |
---|
169 | if(flag) |
---|
170 | return GZIP_COMPRESSOR; |
---|
171 | |
---|
172 | return -1; //fast mode (without GZIP or ZSTD) |
---|
173 | } |
---|
174 | |
---|
175 | unsigned long sz_lossless_compress(int losslessCompressor, int level, unsigned char* data, unsigned long dataLength, unsigned char** compressBytes) |
---|
176 | { |
---|
177 | unsigned long outSize = 0; |
---|
178 | size_t estimatedCompressedSize = 0; |
---|
179 | switch(losslessCompressor) |
---|
180 | { |
---|
181 | case GZIP_COMPRESSOR: |
---|
182 | outSize = zlib_compress5(data, dataLength, compressBytes, level); |
---|
183 | break; |
---|
184 | case ZSTD_COMPRESSOR: |
---|
185 | estimatedCompressedSize = dataLength*1.2; |
---|
186 | *compressBytes = (unsigned char*)malloc(estimatedCompressedSize); |
---|
187 | outSize = ZSTD_compress(*compressBytes, estimatedCompressedSize, data, dataLength, level); //default setting of level is 3 |
---|
188 | break; |
---|
189 | default: |
---|
190 | printf("Error: Unrecognized lossless compressor in sz_lossless_compress()\n"); |
---|
191 | } |
---|
192 | return outSize; |
---|
193 | } |
---|
194 | |
---|
195 | unsigned long sz_lossless_decompress(int losslessCompressor, unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize) |
---|
196 | { |
---|
197 | unsigned long outSize = 0; |
---|
198 | switch(losslessCompressor) |
---|
199 | { |
---|
200 | case GZIP_COMPRESSOR: |
---|
201 | outSize = zlib_uncompress5(compressBytes, cmpSize, oriData, targetOriSize); |
---|
202 | break; |
---|
203 | case ZSTD_COMPRESSOR: |
---|
204 | *oriData = (unsigned char*)malloc(targetOriSize); |
---|
205 | ZSTD_decompress(*oriData, targetOriSize, compressBytes, cmpSize); |
---|
206 | outSize = targetOriSize; |
---|
207 | break; |
---|
208 | default: |
---|
209 | printf("Error: Unrecognized lossless compressor in sz_lossless_decompress()\n"); |
---|
210 | } |
---|
211 | return outSize; |
---|
212 | } |
---|
213 | |
---|
214 | unsigned long sz_lossless_decompress65536bytes(int losslessCompressor, unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData) |
---|
215 | { |
---|
216 | unsigned long outSize = 0; |
---|
217 | switch(losslessCompressor) |
---|
218 | { |
---|
219 | case GZIP_COMPRESSOR: |
---|
220 | outSize = zlib_uncompress65536bytes(compressBytes, cmpSize, oriData); |
---|
221 | break; |
---|
222 | case ZSTD_COMPRESSOR: |
---|
223 | *oriData = (unsigned char*)malloc(65536); |
---|
224 | memset(*oriData, 0, 65536); |
---|
225 | ZSTD_decompress(*oriData, 65536, compressBytes, cmpSize); //the first 32768 bytes should be exact the same. |
---|
226 | outSize = 65536; |
---|
227 | break; |
---|
228 | default: |
---|
229 | printf("Error: Unrecognized lossless compressor\n"); |
---|
230 | } |
---|
231 | return outSize; |
---|
232 | } |
---|