File: buffer.c

package info (click to toggle)
libhdf4 4.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,384 kB
  • sloc: ansic: 128,700; sh: 15,015; fortran: 12,444; java: 5,863; xml: 1,205; makefile: 794; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (525 lines) | stat: -rw-r--r-- 18,804 bytes parent folder | download | duplicates (2)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF 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/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
    FILE - buffer.c
        Test HDF buffered data I/O routines

    DESIGN
        - Create a new data element and get a benchmark time for reading it in
            various ways.
        - Buffer the element and get times for the buffered element.
        - Make a new external element and get benchmark times
        - Buffer the external element and get times for the buffered element.
        - Make a new compressed element and get benchmark times
        - Buffer the compressed element and get times for the buffered element.
        - Make a new linked block element and get benchmark times
        - Buffer the linked block element and get times for the buffered element.
 */

#define TESTMASTER

#include "hdf_priv.h"
#include "tutils.h"
#include "hfile_priv.h"

#define TESTFILE_NAME "tbuffer.hdf"
#define EXTFILE_NAME  "tbuffer.dat"

/* Size of data elements to create */
#define ELEMSIZE 16384

/* define aliases for random number generation */
#define RAND    rand
#define SEED(a) srand((unsigned)(a))

/* Tag to use for creating the test elements */
#define BUFF_TAG 1000

/* Number of tests */
#define NUM_TESTS 4

/* Factor for converting seconds to microseconds */
#define FACTOR 1000000

/* Number of timing tests to run */
/* 0 - read/write entire buffer in one I/O operation */
/* 1 - read/write entire buffer one byte at a time forwards */
/* 2 - read/write entire buffer one byte at a time every other byte forwards */
/* 3 - read/write entire buffer one byte at a time backwards */
/* 4 - read/write entire buffer one byte at a time every other byte backwards */
#define NUM_TIMINGS 5
long read_time[NUM_TESTS][2];  /* 0 is unbuffered, 1 is buffered */
long write_time[NUM_TESTS][2]; /* 0 is unbuffered, 1 is buffered */

int32 elemsize; /* Actual number of elements in buffer */

/* I/O buffers */
uint8 *out_buf; /* Buffer for writing data */
uint8 *in_buf;  /* Buffer for reading data */

/* local function prototypes */
static void  init_buffer(void);
static void  usage(void);
static char *fixname(const char *base_name, char *fullname, size_t size);
static long  read_test(int32 aid);
static long  write_test(int32 aid, intn num_timings);

/* Initialize output buffer */
static void
init_buffer(void)
{
    SEED(time(NULL));
    for (int j = 0; j < elemsize; j++) {
        out_buf[j] = (uint8)RAND();
    }
} /* init_buffers() */

static void
usage(void)
{
    printf("\nUsage: buffer [elemsize] \n\n");
    printf("where elemsize is the number of elements in buffer (default: 1000 in Cray, 16384 in other "
           "platforms)\n");
    printf("\n");
} /* end usage() */

/*
   Creates a file name from a file base name like 'test' and return it through
   the FULLNAME (at most SIZE characters counting the null terminator). The
   full name is created by prepending the contents of HDF4_TESTPREFIX
   (separated from the base name by a slash). Returns NULL if BASENAME or
   FULLNAME is the null pointer or if FULLNAME isn't large enough for the
   result.
*/
static char *
fixname(const char *base_name, char *fullname, size_t size)
{
    const char *prefix = NULL;
    char       *ptr, last = '\0';
    size_t      i, j;

    if (!base_name || !fullname || size < 1)
        return NULL;

    memset(fullname, 0, size);

    /* First use the environment variable, then try the constant */
    prefix = getenv("HDF4_TESTPREFIX");

#ifdef HDF4_TESTPREFIX
    if (!prefix)
        prefix = HDF4_TESTPREFIX;
#endif

    /* Prepend the prefix value to the base name */
    if (prefix && *prefix) {
        if (snprintf(fullname, size, "%s/%s", prefix, base_name) == (int)size)
            /* Buffer is too small */
            return NULL;
    }
    else {
        if (strlen(base_name) >= size)
            /* Buffer is too small */
            return NULL;
        else
            strcpy(fullname, base_name);
    }

    /* Remove any double slashes in the filename */
    for (ptr = fullname, i = j = 0; ptr && i < size; i++, ptr++) {
        if (*ptr != '/' || last != '/')
            fullname[j++] = *ptr;
        last = *ptr;
    }
    return fullname;

} /* end fixname() */

