Handle most writing of attributes in C++

This commit is contained in:
Paul Romano 2018-04-18 22:54:51 -05:00
parent 342d207fc4
commit ad3bb0b43a
3 changed files with 115 additions and 139 deletions

View file

@ -105,6 +105,19 @@ module hdf5_interface
integer(HID_T), value :: file_id
end subroutine file_close
subroutine get_shape_c(obj_id, dims) bind(C, name='get_shape')
import HID_T, HSIZE_T
integer(HID_T), value :: obj_id
integer(HSIZE_T), intent(out) :: dims(*)
end subroutine get_shape_c
subroutine get_shape_attr(obj_id, name, dims) bind(C)
import HID_T, HSIZE_T, C_CHAR
integer(HID_T), value :: obj_id
character(kind=C_CHAR), intent(in) :: name(*)
integer(HSIZE_T), intent(out) :: dims(*)
end subroutine get_shape_attr
subroutine read_double_c(obj_id, name, buffer, indep) &
bind(C, name='read_double')
import HID_T, C_DOUBLE, C_BOOL, C_PTR
@ -114,6 +127,22 @@ module hdf5_interface
logical(C_BOOL), intent(in) :: indep
end subroutine read_double_c
subroutine read_attr_int_c(obj_id, name, buffer) &
bind(C, name='read_attr_int')
import HID_T, C_CHAR, C_INT
integer(HID_T), value :: obj_id
character(kind=C_CHAR), intent(in) :: name(*)
integer(C_INT), intent(in) :: buffer(*)
end subroutine read_attr_int_c
subroutine read_attr_double_c(obj_id, name, buffer) &
bind(C, name='read_attr_double')
import HID_T, C_CHAR, C_DOUBLE
integer(HID_T), value :: obj_id
character(kind=C_CHAR), intent(in) :: name(*)
real(C_DOUBLE), intent(in) :: buffer(*)
end subroutine read_attr_double_c
subroutine read_int_c(obj_id, name, buffer, indep) &
bind(C, name='read_int')
import HID_T, C_INT, C_BOOL, C_PTR
@ -1178,14 +1207,10 @@ contains
integer(HID_T), intent(in) :: obj_id
character(*), intent(in) :: name
integer :: hdf5_err
integer(HID_T) :: attr_id
type(c_ptr) :: f_ptr
real(C_DOUBLE) :: buffer_(1)
call h5aopen_f(obj_id, trim(name), attr_id, hdf5_err)
f_ptr = c_loc(buffer)
call h5aread_f(attr_id, H5T_NATIVE_DOUBLE, f_ptr, hdf5_err)
call h5aclose_f(attr_id, hdf5_err)
call read_attr_double_c(obj_id, to_c_string(name), buffer_)
buffer = buffer_(1)
end subroutine read_attribute_double
subroutine write_attribute_double(obj_id, name, buffer)
@ -1205,39 +1230,16 @@ contains
integer(HID_T), intent(in) :: obj_id
character(*), intent(in) :: name
integer :: hdf5_err
integer(HID_T) :: space_id
integer(HID_T) :: attr_id
integer(HSIZE_T) :: dims(1)
integer(HSIZE_T) :: maxdims(1)
call h5aopen_f(obj_id, trim(name), attr_id, hdf5_err)
if (allocated(buffer)) then
dims(:) = shape(buffer)
else
call h5aget_space_f(attr_id, space_id, hdf5_err)
call h5sget_simple_extent_dims_f(space_id, dims, maxdims, hdf5_err)
if (.not. allocated(buffer)) then
call get_shape_attr(obj_id, to_c_string(name), dims)
allocate(buffer(dims(1)))
call h5sclose_f(space_id, hdf5_err)
end if
call read_attribute_double_1D_explicit(attr_id, dims, buffer)
call h5aclose_f(attr_id, hdf5_err)
call read_attr_double_c(obj_id, to_c_string(name), buffer)
end subroutine read_attribute_double_1D
subroutine read_attribute_double_1D_explicit(attr_id, dims, buffer)
integer(HID_T), intent(in) :: attr_id
integer(HSIZE_T), intent(in) :: dims(1)
real(8), target, intent(inout) :: buffer(dims(1))
integer :: hdf5_err
type(c_ptr) :: f_ptr
f_ptr = c_loc(buffer)
call h5aread_f(attr_id, H5T_NATIVE_DOUBLE, f_ptr, hdf5_err)
end subroutine read_attribute_double_1D_explicit
subroutine write_attribute_double_1D(obj_id, name, buffer)
integer(HID_T), intent(in) :: obj_id
character(*), intent(in) :: name
@ -1254,52 +1256,25 @@ contains
integer(HID_T), intent(in) :: obj_id
character(*), intent(in) :: name
integer :: hdf5_err
integer(HID_T) :: space_id
integer(HID_T) :: attr_id
integer(HSIZE_T) :: dims(2)
integer(HSIZE_T) :: maxdims(2)
call h5aopen_f(obj_id, trim(name), attr_id, hdf5_err)
if (allocated(buffer)) then
dims(:) = shape(buffer)
else
call h5aget_space_f(attr_id, space_id, hdf5_err)
call h5sget_simple_extent_dims_f(space_id, dims, maxdims, hdf5_err)
allocate(buffer(dims(1), dims(2)))
call h5sclose_f(space_id, hdf5_err)
if (.not. allocated(buffer)) then
call get_shape_attr(obj_id, to_c_string(name), dims)
allocate(buffer(dims(2), dims(1)))
end if
call read_attribute_double_2D_explicit(attr_id, dims, buffer)
call h5aclose_f(attr_id, hdf5_err)
call read_attr_double_c(obj_id, to_c_string(name), buffer)
end subroutine read_attribute_double_2D
subroutine read_attribute_double_2D_explicit(attr_id, dims, buffer)
integer(HID_T), intent(in) :: attr_id
integer(HSIZE_T), intent(in) :: dims(2)
real(8), target, intent(inout) :: buffer(dims(1),dims(2))
integer :: hdf5_err
type(c_ptr) :: f_ptr
f_ptr = c_loc(buffer)
call h5aread_f(attr_id, H5T_NATIVE_DOUBLE, f_ptr, hdf5_err)
end subroutine read_attribute_double_2D_explicit
subroutine read_attribute_integer(buffer, obj_id, name)
integer, intent(inout), target :: buffer
integer(HID_T), intent(in) :: obj_id
character(*), intent(in) :: name
integer :: hdf5_err
integer(HID_T) :: attr_id
type(c_ptr) :: f_ptr
integer(C_INT) :: buffer_(1)
call h5aopen_f(obj_id, trim(name), attr_id, hdf5_err)
f_ptr = c_loc(buffer)
call h5aread_f(attr_id, H5T_NATIVE_INTEGER, f_ptr, hdf5_err)
call h5aclose_f(attr_id, hdf5_err)
call read_attr_int_c(obj_id, to_c_string(name), buffer_)
buffer = buffer_(1)
end subroutine read_attribute_integer
subroutine write_attribute_integer(obj_id, name, buffer)
@ -1319,39 +1294,16 @@ contains
integer(HID_T), intent(in) :: obj_id
character(*), intent(in) :: name
integer :: hdf5_err
integer(HID_T) :: space_id
integer(HID_T) :: attr_id
integer(HSIZE_T) :: dims(1)
integer(HSIZE_T) :: maxdims(1)
call h5aopen_f(obj_id, trim(name), attr_id, hdf5_err)
if (allocated(buffer)) then
dims(:) = shape(buffer)
else
call h5aget_space_f(attr_id, space_id, hdf5_err)
call h5sget_simple_extent_dims_f(space_id, dims, maxdims, hdf5_err)
if (.not. allocated(buffer)) then
call get_shape_attr(obj_id, to_c_string(name), dims)
allocate(buffer(dims(1)))
call h5sclose_f(space_id, hdf5_err)
end if
call read_attribute_integer_1D_explicit(attr_id, dims, buffer)
call h5aclose_f(attr_id, hdf5_err)
call read_attr_int_c(obj_id, to_c_string(name), buffer)
end subroutine read_attribute_integer_1D
subroutine read_attribute_integer_1D_explicit(attr_id, dims, buffer)
integer(HID_T), intent(in) :: attr_id
integer(HSIZE_T), intent(in) :: dims(1)
integer, target, intent(inout) :: buffer(dims(1))
integer :: hdf5_err
type(c_ptr) :: f_ptr
f_ptr = c_loc(buffer)
call h5aread_f(attr_id, H5T_NATIVE_INTEGER, f_ptr, hdf5_err)
end subroutine read_attribute_integer_1D_explicit
subroutine write_attribute_integer_1D(obj_id, name, buffer)
integer(HID_T), intent(in) :: obj_id
character(*), intent(in) :: name
@ -1368,39 +1320,16 @@ contains
integer(HID_T), intent(in) :: obj_id
character(*), intent(in) :: name
integer :: hdf5_err
integer(HID_T) :: space_id
integer(HID_T) :: attr_id
integer(HSIZE_T) :: dims(2)
integer(HSIZE_T) :: maxdims(2)
call h5aopen_f(obj_id, trim(name), attr_id, hdf5_err)
if (allocated(buffer)) then
dims(:) = shape(buffer)
else
call h5aget_space_f(attr_id, space_id, hdf5_err)
call h5sget_simple_extent_dims_f(space_id, dims, maxdims, hdf5_err)
allocate(buffer(dims(1), dims(2)))
call h5sclose_f(space_id, hdf5_err)
if (.not. allocated(buffer)) then
call get_shape_attr(obj_id, to_c_string(name), dims)
allocate(buffer(dims(2), dims(1)))
end if
call read_attribute_integer_2D_explicit(attr_id, dims, buffer)
call h5aclose_f(attr_id, hdf5_err)
call read_attr_int_C(obj_id, to_c_string(name), buffer)
end subroutine read_attribute_integer_2D
subroutine read_attribute_integer_2D_explicit(attr_id, dims, buffer)
integer(HID_T), intent(in) :: attr_id
integer(HSIZE_T), intent(in) :: dims(2)
integer, target, intent(inout) :: buffer(dims(1),dims(2))
integer :: hdf5_err
type(c_ptr) :: f_ptr
f_ptr = c_loc(buffer)
call h5aread_f(attr_id, H5T_NATIVE_INTEGER, f_ptr, hdf5_err)
end subroutine read_attribute_integer_2D_explicit
subroutine read_attribute_string(buffer, obj_id, name)
character(*), intent(inout) :: buffer ! read data to here
integer(HID_T), intent(in) :: obj_id
@ -1533,21 +1462,13 @@ contains
integer(HID_T), intent(in) :: obj_id
integer(HSIZE_T), intent(out) :: dims(:)
integer :: hdf5_err
integer :: type
integer(HID_T) :: space_id
integer(HSIZE_T) :: maxdims(size(dims))
integer :: i
integer(HSIZE_T) :: dims_c(size(dims))
call h5iget_type_f(obj_id, type, hdf5_err)
if (type == H5I_DATASET_F) then
call h5dget_space_f(obj_id, space_id, hdf5_err)
call h5sget_simple_extent_dims_f(space_id, dims, maxdims, hdf5_err)
call h5sclose_f(space_id, hdf5_err)
elseif (type == H5I_ATTR_F) then
call h5aget_space_f(obj_id, space_id, hdf5_err)
call h5sget_simple_extent_dims_f(space_id, dims, maxdims, hdf5_err)
call h5sclose_f(space_id, hdf5_err)
end if
call get_shape_c(obj_id, dims_c)
do i = 1, size(dims)
dims(i) = dims_c(size(dims) - i + 1)
end do
end subroutine get_shape
subroutine get_ndims(obj_id, ndims)

