forked from llnl/zfp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzfpEncodeBlockStridedBase.c
More file actions
490 lines (418 loc) · 13.5 KB
/
zfpEncodeBlockStridedBase.c
File metadata and controls
490 lines (418 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include "utils/testMacros.h"
#include "utils/zfpChecksums.h"
#include "utils/zfpHash.h"
#define SX 2
#define SY (3 * BLOCK_SIDE_LEN*SX)
#define SZ (2 * BLOCK_SIDE_LEN*SY)
#define SW (3 * BLOCK_SIDE_LEN*SZ)
#define PX 1
#define PY 2
#define PZ 3
#define PW 4
#define DUMMY_VAL 99
struct setupVars {
size_t dimLens[4];
Scalar* dataArr;
void* buffer;
zfp_stream* stream;
};
// write random output to strided entries, dummyVal elsewhere
void
initializeStridedArray(Scalar** dataArrPtr, Scalar dummyVal)
{
size_t i, j, k, l, countX, countY, countZ, countW;
// absolute entry (i,j,k,l)
// 0 <= i < countX, (same for j,countY and k,countZ and l,countW)
// strided entry iff
// i % countX/BLOCK_SIDE_LEN == 0 (and so on for j,k,l)
switch(DIMS) {
case 1:
countX = BLOCK_SIDE_LEN * SX;
*dataArrPtr = malloc(sizeof(Scalar) * countX);
assert_non_null(*dataArrPtr);
for (i = 0; i < countX; i++) {
if (i % SX) {
(*dataArrPtr)[i] = dummyVal;
} else {
#ifdef FL_PT_DATA
(*dataArrPtr)[i] = nextSignedRandFlPt();
#else
(*dataArrPtr)[i] = nextSignedRandInt();
#endif
}
}
break;
case 2:
countX = BLOCK_SIDE_LEN * SX;
countY = SY / SX;
*dataArrPtr = malloc(sizeof(Scalar) * countX * countY);
assert_non_null(*dataArrPtr);
for (j = 0; j < countY; j++) {
for (i = 0; i < countX; i++) {
size_t index = countX*j + i;
if (i % (countX/BLOCK_SIDE_LEN)
|| j % (countY/BLOCK_SIDE_LEN)) {
(*dataArrPtr)[index] = dummyVal;
} else {
#ifdef FL_PT_DATA
(*dataArrPtr)[index] = nextSignedRandFlPt();
#else
(*dataArrPtr)[index] = nextSignedRandInt();
#endif
}
}
}
break;
case 3:
countX = BLOCK_SIDE_LEN * SX;
countY = SY / SX;
countZ = SZ / SY;
*dataArrPtr = malloc(sizeof(Scalar) * countX * countY * countZ);
assert_non_null(*dataArrPtr);
for (k = 0; k < countZ; k++) {
for (j = 0; j < countY; j++) {
for (i = 0; i < countX; i++) {
size_t index = countX*countY*k + countX*j + i;
if (i % (countX/BLOCK_SIDE_LEN)
|| j % (countY/BLOCK_SIDE_LEN)
|| k % (countZ/BLOCK_SIDE_LEN)) {
(*dataArrPtr)[index] = dummyVal;
} else {
#ifdef FL_PT_DATA
(*dataArrPtr)[index] = nextSignedRandFlPt();
#else
(*dataArrPtr)[index] = nextSignedRandInt();
#endif
}
}
}
}
break;
case 4:
countX = BLOCK_SIDE_LEN * SX;
countY = SY / SX;
countZ = SZ / SY;
countW = SW / SZ;
*dataArrPtr = malloc(sizeof(Scalar) * countX * countY * countZ * countW);
assert_non_null(*dataArrPtr);
for (l = 0; l < countW; l++) {
for (k = 0; k < countZ; k++) {
for (j = 0; j < countY; j++) {
for (i = 0; i < countX; i++) {
size_t index = countX*countY*countZ*l + countX*countY*k + countX*j + i;
if (i % (countX/BLOCK_SIDE_LEN)
|| j % (countY/BLOCK_SIDE_LEN)
|| k % (countZ/BLOCK_SIDE_LEN)
|| l % (countW/BLOCK_SIDE_LEN)) {
(*dataArrPtr)[index] = dummyVal;
} else {
#ifdef FL_PT_DATA
(*dataArrPtr)[index] = nextSignedRandFlPt();
#else
(*dataArrPtr)[index] = nextSignedRandInt();
#endif
}
}
}
}
}
break;
}
}
static void
setupZfpStream(struct setupVars* bundle)
{
memset(bundle->dimLens, 0, sizeof(bundle->dimLens));
#if DIMS >= 1
bundle->dimLens[0] = BLOCK_SIDE_LEN;
#endif
#if DIMS >= 2
bundle->dimLens[1] = BLOCK_SIDE_LEN;
#endif
#if DIMS >= 3
bundle->dimLens[2] = BLOCK_SIDE_LEN;
#endif
#if DIMS >= 4
bundle->dimLens[3] = BLOCK_SIDE_LEN;
#endif
size_t* n = bundle->dimLens;
zfp_type type = ZFP_TYPE;
zfp_field* field;
switch(DIMS) {
case 1:
field = zfp_field_1d(bundle->dataArr, type, n[0]);
zfp_field_set_stride_1d(field, SX);
break;
case 2:
field = zfp_field_2d(bundle->dataArr, type, n[0], n[1]);
zfp_field_set_stride_2d(field, SX, SY);
break;
case 3:
field = zfp_field_3d(bundle->dataArr, type, n[0], n[1], n[2]);
zfp_field_set_stride_3d(field, SX, SY, SZ);
break;
case 4:
field = zfp_field_4d(bundle->dataArr, type, n[0], n[1], n[2], n[3]);
zfp_field_set_stride_4d(field, SX, SY, SZ, SW);
break;
}
zfp_stream* stream = zfp_stream_open(NULL);
zfp_stream_set_rate(stream, ZFP_RATE_PARAM_BITS, type, DIMS, zfp_false);
size_t bufsizeBytes = zfp_stream_maximum_size(stream, field);
char* buffer = calloc(bufsizeBytes, sizeof(char));
assert_non_null(buffer);
bitstream* s = stream_open(buffer, bufsizeBytes);
assert_non_null(s);
zfp_stream_set_bit_stream(stream, s);
zfp_stream_rewind(stream);
zfp_field_free(field);
bundle->buffer = buffer;
bundle->stream = stream;
}
static int
setup(void **state)
{
struct setupVars *bundle = malloc(sizeof(struct setupVars));
assert_non_null(bundle);
resetRandGen();
initializeStridedArray(&bundle->dataArr, DUMMY_VAL);
setupZfpStream(bundle);
*state = bundle;
return 0;
}
static int
teardown(void **state)
{
struct setupVars *bundle = *state;
stream_close(bundle->stream->stream);
zfp_stream_close(bundle->stream);
free(bundle->buffer);
free(bundle->dataArr);
free(bundle);
return 0;
}
size_t
encodeBlockStrided(zfp_stream* stream, Scalar* dataArr)
{
size_t numBitsWritten;
switch (DIMS) {
case 1:
numBitsWritten = _t2(zfp_encode_block_strided, Scalar, 1)(stream, dataArr, SX);
break;
case 2:
numBitsWritten = _t2(zfp_encode_block_strided, Scalar, 2)(stream, dataArr, SX, SY);
break;
case 3:
numBitsWritten = _t2(zfp_encode_block_strided, Scalar, 3)(stream, dataArr, SX, SY, SZ);
break;
case 4:
numBitsWritten = _t2(zfp_encode_block_strided, Scalar, 4)(stream, dataArr, SX, SY, SZ, SW);
break;
}
return numBitsWritten;
}
size_t
encodePartialBlockStrided(zfp_stream* stream, Scalar* dataArr)
{
size_t numBitsWritten;
switch (DIMS) {
case 1:
numBitsWritten = _t2(zfp_encode_partial_block_strided, Scalar, 1)(stream, dataArr, PX, SX);
break;
case 2:
numBitsWritten = _t2(zfp_encode_partial_block_strided, Scalar, 2)(stream, dataArr, PX, PY, SX, SY);
break;
case 3:
numBitsWritten = _t2(zfp_encode_partial_block_strided, Scalar, 3)(stream, dataArr, PX, PY, PZ, SX, SY, SZ);
break;
case 4:
numBitsWritten = _t2(zfp_encode_partial_block_strided, Scalar, 4)(stream, dataArr, PX, PY, PZ, PW, SX, SY, SZ, SW);
break;
}
return numBitsWritten;
}
static void
when_seededRandomDataGenerated_expect_ChecksumMatches(void **state)
{
struct setupVars *bundle = *state;
ptrdiff_t s[4] = {SX, SY, SZ, SW};
size_t n[4];
int i;
for (i = 0; i < 4; i++) {
n[i] = (i < DIMS) ? BLOCK_SIDE_LEN : 0;
}
UInt checksum = _catFunc2(hashStridedArray, SCALAR_BITS)((const UInt*)bundle->dataArr, n, s);
uint64 key1, key2;
computeKeyOriginalInput(BLOCK_FULL_TEST, bundle->dimLens, &key1, &key2);
// entire block is populated, but later tests restrict to reading partial block
ASSERT_EQ_CHECKSUM(DIMS, ZFP_TYPE, checksum, key1, key2);
}
static void
_catFunc3(given_, DIM_INT_STR, Block_when_EncodeBlockStrided_expect_ReturnValReflectsNumBitsWrittenToBitstream)(void **state)
{
struct setupVars *bundle = *state;
zfp_stream* stream = bundle->stream;
bitstream* s = zfp_stream_bit_stream(stream);
size_t returnValBits = encodeBlockStrided(stream, bundle->dataArr);
// do not flush, otherwise extra zeros included in count
assert_int_equal(returnValBits, stream_wtell(s));
}
static void
_catFunc3(given_, DIM_INT_STR, Block_when_EncodeBlockStrided_expect_OnlyStridedEntriesUsed)(void **state)
{
struct setupVars *bundle = *state;
zfp_stream* stream = bundle->stream;
bitstream* s = zfp_stream_bit_stream(stream);
// encode original block
encodeBlockStrided(stream, bundle->dataArr);
zfp_stream_flush(stream);
uint64 originalChecksum = hashBitstream(stream_data(s), stream_size(s));
// zero bitstream's memory
size_t writtenBits = stream_wtell(s);
stream_rewind(s);
stream_pad(s, (uint)writtenBits);
stream_rewind(s);
// tweak non-strided (unused) entries
resetRandGen();
free(bundle->dataArr);
initializeStridedArray(&bundle->dataArr, DUMMY_VAL + 1);
// encode new block
encodeBlockStrided(stream, bundle->dataArr);
zfp_stream_flush(stream);
uint64 newChecksum = hashBitstream(stream_data(s), stream_size(s));
// do not use ASSERT_CHECKSUM macro because both always computed locally
assert_int_equal(newChecksum, originalChecksum);
}
static void
_catFunc3(given_, DIM_INT_STR, Block_when_EncodeBlockStrided_expect_BitstreamChecksumMatches)(void **state)
{
struct setupVars *bundle = *state;
zfp_stream* stream = bundle->stream;
bitstream* s = zfp_stream_bit_stream(stream);
encodeBlockStrided(stream, bundle->dataArr);
zfp_stream_flush(stream);
uint64 checksum = hashBitstream(stream_data(s), stream_size(s));
uint64 key1, key2;
computeKey(BLOCK_FULL_TEST, COMPRESSED_BITSTREAM, bundle->dimLens, zfp_mode_fixed_rate, 0, &key1, &key2);
ASSERT_EQ_CHECKSUM(DIMS, ZFP_TYPE, checksum, key1, key2);
}
static void
_catFunc3(given_, DIM_INT_STR, Block_when_EncodePartialBlockStrided_expect_ReturnValReflectsNumBitsWrittenToBitstream)(void **state)
{
struct setupVars *bundle = *state;
zfp_stream* stream = bundle->stream;
bitstream* s = zfp_stream_bit_stream(stream);
size_t returnValBits = encodePartialBlockStrided(stream, bundle->dataArr);
// do not flush, otherwise extra zeros included in count
assert_int_equal(returnValBits, stream_wtell(s));
}
static void
_catFunc3(given_, DIM_INT_STR, Block_when_EncodePartialBlockStrided_expect_OnlyStridedEntriesUsed)(void **state)
{
struct setupVars *bundle = *state;
zfp_stream* stream = bundle->stream;
bitstream* s = zfp_stream_bit_stream(stream);
// encode original block
encodePartialBlockStrided(stream, bundle->dataArr);
zfp_stream_flush(stream);
uint64 originalChecksum = hashBitstream(stream_data(s), stream_size(s));
// zero bitstream's memory
size_t writtenBits = stream_wtell(s);
stream_rewind(s);
stream_pad(s, (uint)writtenBits);
stream_rewind(s);
// tweak non-strided (unused) entries
resetRandGen();
free(bundle->dataArr);
initializeStridedArray(&bundle->dataArr, DUMMY_VAL + 1);
// encode new block
encodePartialBlockStrided(stream, bundle->dataArr);
zfp_stream_flush(stream);
uint64 newChecksum = hashBitstream(stream_data(s), stream_size(s));
// do not use ASSERT_CHECKSUM macro because both always computed locally
assert_int_equal(newChecksum, originalChecksum);
}
static void
_catFunc3(given_, DIM_INT_STR, Block_when_EncodePartialBlockStrided_expect_OnlyEntriesWithinPartialBlockBoundsUsed)(void **state)
{
struct setupVars *bundle = *state;
zfp_stream* stream = bundle->stream;
bitstream* s = zfp_stream_bit_stream(stream);
// encode original block
encodePartialBlockStrided(stream, bundle->dataArr);
zfp_stream_flush(stream);
uint64 originalChecksum = hashBitstream(stream_data(s), stream_size(s));
// zero bitstream's memory
size_t writtenBits = stream_wtell(s);
stream_rewind(s);
stream_pad(s, (uint)writtenBits);
stream_rewind(s);
// tweak block entries outside partial block subset
// block entry (i, j, k, l)
size_t i, j, k, l;
switch(DIMS) {
case 1:
for (i = PX; i < BLOCK_SIDE_LEN; i++) {
bundle->dataArr[SX*i] = DUMMY_VAL;
}
break;
case 2:
for (j = 0; j < BLOCK_SIDE_LEN; j++) {
for (i = 0; i < BLOCK_SIDE_LEN; i++) {
if (i >= PX || j >= PY) {
bundle->dataArr[SY*j + SX*i] = DUMMY_VAL;
}
}
}
break;
case 3:
for (k = 0; k < BLOCK_SIDE_LEN; k++) {
for (j = 0; j < BLOCK_SIDE_LEN; j++) {
for (i = 0; i < BLOCK_SIDE_LEN; i++) {
if (i >= PX || j >= PY || k >= PZ) {
bundle->dataArr[SZ*k + SY*j + SX*i] = DUMMY_VAL;
}
}
}
}
break;
case 4:
for (l = 0; l < BLOCK_SIDE_LEN; l++) {
for (k = 0; k < BLOCK_SIDE_LEN; k++) {
for (j = 0; j < BLOCK_SIDE_LEN; j++) {
for (i = 0; i < BLOCK_SIDE_LEN; i++) {
if (i >= PX || j >= PY || k >= PZ) {
bundle->dataArr[SW*l + SZ*k + SY*j + SX*i] = DUMMY_VAL;
}
}
}
}
}
break;
}
// encode new block
encodePartialBlockStrided(stream, bundle->dataArr);
zfp_stream_flush(stream);
uint64 newChecksum = hashBitstream(stream_data(s), stream_size(s));
// do not use ASSERT_CHECKSUM macro because both always computed locally
assert_int_equal(newChecksum, originalChecksum);
}
static void
_catFunc3(given_, DIM_INT_STR, Block_when_EncodePartialBlockStrided_expect_BitstreamChecksumMatches)(void **state)
{
struct setupVars *bundle = *state;
zfp_stream* stream = bundle->stream;
bitstream* s = zfp_stream_bit_stream(stream);
encodePartialBlockStrided(stream, bundle->dataArr);
zfp_stream_flush(stream);
uint64 checksum = hashBitstream(stream_data(s), stream_size(s));
uint64 key1, key2;
computeKey(BLOCK_PARTIAL_TEST, COMPRESSED_BITSTREAM, bundle->dimLens, zfp_mode_fixed_rate, 0, &key1, &key2);
ASSERT_EQ_CHECKSUM(DIMS, ZFP_TYPE, checksum, key1, key2);
}