summaryrefslogtreecommitdiffstats
path: root/test/h5test.c
blob: 0571493a3aa65453253a2c765801156a83ecaa4c (plain)
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
/*
 * Copyright (c) 1998-2001 National Center for Supercomputing Applications
 *                         All rights reserved.
 *
 * Programmer:  Robb Matzke <matzke@llnl.gov>
 *              Thursday, November 19, 1998
 *
 * Purpose:	Provides support functions for most of the hdf5 tests cases.
 *		
 */

#undef NDEBUG			/*override -DNDEBUG			*/
#include "h5test.h"
#include <sys/types.h>
#include <sys/stat.h>

#ifdef WIN32
#include <process.h>
#include <direct.h>
#endif  /* WIN32 */

/*
 * Define these environment variables or constants to influence functions in
 * this test support library.  The environment variable is used in preference
 * to the cpp constant.  If neither is defined then use some default value.
 *
 * HDF5_DRIVER:		This string describes what low level file driver to
 *			use for HDF5 file access.  The first word in the
 *			value is the name of the driver and subsequent data
 *			is interpreted according to the driver.  See
 *			h5_fileaccess() for details.
 *
 * HDF5_PREFIX:		A string to add to the beginning of all serial test
 *			file names.  This can be used to run tests in a
 *			different file system (e.g., "/tmp" or "/tmp/myname").
 *			The prefix will be separated from the base file name
 *			by a slash. See h5_fixname() for details.
 *
 * HDF5_PARAPREFIX:	A string to add to the beginning of all parallel test
 *			file names.  This can be used to tell MPIO what driver
 *			to use (e.g., "gfs:", "ufs:", or "nfs:") or to use a
 *			different file system (e.g., "/tmp" or "/tmp/myname").
 *			The prefix will be separated from the base file name
 *			by a slash. See h5_fixname() for details.
 *
 */
/*
 * In a parallel machine, the filesystem suitable for compiling is
 * unlikely a parallel file system that is suitable for parallel I/O.
 * There is no standard pathname for the parallel file system.  /tmp
 * is about the best guess.
 */
#ifndef HDF5_PARAPREFIX
#ifdef __PUMAGON__
/* For the PFS of TFLOPS */
#define HDF5_PARAPREFIX "pfs:/pfs_grande/multi/tmp_1"
#else
#define HDF5_PARAPREFIX "/tmp"
#endif
#endif
char	*paraprefix = NULL;	/* for command line option para-prefix */

/*
 * These are the letters that are appended to the file name when generating
 * names for the split and multi drivers. They are:
 *
 * 	m: All meta data when using the split driver.
 *	s: The userblock, superblock, and driver info block
 *	b: B-tree nodes
 *	r: Dataset raw data
 *	g: Global heap
 *	l: local heap (object names)
 *	o: object headers
 */
static const char *multi_letters = "msbrglo";


/*-------------------------------------------------------------------------
 * Function:	h5_errors
 *
 * Purpose:	Displays the error stack after printing "*FAILED*".
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Robb Matzke
 *		Wednesday, March  4, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
h5_errors(void UNUSED *client_data)
{
    H5_FAILED();
    H5Eprint (stdout);
    return 0;
}


/*-------------------------------------------------------------------------
 * Function:	h5_cleanup
 *
 * Purpose:	Cleanup temporary test files.
 *		base_name contains the list of test file names.
 *		The file access property list is also closed.
 *
 * Return:	Non-zero if cleanup actions were performed; zero otherwise.
 *
 * Programmer:	Albert Cheng
 *              May 28, 1998
 *
 * Modifications:
 *		Albert Cheng, 2000-09-09
 *		Added the explicite base_name argument to replace the
 *		global variable FILENAME.
 *
 *-------------------------------------------------------------------------
 */