View file

@ -31,6 +31,32 @@ using_mpio_device(hid_t obj_id)
}
void
get_shape(hid_t obj_id, hsize_t* dims)
{
auto type = H5Iget_type(obj_id);
hid_t dspace;
if (type == H5I_DATASET) {
dspace = H5Dget_space(obj_id);
} else if (type == H5I_ATTR) {
dspace = H5Aget_space(obj_id);
}
H5Sget_simple_extent_dims(dspace, dims, nullptr);
H5Sclose(dspace);
}
void
get_shape_attr(hid_t obj_id, const char* name, hsize_t* dims)
{
hid_t attr = H5Aopen(obj_id, name, H5P_DEFAULT);
hid_t dspace = H5Aget_space(attr);
H5Sget_simple_extent_dims(dspace, dims, nullptr);
H5Sclose(dspace);
H5Aclose(attr);
}
hid_t
create_group(hid_t parent_id, char const *name)
{
@ -159,10 +185,32 @@ open_group(hid_t group_id, const char* name)
}
}
void
read_attr(hid_t obj_id, const char* name, hid_t mem_type_id, void* buffer)
{
hid_t attr = H5Aopen(obj_id, name, H5P_DEFAULT);
H5Aread(attr, mem_type_id, buffer);
H5Aclose(attr);
}
void
read_attr_double(hid_t obj_id, const char* name, double* buffer)
{
read_attr(obj_id, name, H5T_NATIVE_DOUBLE, buffer);
}
void
read_attr_int(hid_t obj_id, const char* name, int* buffer)
{
read_attr(obj_id, name, H5T_NATIVE_INT, buffer);
}
void
read_dataset(hid_t obj_id, const char* name, hid_t mem_type_id,
void* buffer, bool indep)
void* buffer, bool indep)
{
hid_t dset = obj_id;
if (name) dset = H5Dopen(obj_id, name, H5P_DEFAULT);
@ -176,7 +224,7 @@ read_dataset(hid_t obj_id, const char* name, hid_t mem_type_id,
hid_t plist = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist, data_xfer_mode);
// Write data
// Read data
H5Dread(dset, mem_type_id, H5S_ALL, H5S_ALL, plist, buffer);
H5Pclose(plist);
#endif

View file

@ -11,7 +11,6 @@
namespace openmc {
bool using_mpio_device(hid_t obj_id);
hid_t create_group(hid_t parent_id, const char* name);
hid_t create_group(hid_t parent_id, const std::string& name);
void close_dataset(hid_t dataset_id);
@ -19,9 +18,12 @@ void close_group(hid_t group_id);
extern "C" hid_t file_open(const char* filename, char mode, bool parallel);
hid_t file_open(const std::string& filename, char mode, bool parallel);
extern "C" void file_close(hid_t file_id);
extern "C" void get_shape(hid_t obj_if, hsize_t* dims);
extern "C" void get_shape_attr(hid_t obj_if, const char* name, hsize_t* dims);
bool object_exists(hid_t object_id, const char* name);
hid_t open_dataset(hid_t group_id, const char* name);
hid_t open_group(hid_t group_id, const char* name);
bool using_mpio_device(hid_t obj_id);
template<std::size_t array_len> void
@ -41,8 +43,13 @@ write_double_1D(hid_t group_id, char const *name,
H5Dclose(dataset);
}
void read_dataset(hid_t obj_id, const char* name, double* buffer,
hid_t mem_type_id, bool indep);
void read_attr(hid_t obj_id, const char* name, hid_t mem_type_id,
const void* buffer);
extern "C" void read_attr_double(hid_t obj_id, const char* name, double* buffer);
extern "C" void read_attr_int(hid_t obj_id, const char* name, int* buffer);
void read_dataset(hid_t obj_id, const char* name, hid_t mem_type_id,
void* buffer, bool indep);
extern "C" void read_double(hid_t obj_id, const char* name, double* buffer,
bool indep);
extern "C" void read_int(hid_t obj_id, const char* name, int* buffer,