Skip to content

Commit f18858a

Browse files
committed
add compact write option
add scalar shape test
1 parent 1130fe9 commit f18858a

File tree

11 files changed

+318
-114
lines changed

11 files changed

+318
-114
lines changed

src/interface.f90

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,17 @@ module h5mpi
7777
hdf5version, HSIZE_T
7878

7979
interface !< write.f90
80-
module subroutine hdf_create(self, dname, dtype, mem_dims, dset_dims, filespace, memspace, dset_id, istart, iend, chunk_size)
80+
module subroutine hdf_create(self, dname, dtype, mem_dims, dset_dims, filespace, memspace, dset_id, &
81+
istart, iend, chunk_size, compact)
82+
8183
class(hdf5_file), intent(inout) :: self
8284
character(*), intent(in) :: dname
8385
integer(HID_T), intent(in) :: dtype
8486
integer(HSIZE_T), dimension(:), intent(in) :: mem_dims, dset_dims
8587
integer(HID_T), intent(out), optional :: filespace, memspace, dset_id
8688
integer(HSIZE_T), dimension(:), intent(in), optional :: istart, iend
8789
integer, dimension(:), intent(in), optional :: chunk_size
90+
logical, intent(in), optional :: compact
8891
end subroutine hdf_create
8992
end interface
9093

@@ -97,73 +100,81 @@ end subroutine get_hdf5_config
97100

98101
interface !< writer.f90
99102

100-
module subroutine h5write_scalar(self, dname, value)
103+
module subroutine h5write_scalar(self, dname, value, compact)
101104
class(hdf5_file), intent(inout) :: self
102105
character(*), intent(in) :: dname
103106
class(*), intent(in) :: value
107+
logical, intent(in), optional :: compact
104108
end subroutine h5write_scalar
105109

106-
module subroutine ph5write_1d(self, dname, value, dset_dims, istart, iend, chunk_size)
110+
module subroutine ph5write_1d(self, dname, value, dset_dims, istart, iend, chunk_size, compact)
107111
class(hdf5_file), intent(inout) :: self
108112
character(*), intent(in) :: dname
109113
class(*), intent(in) :: value(:)
110114
class(*), intent(in), dimension(1), optional :: dset_dims !< integer or integer(HSIZE_T) full disk shape (not just per worker)
111115
integer(HSIZE_T), intent(in), dimension(1), optional :: istart, iend
112116
integer, intent(in), dimension(1), optional :: chunk_size
117+
logical, intent(in), optional :: compact
113118
end subroutine ph5write_1d
114119

115-
module subroutine ph5write_2d(self, dname, value, dset_dims, istart, iend, chunk_size)
120+
module subroutine ph5write_2d(self, dname, value, dset_dims, istart, iend, chunk_size, compact)
116121
class(hdf5_file), intent(inout) :: self
117122
character(*), intent(in) :: dname
118123
class(*), intent(in) :: value(:,:)
119124
class(*), intent(in), dimension(2), optional :: dset_dims
120125
integer(HSIZE_T), intent(in), dimension(2), optional :: istart, iend
121126
integer, intent(in), dimension(2), optional :: chunk_size
127+
logical, intent(in), optional :: compact
122128
end subroutine ph5write_2d
123129

124-
module subroutine ph5write_3d(self, dname, value, dset_dims, istart, iend, chunk_size)
130+
module subroutine ph5write_3d(self, dname, value, dset_dims, istart, iend, chunk_size, compact)
125131
class(hdf5_file), intent(inout) :: self
126132
character(*), intent(in) :: dname
127133
class(*), intent(in) :: value(:,:,:)
128134
class(*), intent(in), dimension(3), optional :: dset_dims
129135
integer(HSIZE_T), intent(in), dimension(3), optional :: istart, iend
130136
integer, intent(in), dimension(3), optional :: chunk_size
137+
logical, intent(in), optional :: compact
131138
end subroutine ph5write_3d
132139

133-
module subroutine ph5write_4d(self, dname, value, dset_dims, istart, iend, chunk_size)
140+
module subroutine ph5write_4d(self, dname, value, dset_dims, istart, iend, chunk_size, compact)
134141
class(hdf5_file), intent(inout) :: self
135142
character(*), intent(in) :: dname
136143
class(*), intent(in) :: value(:,:,:,:)
137144
class(*), intent(in), dimension(4), optional :: dset_dims
138145
integer(HSIZE_T), intent(in), dimension(4), optional :: istart, iend
139146
integer, intent(in), dimension(4), optional :: chunk_size
147+
logical, intent(in), optional :: compact
140148
end subroutine ph5write_4d
141149

