Skip to content

Commit eb4b718

Browse files
committed
enhance use of chunk_size per-variable
1 parent 356647d commit eb4b718

File tree

7 files changed

+80
-83
lines changed

7 files changed

+80
-83
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if(NOT CMAKE_BUILD_TYPE)
44
endif()
55
project(hdf5iface
66
LANGUAGES Fortran
7-
VERSION 2.3.1
7+
VERSION 2.4.0
88
HOMEPAGE_URL https://github.com/scivision/h5fortran)
99
enable_testing()
1010

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ call h5f%finalize(ierr)
187187

188188
chunk_size may optionally be set in the `%write()` method.
189189

190+
compression and chunking are disabled if:
191+
192+
* any element of chunk_size is less than 1
193+
* chunk_size is not given in the initialize() call AND not specified in %write()
194+
190195
### Read scalar, 3-D array of unknown size
191196

192197
```fortran

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
project('h5fortran', 'fortran',
22
meson_version : '>=0.52.0',
3-
version : '2.3.1',
3+
version : '2.4.0',
44
default_options : ['default_library=static', 'buildtype=release', 'warning_level=3'])
55

66
subdir('meson')

src/hdf5_interface.f90

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ module h5fortran
1313
type :: hdf5_file
1414

1515
character(:),allocatable :: filename
16-
integer(HID_T) :: lid, & !< location identifier
17-
gid, & !< group identifier
18-
glid, & !< group location identifier
19-
sid, did, pid
20-
21-
integer :: comp_lvl = 0 !< compression level (1-9) 0: disable compression
22-
integer(HSIZE_T) :: chunk_size(7) = [64,64,1,1,1,1,1] !< chunk size per dimension (arbitrary)
16+
integer(HID_T) :: lid, & !< location ID
17+
gid, & !< group ID
18+
glid, & !< group location ID
19+
sid, & !< dataspace ID
20+
did !< dataset ID
21+
22+
integer :: comp_lvl = 0 !< compression level (1-9) 0: disable compression
23+
integer(HSIZE_T) :: chunk_size(7) = [1,1,1,1,1,1,1] !< chunk size per dimension
2324
logical :: verbose=.false.
2425

2526
contains
@@ -70,10 +71,11 @@ module subroutine hdf_wrapup(self, ierr)
7071
integer, intent(out) :: ierr
7172
end subroutine hdf_wrapup
7273

73-
module subroutine hdf_set_deflate(self, dims, ierr)
74+
module subroutine hdf_set_deflate(self, dims, ierr, chunk_size)
7475
class(hdf5_file), intent(inout) :: self
7576
integer(HSIZE_T), intent(in) :: dims(:)
7677
integer, intent(out) :: ierr
78+
integer, intent(in), optional :: chunk_size(:)
7779
end subroutine hdf_set_deflate
7880

7981
module subroutine hdf_write_scalar(self,dname,value, ierr)
@@ -94,47 +96,47 @@ module subroutine hdf_write_2d(self,dname,value, ierr, chunk_size)
9496
class(hdf5_file), intent(inout) :: self
9597
character(*), intent(in) :: dname
9698
class(*), intent(in) :: value(:,:)
97-
integer, intent(in), optional :: chunk_size(:)
99+
integer, intent(in), optional :: chunk_size(rank(value))
98100
integer, intent(out) :: ierr
99101
end subroutine hdf_write_2d
100102

101103
module subroutine hdf_write_3d(self,dname,value, ierr, chunk_size)
102104
class(hdf5_file), intent(inout) :: self
103105
character(*), intent(in) :: dname
104106
class(*), intent(in) :: value(:,:,:)
105-
integer, intent(in), optional :: chunk_size(:)
107+
integer, intent(in), optional :: chunk_size(rank(value))
106108
integer, intent(out) :: ierr
107109
end subroutine hdf_write_3d
108110

109111
module subroutine hdf_write_4d(self,dname,value, ierr, chunk_size)
110112
class(hdf5_file), intent(inout) :: self
111113
character(*), intent(in) :: dname
112114
class(*), intent(in) :: value(:,:,:,:)
113-
integer, intent(in), optional :: chunk_size(:)
115+
integer, intent(in), optional :: chunk_size(rank(value))
114116
integer, intent(out) :: ierr
115117
end subroutine hdf_write_4d
116118

117119
module subroutine hdf_write_5d(self,dname,value, ierr, chunk_size)
118120
class(hdf5_file), intent(inout) :: self
119121
character(*), intent(in) :: dname
120122
class(*), intent(in) :: value(:,:,:,:,:)
121-
integer, intent(in), optional :: chunk_size(:)
123+
integer, intent(in), optional :: chunk_size(rank(value))
122124
integer, intent(out) :: ierr
123125
end subroutine hdf_write_5d
124126

125127
module subroutine hdf_write_6d(self,dname,value, ierr, chunk_size)
126128
class(hdf5_file), intent(inout) :: self
127129
character(*), intent(in) :: dname
128130
class(*), intent(in) :: value(:,:,:,:,:,:)
129-
integer, intent(in), optional :: chunk_size(:)
131+
integer, intent(in), optional :: chunk_size(rank(value))
130132
integer, intent(out) :: ierr
131133
end subroutine hdf_write_6d
132134

133135
module subroutine hdf_write_7d(self,dname,value, ierr, chunk_size)
134136
class(hdf5_file), intent(inout) :: self
135137
character(*), intent(in) :: dname
136138
class(*), intent(in) :: value(:,:,:,:,:,:,:)
137-
integer, intent(in), optional :: chunk_size(:)
139+
integer, intent(in), optional :: chunk_size(rank(value))
138140
integer, intent(out) :: ierr
139141
end subroutine hdf_write_7d
140142

@@ -232,7 +234,7 @@ end subroutine writeattr
232234
contains
233235

234236

235-
subroutine hdf_initialize(self,filename,ierr, status,action,comp_lvl)
237+
subroutine hdf_initialize(self,filename,ierr, status,action,comp_lvl,chunk_size)
236238
!! Opens hdf5 file
237239

238240
class(hdf5_file), intent(inout) :: self
@@ -241,16 +243,17 @@ subroutine hdf_initialize(self,filename,ierr, status,action,comp_lvl)
241243
character(*), intent(in), optional :: status
242244
character(*), intent(in), optional :: action
243245
integer, intent(in), optional :: comp_lvl
246+
integer, intent(in), optional :: chunk_size(:)
244247

245248
character(:), allocatable :: lstatus, laction
246249
logical :: exists
247250

248-
self%pid = 0
249251
self%sid = 0
250252
!! arbitrary sentinel values, telling us it hasn't been used by HDF5
251253
self%filename = filename
252254

253255
if (present(comp_lvl)) self%comp_lvl = comp_lvl
256+
if (present(chunk_size)) self%chunk_size(1:size(chunk_size)) = chunk_size
254257

255258
!> Initialize FORTRAN interface.
256259
call h5open_f(ierr)

src/string_utils.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ elemental function toLower(str)
1919

2020
toLower = str
2121

22-
do concurrent (i = 1:len(str))
22+
do i = 1,len(str)
2323
j = index(upper,str(i:i))
2424
if (j > 0) toLower(i:i) = lower(j:j)
2525
end do

src/write.f90

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@
5555
if(exists) then
5656
!> open dataset
5757
call h5dopen_f(self%lid, dname, self%did, ierr)
58-
if (ierr /= 0) then
59-
write(stderr,*) 'ERROR: open ' // dname // ' ' // self%filename
60-
return
61-
endif
58+
if (ierr /= 0) write(stderr,*) 'ERROR: open ' // dname // ' ' // self%filename
59+
return
6260
else
6361
call self%write(dname, ierr)
6462
if (ierr /= 0) then
@@ -67,16 +65,7 @@
6765
endif
6866
endif
6967

70-
if(size(dims) >= 2) then
71-
if (present(chunk_size)) self%chunk_size(:size(dims)) = chunk_size
72-
73-
call hdf_set_deflate(self, dims, ierr)
74-
else
75-
self%pid = 0
76-
!! sentinel value for unused property
77-
endif
78-
79-
if(exists) return
68+
if(size(dims) >= 2) call hdf_set_deflate(self, dims, ierr, chunk_size)
8069

8170
if(size(dims) == 0) then
8271
call h5screate_f(H5S_SCALAR_F, self%sid, ierr)
@@ -89,7 +78,7 @@
8978
endif
9079

9180
if(size(dims) >= 2) then
92-
call h5dcreate_f(self%lid, dname, dtype, self%sid, self%did, ierr, self%pid)
81+
call h5dcreate_f(self%lid, dname, dtype, self%sid, self%did, ierr)
9382
else
9483
call h5dcreate_f(self%lid, dname, dtype, self%sid, self%did, ierr)
9584
endif
@@ -100,42 +89,50 @@
10089

10190
module procedure hdf_set_deflate
10291

103-
integer :: ndims, i
104-
integer(HSIZE_T), allocatable :: chunk_size(:)
105-
92+
integer :: i
93+
integer(HSIZE_T) :: cs(size(dims))
94+
integer(HID_T) :: pid
10695

107-
ndims = size(dims)
108-
allocate(chunk_size(ndims))
96+
if (self%comp_lvl < 1 .or. self%comp_lvl > 9) return
97+
if (.not.present(chunk_size) .and. all(self%chunk_size == 1) .or. any(self%chunk_size < 1)) return
98+
if (present(chunk_size)) then
99+
if (any(chunk_size < 1)) return
100+
endif
109101

110-
do i=1,ndims
111-
chunk_size(i) = min(self%chunk_size(i), dims(i))
112-
enddo
102+
if (present(chunk_size)) then
103+
cs = chunk_size
104+
else
105+
do i = 1,size(dims)
106+
cs(i) = min(self%chunk_size(i), dims(i))
107+
enddo
108+
endif
113109

114-
if (self%verbose) print *,'dims: ',dims,'chunk size: ',chunk_size
110+
if (self%verbose) print *,'dims: ',dims,'chunk size: ', cs
115111

116-
call h5pcreate_f(H5P_DATASET_CREATE_F, self%pid, ierr)
112+
call h5pcreate_f(H5P_DATASET_CREATE_F, pid, ierr)
117113
if (ierr /= 0) then
118114
write(stderr,*) 'ERROR: create property ' // self%filename
119115
return
120116
endif
121117

122-
call h5pset_chunk_f(self%pid, ndims, chunk_size, ierr)
118+
call h5pset_chunk_f(pid, size(dims), cs, ierr)
123119
if (ierr /= 0) then
124120
write(stderr,*) 'ERROR: set chunk ' // self%filename
125121
return
126122
endif
127123

128124
if (self%comp_lvl < 1 .or. self%comp_lvl > 9) return
129125

130-
call h5pset_shuffle_f(self%pid, ierr)
131-
if (ierr /= 0) then
132-
write(stderr,*) 'ERROR: enable Shuffle ' // self%filename
133-
return
134-
endif
126+
!! let these errors continue to close property
127+
call h5pset_shuffle_f(pid, ierr)
128+
if (ierr /= 0) write(stderr,*) 'ERROR: enable Shuffle ' // self%filename
135129

136-
call h5pset_deflate_f(self%pid, self%comp_lvl, ierr)
130+
call h5pset_deflate_f(pid, self%comp_lvl, ierr)
137131
if (ierr /= 0) write(stderr,*) 'ERROR: enable Deflate compression ' // self%filename
138132

133+
call h5pclose_f(pid, ierr)
134+
if (ierr /= 0) write(stderr,*) 'ERROR: close property: ', pid, self%filename
135+
139136
end procedure hdf_set_deflate
140137

141138

@@ -149,14 +146,6 @@
149146
endif
150147
endif
151148

152-
if(self%pid /= 0) then
153-
call h5pclose_f(self%pid, ierr)
154-
if (ierr /= 0) then
155-
write(stderr,*) 'ERROR: close property: ', self%pid, self%filename
156-
return
157-
endif
158-
endif
159-
160149
call h5dclose_f(self%did, ierr)
161150
if (ierr /= 0) write(stderr,*) 'ERROR: close dataset: ',self%did, self%filename
162151

0 commit comments

Comments
 (0)