int
h5_cleanup(const char *base_name[], hid_t fapl)
{
    char	filename[1024];
    char	temp[2048];
    int		i, j;
    int		retval=0;
#ifndef H5_WANT_H5_V1_2_COMPAT
    hid_t	driver;
#endif /* H5_WANT_H5_V1_2_COMPAT */

    if (!HDgetenv("HDF5_NOCLEANUP")) {
	for (i = 0; base_name[i]; i++) {
	    if (h5_fixname(base_name[i], fapl, filename, sizeof(filename)) == NULL)
		continue;

#ifdef H5_WANT_H5_V1_2_COMPAT
	    switch (H5Pget_driver(fapl)) {
            case H5F_LOW_CORE:
                break; /*nothing to remove*/
		
            case H5F_LOW_SPLIT:
                HDsnprintf(temp, sizeof(temp), "%s.raw", filename);
                HDremove(temp);
                HDsnprintf(temp, sizeof(temp), "%s.meta", filename);
                HDremove(temp);
                break;

            case H5F_LOW_FAMILY:
                for (j = 0; /*void*/; j++) {
                    HDsnprintf(temp, sizeof(temp), filename, j);

                    if (HDaccess(temp, F_OK) < 0)
                        break;

                    HDremove(temp);
                }

                break;

            default:
                HDremove(filename);
                break;
	    }
#else /* H5_WANT_H5_V1_2_COMPAT */

	    driver = H5Pget_driver(fapl);

	    if (driver == H5FD_FAMILY) {
		for (j = 0; /*void*/; j++) {
		    HDsnprintf(temp, sizeof temp, filename, j);

		    if (HDaccess(temp, F_OK) < 0)
                        break;

		    HDremove(temp);
		}
	    } else if (driver == H5FD_CORE) {
		/*void*/
	    } else if (driver == H5FD_MULTI) {
		H5FD_mem_t mt;
		assert(strlen(multi_letters)==H5FD_MEM_NTYPES);

		for (mt = H5FD_MEM_DEFAULT; mt < H5FD_MEM_NTYPES; mt++) {
		    HDsnprintf(temp, sizeof temp, "%s-%c.h5",
			       filename, multi_letters[mt]);
		    HDremove(temp); /*don't care if it fails*/
		}
	    } else {
		HDremove(filename);
	    }
#endif /* H5_WANT_H5_V1_2_COMPAT */
	}

	retval = 1;
    }

    H5Pclose(fapl);
    return retval;
}


/*-------------------------------------------------------------------------
 * Function:	h5_reset
 *
 * Purpose:	Reset the library by closing it.
 *
 * Return:	void
 *
 * Programmer:	Robb Matzke
 *              Friday, November 20, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void
h5_reset(void)
{
    char	filename[1024];
    
    HDfflush(stdout);
    HDfflush(stderr);
    H5close();
    H5Eset_auto (h5_errors, NULL);

    /*
     * Cause the library to emit some diagnostics early so they don't
     * interfere with other formatted output.
     */
    sprintf(filename, "/tmp/h5emit-%05d.h5", HDgetpid());
    H5E_BEGIN_TRY {
	hid_t file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT,
			       H5P_DEFAULT);
	hid_t grp = H5Gcreate(file, "emit", 0);
	H5Gclose(grp);
	H5Fclose(file);
	HDunlink(filename);
    } H5E_END_TRY;
}


/*-------------------------------------------------------------------------
 * Function:	h5_fixname
 *
 * Purpose:	Create 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 HDF5_PREFIX (separated from the
 *		base name by a slash) and appending a file extension based on
 *		the driver supplied, resulting in something like
 *		`ufs:/u/matzke/test.h5'.
 *
 * Return:	Success:	The FULLNAME pointer.
 *
 *		Failure:	NULL if BASENAME or FULLNAME is the null
 *				pointer or if FULLNAME isn't large enough for
 *				the result.
 *
 * Programmer:	Robb Matzke
 *              Thursday, November 19, 1998
 *
 * Modifications:
 *		Robb Matzke, 1999-08-03
 *		Modified to use the virtual file layer.
 *
 *		Albert Cheng, 2000-01-25
 *		Added prefix for parallel test files.
 *-------------------------------------------------------------------------
 */
