Skip to content

Commit e02f503

Browse files
committed
add flush method
1 parent 1f9eaee commit e02f503

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Examples.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ If `%finalize()` or hdf5_close is not called, data loss can result.
4242
call h5f%finalize()
4343
```
4444

45+
At any time during the program, the `%flush()` method can be called to request the operating system to write a file to disk.
46+
This could be useful during a long-running program (say, an HPC simulation) to help ensure data isn't lost of an HDF5 file is open for a long time.
47+
The flush request is on a per-file basis, so if multiple files are open, flush each file to protect against data loss in this case.
48+
49+
```fortran
50+
call h5f%flush()
51+
```
52+
4553
## create temporary "scratch" file
4654

4755
Analogous to regular Fortran `open(status='scratch')`, the file created will attempt to be deleted.

src/interface.f90

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ module h5fortran
44
use, intrinsic :: iso_fortran_env, only : real32, real64, int64, int32, stderr=>error_unit
55
use hdf5, only : HID_T, SIZE_T, HSIZE_T, H5F_ACC_RDONLY_F, H5F_ACC_RDWR_F, H5F_ACC_TRUNC_F, &
66
H5S_ALL_F, H5S_SELECT_SET_F, &
7-
H5T_NATIVE_DOUBLE, H5T_NATIVE_REAL, H5T_NATIVE_INTEGER, H5T_NATIVE_CHARACTER, &
7+
H5T_NATIVE_DOUBLE, H5T_NATIVE_REAL, H5T_NATIVE_INTEGER, H5T_NATIVE_CHARACTER, H5F_SCOPE_GLOBAL_F, &
88
h5open_f, h5close_f, &
99
h5dopen_f, h5dclose_f, h5dget_space_f, &
1010
h5gcreate_f, h5gclose_f, &
1111
h5fopen_f, h5fcreate_f, h5fclose_f, h5fis_hdf5_f, &
1212
h5lexists_f, &
1313
h5sclose_f, h5sselect_hyperslab_f, h5screate_simple_f, &
14-
h5get_libversion_f, h5eset_auto_f
14+
h5get_libversion_f, h5eset_auto_f, h5fflush_f
1515
use h5lt, only : h5ltget_dataset_ndims_f, h5ltget_dataset_info_f
1616

1717
use pathlib, only : unlink, get_tempdir, is_absolute_path
@@ -47,7 +47,7 @@ module h5fortran
4747
!> initialize HDF5 file
4848
procedure, public :: initialize => hdf_initialize, finalize => hdf_finalize, &
4949
write_group, writeattr, &
50-
open => hdf_open_group, close => hdf_close_group, &
50+
open => hdf_open_group, close => hdf_close_group, flush => hdf_flush, &
5151
rank => hdf_get_ndims, ndims => hdf_get_ndims, &
5252
shape => hdf_get_shape, layout => hdf_get_layout, chunks => hdf_get_chunk, &
5353
exist => hdf_check_exist, exists => hdf_check_exist, &
@@ -522,6 +522,22 @@ subroutine hdf_finalize(self, ierr, close_hdf5_interface)
522522
end subroutine hdf_finalize
523523

524524

525+
subroutine hdf_flush(self, ierr)
526+
527+
class(hdf5_file), intent(in) :: self
528+
integer, intent(out), optional :: ierr
529+
integer :: ier
530+
531+
call h5fflush_f(self%lid, H5F_SCOPE_GLOBAL_F, ier)
532+
if (present(ierr)) ierr = ier
533+
if (check(ier, 'ERROR: HDF5 flush ' // self%filename)) then
534+
if (present(ierr)) return
535+
error stop
536+
endif
537+
538+
end subroutine hdf_flush
539+
540+
525541
subroutine hdf5_close(ierr)
526542
!! this subroutine will close ALL existing file handles
527543
!! only call it at end of your program

src/tests/test_exist.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ subroutine test_multifiles()
103103
if (h%is_open) error stop 'is_open not isolated at constructor'
104104
call h%initialize(filename='C.h5', status='scratch')
105105

106+
call f%flush()
106107

107108
call f%finalize(ierr)
108109
if (ierr/=0) error stop 'close a.h5'

0 commit comments

Comments
 (0)