static long
read_test(int32 aid)
{
    struct timeval start_time, end_time; /* timing counts */
    long           acc_time;
    int32          ret;
    intn           i;         /* local counting index */
    intn           timing;    /* Which timing test we are on */
    intn           err_count; /* number of incorrect array positions */

    acc_time = 0;
    for (timing = 0; timing < NUM_TIMINGS; timing++) {

        /* Seek to beginning of element */
        ret = Hseek(aid, 0, DF_START);
        CHECK(ret, FAIL, "Hseek");

        switch (timing) {
            case 0: /* Read entire buffer in one I/O operation */
                gettimeofday(&start_time, NULL);
                ret = Hread(aid, elemsize, in_buf);
                VERIFY(ret, elemsize, "Hread");
                gettimeofday(&end_time, NULL);
                break;

            case 1: /* Read entire buffer one byte at a time forwards */
                gettimeofday(&start_time, NULL);
                for (i = 0; i < elemsize; i++) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hread(aid, 1, &in_buf[i]);
                    VERIFY(ret, 1, "Hread");
                }
                gettimeofday(&end_time, NULL);
                break;

            case 2: /* Read entire buffer one byte at a time every one byte forwards */
                gettimeofday(&start_time, NULL);
                for (i = 0; i < elemsize; i += 2) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hread(aid, 1, &in_buf[i]);
                    VERIFY(ret, 1, "Hread");
                }
                for (i = 1; i < elemsize; i += 2) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hread(aid, 1, &in_buf[i]);
                    VERIFY(ret, 1, "Hread");
                }
                gettimeofday(&end_time, NULL);
                break;

            case 3: /* Read entire buffer one byte at a time backwards */
                gettimeofday(&start_time, NULL);
                for (i = elemsize - 1; i >= 0; i--) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hread(aid, 1, &in_buf[i]);
                    VERIFY(ret, 1, "Hread");
                }
                gettimeofday(&end_time, NULL);
                break;

            case 4: /* Read entire buffer one byte at a time every one byte backwards */
                gettimeofday(&start_time, NULL);
                for (i = elemsize - 1; i >= 0; i -= 2) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hread(aid, 1, &in_buf[i]);
                    VERIFY(ret, 1, "Hread");
                }
                for (i = elemsize - 2; i >= 0; i -= 2) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hread(aid, 1, &in_buf[i]);
                    VERIFY(ret, 1, "Hread");
                }
                gettimeofday(&end_time, NULL);
                break;
        } /* end switch */

        /* Verify buffer contents */
        for (err_count = 0, i = 0; i < elemsize; i++) {
            if (out_buf[i] != in_buf[i]) {
                printf("Position (%d) read in is (%d), should be (%d)\n", i, (int)in_buf[i], (int)out_buf[i]);
                num_errs++;
                err_count++;
                if (err_count > 10)
                    break;
            }
        }

        /* Clear input buffer */
        memset(in_buf, 0, (size_t)elemsize);

        /* Increment the total I/O time */
        acc_time += (end_time.tv_sec - start_time.tv_sec) * FACTOR + (end_time.tv_usec - start_time.tv_usec);
    } /* end for */

    return acc_time;
} /* end read_test() */

