Skip to content

Commit a4828a4

Browse files
committed
put HDF5-MPI procedures in own mpi.F90
1 parent 5a44621 commit a4828a4

File tree

12 files changed

+110
-60
lines changed

12 files changed

+110
-60
lines changed

.github/workflows/ci_fpm.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ jobs:
3333
- uses: actions/checkout@v2
3434

3535
- run: fpm build
36+
env:
37+
FPM_FFLAGS: -Dh5fortran_HAVE_PARALLEL
38+
3639
- run: fpm test

API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ call h%open(filename, action, mpi, comp_lvl)
3232
3333
character(*), intent(in) :: filename
3434
character(*), intent(in), optional :: action !< 'r', 'r+', 'w', 'rw' (default 'r')
35-
logical, intent(in) :: mpi !< .true.: use HDF5-MPI .false.: use serial HDF5
35+
logical, intent(in) :: mpi !< .true.: use HDF5-MPI .false.: non-MPI HDF5 (default)
3636
integer, intent(in), optional :: comp_lvl !< 0: no compression. 1-9: ZLIB compression, higher is more compressior
3737
```
3838

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ endif()
5151
# --- HDF5-MPI object oriented API
5252

5353
add_library(h5fortran)
54+
target_compile_definitions(h5fortran PRIVATE -Dh5fortran_HAVE_PARALLEL)
5455
target_include_directories(h5fortran PUBLIC
5556
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
5657
$<INSTALL_INTERFACE:include>

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ cmake -B build -DHDF5_ROOT=~/lib_par
6262
cmake --build build
6363
```
6464

65+
---
66+
67+
Fortran Package Manager (FPM) users build like:
68+
69+
```sh
70+
fpm build --flag -Dh5fortran_HAVE_PARALLEL
71+
72+
fpm test
73+
```
74+
6575
## Notes
6676

6777
To build and install the HDF5 parallel library use the script:

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ read.f90 read_scalar.f90 read_scalar_ascii.f90 reader.f90
1111
write.f90 write_scalar.f90 writer.f90
1212
interface.f90
1313
attr.f90 attr_read.f90 attr_write.f90
14+
mpi.F90
1415
)

