Skip to content

Commit 1ba3719

Browse files
committed
add lt attr read/write and nompi attr test
1 parent c0a6341 commit 1ba3719

File tree

8 files changed

+212
-30
lines changed

8 files changed

+212
-30
lines changed

src/attr_read.f90

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,27 @@ end subroutine readattr_char
180180
include 'attr_read.inc'
181181
end procedure
182182

183+
module procedure lt0readattr
184+
185+
type(hdf5_file) :: h
186+
187+
call h%open(filename, action='r')
188+
call h%readattr(obj_name, attr, A)
189+
call h%close()
190+
191+
end procedure lt0readattr
192+
193+
194+
module procedure lt1readattr
195+
196+
type(hdf5_file) :: h
197+
198+
call h%open(filename, action='r')
199+
call h%readattr(obj_name, attr, A)
200+
call h%close()
201+
202+
end procedure lt1readattr
203+
183204

184205
subroutine get_attr_class(self, obj_name, attr_name, class, attr_id, size_bytes, pad_type)
185206
!! get the attribute class (integer, float, string, ...)

src/attr_write.f90

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,26 @@
7575
end procedure
7676

7777

78+
module procedure lt0writeattr
79+
80+
type(hdf5_file) :: h
81+
82+
call h%open(filename, action='r+')
83+
call h%writeattr(obj_name, attr, A)
84+
call h%close()
85+
86+
end procedure lt0writeattr
87+
88+
89+
module procedure lt1writeattr
90+
91+
type(hdf5_file) :: h
92+
93+
call h%open(filename, action='r+')
94+
call h%writeattr(obj_name, attr, A)
95+
call h%close()
96+
97+
end procedure lt1writeattr
98+
99+
78100
end submodule attr_write

src/interface.f90

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module h5fortran
22

33
use, intrinsic :: iso_c_binding, only : c_ptr, c_loc
4-
use, intrinsic :: iso_fortran_env, only : real32, real64, int32, int64, stderr=>error_unit
4+
use, intrinsic :: iso_fortran_env, only : real32, real64, int64, int32, stderr=>error_unit
55

66
use hdf5, only : HID_T, SIZE_T, HSIZE_T, &
77
H5S_ALL_F, H5S_SELECT_SET_F, &
@@ -31,8 +31,9 @@ module h5fortran
3131
!! compression level (1-9) 0: disable compression
3232
!! compression with MPI requires MPI-3 and HDF5 >= 1.10.2
3333

34-
contains
3534

35+
contains
36+
!> define methods (procedures) that don't need generic procedure
3637
procedure, public :: open => h5open
3738
procedure, public :: close => h5close
3839
procedure, public :: write_group
@@ -85,6 +86,16 @@ module h5fortran
8586
public :: hdf5_file, is_hdf5
8687
public :: hdf_rank_check, hdf_shape_check, hdf5version, h5exist, hdf5_close
8788
public :: mpi_collective, mpi_hyperslab
89+
public :: h5write_attr, h5read_attr
90+
91+
interface h5write_attr
92+
procedure lt0writeattr, lt1writeattr
93+
end interface
94+
95+
interface h5read_attr
96+
procedure lt0readattr, lt1readattr
97+
end interface
98+
8899

89100
!! for submodules only
90101
public :: HSIZE_T, H5T_NATIVE_REAL, H5T_NATIVE_DOUBLE, H5T_NATIVE_INTEGER, H5T_NATIVE_CHARACTER, H5T_STD_I64LE
@@ -436,6 +447,30 @@ module subroutine writeattr_7d(self, obj_name, attr, A)
436447
end subroutine
437448

438449

450+
module subroutine lt0writeattr(filename, obj_name, attr, A)
451+
character(*), intent(in) :: filename
452+
character(*), intent(in) :: obj_name, attr
453+
class(*), intent(in) :: A
454+
end subroutine
455+
456+
module subroutine lt1writeattr(filename, obj_name, attr, A)
457+
character(*), intent(in) :: filename
458+
character(*), intent(in) :: obj_name, attr
459+
class(*), intent(in) :: A(:)
460+
end subroutine
461+
462+
module subroutine lt0readattr(filename, obj_name, attr, A)
463+
character(*), intent(in) :: filename
464+
character(*), intent(in) :: obj_name, attr
465+
class(*), intent(inout) :: A
466+
end subroutine
467+
468+
module subroutine lt1readattr(filename, obj_name, attr, A)
469+
character(*), intent(in) :: filename
470+
character(*), intent(in) :: obj_name, attr
471+
class(*), intent(inout) :: A(:)
472+
end subroutine
473+
439474
module subroutine attr_delete(self, obj_name, attr_name)
440475
class(hdf5_file), intent(in) :: self
441476
character(*), intent(in) :: obj_name, attr_name

