Commit 11e399b6 authored by gsell's avatar gsell
Browse files

C and Fortran examples reviewed, new examples added

parent e839226f
......@@ -6,6 +6,7 @@ LDADD =
if ENABLE_FORTRAN
LDADD += -lH5hutF
AM_LDFLAGS += -L${abs_top_builddir}/src/Fortran/.libs
endif
LDADD += -lH5hut
......@@ -13,19 +14,32 @@ LDADD += -lH5hut
noinst_PROGRAMS =
if ENABLE_C
noinst_PROGRAMS += \
openclose \
file_attribs \
attach_file
noinst_PROGRAMS += \
attach_file \
openclose \
query \
read_file_attribs \
read_step_attribs \
write_file_attribs \
write_step_attribs
endif
if ENABLE_FORTRAN
noinst_PROGRAMS +=
noinst_PROGRAMS += \
openclosef \
queryf \
read_file_attribsf \
read_step_attribsf \
write_file_attribsf \
write_step_attribsf
openclosef_SOURCES = openclosef.f90
queryf_SOURCES = queryf.f90
read_file_attribsf_SOURCES = read_file_attribsf.f90
read_step_attribsf_SOURCES = read_step_attribsf.f90
write_file_attribsf_SOURCES = write_file_attribsf.f90
write_step_attribsf_SOURCES = write_step_attribsf.f90
endif
#EXTRA_PROGRAMS = openclose
#openclose_SOURCES = openclose.c
#%.o : %.f90
# $(FC) $(FFLAGS) -c $<
%.o : %.f90
$(FC) $(FFLAGS) -c $<
......@@ -19,20 +19,14 @@ main (
int argc,
char* argv[]
) {
MPI_Comm comm = MPI_COMM_WORLD;
int myproc;
int nprocs;
MPI_Init (&argc, &argv);
MPI_Comm_size (comm, &nprocs);
MPI_Comm_rank (comm, &myproc);
H5SetErrorHandler (H5AbortErrorhandler);
H5SetVerbosityLevel (255);
h5_file_t f = H5OpenFile (FNAME, H5_O_WRONLY, comm);
h5_file_t f = H5OpenFile (FNAME, H5_O_WRONLY, H5_PROP_DEFAULT);
H5AddAttachment (f, ATTACHMENT);
H5CloseFile (f);
f = H5OpenFile (FNAME, H5_O_RDONLY, 0);
f = H5OpenFile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT);
h5_ssize_t num_attachments = H5GetNumAttachments (f);
printf ("Number of attachments: %lld\n", (long long int)num_attachments);
int i;
......
......@@ -21,12 +21,9 @@ main (
MPI_Comm_size (comm, &nprocs);
MPI_Comm_rank (comm, &myproc);
h5_file_t f = H5OpenFile ("testfile.h5", H5_O_WRONLY, comm);
H5CloseFile (f);
h5_prop_t prop = H5CreateFileProp ();
H5SetPropFileMPIO (prop, &comm);
f = H5OpenFile2 ("testfile.h5", H5_O_WRONLY, prop);
h5_file_t f = H5OpenFile ("testfile.h5", H5_O_WRONLY, prop);
H5CloseProp (prop);
H5CloseFile (f);
......
!
! Copyright (c) 2006-2013, The Regents of the University of California,
! through Lawrence Berkeley National Laboratory (subject to receipt of any
! required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
! Institut (Switzerland). All rights reserved.!
!
! License: see file COPYING in top level of source distribution.
!
include 'H5hut.f90'
program openclose
use H5hut
implicit none
include 'mpif.h'
integer :: comm, rank, ierr
integer*8 :: file_id, status
integer*8 :: props
comm = MPI_COMM_WORLD
call mpi_init(ierr)
call mpi_comm_rank(comm, rank, ierr)
props = h5_createprop_file ()
status = h5_setprop_filempio (props, comm)
file_id = h5_openfile ("testfile.h5", H5_O_WRONLY, props)
status = h5_closeprop (props)
status = h5_closefile (file_id);
call mpi_finalize(ierr)
end program openclose
/*
Copyright (c) 2006-2013, The Regents of the University of California,
through Lawrence Berkeley National Laboratory (subject to receipt of any
required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
Institut (Switzerland). All rights reserved.
License: see file COPYING in top level of source distribution.
*/
#include "H5hut.h"
#define FNAME1 "example_file_attribs.h5"
#define FNAME2 "example_step_attribs.h5"
/*
Due to the way types are defined in H5hut, we cannot use "switch() {}"
*/
const char*
type2string (
h5_int64_t type
) {
if (type == H5_FLOAT64_T)
return "H5_FLOAT64_T";
if (type == H5_FLOAT32_T)
return "H5_FLOAT32_T";
if (type == H5_INT64_T)
return "H5_INT64_T";
if (type == H5_INT32_T)
return "H5_INT32_T";
if (type == H5_STRING_T)
return "H5_STRING_T";
return "unknown type";
}
static inline void
print_header (
h5_int64_t n
) {
if (n > 0) {
printf ("\t%-6s %-30s %-15s %-10s\n", "idx", "name", "type", "dim");
}
}
static inline void
print_query_result (
h5_int64_t i,
const char* const name,
h5_int64_t type,
h5_int64_t dim
) {
printf ("\t%-6lld %-30s %-15s %-10lld\n", i, name, type2string(type), dim);
}
void
query_file_attribs (
h5_int64_t f
) {
char name[H5_MAX_NAME_LEN];
h5_int64_t type;
h5_size_t dim;
// query # of file attributes
h5_int64_t n = H5GetNumFileAttribs (f);
printf ("\tNumber of file attributes: %lld\n", n);
// output name and type of all file attribute
print_header (n);
for (h5_int64_t i = 0; i < n; i++) {
H5GetFileAttribInfo (f, i, name, sizeof(name), &type, &dim);
print_query_result (i, name, type, dim);
}
printf ("\n");
}
void
query_step_attribs (
h5_int64_t f,
h5_int64_t step
) {
char name[H5_MAX_NAME_LEN];
h5_int64_t type;
h5_size_t dim;
H5SetStep (f, step);
// query # of step attributes
h5_int64_t n = H5GetNumStepAttribs (f);
printf ("\tNumber of step attributes: %lld\n", n);
// output name and type of all step attribute
print_header (n);
for (h5_int64_t i = 0; i < n; i++) {
H5GetStepAttribInfo (f, i, name, sizeof(name), &type, &dim);
print_query_result (i, name, type, dim);
}
}
void
query_file (
const char* const fname
) {
printf ("\nFile: %s\n", fname);
// if file properties is set to default, MPI_COMM_WORLD will be used
h5_file_t f = H5OpenFile (fname, H5_O_RDONLY, H5_PROP_DEFAULT);
// query and output file attribs
query_file_attribs (f);
// query # of steps, if > 0: go to first step, query and output step attribs
h5_int64_t n = H5GetNumSteps (f);
printf ("\tNumber of steps: %lld\n", n);
if (n > 0) {
// go to first step
h5_int64_t i = -1;
while (!H5HasStep (f, ++i));
query_step_attribs (f, i);
}
H5CloseFile (f);
}
int
main (
int argc,
char** argv
) {
MPI_Init (&argc, &argv);
H5AbortOnError ();
query_file (FNAME1);
query_file (FNAME2);
MPI_Finalize ();
return 0;
}
!
! Copyright (c) 2006-2013, The Regents of the University of California,
! through Lawrence Berkeley National Laboratory (subject to receipt of any
! required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
! Institut (Switzerland). All rights reserved.!
!
! License: see file COPYING in top level of source distribution.
!
include 'H5hut.f90'
program query
use H5hut
implicit none
include 'mpif.h'
! the file name we want to read
character (len=*), parameter :: FNAME1 = "example_file_attribs.h5"
character (len=*), parameter :: FNAME2 = "example_step_attribs.h5"
! verbosity level: set it to a power of 2 minus one or zero
integer*8, parameter :: verbosity_level = 1
! used for mpi error return
integer :: ierr
call mpi_init (ierr)
! abort program on any H5hut error
call h5_abort_on_error ()
call h5_set_verbosity_level (verbosity_level)
call query_file (FNAME1);
call query_file (FNAME2);
call mpi_finalize(ierr)
call exit (ierr)
contains
subroutine query_file (fname)
character(len=*), intent(in):: fname
integer*8 file_id
integer*8 i, n, status
write (*, '("File: ", a)') fname
! if file properties is set to default, MPI_COMM_WORLD will be used
file_id = h5_openfile (fname, H5_O_RDONLY, H5_PROP_DEFAULT)
! query and output file attribs
call query_file_attribs (file_id);
! query # of steps, if > 0: go to first step, query and output step attribs
n = h5_getnsteps (file_id);
write (*, '(T8, "Number of steps: ", I0)') n
if (n > 0) then
! go to first step
i = 0;
do
if (h5_hasstep (file_id, i)) exit
i = i+1
end do
call query_step_attribs (file_id, i);
end if
status = h5_closefile (file_id)
end subroutine query_file
! print header for attribute metadata table
subroutine print_header (n)
integer*8, intent(in):: n
character(len=6), parameter :: idx = "idx"
character(len=30), parameter :: name = "name"
character(len=15), parameter :: type = "type"
character(len=10), parameter :: dim = "dim"
if (n > 0) then
write (*, '(T8, A, 1X, A, 1X, A, 1X, A)') idx, name, type, dim
end if
end subroutine print_header
! output attribute metadata
subroutine print_query_result (i, name, type, dim)
integer*8, intent(in):: i
character(len=*), intent(in):: name
integer*8, intent(in):: type
integer*8, intent(in):: dim
character(len=30) name_
character(len=15) type_, type_char
character(len=6) i_
character(len=10) dim_
if (type == H5_FLOAT64_T) then
type_char = "H5_FLOAT64_T"
else if (type == H5_FLOAT32_T) then
type_char = "H5_FLOAT32_T"
else if (type == H5_INT64_T) then
type_char = "H5_INT64_T"
else if (type == H5_INT32_T) then
type_char = "H5_INT32_T"
else if (type == H5_STRING_T) then
type_char = "H5_STRING_T"
else
type_char = "unknown type"
end if
write (i_, '(I6)') i
write (name_, '(A30)') name
write (type_, '(A15)') type_char
write (dim_, '(I10)') dim
write (*, '(T8, A6, 1X, A30, 1X, A15, 1X, A10)') adjustl(i_), adjustl(name_), adjustl(type_), adjustl(dim_)
end subroutine print_query_result
subroutine query_file_attribs (file_id)
integer*8, intent(in):: file_id
integer*8 status
integer*8 i, n
character(len=H5_MAX_NAME_LEN) name
integer*8 type, dim
! query # of file attributes
n = h5_getnfileattribs (file_id);
write (*, '(T8, "Number of file attributes: ", I0)') n
! output name and type of all file attribute
call print_header (n);
do i = 1, n
status = h5_getfileattribinfo (file_id, i, name, type, dim);
call print_query_result (i, name, type, dim);
end do
write (*,*)
end subroutine query_file_attribs
subroutine query_step_attribs (file_id, stepno)
integer*8, intent(in):: file_id
integer*8, intent(in):: stepno
integer*8 status
integer*8 i, n
character(len=H5_MAX_NAME_LEN) name
integer*8 type, dim
! Go to step #1
status = h5_setstep (file_id, stepno);
! query # of step attributes
n = h5_getnstepattribs (file_id)
write (*, '(T8, "Number of step attributes: ", i0)') n
! output name and type of all step attribute
call print_header (n)
do i = 1, n
status = h5_getstepattribinfo (file_id, i, name, type, dim)
call print_query_result (i, name, type, dim)
end do
end subroutine query_step_attribs
end program query
/*
Copyright (c) 2006-2013, The Regents of the University of California,
through Lawrence Berkeley National Laboratory (subject to receipt of any
required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
Institut (Switzerland). All rights reserved.
License: see file COPYING in top level of source distribution.
*/
#include "H5hut.h"
#define FNAME "example_file_attribs.h5"
#define ATTR_STRING "FileAttrString"
#define ATTR_INT32 "FileAttrInt32"
#define ATTR_INT64 "FileAttrInt64"
#define ATTR_FLOAT32 "FileAttrFloat32"
#define ATTR_FLOAT64 "FileAttrFloat64"
int
main (
int argc,
char** argv
) {
MPI_Init (&argc, &argv);
H5AbortOnError ();
// if file properties is set to default, MPI_COMM_WORLD will be used
h5_file_t f = H5OpenFile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT);
h5_size_t len;
H5GetFileAttribInfoByName (f, ATTR_STRING, NULL, &len);
char* attr_string = malloc (len+1);
H5ReadFileAttribString (f, ATTR_STRING, attr_string);
printf ("%s: %s\n", ATTR_STRING, attr_string);
free (attr_string);
H5GetFileAttribInfoByName (f, ATTR_INT32, NULL, &len);
int32_t* attr_int32 = malloc (sizeof(*attr_int32)*len);
H5ReadFileAttribInt32 (f, ATTR_INT32, attr_int32);
printf ("%s:", ATTR_INT32);
for (int i = 0; i < len; i++) {
printf (" %d", attr_int32[i]);
}
printf ("\n");
free (attr_int32);
H5GetFileAttribInfoByName (f, ATTR_INT64, NULL, &len);
int64_t* attr_int64 = malloc (sizeof(*attr_int64)*len);
H5ReadFileAttribInt64 (f, ATTR_INT64, attr_int64);
printf ("%s:", ATTR_INT64);
for (int i = 0; i < len; i++) {
printf (" %lld", (long long int)attr_int64[i]);
}
printf ("\n");
free (attr_int64);
H5GetFileAttribInfoByName (f, ATTR_FLOAT32, NULL, &len);
h5_float32_t* attr_float32 = malloc (sizeof(*attr_float32)*len);
H5ReadFileAttribFloat32 (f, ATTR_FLOAT32, attr_float32);
printf ("%s:", ATTR_FLOAT32);
for (int i = 0; i < len; i++) {
printf (" %f", attr_float32[i]);
}
printf ("\n");
free (attr_float32);
H5GetFileAttribInfoByName (f, ATTR_FLOAT64, NULL, &len);
h5_float64_t* attr_float64 = malloc (sizeof(*attr_float64)*len);
H5ReadFileAttribFloat64 (f, ATTR_FLOAT64, attr_float64);
printf ("%s:", ATTR_FLOAT64);
for (int i = 0; i < len; i++) {
printf (" %f", attr_float64[i]);
}
printf ("\n");
free (attr_float64);
H5CloseFile (f);
MPI_Finalize ();
return 0;
}
!
! Copyright (c) 2006-2013, The Regents of the University of California,
! through Lawrence Berkeley National Laboratory (subject to receipt of any
! required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
! Institut (Switzerland). All rights reserved.!
!
! License: see file COPYING in top level of source distribution.
!
include 'H5hut.f90'
program read_file_attribs
use H5hut
implicit none
include 'mpif.h'
! the file name we want to read
character (len=*), parameter :: FNAME = "example_file_attribs.h5"
! verbosity level: set it to a power of 2 minus one or zero
integer*8, parameter :: verbosity_level = 1
! we know the attribute names!
character (len=*), parameter :: ATTR_STRING = "FileAttrString"
character (len=*), parameter :: ATTR_I4 = "FileAttrInt32"
character (len=*), parameter :: ATTR_I8 = "FileAttrInt64"
character (len=*), parameter :: ATTR_R4 = "FileAttrFloat32"
character (len=*), parameter :: ATTR_R8 = "FileAttrFloat64"
! for formated output
character (len=128) :: fmt
! attribute values. Note: allocatable strings aren't supported in Fortran90
character (len=256) :: string_value
integer*4, allocatable :: i4_value (:)
integer*8, allocatable :: i8_value (:)
real*4, allocatable :: r4_value (:)
real*8, allocatable :: r8_value (:)
! used for mpi error return
integer :: ierr
! H5hut file id
integer*8 :: file_id
! H5hut API status return
integer*8 status
! type of attribute
integer*8 type
! len of attribute
integer*8 len
! loop index
integer*8 i
call mpi_init (ierr)
! abort program on any H5hut error
call h5_abort_on_error ()
call h5_set_verbosity_level (verbosity_level)
! MPI_COMM_WORLD is used, if file is opened with default properties
file_id = h5_openfile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT)
! read and output string attribute
status = h5_getfileattribinfo_by_name (file_id, ATTR_STRING, type, len)
status = h5_readfileattrib_string (file_id, ATTR_STRING, string_value)
write (fmt, "(a, i0, a)") '(a', len, ')'
write (*, "(a, ' = ')", advance='no') ATTR_STRING
write (*, fmt) string_value
! read and output 32bit integer attribute
status = h5_getfileattribinfo_by_name (file_id, ATTR_I4, type, len)
allocate (i4_value(len))
status = h5_readfileattrib_i4 (file_id, ATTR_I4, i4_value)
write (fmt, "(a, i0, a)") '(', len, 'i4)'
write (*, "(a, ' =')", advance='no') ATTR_I4
write (*, fmt) (i4_value(i), i = 1, len)
! read and output 64bit integer attribute
status = h5_getfileattribinfo_by_name (file_id, ATTR_I8, type, len)
allocate (i8_value(len))
status = h5_readfileattrib_i8 (file_id, ATTR_I8, i8_value)
write (fmt, "(a, i0, a)") '(', len, 'i4)'
write (*, "(a, ' =')", advance='no') ATTR_I8
write (*, fmt) (i8_value(i), i = 1, len)
! read and output 32bit floating point attribute
status = h5_getfileattribinfo_by_name (file_id, ATTR_R4, type, len)
allocate (r4_value(len))
status = h5_readfileattrib_r4 (file_id, ATTR_R4, r4_value)