142-
module subroutine ph5write_5d(self, dname, value, dset_dims, istart, iend, chunk_size)
150+
module subroutine ph5write_5d(self, dname, value, dset_dims, istart, iend, chunk_size, compact)
143151
class(hdf5_file), intent(inout) :: self
144152
character(*), intent(in) :: dname
145153
class(*), intent(in) :: value(:,:,:,:,:)
146154
class(*), intent(in), dimension(5), optional :: dset_dims
147155
integer(HSIZE_T), intent(in), dimension(5), optional :: istart, iend
148156
integer, intent(in), dimension(5), optional :: chunk_size
157+
logical, intent(in), optional :: compact
149158
end subroutine ph5write_5d
150159

151-
module subroutine ph5write_6d(self, dname, value, dset_dims, istart, iend, chunk_size)
160+
module subroutine ph5write_6d(self, dname, value, dset_dims, istart, iend, chunk_size, compact)
152161
class(hdf5_file), intent(inout) :: self
153162
character(*), intent(in) :: dname
154163
class(*), intent(in) :: value(:,:,:,:,:,:)
155164
class(*), intent(in), dimension(6), optional :: dset_dims
156165
integer(HSIZE_T), intent(in), dimension(6), optional :: istart, iend
157166
integer, intent(in), dimension(6), optional :: chunk_size
167+
logical, intent(in), optional :: compact
158168
end subroutine ph5write_6d
159169

160-
module subroutine ph5write_7d(self, dname, value, dset_dims, istart, iend, chunk_size)
170+
module subroutine ph5write_7d(self, dname, value, dset_dims, istart, iend, chunk_size, compact)
161171
class(hdf5_file), intent(inout) :: self
162172
character(*), intent(in) :: dname
163173
class(*), intent(in) :: value(:,:,:,:,:,:,:)
164174
class(*), intent(in), dimension(7), optional :: dset_dims
165175
integer(HSIZE_T), intent(in), dimension(7), optional :: istart, iend
166176
integer, intent(in), dimension(7), optional :: chunk_size
177+
logical, intent(in), optional :: compact
167178
end subroutine ph5write_7d
168179

169180
end interface
@@ -587,17 +598,16 @@ function hdf5version() result(v)
587598
end function hdf5version
588599

589600

590-
subroutine hdf_flush(self, ierr)
601+
subroutine hdf_flush(self)
591602
!! request operating system flush data to disk.
592603
!! The operating system can do this when it desires, which might be a while.
593604
class(hdf5_file), intent(in) :: self
594-
integer, intent(out), optional :: ierr
595-
integer :: ier
596605

597-
call h5fflush_f(self%file_id, H5F_SCOPE_GLOBAL_F, ier)
606+
integer :: ierr
607+
608+
call h5fflush_f(self%file_id, H5F_SCOPE_GLOBAL_F, ierr)
598609