src/reader.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
submodule (h5fortran:hdf5_read) hdf5_reader
22

3-
use hdf5, only: h5dread_f, h5sclose_f
3+
use hdf5, only : h5dread_f, h5sclose_f
44

55
implicit none (type, external)
66

src/write.f90

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ subroutine set_compact(dcpl, dset_dims, compact, dset_name)
211211
call h5pset_layout_f(dcpl, H5D_COMPACT_F, ierr)
212212
if (ierr /= 0) error stop "ERROR:h5fortran:hdf_create:h5pset_layout:set_compact"
213213

214-
print *, "TRACE:h5fortran:set_compact: " // dset_name
215-
216214
end subroutine set_compact
217215

218216

test/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ cmake_path(SET string_file ${CMAKE_CURRENT_BINARY_DIR}/test_string_py.h5)
5656

5757
# --- non-MPI tests
5858

59-
set(nompi_tests array)
59+
set(nompi_tests array attributes)
6060

61-
nompi_test(${nompi_tests})
61+
nompi_test("${nompi_tests}")
6262

6363

6464
# --- runner
@@ -80,7 +80,7 @@ endif()
8080

8181
# --- MPI tests
8282

83-
set(mpi_tests array_mpi attributes cast destructor exist fill groups layout shape
83+
set(mpi_tests array_mpi attributes_mpi cast destructor exist fill groups layout shape
8484
string string_read write
8585
)
8686

test/test_attributes.f90

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
program test_attributes
22

33
use, intrinsic:: iso_fortran_env, only: int32, real32, real64, stderr=>error_unit
4-
5-
use h5fortran, only: hdf5_file
6-
use mpi, only : mpi_init, MPI_COMM_WORLD, mpi_comm_rank
4+
use h5fortran, only: hdf5_file, h5write_attr, h5read_attr
75

86
implicit none (type, external)
97

10-
external :: mpi_finalize
11-
128
character(*), parameter :: filename = 'test_attr.h5'
13-
14-
integer :: ierr, mpi_id
15-
16-
call mpi_init(ierr)
17-
if (ierr /= 0) error stop "mpi_init"
18-
19-
call mpi_comm_rank(MPI_COMM_WORLD, mpi_id, ierr)
20-
if (ierr /= 0) error stop "mpi_comm_rank"
21-
9+
character(8) :: s32 !< arbitrary length
10+
integer :: i32(1)
2211

2312
call test_write_attributes(filename)
24-
if(mpi_id == 0) print *,'PASSED: HDF5 write attributes'
13+
call h5write_attr(filename, '/x', 'str29', '29')
14+
call h5write_attr(filename, '/x', 'int29', [29])
15+
print *,'PASSED: HDF5 write attributes'
2516

2617
call test_read_attributes(filename)
27-
if(mpi_id == 0) print *, 'PASSED: HDF5 read attributes'
28-
18+
call h5read_attr(filename, '/x', 'str29', s32)
19+
if (s32 /= '29') error stop 'readattr_lt string'
2920

30-
call mpi_finalize(ierr)
31-
if (ierr /= 0) error stop "mpi_finalize"
21+
call h5read_attr(filename, '/x', 'int29', i32)
22+
if (i32(1) /= 29) error stop 'readattr_lt integer'
3223

24+
print *, 'PASSED: HDF5 read attributes'
3325

3426
contains
3527

@@ -40,7 +32,7 @@ subroutine test_write_attributes(path)
4032

4133
integer :: i2(1,1), i3(1,1,1), i4(1,1,1,1), i5(1,1,1,1,1), i6(1,1,1,1,1,1), i7(1,1,1,1,1,1,1)
4234

43-
call h%open(path, action='w', mpi=.true.)
35+
call h%open(path, action='w')
4436

4537
call h%write('/x', 1)
4638

@@ -59,7 +51,7 @@ subroutine test_write_attributes(path)
5951

6052
call h%close()
6153

62-
call h%open(path, action='a', mpi=.true.)
54+
call h%open(path, action='a')
6355
call h%writeattr('/x', 'int32-scalar', 142)
6456
call h%writeattr('/x', 'real32_1d', [real(real32) :: 142, 84])
6557
call h%writeattr('/x', 'char', 'overwrite attrs')
@@ -82,7 +74,7 @@ subroutine test_read_attributes(path)
8274

8375
integer :: i2(1,1), i3(1,1,1), i4(1,1,1,1), i5(1,1,1,1,1), i6(1,1,1,1,1,1), i7(1,1,1,1,1,1,1)
8476

85-
call h%open(path, action='r', mpi=.true.)
77+
call h%open(path, action='r')
8678

8779
call h%read('/x', x)
8880
if (x/=1) error stop 'readattr: unexpected value'

test/test_attributes_mpi.f90

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
program test_attributes
2+
3+
use, intrinsic:: iso_fortran_env, only: int32, real32, real64, stderr=>error_unit
4+
5+
use h5fortran, only: hdf5_file
6+
use mpi, only : mpi_init, MPI_COMM_WORLD, mpi_comm_rank
7+
8+
implicit none (type, external)
9+
10+
external :: mpi_finalize
11+
12+
character(*), parameter :: filename = 'test_attr.h5'
13+
14+
integer :: ierr, mpi_id
15+
16+
call mpi_init(ierr)
17+
if (ierr /= 0) error stop "mpi_init"
18+
19+
call mpi_comm_rank(MPI_COMM_WORLD, mpi_id, ierr)
20+
if (ierr /= 0) error stop "mpi_comm_rank"
21+
22+
23+
call test_write_attributes(filename)
24+
if(mpi_id == 0) print *,'PASSED: HDF5 write attributes'
25+
26+
call test_read_attributes(filename)
27+
if(mpi_id == 0) print *, 'PASSED: HDF5 read attributes'
28+
29+
call mpi_finalize(ierr)
30+
if (ierr /= 0) error stop "mpi_finalize"
31+
32+
33+
contains
34+
35+
subroutine test_write_attributes(path)
36+
37+
type(hdf5_file) :: h
38+
character(*), intent(in) :: path
39+
40+
integer :: i2(1,1), i3(1,1,1), i4(1,1,1,1), i5(1,1,1,1,1), i6(1,1,1,1,1,1), i7(1,1,1,1,1,1,1)
41+
42+
call h%open(path, action='w', mpi=.true.)
43+
44+
call h%write('/x', 1)
45+
46+
call h%writeattr('/x', 'int32-scalar', 42)
47+
call h%writeattr('/x', 'char','this is just a little number')
48+
call h%writeattr('/x', 'hello', 'hi')
49+
call h%writeattr('/x', 'real32_1d', [real(real32) :: 42, 84])
50+
call h%writeattr('/x', 'real64_1d0', [42._real64])
51+
52+
call h%writeattr('/x', 'i2', i2)
53+
call h%writeattr('/x', 'i3', i3)
54+
call h%writeattr('/x', 'i4', i4)
55+
call h%writeattr('/x', 'i5', i5)
56+
call h%writeattr('/x', 'i6', i6)
57+
call h%writeattr('/x', 'i7', i7)
58+
59+
call h%close()
60+
61+
call h%open(path, action='a', mpi=.true.)
62+
call h%writeattr('/x', 'int32-scalar', 142)
63+
call h%writeattr('/x', 'real32_1d', [real(real32) :: 142, 84])
64+
call h%writeattr('/x', 'char', 'overwrite attrs')
65+
call h%delete_attr('/x', 'hello')
66+
call h%close()
67+
68+
end subroutine test_write_attributes
69+
70+
71+
subroutine test_read_attributes(path)
72+
73+
type(hdf5_file) :: h
74+
character(*), intent(in) :: path
75+
character(1024) :: attr_str
76+
integer :: int32_0
77+
real(real32) :: attr32(2)
78+
real(real64) :: attr64
79+
80+
integer :: x
81+
82+
integer :: i2(1,1), i3(1,1,1), i4(1,1,1,1), i5(1,1,1,1,1), i6(1,1,1,1,1,1), i7(1,1,1,1,1,1,1)
83+
84+
call h%open(path, action='r', mpi=.true.)
85+
86+
call h%read('/x', x)
87+
if (x/=1) error stop 'readattr: unexpected value'
88+
89+
call h%readattr('/x', 'char', attr_str)
90+
if (attr_str /= 'overwrite attrs') error stop 'overwrite attrs failed: ' // attr_str
91+
92+
call h%readattr('/x', 'int32-scalar', int32_0)
93+
if (int32_0 /= 142) error stop 'readattr: int32-scalar'
94+
95+
call h%readattr('/x', 'real32_1d', attr32)
96+
if (any(attr32 /= [real(real32) :: 142, 84])) error stop 'readattr: real32'
97+
98+
call h%readattr('/x', 'real64_1d0', attr64)
99+
if (attr64 /= 42._real64) error stop 'readattr: real64'
100+
101+
if (h%exist_attr('/x', 'hello')) error stop "delete attr failed"
102+
103+
call h%readattr('/x', 'i2', i2)
104+
call h%readattr('/x', 'i3', i3)
105+
call h%readattr('/x', 'i4', i4)
106+
call h%readattr('/x', 'i5', i5)
107+
call h%readattr('/x', 'i6', i6)
108+
call h%readattr('/x', 'i7', i7)
109+
110+
call h%close()
111+
112+
end subroutine test_read_attributes
113+
114+
end program

0 commit comments

Comments
 (0)