static long
write_test(int32 aid, intn num_timings)
{
    struct timeval start_time, end_time; /* timing counts */
    long           acc_time;
    int32          ret;
    intn           i;      /* local counting index */
    intn           timing; /* Which timing test we are on */

    acc_time = 0;
    for (timing = 0; timing < num_timings; timing++) {

        /* Refresh output buffer with new values */
        init_buffer();

        /* Seek to beginning of element */
        ret = Hseek(aid, 0, DF_START);
        CHECK(ret, FAIL, "Hseek");

        switch (timing) {
            case 0: /* Write entire buffer in one I/O operation */
                gettimeofday(&start_time, NULL);
                ret = Hwrite(aid, elemsize, out_buf);
                VERIFY(ret, elemsize, "Hwrite");
                gettimeofday(&end_time, NULL);
                break;

            case 1: /* Write entire buffer one byte at a time forwards */
                gettimeofday(&start_time, NULL);
                for (i = 0; i < elemsize; i++) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hwrite(aid, 1, &out_buf[i]);
                    VERIFY(ret, 1, "Hwrite");
                }
                gettimeofday(&end_time, NULL);
                break;

            case 2: /* Write entire buffer one byte at a time every one byte forwards */
                gettimeofday(&start_time, NULL);
                for (i = 0; i < elemsize; i += 2) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hwrite(aid, 1, &out_buf[i]);
                    VERIFY(ret, 1, "Hwrite");
                }
                for (i = 1; i < elemsize; i += 2) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hwrite(aid, 1, &out_buf[i]);
                    VERIFY(ret, 1, "Hwrite");
                }
                gettimeofday(&end_time, NULL);
                break;

            case 3: /* Write entire buffer one byte at a time backwards */
                gettimeofday(&start_time, NULL);
                for (i = elemsize - 1; i >= 0; i--) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hwrite(aid, 1, &out_buf[i]);
                    VERIFY(ret, 1, "Hwrite");
                }
                gettimeofday(&end_time, NULL);
                break;

            case 4: /* Write entire buffer one byte at a time every one byte backwards */
                gettimeofday(&start_time, NULL);
                for (i = elemsize - 1; i >= 0; i -= 2) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hwrite(aid, 1, &out_buf[i]);
                    VERIFY(ret, 1, "Hwrite");
                }
                for (i = elemsize - 2; i >= 0; i -= 2) {
                    /* Seek to correct location within element */
                    ret = Hseek(aid, i, DF_START);
                    CHECK(ret, FAIL, "Hseek");

                    ret = Hwrite(aid, 1, &out_buf[i]);
                    VERIFY(ret, 1, "Hwrite");
                }
                gettimeofday(&end_time, NULL);
                break;
        } /* end switch */

        /* Seek to beginning of element */
        ret = Hseek(aid, 0, DF_START);
        CHECK(ret, FAIL, "Hseek");

        /* Read buffer contents */
        ret = Hread(aid, elemsize, in_buf);
        VERIFY(ret, elemsize, "Hread");

        /* Verify buffer contents */
        for (i = 0; i < elemsize; i++) {
            if (out_buf[i] != in_buf[i]) {
                printf("Position (%d) read in is (%d), should be (%d)\n", i, (int)in_buf[i], (int)out_buf[i]);
                num_errs++;
                break;
            }
        }

        /* Clear input buffer */
        memset(in_buf, 0, (size_t)elemsize);

        /* Increment the total I/O time */
        acc_time += (end_time.tv_sec - start_time.tv_sec) * FACTOR + (end_time.tv_usec - start_time.tv_usec);
    } /* end for */

    return acc_time;
} /* end read_test() */