599-
if (present(ierr)) ierr = ier
600-
if (check(ier, 'ERROR: HDF5 flush ' // self%filename) .and. .not.present(ierr)) error stop
610+
if (ierr /= 0) error stop 'ERROR: HDF5 flush ' // self%filename
601611

602612
end subroutine hdf_flush
603613

src/read/reader.in.f90

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
module procedure h5read_scalar
99

10-
integer(HSIZE_T) :: dims(rank(value))
11-
integer(hid_t) :: dset_id
10+
integer(HSIZE_T) :: dims(0)
11+
integer(HID_T) :: dset_id, xfer_id
1212
integer :: dclass, ier
1313

1414
logical :: vector_scalar
@@ -44,6 +44,10 @@
4444

4545
call get_dset_class(self, dname, dclass, dset_id)
4646

47+
if(self%use_mpi) then
48+
xfer_id = mpi_collective(dname)
49+
endif
50+
4751
!> cast the dataset read from disk to the variable type presented by user h5f%read("/my_dataset", x)
4852
!> We only cast when needed to save memory.
4953
!! select case doesn't allow H5T_*

src/read/reader_template.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
call h5open_read(self, dname, dims, dset_dims, file_space_id, mem_space_id, dset_id)
1010

1111
if(self%use_mpi) then
12-
call mpi_hyperslab(dims, dset_dims, dset_id, file_space_id, mem_space_id, dname, istart=istart, iend=iend)
12+
if(present(istart) .and. present(iend)) then
13+
call mpi_hyperslab(dims, dset_dims, dset_id, file_space_id, mem_space_id, dname, istart=istart, iend=iend)
14+
endif
1315
xfer_id = mpi_collective(dname)
1416
endif
1517

src/tests/unit/CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ max_gcd(${MPI_MAX} ${MPIEXEC_MAX_NUMPROCS} Nmpi)
55

66
message(STATUS "Unit tests using ${Nmpi} processes")
77

8-
set(test_names deflate_write deflate_props deflate_read scalar shape)
8+
set(test_names deflate_write deflate_props deflate_read layout shape)
99

1010
foreach(t IN LISTS test_names)
1111

@@ -18,11 +18,28 @@ foreach(t IN LISTS test_names)
1818

1919
endforeach()
2020

21+
# --- write test data
22+
add_executable(test_write test_write.f90)
23+
target_link_libraries(test_write PRIVATE h5mpi HDF5::HDF5 MPI::MPI_Fortran)
24+
25+
add_test(NAME write COMMAND test_write)
26+
set_tests_properties(write PROPERTIES
27+
FIXTURES_SETUP test_files
28+
TIMEOUT 10
29+
)
30+
31+
# --- test dependencies
32+
2133
set_tests_properties(${test_names} PROPERTIES
2234
TIMEOUT 10
2335
PROCESSORS ${Nmpi}
2436
)
2537

38+
set_tests_properties(layout shape PROPERTIES
39+
FIXTURES_REQUIRED test_files
40+
REQUIRED_FILES ${CMAKE_CURRENT_BINARY_DIR}/test_write.h5
41+
)
42+
2643
set_tests_properties(deflate_write PROPERTIES
2744
FIXTURES_SETUP deflate_files
2845
)

src/tests/unit/test_layout.f90

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
program test_layout
2+
3+
use, intrinsic :: iso_fortran_env, only : real32, real64, int32
4+
5+
use h5mpi, only : hdf5_file
6+
7+
use hdf5, only : H5D_COMPACT_F
8+
use mpi, only : mpi_init, MPI_COMM_WORLD, mpi_comm_rank, mpi_barrier
9+
10+
implicit none (type, external)
11+
12+
external :: mpi_finalize
13+
14+
character(*), parameter :: fn = 'test_layout.h5'
15+
integer :: ierr, mpi_id
16+
17+
call mpi_init(ierr)
18+
if (ierr /= 0) error stop "mpi_init"
19+
20+
call mpi_comm_rank(MPI_COMM_WORLD, mpi_id, ierr)
21+
if (ierr /= 0) error stop "mpi_comm_rank"
22+
23+
call test_layout_read(fn)
24+
if(mpi_id == 0) print *, "OK: read layout"
25+
26+
call test_layout_props(fn)
27+
if(mpi_id == 0) print *, "OK: layout props"
28+
29+
call mpi_finalize(ierr)
30+
31+
contains
32+
33+
34+
subroutine test_layout_read(fn)
35+
36+
character(*), intent(in) :: fn
37+
38+
type(hdf5_file) :: h
39+
40+
real(real32) :: r32
41+
real(real64) :: r64
42+
integer(int32) :: i32
43+
44+
real(real32), dimension(1,1,1,1,1,1,1) :: r7_32
45+
real(real64), dimension(1,1,1,1,1,1,1) :: r7_64
46+
47+
!> read casting
48+
call h%open(fn, action="r", mpi=.true.)
49+
50+
call h%read("/compact_r32", r64)
51+
if(r64 /= 142) error stop "read real32 => real64"
52+
call h%read("/compact_r64", r32)
53+
if(r32 /= 142) error stop "read real64 => real32"
54+
call h%read("/compact_i32", i32)
55+
if(r32 /= 142) error stop "read int32 => int32"
56+
57+
58+
call h%read("/compact7d_32", r7_64)
59+
if (any(r7_64 /= 42)) error stop "read real32 => real64"
60+
61+
call h%read("/compact7d_64", r7_32)
62+
if (any(r7_32 /= 42)) error stop "read real64 => real32"
63+
64+
call h%close()
65+
66+
end subroutine test_layout_read
67+
68+
69+
subroutine test_layout_props(fn)
70+
71+
character(*), intent(in) :: fn
72+
73+
type(hdf5_file) :: h
74+
75+
call h%open(fn, action="r", mpi=.true.)
76+
77+
if (h%layout("/compact1d") /= H5D_COMPACT_F) error stop "expected compact"
78+
if (.not. h%is_compact("/compact1d")) error stop "1d is_compact fail"
79+
80+
if (.not. h%is_compact("/compact7d_32")) error stop "7d is_compact fail"
81+
if (.not. h%is_compact("/compact0d")) error stop "0d is_compact fail"
82+
83+
call h%close()
84+
85+
end subroutine test_layout_props
86+
87+
end program

src/tests/unit/test_scalar.f90

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)