src/interface.f90

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,6 @@ module subroutine mpi_hyperslab(mem_dims, dset_dims, dset_id, filespace, memspac
554554
integer, dimension(size(istart)), intent(in), optional :: stride
555555
end subroutine
556556

557-
module integer(HID_T) function mpi_collective(dname) result(xfer_id)
558-
character(*), intent(in) :: dname !< just for error messages
559-
end function
560-
561557
module subroutine hdf_rank_check(self, dname, mrank, vector_scalar)
562558
class(hdf5_file), intent(in) :: self
563559
character(*), intent(in) :: dname
@@ -579,4 +575,20 @@ module integer(HSIZE_T) function hdf_filesize(self)
579575

580576
end interface
581577

578+
579+
interface !< mpi.f90
580+
581+
module integer(HID_T) function mpi_collective(dname, use_mpi) result(xfer_id)
582+
character(*), intent(in) :: dname !< just for error messages
583+
logical, intent(in) :: use_mpi
584+
end function
585+
586+
module integer(HID_T) function mpi_opener(filename, use_mpi, mpi_id) result(fapl)
587+
character(*), intent(in) :: filename !< just for error messages
588+
logical, intent(in) :: use_mpi
589+
integer, intent(in) :: mpi_id
590+
end function
591+
592+
end interface
593+
582594
end module h5fortran

src/mpi.F90

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
!! procedures that require HDF5-MPI
2+
3+
submodule (h5fortran) mpi_smod
4+
5+
#ifdef h5fortran_HAVE_PARALLEL
6+
use hdf5, only : h5pcreate_f, h5pset_dxpl_mpio_f, h5pset_fapl_mpio_f, &
7+
H5FD_MPIO_COLLECTIVE_F, &
8+
H5P_DATASET_XFER_F
9+
#endif
10+
11+
use hdf5, only : H5P_FILE_ACCESS_F
12+
13+
implicit none (type, external)
14+
15+
contains
16+
17+
18+
module procedure mpi_collective
19+
20+
integer :: ier
21+
22+
xfer_id = H5P_DEFAULT_F
23+
24+
if (.not. use_mpi) return
25+
26+
#ifdef h5fortran_HAVE_PARALLEL
27+
28+
!! Create property list for collective dataset operations
29+
call H5Pcreate_f(H5P_DATASET_XFER_F, xfer_id, ier)
30+
if (ier/=0) error stop "ERROR:h5fortran:h5pcreate dataset xfer: " // dname
31+
32+
call H5Pset_dxpl_mpio_f(xfer_id, H5FD_MPIO_COLLECTIVE_F, ier)
33+
if (ier/=0) error stop "ERROR:h5fortran:h5pset_dxpl_mpio collective: " // dname
34+
35+
! For independent dataset operations
36+
! call H5Pset_dxpl_mpio_f(xfer_id, H5FD_MPIO_INDEPENDENT_F, ier)
37+
38+
#endif
39+
40+
end procedure mpi_collective
41+
42+
43+
module procedure mpi_opener
44+
45+
integer :: ier
46+
47+
fapl = H5P_DEFAULT_F
48+
49+
if (.not. use_mpi) return
50+
51+
#ifdef h5fortran_HAVE_PARALLEL
52+
53+
call mpi_comm_rank(mpi_h5comm, mpi_id, ier)
54+
if(ier /= 0) error stop "ERROR:h5fortran:opener: could not get MPI ID"
55+
56+
!! collective: setup for MPI access
57+
call H5Pcreate_f(H5P_FILE_ACCESS_F, fapl, ier)
58+
if(ier /= 0) error stop "ERROR:h5fortran:opener:h5pcreate could not collective open property for " // filename
59+
60+
call H5Pset_fapl_mpio_f(fapl, mpi_h5comm, mpi_h5info, ier)
61+
if(ier /= 0) error stop "ERROR:h5fortran:opener:h5pset_fapl_mpio could not collective open file for " // filename
62+
63+
#endif
64+
65+
end procedure mpi_opener
66+
67+
end submodule mpi_smod

src/read_scalar.f90

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ module subroutine read_scalar_char(A, dset_id, file_space_id, mem_space_id, dims
3737

3838
call get_dset_class(self, dname, dclass, dset_id)
3939

40-
if(self%use_mpi) then
41-
xfer_id = mpi_collective(dname)
42-
endif
40+
xfer_id = mpi_collective(dname, self%use_mpi)
4341

4442
!> cast the dataset read from disk to the variable type presented by user h5f%read("/my_dataset", x)
4543
!> We only cast when needed to save memory.

src/reader.inc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ else
1616
call hdf_shape_check(self, dname, dims, dset_dims)
1717
endif
1818

19-
if(self%use_mpi) then
20-
xfer_id = mpi_collective(dname)
21-
else
22-
xfer_id = H5P_DEFAULT_F
23-
endif
19+
xfer_id = mpi_collective(dname, self%use_mpi)
2420

2521
call get_dset_class(self, dname, dclass, dset_id)
2622

src/utils.f90

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
h5sselect_hyperslab_f, h5screate_simple_f, &
1010
h5dopen_f, h5dclose_f, h5dget_space_f, &
1111
h5dget_create_plist_f, &
12-
h5pcreate_f, h5pset_fapl_mpio_f, h5pall_filters_avail_f, h5pset_dxpl_mpio_f, h5pclose_f, &
12+
h5pcreate_f, h5pall_filters_avail_f, h5pclose_f, &
1313
H5F_ACC_RDONLY_F, H5F_ACC_RDWR_F, H5F_ACC_TRUNC_F, &
1414
H5F_OBJ_FILE_F, H5F_OBJ_GROUP_F, H5F_OBJ_DATASET_F, H5F_OBJ_DATATYPE_F, H5F_OBJ_ALL_F, &
15-
H5D_CONTIGUOUS_F, H5D_CHUNKED_F, H5D_COMPACT_F, &
16-
H5FD_MPIO_COLLECTIVE_F, &
17-
H5P_DATASET_XFER_F, H5P_FILE_ACCESS_F
15+
H5D_CONTIGUOUS_F, H5D_CHUNKED_F, H5D_COMPACT_F
1816

1917
use h5lt, only : h5ltget_dataset_ndims_f, h5ltget_dataset_info_f
2018

@@ -56,11 +54,6 @@
5654

5755
self%use_mpi = mpi
5856

59-
if(self%use_mpi) then
60-
call mpi_comm_rank(mpi_h5comm, self%mpi_id, ier)
61-
if(ier /= 0) error stop "ERROR:h5fortran:open: could not get MPI ID"
62-
endif
63-
6457
if(present(debug)) self%debug = debug
6558

6659
call get_hdf5_config(self%parallel_compression)
@@ -102,16 +95,6 @@
10295
!! OK to call repeatedly
10396
!! https://support.hdfgroup.org/HDF5/doc/RM/RM_H5.html#Library-Open
10497

105-
if(self%use_mpi) then
106-
!! collective: setup for MPI access
107-
call H5Pcreate_f(H5P_FILE_ACCESS_F, fapl, ier)
108-
if(ier /= 0) error stop "ERROR:h5fortran:open:h5pcreate could not collective open property for " // filename
109-
call H5Pset_fapl_mpio_f(fapl, mpi_h5comm, mpi_h5info, ier)
110-
if(ier /= 0) error stop "ERROR:h5fortran:open:h5pset_fapl_mpio could not collective open file for " // filename
111-
else
112-
fapl = H5P_DEFAULT_F
113-
endif
114-
11598
select case(laction)
11699
case('r')
117100
file_mode = H5F_ACC_RDONLY_F
@@ -129,6 +112,8 @@
129112
error stop 'ERROR:h5fortran:open Unsupported action ' // laction // ' for ' // filename
130113
end select
131114

115+
fapl = mpi_opener(filename, self%use_mpi, self%mpi_id)
116+
132117
if (file_mode == H5F_ACC_RDONLY_F .or. file_mode == H5F_ACC_RDWR_F) then
133118
if(.not. is_hdf5(filename)) error stop "ERROR:h5fortran:open: not an HDF5 file: "//filename
134119
call H5Fopen_f(filename, file_mode, self%file_id, ier, access_prp=fapl)
@@ -370,23 +355,6 @@
370355
end procedure mpi_hyperslab
371356

372357

373-
module procedure mpi_collective
374-
375-
integer :: ierr
376-
377-
!! Create property list for collective dataset operations
378-
call h5pcreate_f(H5P_DATASET_XFER_F, xfer_id, ierr)
379-
if (ierr/=0) error stop "ERROR:h5fortran:h5pcreate dataset xfer: " // dname
380-
381-
call h5pset_dxpl_mpio_f(xfer_id, H5FD_MPIO_COLLECTIVE_F, ierr)
382-
if (ierr/=0) error stop "ERROR:h5fortran:h5pset_dxpl_mpio collective: " // dname
383-
384-
! For independent dataset operations
385-
! call h5pset_dxpl_mpio_f(xfer_id, H5FD_MPIO_INDEPENDENT_F, ierr)
386-
387-
end procedure mpi_collective
388-
389-
390358
module procedure hdf_rank_check
391359

392360
integer(HSIZE_T) :: ddims(1)

0 commit comments

Comments
 (0)