int
main(int argc, char *argv[])
{
    model_info m_info;
    comp_info  c_info;
    uint16     ref_num; /* reference number of the data written out */
    int32      fid;     /* file ID of HDF file for testing */
    int32      aid;     /* AID of element to test */
    intn       test_num;
    int32      ret;
    char       hfilename[32];
    char       extfilename[32];
    int        CleanUp = 1;
    int        Cache   = 1;
    uint32     lmajor, lminor, lrelease;
    char       lstring[81];

    /* Un-buffer stdout */
    setbuf(stdout, NULL);

    if (argc > 2) {
        usage();
        exit(1);
    }
    else
        elemsize = (argc == 2) ? (int32)atol(argv[1]) : (int32)ELEMSIZE;

    if (elemsize <= 0) {
        usage();
        exit(1);
    }

    out_buf = malloc((size_t)elemsize * sizeof(uint8));
    in_buf  = malloc((size_t)elemsize * sizeof(uint8));

    Verbosity = 4; /* Default Verbosity is Low */

    Hgetlibversion(&lmajor, &lminor, &lrelease, lstring);

    printf("Built with HDF Library Version: %u.%u.%u, %s\n\n", (unsigned)lmajor, (unsigned)lminor,
           (unsigned)lrelease, lstring);

    MESSAGE(6, printf("Starting buffered element test (elemsize=%d)\n", elemsize);)

    if (Cache) /* turn on caching, unless we were instructed not to */
        Hcache(CACHE_ALL_FILES, TRUE);

    /* fill the buffer with interesting data to compress */
    init_buffer();

    fixname(TESTFILE_NAME, hfilename, sizeof hfilename);

    /* open the HDF file */
    fid = Hopen(hfilename, DFACC_ALL, 0);
    CHECK(fid, FAIL, "Hopen");

    /* Cycle through the different testing element types */
    /* Performing timings on each type of buffer and record results for output */
    /* if verbosity level is high enough */
    for (test_num = 0; test_num < NUM_TESTS; test_num++) {
        /* Get a new reference number */
        ref_num = Htagnewref(fid, BUFF_TAG);
        CHECK(ref_num, 0, "Htagnewref");

        /* Create the data element to perform the tests on */
        switch (test_num) {
            case 0: /* create plain data element */
                aid = Hstartaccess(fid, BUFF_TAG, ref_num, DFACC_RDWR);
                CHECK(aid, FAIL, "Hstartaccess");
                break;

            case 1: /* create external data element */
                fixname(EXTFILE_NAME, extfilename, sizeof extfilename);
                aid = HXcreate(fid, BUFF_TAG, ref_num, extfilename, 0, elemsize);
                CHECK(aid, FAIL, "HXcreate");
                break;

            case 2: /* create compressed data element */
                c_info.deflate.level = 9;
                aid = HCcreate(fid, BUFF_TAG, ref_num, COMP_MODEL_STDIO, &m_info, COMP_CODE_DEFLATE, &c_info);
                CHECK(aid, FAIL, "HCcreate");
                break;

            case 3: /* create linked-block data element */
                aid = HLcreate(fid, BUFF_TAG, ref_num, HDF_APPENDABLE_BLOCK_LEN, HDF_APPENDABLE_BLOCK_NUM);
                CHECK(aid, FAIL, "HLcreate");
                break;
        }

        /* Write the initial data to the data element */
        ret = Hwrite(aid, elemsize, out_buf);
        VERIFY(ret, elemsize, "Hwrite");

        /* Perform read timing tests on un-buffered data element */
        read_time[test_num][0] = read_test(aid);

        /* Perform write timing tests on un-buffered data element */
        /* Just write un-buffered compressed data in one block */
        write_time[test_num][0] = write_test(aid, (test_num == 2 ? 1 : NUM_TIMINGS));

        /* Convert element to a buffered element */
        ret = HBconvert(aid);
        CHECK(ret, FAIL, "HBconvert");

        /* Perform read timing tests on buffered data element */
        read_time[test_num][1] = read_test(aid);

        /* Perform write timing tests on un-buffered data element */
        write_time[test_num][1] = write_test(aid, NUM_TIMINGS);

        /* Close data element */
        ret = Hendaccess(aid);
        CHECK(ret, FAIL, "Hendaccess");

        MESSAGE(3, {
            printf("Unbuffered read time=%f seconds\n", ((double)read_time[test_num][0] / FACTOR));
            printf("Unbuffered write time=%f seconds\n", ((double)write_time[test_num][0] / FACTOR));
            printf("Buffered read time=%f seconds\n", ((double)read_time[test_num][1] / FACTOR));
            printf("Buffered write time=%f seconds\n", ((double)write_time[test_num][1] / FACTOR));
        })

    } /* end for */

    /* close the HDF file */
    ret = Hclose(fid);
    CHECK(ret, FAIL, "Hclose");

    /* Clean up files created */
    if (CleanUp) {
        remove(extfilename);
        remove(hfilename);
    }

    free(out_buf);
    free(in_buf);

    MESSAGE(6, printf("Finished buffered element test\n");)
    return num_errs;
} /* end main() */