char *
h5_fixname(const char *base_name, hid_t fapl, char *fullname, size_t size)
{
    const char     *prefix = NULL;
    const char     *suffix = ".h5"; /* suffix has default */
    char           *ptr, last = '\0';
    size_t          i, j;
#ifdef H5_WANT_H5_V1_2_COMPAT
    H5F_driver_t    driver;
#else
    hid_t           driver;
#endif /* H5_WANT_H5_V1_2_COMPAT */
    
    if (!base_name || !fullname || size < 1)
        return NULL;

    memset(fullname, 0, size);

#ifdef H5_WANT_H5_V1_2_COMPAT
    /* figure out the suffix */
    if (H5P_DEFAULT != fapl){
	if ((driver = H5Pget_driver(fapl)) < 0)
            return NULL;

	if (H5F_LOW_FAMILY == driver) {
	    suffix = "%05d.h5";
	} else if (H5F_LOW_CORE == driver) {
	    suffix = NULL;
	} 
    }
    
    /* Use different ones depending on parallel or serial driver used. */
    if (H5P_DEFAULT != fapl && H5F_LOW_MPIO == driver){
	/* For parallel:
	 * First use command line option, then the environment variable,
	 * then try the constant
	 */
	prefix = (paraprefix ? paraprefix : getenv("HDF5_PARAPREFIX"));

#ifdef HDF5_PARAPREFIX
	if (!prefix)
            prefix = HDF5_PARAPREFIX;
#endif  /* HDF5_PARAPREFIX */
    }else{
	/* For serial:
	 * First use the environment variable, then try the constant
	 */
	prefix = HDgetenv("HDF5_PREFIX");

#ifdef HDF5_PREFIX
	if (!prefix)
            prefix = HDF5_PREFIX;
#endif  /* HDF5_PREFIX */
    }
#else /* H5_WANT_H5_V1_2_COMPAT */
    /* figure out the suffix */
    if (H5P_DEFAULT != fapl){
	if ((driver = H5Pget_driver(fapl)) < 0)
            return NULL;

	if (H5FD_FAMILY == driver)
	    suffix = "%05d.h5";
	else if (H5FD_CORE == driver || H5FD_MULTI == driver)
	    suffix = NULL;
    }
    
    /* Use different ones depending on parallel or serial driver used. */
    if (H5P_DEFAULT != fapl && H5FD_MPIO == driver){
	/* For parallel:
	 * First use command line option, then the environment variable,
	 * then try the constant
	 */
	prefix = (paraprefix ? paraprefix : getenv("HDF5_PARAPREFIX"));

#ifdef HDF5_PARAPREFIX
	if (!prefix)
            prefix = HDF5_PARAPREFIX;
#endif  /* HDF5_PARAPREFIX */
    }else{
	/* For serial:
	 * First use the environment variable, then try the constant
	 */
	prefix = HDgetenv("HDF5_PREFIX");

#ifdef HDF5_PREFIX
	if (!prefix)
            prefix = HDF5_PREFIX;
#endif  /* HDF5_PREFIX */
    }
#endif  /* H5_WANT_H5_V1_2_COMPAT */

    /* Prepend the prefix value to the base name */
    if (prefix && *prefix) {
#ifdef H5_WANT_H5_V1_2_COMPAT
        if (H5P_DEFAULT != fapl && H5F_LOW_MPIO == driver) {
#else
        if (H5P_DEFAULT != fapl && H5FD_MPIO == driver) {
#endif  /* H5_WANT_H5_V1_2_COMPAT */
            /* This is a parallel system */
            char *subdir;

            if (!HDstrcmp(prefix, HDF5_PARAPREFIX)) {
                /* If the prefix specifies the HDF5_PARAPREFIX directory, then
                 * default to using the "/tmp/$USER" or "/tmp/$LOGIN"
                 * directory instead. */
                char *user, *login;

                user = HDgetenv("USER");
                login = HDgetenv("LOGIN");
                subdir = (user ? user : login);

                if (subdir) {
                    for (i = 0; i < size && prefix[i]; i++)
                        fullname[i] = prefix[i];

                    fullname[i++] = '/';

                    for (j = 0; i < size && subdir[j]; i++, j++)
                        fullname[i] = subdir[j];
                }
            }

            if (!fullname[0])
                /* We didn't append the prefix yet */
                HDstrncpy(fullname, prefix, MIN(strlen(prefix), size));

            if (HDstrlen(fullname) + HDstrlen(base_name) + 1 < size) {
                /* Append the base_name with a slash first. Multiple slashes are
                 * handled below. */
                struct stat buf;

                if (HDstat(fullname, &buf) < 0)
                    /* The directory doesn't exist just yet */
                    if (HDmkdir(fullname, (mode_t)0755) < 0 && errno != EEXIST) {
                        /* We couldn't make the "/tmp/${USER,LOGIN}" subdirectory.
                         * Default to PREFIX's original prefix value. */
                        HDstrcpy(fullname, prefix);
                    }

                HDstrcat(fullname, "/");
                HDstrcat(fullname, base_name);
            } else {
                /* Buffer is too small */
                return NULL;
            }
        } else {
            if (HDsnprintf(fullname, size, "%s/%s", prefix, base_name) == (int)size)
                /* Buffer is too small */
                return NULL;
        }
    } else if (HDstrlen(base_name) >= size) {
	/* Buffer is too small */
	return NULL;
    } else {
	HDstrcpy(fullname, base_name);
   }

#ifdef H5_WANT_H5_V1_2_COMPAT
    /* Append a suffix */
    if ((driver = H5Pget_driver(fapl)) < 0)
        return NULL;

    switch (driver) {
        case H5F_LOW_SPLIT:
        case H5F_LOW_CORE:
            suffix = NULL;
            break;
        case H5F_LOW_FAMILY:
            suffix = "%05d.h5";
            break;
        default:
            suffix = ".h5";
            break;
    }
#endif /* H5_WANT_H5_V1_2_COMPAT */

    /* Append a suffix */
    if (suffix) {
	if (HDstrlen(fullname) + HDstrlen(suffix) >= size)
            return NULL;

	HDstrcat(fullname, suffix);
    }

    /* 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;
}


/*-------------------------------------------------------------------------
 * Function:	h5_fileaccess
 *
 * Purpose:	Returns a file access template which is the default template
 *		but with a file driver set according to the constant or
 *		environment variable HDF5_DRIVER
 *
 * Return:	Success:	A file access property list
 *
 *		Failure:	-1
 *
 * Programmer:	Robb Matzke
 *              Thursday, November 19, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hid_t
h5_fileaccess(void)
{
    const char	*val = NULL;
    const char	*name;
    char s[1024];
    hid_t fapl = -1;
    hsize_t fam_size = 100*1024*1024; /*100 MB*/
    long verbosity = 1;
    H5FD_mem_t	mt;
    
    /* First use the environment variable, then the constant */
    val = HDgetenv("HDF5_DRIVER");
#ifdef HDF5_DRIVER
    if (!val) val = HDF5_DRIVER;
#endif

    if ((fapl=H5Pcreate(H5P_FILE_ACCESS))<0) return -1;
    if (!val || !*val) return fapl; /*use default*/
    
    HDstrncpy(s, val, sizeof s);
    s[sizeof(s)-1] = '\0';
    if (NULL==(name=HDstrtok(s, " \t\n\r"))) return fapl;

    if (!HDstrcmp(name, "sec2")) {
	/* Unix read() and write() system calls */
	if (H5Pset_fapl_sec2(fapl)<0) return -1;
    } else if (!HDstrcmp(name, "stdio")) {
	/* Standard C fread() and fwrite() system calls */
	if (H5Pset_fapl_stdio(fapl)<0) return -1;
    } else if (!HDstrcmp(name, "core")) {
	/* In-core temporary file with 1MB increment */
	if (H5Pset_fapl_core(fapl, 1024*1024, FALSE)<0) return -1;
    } else if (!HDstrcmp(name, "split")) {
	/* Split meta data and raw data each using default driver */
	if (H5Pset_fapl_split(fapl,
			      "-m.h5", H5P_DEFAULT,
			      "-r.h5", H5P_DEFAULT)<0)
	    return -1;
    } else if (!HDstrcmp(name, "multi")) {
	/* Multi-file driver, general case of the split driver */
	H5FD_mem_t memb_map[H5FD_MEM_NTYPES];
	hid_t memb_fapl[H5FD_MEM_NTYPES];
	const char *memb_name[H5FD_MEM_NTYPES];
	char sv[H5FD_MEM_NTYPES][1024];
	haddr_t memb_addr[H5FD_MEM_NTYPES];

	HDmemset(memb_map, 0, sizeof memb_map);
	HDmemset(memb_fapl, 0, sizeof memb_fapl);
	HDmemset(memb_name, 0, sizeof memb_name);
	HDmemset(memb_addr, 0, sizeof memb_addr);

	assert(HDstrlen(multi_letters)==H5FD_MEM_NTYPES);
	for (mt=H5FD_MEM_DEFAULT; mt<H5FD_MEM_NTYPES; mt++) {
	    memb_fapl[mt] = H5P_DEFAULT;
	    sprintf(sv[mt], "%%s-%c.h5", multi_letters[mt]);
	    memb_name[mt] = sv[mt];
	    memb_addr[mt] = MAX(mt-1,0)*(HADDR_MAX/10);
	}

	if (H5Pset_fapl_multi(fapl, memb_map, memb_fapl, memb_name,
			      memb_addr, FALSE)<0) {
	    return -1;
	}
    } else if (!HDstrcmp(name, "family")) {
	/* Family of files, each 1MB and using the default driver */
	if ((val=HDstrtok(NULL, " \t\n\r"))) {
	    fam_size = HDstrtod(val, NULL) * 1024*1024;
	}
	if (H5Pset_fapl_family(fapl, fam_size, H5P_DEFAULT)<0) return -1;
    } else if (!HDstrcmp(name, "log")) {
        /* Log file access */
        if ((val = HDstrtok(NULL, " \t\n\r")))
            verbosity = HDstrtol(val, NULL, 0);

        if (H5Pset_fapl_log(fapl, NULL, (int)verbosity) < 0)
	    return -1;
    } else {
	/* Unknown driver */
	return -1;
    }

    return fapl;
}


/*-------------------------------------------------------------------------
 * Function:	h5_no_hwconv
 *
 * Purpose:	Turn off hardware data type conversions.
 *
 * Return:	void
 *
 * Programmer:	Robb Matzke
 *              Friday, November 20, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void
h5_no_hwconv(void)
{
    H5Tunregister(H5T_PERS_HARD, NULL, -1, -1, NULL);
}