1
2 #include <stdio.h>
3 #include <math.h>
4 #include <time.h>
5 #include <sys/time.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <algorithm>
11
12 #include "dl_compression.h"
13
14 #define DATA_LEN 100000000
15
16 float data1[DATA_LEN];
17
18 float data2[DATA_LEN];
19
20 void dataSetUp(void);
21
22 bool test_compress_buffer();
23
24 bool test_decompress_buffer();
25
26 bool test_compressed_buffer_reduce_sum();
27
28 bool test_compressed_buffer_sum();
29
30 #if 0
31 void addVec(const float *vec1, const float *vec2, float *vec3, int count) {
32 for (int i = 0; i < count; i++) {
33 vec3[i] = vec1[i] + vec2[i];
34 }
35 }
36
37 void cmpVec(const float *vec1, const float *vec2, int count) {
38 for (int i = 0; i < count; i++) {
39 if (std::abs(vec1[i] - vec2[i]) > 1e-3) {
40 printf("Detect big gap index: %d\n", i);
41 }
42 }
43 }
44 #endif
getSum(const float * src,int count)45 float getSum(const float *src, int count) {
46 float sum = 0.0f;
47 for (int i = 0; i < count; i++) {
48 sum += src[i];
49 }
50 return sum;
51 }
52
53 #if 0
54 void dumpVec(const float *vec, int count) {
55 for (int i = 0; i < count; i++) {
56 printf("vec[%d] = %lf\n", i, vec[i]);
57 }
58 }
59 #endif
60
sumVec(const float * vec1,const float * vec2,int count)61 float sumVec(const float *vec1, const float *vec2, int count) {
62 float sum = 0.0f;
63 for (int i = 0; i < count; i++) {
64 sum = sum + vec1[i] + vec2[i];
65 // printf("data1[%d] = %lf data2[%d] = %lf sum = %lf\n", i, vec1[i], i, vec2[i], vec1[i] + vec2[i]);
66 }
67 return sum;
68 }
69
70 #if 0
71 float sumVec2(const float *vec1, const float *vec2, int count) {
72 float sum = 0.0f;
73 for (int i = 0; i < count; i++) {
74 sum = sum + vec1[i] + vec2[i];
75 // printf("tempData1[%d] = %lf tempData2[%d] = %lf sum = %lf\n", i, vec1[i], i, vec2[i], vec1[i] + vec2[i]);
76 }
77 return sum;
78 }
79 #endif
80
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 dataSetUp();
84
85 if (!test_compress_buffer()) {
86 printf("test_compress_buffer failure!\n");
87 } else {
88 printf("test_compress_buffer successful!\n");
89 }
90
91 if (!test_decompress_buffer()) {
92 printf("test_decompress_buffer failure!\n");
93 } else {
94 printf("test_decompress_buffer successful!\n");
95 }
96
97 if (!test_compressed_buffer_reduce_sum()) {
98 printf("test_compressed_buffer_reduce_sum failure!\n");
99 } else {
100 printf("test_compressed_buffer_reduce_sum successful!\n");
101 }
102
103 if (!test_compressed_buffer_sum()) {
104 printf("test_compressed_buffer_sum failure!\n");
105 } else {
106 printf("test_compressed_buffer_sum successful!\n");
107 }
108
109 return 0;
110 }
111
dataSetUp()112 void dataSetUp()
113 {
114 srand((int)time(0));
115
116 for (int i = 0; i < DATA_LEN; i++) {
117 data1[i] = (rand() % 10000) / (-100000.f) ;
118 }
119
120 for (int i = 0; i < DATA_LEN; i++) {
121 data2[i] = (rand() % 10000) / (-100000.f);
122 }
123 }
124
test_compress_buffer()125 bool test_compress_buffer()
126 {
127 float *tempData = (float *)malloc(sizeof(float) * DATA_LEN);
128 memcpy(tempData, data1, sizeof(float) * DATA_LEN);
129
130 dl_comp_return_t ret = dl_comp_compress_buffer((const void *)tempData,
131 tempData,
132 DATA_LEN,
133 NULL,
134 DL_COMP_FLOAT32,
135 4,
136 DL_COMP_DFP);
137 free(tempData);
138 if (ret != DL_COMP_OK) {
139 printf("compress failed error = %d!\n", ret);
140 return false;
141 }
142
143 return true;
144 }
145
test_decompress_buffer()146 bool test_decompress_buffer()
147 {
148 float *tempData = (float *)malloc(sizeof(float) * DATA_LEN);
149 float *diff = (float *)malloc(sizeof(float) * DATA_LEN);
150 memcpy(tempData, data1, sizeof(float) * DATA_LEN);
151 memset(diff, 0, sizeof(float) * DATA_LEN);
152
153 printf("before compress Total Sum: %f\n", getSum(data1, DATA_LEN));
154 dl_comp_return_t ret = dl_comp_compress_buffer((const void *)tempData,
155 tempData,
156 DATA_LEN,
157 diff,
158 DL_COMP_FLOAT32,
159 4,
160 DL_COMP_DFP);
161 if (ret != DL_COMP_OK) {
162 printf("compress failed error = %d!\n", ret);
163 free(tempData);
164 free(diff);
165 return false;
166 }
167
168 ret = dl_comp_decompress_buffer((const void *)tempData,
169 tempData,
170 DATA_LEN);
171 if (ret != DL_COMP_OK) {
172 printf("de-compress failed error = %d!\n", ret);
173 free(tempData);
174 free(diff);
175 return false;
176 }
177
178 printf("after compress Total Sum: %f diff: %f\n", getSum(tempData, DATA_LEN), getSum(diff, DATA_LEN));
179 printf("after diff compensation Total Sum: %f\n", sumVec(tempData, diff, DATA_LEN));
180 free(tempData);
181 free(diff);
182 return true;
183 }
184
test_compressed_buffer_reduce_sum()185 bool test_compressed_buffer_reduce_sum()
186 {
187 float *tempData1 = (float *)malloc(sizeof(float) * DATA_LEN);
188 float *tempData2 = (float *)malloc(sizeof(float) * DATA_LEN);
189 float *tempData3 = (float *)malloc(sizeof(float) * DATA_LEN);
190 float *tempData4 = (float *)malloc(sizeof(float) * DATA_LEN);
191 float *sum1 = (float *)malloc(sizeof(float) * DATA_LEN);
192 float *sum2 = (float *)malloc(sizeof(float) * DATA_LEN);
193 float *sum3 = (float *)malloc(sizeof(float) * DATA_LEN);
194 memcpy(tempData1, data1, sizeof(float) * DATA_LEN);
195 memcpy(tempData2, data2, sizeof(float) * DATA_LEN);
196
197 dl_comp_return_t ret = dl_comp_compress_buffer((const void *)tempData1,
198 tempData1,
199 DATA_LEN,
200 NULL,
201 DL_COMP_FLOAT32,
202 4,
203 DL_COMP_DFP);
204
205 if (ret != DL_COMP_OK) {
206 printf("compress failed error = %d!\n", ret);
207 free(tempData1);
208 free(tempData2);
209 free(tempData3);
210 free(tempData4);
211 free(sum1);
212 free(sum2);
213 free(sum3);
214 return false;
215 }
216
217 ret = dl_comp_compress_buffer((const void *)tempData2,
218 tempData2,
219 DATA_LEN,
220 NULL,
221 DL_COMP_FLOAT32,
222 4,
223 DL_COMP_DFP);
224
225 if (ret != DL_COMP_OK) {
226 printf("compress failed error = %d!\n", ret);
227 free(tempData1);
228 free(tempData2);
229 free(tempData3);
230 free(tempData4);
231 free(sum1);
232 free(sum2);
233 free(sum3);
234 return false;
235 }
236
237 #if 0
238 ret = dl_comp_decompress_buffer((const void *)tempData1,
239 (void *)tempData3,
240 DATA_LEN);
241 ret = dl_comp_decompress_buffer((const void *)tempData2,
242 (void *)tempData4,
243 DATA_LEN);
244
245 printf("orig data sum = %lf\n", sumVec(data1, data2, DATA_LEN));
246 printf("new data sum = %lf\n", sumVec2(tempData3, tempData4, DATA_LEN));
247 #endif
248
249 #if 1
250 size_t blockCount = dl_comp_convert_block_count(DATA_LEN);
251
252 ret = dl_comp_compressed_buffer_reduce_sum((const void *)tempData1,
253 (void *)tempData2,
254 blockCount);
255
256 if (ret != DL_COMP_OK) {
257 printf("reduce sum failed error = %d!\n", ret);
258 free(tempData1);
259 free(tempData2);
260 free(tempData3);
261 free(tempData4);
262 free(sum1);
263 free(sum2);
264 free(sum3);
265 return false;
266 }
267
268 ret = dl_comp_decompress_buffer((const void *)tempData2,
269 (void *)tempData2,
270 DATA_LEN);
271
272 if (ret != DL_COMP_OK) {
273 printf("de compress failed error = %d!\n", ret);
274 free(tempData1);
275 free(tempData2);
276 free(tempData3);
277 free(tempData4);
278 free(sum1);
279 free(sum2);
280 free(sum3);
281 return false;
282 }
283
284 printf("orig data sum = %lf\n", sumVec(data1, data2, DATA_LEN));
285 printf("new reduce sum = %lf\n", getSum(tempData2, DATA_LEN));
286 #endif
287
288 // addVec(data1, data2, sum1, DATA_LEN);
289 // addVec(tempData3, tempData4, sum2, DATA_LEN);
290
291 // printf("start to cmp sum1 and tempData2!\n");
292 // cmpVec(sum1, tempData2, DATA_LEN);
293
294 // printf("start to cmp sum2 and sum1!\n");
295 // cmpVec(sum2, sum1, DATA_LEN);
296
297
298 free(tempData1);
299 free(tempData2);
300 free(tempData3);
301 free(tempData4);
302 free(sum1);
303 free(sum2);
304 free(sum3);
305 return true;
306 }
307
test_compressed_buffer_sum()308 bool test_compressed_buffer_sum()
309 {
310 float *tempData1 = (float *)malloc(sizeof(float) * DATA_LEN);
311 float *tempData2 = (float *)malloc(sizeof(float) * DATA_LEN);
312 float *sum1 = (float *)malloc(sizeof(float) * DATA_LEN);
313
314 memcpy(tempData1, data1, sizeof(float) * DATA_LEN);
315 memcpy(tempData2, data2, sizeof(float) * DATA_LEN);
316
317 dl_comp_get_sizeof_block(DL_COMP_FLOAT32,
318 4,
319 DL_COMP_DFP);
320
321 dl_comp_return_t ret = dl_comp_compress_buffer((const void *)tempData1,
322 tempData1,
323 DATA_LEN,
324 NULL,
325 DL_COMP_FLOAT32,
326 4,
327 DL_COMP_DFP);
328
329 if (ret != DL_COMP_OK) {
330 printf("compress failed error = %d!\n", ret);
331 free(tempData1);
332 free(tempData2);
333 free(sum1);
334 return false;
335 }
336
337 dl_comp_compress_buffer_FLOAT32ToINT8((const void *)tempData2,
338 tempData2,
339 NULL,
340 DATA_LEN);
341
342 dl_comp_compressed_buffer_sum(tempData1,
343 tempData2,
344 DATA_LEN,
345 sum1);
346
347 dl_comp_get_elem_num_in_block();
348
349 dl_comp_decompress_buffer_INT8ToFLOAT32(sum1, sum1, DATA_LEN);
350
351 return true;
352
353 }
354