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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This test is for the DECTRIS project to the H5DOwrite_chunk function
*
*/
#include "hdf5_hl.h"
#include <zlib.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
const char *FILENAME[] = {
"dectris_perf",
"unix.raw",
NULL
};
/*
* Print the current location on the standard output stream.
*/
#define FUNC __func__
#define AT() printf (" at %s:%d in %s()...\n", \
__FILE__, __LINE__, FUNC);
#define H5_FAILED() {puts("*FAILED*");fflush(stdout);}
#define TEST_ERROR {H5_FAILED(); AT(); goto error;}
#define TESTING(WHAT) {printf("Testing %-62s",WHAT); fflush(stdout);}
#define PASSED() {puts(" PASSED");fflush(stdout);}
#define DIRECT_UNCOMPRESSED_DSET "direct_uncompressed_dset"
#define DIRECT_COMPRESSED_DSET "direct_compressed_dset"
#define REG_COMPRESSED_DSET "reg_compressed_dset"
#define REG_NO_COMPRESS_DSET "reg_no_compress_dset"
#define RANK 3
#define NX 100
#define NY 1000
#define NZ 250
#define CHUNK_NX 1
#define CHUNK_NY 1000
#define CHUNK_NZ 250
#define DEFLATE_SIZE_ADJUST(s) (ceil(((double)(s))*1.001)+12)
char filename[1024];
unsigned int *outbuf[NX];
size_t data_size[NX];
double total_size = 0.0;
unsigned int *direct_buf[NX];
double MB = 1048576.0;
/*--------------------------------------------------
* Function to report IO rate
*--------------------------------------------------
*/
void reportTime(struct timeval start, double mbytes)
{
struct timeval timeval_stop,timeval_diff;
/*end timing*/
gettimeofday(&timeval_stop,NULL);
/* Calculate the elapsed gettimeofday time */
timeval_diff.tv_usec=timeval_stop.tv_usec-start.tv_usec;
timeval_diff.tv_sec=timeval_stop.tv_sec-start.tv_sec;
if(timeval_diff.tv_usec<0) {
timeval_diff.tv_usec+=1000000;
timeval_diff.tv_sec--;
} /* end if */
/*printf("mbytes=%lf, sec=%lf, usec=%lf\n", mbytes, (double)timeval_diff.tv_sec, (double)timeval_diff.tv_usec);*/
printf("MBytes/second: %lf\n", (double)mbytes/((double)timeval_diff.tv_sec+((double)timeval_diff.tv_usec/(double)1000000.0)));
}
/*--------------------------------------------------
* Create file, datasets, and initialize data
*--------------------------------------------------
*/
int create_file(hid_t fapl_id)
{
hid_t file; /* handles */
hid_t fapl;
hid_t cparms;
hid_t dataspace, dataset;
hsize_t dims[RANK] = {NX, NY, NZ};
hsize_t chunk_dims[RANK] ={CHUNK_NX, CHUNK_NY, CHUNK_NZ};
unsigned int aggression = 9; /* Compression aggression setting */
int ret;
int i, j, n;
int flag;
int unix_file;
unsigned int *p;
size_t buf_size = CHUNK_NY*CHUNK_NZ*sizeof(unsigned int);
const Bytef *z_src;
Bytef *z_dst; /*destination buffer */
uLongf z_dst_nbytes = (uLongf)DEFLATE_SIZE_ADJUST(buf_size);
uLong z_src_nbytes = (uLong)buf_size;
TESTING("Create a file and dataset");
/*
* Create the data space with unlimited dimensions.
*/
if((dataspace = H5Screate_simple(RANK, dims, NULL)) < 0)
TEST_ERROR;
/*
* Create a new file. If file exists its contents will be overwritten.
*/
if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id)) < 0)
TEST_ERROR;
/*
* Modify dataset creation properties, i.e. enable chunking and compression
*/
if((cparms = H5Pcreate(H5P_DATASET_CREATE)) < 0)
TEST_ERROR;
if(H5Pset_chunk( cparms, RANK, chunk_dims) < 0)
TEST_ERROR;
/*
* Create a new dataset within the file using cparms
* creation properties.
*/
if((dataset = H5Dcreate2(file, DIRECT_UNCOMPRESSED_DSET, H5T_NATIVE_INT, dataspace, H5P_DEFAULT,
cparms, H5P_DEFAULT)) < 0)
TEST_ERROR;
if(H5Dclose(dataset) < 0)
TEST_ERROR;
if((dataset = H5Dcreate2(file, REG_NO_COMPRESS_DSET, H5T_NATIVE_INT, dataspace, H5P_DEFAULT,
cparms, H5P_DEFAULT)) < 0)
TEST_ERROR;
if(H5Dclose(dataset) < 0)
TEST_ERROR;
/* Set compression */
if(H5Pset_deflate( cparms, aggression) < 0)
TEST_ERROR;
if((dataset = H5Dcreate2(file, DIRECT_COMPRESSED_DSET, H5T_NATIVE_INT, dataspace, H5P_DEFAULT,
cparms, H5P_DEFAULT)) < 0)
TEST_ERROR;
if(H5Dclose(dataset) < 0)
TEST_ERROR;
if((dataset = H5Dcreate2(file, REG_COMPRESSED_DSET, H5T_NATIVE_INT, dataspace, H5P_DEFAULT,
cparms, H5P_DEFAULT)) < 0)
TEST_ERROR;
if(H5Dclose(dataset) < 0)
TEST_ERROR;
if(H5Fclose(file) < 0)
TEST_ERROR;
if(H5Sclose(dataspace) < 0)
TEST_ERROR;
if(H5Pclose(cparms) < 0)
TEST_ERROR;
/* create a unix file*/
flag = O_CREAT|O_TRUNC|O_WRONLY;
if ((unix_file=open(FILENAME[1],flag,S_IRWXU))== -1)
TEST_ERROR;
if (close(unix_file) < 0)
{
printf(" unable to close the file\n");
TEST_ERROR;
}
/* Initialize data for chunks */
for(i = 0; i < NX; i++) {
p = direct_buf[i] = (unsigned int*)malloc(CHUNK_NY*CHUNK_NZ*sizeof(unsigned int));
for(j=0; j < CHUNK_NY*CHUNK_NZ; j++, p++)
*p = rand() % 65000;
z_src = (const Bytef*)direct_buf[i];
z_dst_nbytes = (uLongf)DEFLATE_SIZE_ADJUST(buf_size);
/* Allocate output (compressed) buffer */
outbuf[i] = (unsigned int*)malloc((size_t)z_dst_nbytes);
z_dst = (Bytef *)outbuf[i];
/* Perform compression from the source to the destination buffer */
ret = compress2(z_dst, &z_dst_nbytes, z_src, z_src_nbytes, aggression);
data_size[i] = (size_t)z_dst_nbytes;
total_size += data_size[i];
/* Check for various zlib errors */
if(Z_BUF_ERROR == ret) {
fprintf(stderr, "overflow");
TEST_ERROR;
} else if(Z_MEM_ERROR == ret) {
fprintf(stderr, "deflate memory error");
TEST_ERROR;
} else if(Z_OK != ret) {
fprintf(stderr, "other deflate error");
TEST_ERROR;
}
}
PASSED();
error:
H5E_BEGIN_TRY {
H5Dclose(dataset);
H5Sclose(dataspace);
H5Pclose(cparms);
H5Fclose(file);
} H5E_END_TRY;
return 1;
}
/*--------------------------------------------------
* Benchmark the performance of the new function
* with precompressed data.
*--------------------------------------------------
*/
int
test_direct_write_uncompressed_data(hid_t fapl_id)
{
hid_t file; /* handles */
hid_t dataspace, dataset;
hid_t dxpl;
herr_t status;
int i;
unsigned filter_mask = 0;
hsize_t offset[RANK] = {0, 0, 0};
struct timeval timeval_start;
TESTING("H5DOwrite_chunk for uncompressed data");
if((dxpl = H5Pcreate(H5P_DATASET_XFER)) < 0)
TEST_ERROR;
/* Start the timer */
gettimeofday(&timeval_start,NULL);
/* Reopen the file and dataset */
if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl_id)) < 0)
TEST_ERROR;
if((dataset = H5Dopen2(file, DIRECT_UNCOMPRESSED_DSET, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Write the compressed chunk data repeatedly to cover all the chunks in the
* dataset, using the direct writing function. */
for(i=0; i<NX; i++) {
status = H5DOwrite_chunk(dataset, dxpl, filter_mask, offset, CHUNK_NY*CHUNK_NZ*sizeof(unsigned int), direct_buf[i]);
(offset[0])++;
}
/*
* Close/release resources.
*/
H5Dclose(dataset);
H5Pclose(dxpl);
H5Fclose(file);
/* Report the performance */
reportTime(timeval_start, (double)(NX*NY*NZ*sizeof(unsigned int)/MB));
PASSED();
return 0;
error:
H5E_BEGIN_TRY {
H5Dclose(dataset);
H5Pclose(dxpl);
H5Fclose(file);
} H5E_END_TRY;
return 1;
}
/*--------------------------------------------------
* Benchmark the performance of the new function
* with precompressed data.
*--------------------------------------------------
*/
int
test_direct_write_compressed_data(hid_t fapl_id)
{
hid_t file; /* handles */
hid_t dataspace, dataset;
hid_t dxpl;
herr_t status;
int i;
unsigned filter_mask = 0;
hsize_t offset[RANK] = {0, 0, 0};
struct timeval timeval_start;
TESTING("H5DOwrite_chunk for pre-compressed data");
if((dxpl = H5Pcreate(H5P_DATASET_XFER)) < 0)
TEST_ERROR;
/* Start the timer */
gettimeofday(&timeval_start,NULL);
/* Reopen the file and dataset */
if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl_id)) < 0)
TEST_ERROR;
if((dataset = H5Dopen2(file, DIRECT_COMPRESSED_DSET, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Write the compressed chunk data repeatedly to cover all the chunks in the
* dataset, using the direct writing function. */
for(i=0; i<NX; i++) {
status = H5DOwrite_chunk(dataset, dxpl, filter_mask, offset, data_size[i], outbuf[i]);
(offset[0])++;
}
/*
* Close/release resources.
*/
H5Dclose(dataset);
H5Pclose(dxpl);
H5Fclose(file);
/* Report the performance */
reportTime(timeval_start, (double)(total_size/MB));
PASSED();
return 0;
error:
H5E_BEGIN_TRY {
H5Dclose(dataset);
H5Pclose(dxpl);
H5Fclose(file);
} H5E_END_TRY;
return 1;
}
/*--------------------------------------------------
* Benchmark the performance of the regular H5Dwrite
* with compression filter enabled.
*--------------------------------------------------
*/
int
test_compressed_write(hid_t fapl_id)
{
hid_t file; /* handles */
hid_t dataspace, dataset;
hid_t mem_space;
hsize_t chunk_dims[RANK] ={CHUNK_NX, CHUNK_NY, CHUNK_NZ};
hid_t dxpl;
herr_t status;
int i;
hsize_t start[RANK]; /* Start of hyperslab */
hsize_t stride[RANK]; /* Stride of hyperslab */
hsize_t count[RANK]; /* Block count */
hsize_t block[RANK]; /* Block sizes */
struct timeval timeval_start;
TESTING("H5Dwrite with compression enabled");
if((dxpl = H5Pcreate(H5P_DATASET_XFER)) < 0)
TEST_ERROR;
if((mem_space = H5Screate_simple(RANK, chunk_dims, NULL)) < 0)
TEST_ERROR;
/* Start the timer */
gettimeofday(&timeval_start,NULL);
/* Reopen the file and dataset */
if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl_id)) < 0)
TEST_ERROR;
if((dataset = H5Dopen2(file, REG_COMPRESSED_DSET, H5P_DEFAULT)) < 0)
TEST_ERROR;
if((dataspace = H5Dget_space(dataset)) < 0)
TEST_ERROR;
start[0] = start[1] = start[2] = 0;
stride[0] = stride[1] = stride[2] = 1;
count[0] = count[1] = count[2] = 1;
block[0] = CHUNK_NX; block[1] = CHUNK_NY; block[2] = CHUNK_NZ;
for(i=0; i<NX; i++) {
/*
* Select hyperslab for one chunk in the file
*/
if((status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start, stride, count, block)) < 0)
TEST_ERROR;
(start[0])++;
if((status = H5Dwrite(dataset, H5T_NATIVE_INT, mem_space, dataspace,
H5P_DEFAULT, direct_buf[i])) < 0)
TEST_ERROR;
}
/*
* Close/release resources.
*/
H5Dclose(dataset);
H5Sclose(dataspace);
H5Sclose(mem_space);
H5Pclose(dxpl);
H5Fclose(file);
/* Report the performance */
reportTime(timeval_start, (double)(NX*NY*NZ*sizeof(unsigned int)/MB));
PASSED();
return 0;
error:
H5E_BEGIN_TRY {
H5Dclose(dataset);
H5Sclose(dataspace);
H5Sclose(mem_space);
H5Pclose(dxpl);
H5Fclose(file);
} H5E_END_TRY;
return 1;
}
/*--------------------------------------------------
* Benchmark the performance of the regular H5Dwrite
* with compression
*--------------------------------------------------
*/
int
test_no_compress_write(hid_t fapl_id)
{
hid_t file; /* handles */
hid_t dataspace, dataset;
hid_t mem_space;
hsize_t chunk_dims[RANK] ={CHUNK_NX, CHUNK_NY, CHUNK_NZ};
hid_t dxpl;
herr_t status;
int i;
hsize_t start[RANK]; /* Start of hyperslab */
hsize_t stride[RANK]; /* Stride of hyperslab */
hsize_t count[RANK]; /* Block count */
hsize_t block[RANK]; /* Block sizes */
struct timeval timeval_start;
TESTING("H5Dwrite without compression");
if((dxpl = H5Pcreate(H5P_DATASET_XFER)) < 0)
TEST_ERROR;
if((mem_space = H5Screate_simple(RANK, chunk_dims, NULL)) < 0)
TEST_ERROR;
/* Start the timer */
gettimeofday(&timeval_start,NULL);
/* Reopen the file and dataset */
if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl_id)) < 0)
TEST_ERROR;
if((dataset = H5Dopen2(file, REG_NO_COMPRESS_DSET, H5P_DEFAULT)) < 0)
TEST_ERROR;
if((dataspace = H5Dget_space(dataset)) < 0)
TEST_ERROR;
start[0] = start[1] = start[2] = 0;
stride[0] = stride[1] = stride[2] = 1;
count[0] = count[1] = count[2] = 1;
block[0] = CHUNK_NX; block[1] = CHUNK_NY; block[2] = CHUNK_NZ;
for(i=0; i<NX; i++) {
/*
* Select hyperslab for one chunk in the file
*/
if((status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start, stride, count, block)) < 0)
TEST_ERROR;
(start[0])++;
if((status = H5Dwrite(dataset, H5T_NATIVE_INT, mem_space, dataspace,
H5P_DEFAULT, direct_buf[i])) < 0)
TEST_ERROR;
}
/*
* Close/release resources.
*/
H5Dclose(dataset);
H5Sclose(dataspace);
H5Sclose(mem_space);
H5Pclose(dxpl);
H5Fclose(file);
/* Report the performance */
reportTime(timeval_start, (double)(NX*NY*NZ*sizeof(unsigned int)/MB));
PASSED();
return 0;
error:
H5E_BEGIN_TRY {
H5Dclose(dataset);
H5Sclose(dataspace);
H5Sclose(mem_space);
H5Pclose(dxpl);
H5Fclose(file);
} H5E_END_TRY;
return 1;
}
/*--------------------------------------------------
* Benchmark the performance for writing compressed
* data to a Unix file
*--------------------------------------------------
*/
int
test_unix_write(void)
{
int file, flag;
ssize_t op_size;
int i;
struct timeval timeval_start;
TESTING("Write compressed data to a Unix file");
/* create file*/
flag = O_WRONLY;
/* Start the timer */
gettimeofday(&timeval_start,NULL);
if ((file=open(FILENAME[1],flag))== -1)
TEST_ERROR;
/* Write the compressed chunk data repeatedly to cover all the chunks in the
* dataset, using the direct writing function. */
for(i=0; i<NX; i++) {
op_size = write(file, outbuf[i],data_size[i]);
if (op_size < 0)
{
printf(" Error in writing data to file because %s \n", strerror(errno));
TEST_ERROR;
}
else if (op_size == 0)
{
printf(" unable to write sufficent data to file because %s \n", strerror(errno));
TEST_ERROR;
}
}
if (close(file) < 0)
{
printf(" unable to close the file\n");
TEST_ERROR;
}
/* Report the performance */
reportTime(timeval_start, (double)(total_size/MB));
PASSED();
return 0;
error:
return 1;
}
/*--------------------------------------------------
* Main function
*--------------------------------------------------
*/
int
main (void)
{
hid_t fapl = H5P_DEFAULT;
int i;
/* Testing setup */
/* h5_reset();
fapl = h5_fileaccess();
h5_fixname(FILENAME[0], fapl, filename, sizeof filename);*/
sprintf(filename, "%s.h5", FILENAME[0]);
create_file(fapl);
test_direct_write_uncompressed_data(fapl);
test_direct_write_compressed_data(fapl);
test_no_compress_write(fapl);
test_compressed_write(fapl);
test_unix_write();
for(i=0; i<NX; i++) {
free(outbuf[i]);
free(direct_buf[i]);
}
/* h5_cleanup(FILENAME, fapl);*/
return 0;
}
|