Skip to content

Commit 3b24dc9

Browse files
committed
allow whole array write from root
1 parent 83c1a06 commit 3b24dc9

File tree

7 files changed

+51
-22
lines changed

7 files changed

+51
-22
lines changed

src/tests/unit/CMakeLists.txt

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

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

8+
# --- coverage
9+
810
if(ENABLE_COVERAGE)
911
setup_target_for_coverage_gcovr_html(
1012
NAME coverage
1113
EXECUTABLE ${CMAKE_CTEST_COMMAND}
1214
)
1315
endif()
1416

17+
# --- test utils
18+
19+
add_library(test_utils OBJECT utils.f90)
20+
21+
# --- unit tests
22+
1523
set(test_names cast deflate_write deflate_props deflate_read destructor exist layout shape string)
1624

1725
foreach(t IN LISTS test_names)
1826

1927
add_executable(test_${t} test_${t}.f90)
20-
target_link_libraries(test_${t} PRIVATE h5mpi HDF5::HDF5 MPI::MPI_Fortran)
28+
target_link_libraries(test_${t} PRIVATE h5mpi test_utils HDF5::HDF5 MPI::MPI_Fortran)
2129

2230
add_test(NAME ${t}
2331
COMMAND ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} ${Nmpi} $<TARGET_FILE:test_${t}>

src/tests/unit/test_cast.f90

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ program test_cast
2222
call mpi_comm_rank(MPI_COMM_WORLD, mpi_id, ierr)
2323
if (ierr /= 0) error stop "mpi_comm_rank"
2424

25-
if(mpi_id == 0) then
26-
call test_cast_write(fn)
27-
print "(A)", "OK: cast write"
28-
endif
25+
26+
call test_cast_write(fn)
27+
if(mpi_id == 0) print "(A)", "OK: cast write"
2928

3029
call test_cast_read(fn)
3130
if(mpi_id == 0) print "(A)", "OK: cast read"
@@ -43,7 +42,7 @@ subroutine test_cast_write(fn)
4342

4443
type(hdf5_file) :: h
4544

46-
call h%open(fn, action='w', mpi=.false.)
45+
call h%open(fn, action='w', mpi=.true.)
4746

4847
!> test values
4948
call h%write('/scalar_int32', 42_int32)

src/tests/unit/test_exist.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ program exist_tests
1313
external :: mpi_finalize
1414

1515
integer :: ierr, mpi_id
16-
character(*), parameter :: fn = "test_destruct.h5"
1716

1817

1918
call mpi_init(ierr)

src/tests/unit/utils.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module test_utils
2+
3+
implicit none (type, external)
4+
5+
contains
6+
7+
subroutine unlink(filename)
8+
9+
character(*), intent(in) :: filename
10+
integer :: u
11+
12+
open(newunit=u, file=filename, status='unknown')
13+
close(u, status='delete')
14+
15+
16+
end subroutine unlink
17+
18+
19+
end module test_utils

src/write/write.f90

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
if(exists) then
3535
if (.not.present(istart)) then
3636
if (is_scalar) then
37-
!! scalar
3837
call hdf_rank_check(self, dname, size(mem_dims))
3938
else
4039
call hdf_shape_check(self, dname, mem_dims)
@@ -50,10 +49,6 @@
5049
call h5dget_space_f(dset_id, filespace, ierr)
5150
if(ierr /= 0) error stop 'ERROR:h5fortran:create could not get dataset ' // dname // ' in ' // self%filename
5251

53-
if(self%use_mpi .and. is_scalar .and. self%mpi_id > 0) call h5sselect_none_f(filespace, ierr)
54-
!! for MPI collective scalar writes, only root worker can write.
55-
!! otherwise race condition would result
56-
5752
return
5853
endif
5954

@@ -70,10 +65,10 @@
7065
if(compact .and. dcpl == H5P_DEFAULT_F .and. product(dset_dims) * 8 < 60000) then
7166
!! 64000 byte limit, here we assumed 8 bytes / element
7267
call h5pcreate_f(H5P_DATASET_CREATE_F, dcpl, ierr)
73-
if (check(ierr, self%filename)) error stop "ERROR:h5fortran:hdf_create:h5pcreate: " // dname
68+
if (ierr /= 0) error stop "ERROR:h5fortran:hdf_create:h5pcreate: " // dname
7469

7570
call h5pset_layout_f(dcpl, H5D_COMPACT_F, ierr)
76-
if (check(ierr, self%filename)) error stop "ERROR:h5fortran:hdf_create:h5pset_layout: " // dname
71+
if (ierr /= 0) error stop "ERROR:h5fortran:hdf_create:h5pset_layout: " // dname
7772
endif
7873
endif
7974

@@ -85,10 +80,6 @@
8580
endif
8681
if (ierr/=0) error stop "ERROR:h5fortran:hdf_create:h5screate:filespace " // dname // " " // self%filename
8782

88-
if(self%use_mpi .and. is_scalar .and. self%mpi_id > 0) call h5sselect_none_f(filespace, ierr)
89-
!! for MPI collective scalar writes, only root worker can write.
90-
!! otherwise race condition would result
91-
9283
!> create dataset
9384
call h5dcreate_f(self%file_id, dname, dtype, space_id=filespace, dset_id=dset_id, hdferr=ierr, dcpl_id=dcpl)
9485
if (ierr/=0) error stop "ERROR:h5fortran:hdf_create:h5dcreate: " // dname // " " // self%filename

src/write/writer.in.f90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
filespace=file_space_id, memspace=mem_space_id, dset_id=dset_id, compact=compact)
4242

4343
if(self%use_mpi) then
44+
if(self%mpi_id > 0) then
45+
call h5sselect_none_f(file_space_id, ier)
46+
if(ier /= 0) error stop "ERROR:h5fortran:write:h5sselect_none: selecting no write failed for worker. " // dname
47+
!! for MPI collective scalar writes, only root worker can write.
48+
!! otherwise race condition would result
49+
endif
50+
4451
xfer_id = mpi_collective(dname)
4552
endif
4653

src/write/writer_template.f90

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xfer_id = H5P_DEFAULT_F
66

77
mem_dims = shape(value, HSIZE_T)
8+
89
if(present(dset_dims)) then
910
select type (dset_dims)
1011
type is (integer(HSIZE_T))
@@ -14,9 +15,6 @@
1415
class default
1516
error stop "write: expecting dset_dims to be integer"
1617
end select
17-
elseif(self%use_mpi) then
18-
error stop "h5mpi:write: must specify dset_dims if using MPI"
19-
!! FIXME: there may be a way to do indepedent write rather than simply fail
2018
else
2119
dims_dset = mem_dims
2220
endif
@@ -41,7 +39,15 @@
4139
compact=compact)
4240

4341
if(self%use_mpi) then
44-
call mpi_hyperslab(mem_dims, dims_dset, dset_id, file_space_id, mem_space_id, istart=istart, iend=iend)
42+
if(present(dset_dims)) then
43+
call mpi_hyperslab(mem_dims, dims_dset, dset_id, file_space_id, mem_space_id, istart=istart, iend=iend)
44+
elseif(self%mpi_id > 0) then
45+
call h5sselect_none_f(file_space_id, ier)
46+
if(ier /= 0) error stop "ERROR:h5fortran:writer:h5sselect_none: selecting no write failed for worker. " // dname
47+
!! for MPI collective scalar or whole array writes, only root worker can write.
48+
!! otherwise race condition would result
49+
endif
50+
4551
xfer_id = mpi_collective(dname)
4652
endif
4753

0 commit comments

Comments
 (0)