Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
hdf5_file.F90
Go to the documentation of this file.
1! Copyright (c) 2024-2026, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32!
33!> HDF5 file format.
35 use num_types, only : rp, dp, sp, i8
37 use checkpoint, only : chkp_t
41 use mesh, only : mesh_t
42 use field, only : field_t, field_ptr_t
43 use field_list, only : field_list_t
45 use dofmap, only : dofmap_t
46 use space, only : neko_space_t => space_t, gll
49 use vector, only : vector_t
50 use host_array, only : host_array_t
51 use matrix, only : matrix_t
52 use datadist, only : linear_dist_t
54 use math, only : rzero
56 use mpi_f08, only : mpi_info_null, mpi_allreduce, mpi_allgather, &
57 mpi_in_place, mpi_integer, mpi_sum, mpi_max, mpi_comm_size, mpi_exscan, &
58 mpi_barrier, mpi_integer8, mpi_scan, mpi_bcast, mpi_logical, &
59 mpi_double_precision
61#ifdef HAVE_HDF5
62 use hdf5
63#endif
64 implicit none
65 private
66
67 !> Reusable distribution of a rank-ordered host array over NEKO_COMM.
68 !! Counts are vector elements or matrix columns, not total matrix elements.
69 !! Matrices distribute their second dimension; all ranks have the same
70 !! number of rows and write every row of their local columns.
71 !! Initialise collectively once, then reuse while every rank's local size
72 !! and NEKO_COMM remain unchanged. Reinitialise after redistribution.
73 type, public :: hdf5_layout_t
74 private
75 !> Number of entries on this rank; -1 marks an uninitialised layout.
76 integer :: local_count = -1
77 !> First entry owned by this rank, using zero-based indexing.
78 integer :: offset = 0
79 !> Sum and maximum of local counts over NEKO_COMM.
80 integer :: total_count = 0
81 integer :: max_count = 0
82 contains
83 !> Calculate the distribution collectively.
84 procedure :: init => hdf5_layout_init
85 end type hdf5_layout_t
86
87#ifdef HAVE_HDF5
88 !> Dataset resources retained while the active group remains open.
90 !> Dataset name relative to the active group.
91 character(len=:), allocatable :: name
92 !> Open dataset and memory dataspace handles.
93 integer(hid_t) :: id = -1_hid_t, memspace = -1_hid_t
94 !> Array rank (number of dimensions), not the MPI rank.
95 integer :: dset_rank = 0
96 !> Current extent (writers support up to four array dimensions).
97 integer(hsize_t) :: extent(4) = 0_hsize_t
98 !> Local shape used to construct the reusable memory dataspace.
99 integer(hsize_t) :: memory_shape(4) = 0_hsize_t
100 contains
101 !> Release retained handles and the dataset name.
102 procedure :: free => hdf5_dataset_free
103 end type hdf5_dataset_t
104#endif
105
106 !> Interface for HDF5 files
107 type, public, extends(generic_file_t) :: hdf5_file_t
108
109 ! HDF5 members
110#ifdef HAVE_HDF5
111 integer(hid_t) :: file_id = -1_hid_t
112 integer(hid_t) :: active_group_id = -1_hid_t
113 integer(hid_t) :: plist_id = -1_hid_t
114 !> Collective transfer properties shared by writes to this open file.
115 integer(hid_t), private :: xfer_id = -1_hid_t
116 !> Datasets belonging to the active group, released before changing it.
117 type(hdf5_dataset_t), allocatable, private :: datasets(:)
118#endif
119 character(len=1) :: mode
120 integer :: precision = -1
121 integer :: offset = 0
122 integer :: count = 0
123
124 contains
125 ! General methods for reading/writing HDF5 files
126 !> Read data in HDF5 format.
127 procedure :: read => hdf5_file_read
128 !> Write data in HDF5 format.
129 procedure :: write => hdf5_file_write
130 procedure :: get_next_output_fname => hdf5_file_get_next_output_fname
131 procedure :: set_overwrite => hdf5_file_set_overwrite
132 ! Granular methods for dealing with HDF5 files
133 procedure :: open => hdf5_file_open
134 !> Open a fresh numbered file, skipping names already present on disk.
135 procedure :: open_new => hdf5_file_open_new
136 procedure :: close => hdf5_file_close
137 !> Release an open file and its retained dataset resources.
138 procedure :: free => hdf5_file_free
139#ifdef HAVE_HDF5
140 !> Release the datasets of the active group.
141 procedure, private :: clear_datasets => hdf5_file_clear_datasets
142 !> Open or reuse a dataset and prepare the next write selection.
143 procedure, private :: prepare_dataset => hdf5_file_prepare_dataset
144#endif
145 procedure :: set_active_group => hdf5_file_set_group
146 procedure :: set_precision => hdf5_file_set_precision
147 !> Write a one-dimensional host array, optionally reusing its layout.
148 procedure :: write_array => hdf5_file_write_array
149 procedure, pass(this) :: write_vector => hdf5_file_write_vector
150 procedure, pass(this) :: write_matrix => hdf5_file_write_matrix
151 procedure, pass(this) :: write_field => hdf5_file_write_field
152 procedure, pass(this) :: write_int_attribute => &
154 procedure, pass(this) :: write_rp_attribute => hdf5_file_write_rp_attribute
155 procedure, pass(this) :: read_vector => hdf5_file_read_vector
156 procedure, pass(this) :: read_matrix => hdf5_file_read_matrix
157 procedure, pass(this) :: read_int_attribute => hdf5_file_read_int_attribute
158 procedure, pass(this) :: read_rp_attribute => hdf5_file_read_rp_attribute
159 procedure :: write_dataset => hdf5_file_write_dataset
160 procedure :: read_dataset => hdf5_file_read_dataset
161 procedure :: write_attribute => hdf5_file_write_attribute
162 procedure :: read_attribute => hdf5_file_read_attribute
163 end type hdf5_file_t
164
165 !> Where the data in a checkpoint file sits and how it maps onto the
166 !! running case. Built once per read and shared by every dataset in the
167 !! file, so a mesh-to-mesh point search happens once rather than per field.
169 !> Mesh whose element ordering and offsets the file is laid out in:
170 !! the running case's mesh unless `case.restart_mesh_file` named the
171 !! mesh the checkpoint was written on.
172 type(mesh_t), pointer :: msh => null()
173 !> Function space the file was written in.
174 type(neko_space_t) :: xh
175 !> Whether `msh` is a different mesh from the running case's.
176 logical :: mesh2mesh = .false.
177 !> Dofmap of the file's discretisation, needed for the point search.
178 type(dofmap_t) :: src_dof
179 !> Evaluates data given on `msh` at the running case's nodes.
180 type(global_interpolation_t) :: global_interp
181 contains
182 !> Release the interpolation state and the function space.
183 procedure, pass(this) :: free => hdf5_checkpoint_layout_free
185
186contains
187
188 !> Release an open file and its retained dataset resources.
189 subroutine hdf5_file_free(this)
190 class(hdf5_file_t), intent(inout) :: this
191#ifdef HAVE_HDF5
192 if (this%file_id /= -1_hid_t) call this%close()
193#endif
194 end subroutine hdf5_file_free
195
196 !> Open a new numbered file without modifying any existing file.
197 !! Uses the inherited filename and counter routines. Rank zero checks
198 !! each candidate and shares the result before any collective file open.
199 subroutine hdf5_file_open_new(this)
200 class(hdf5_file_t), intent(inout) :: this
201 character(len=1024) :: fname
202 logical :: file_exists
203 integer :: ierr
204
205 call this%free()
206 call this%increment_counter()
207 do
208 fname = this%get_fname()
209 file_exists = .false.
210 if (pe_rank == 0) inquire(file = trim(fname), exist = file_exists)
211 call mpi_bcast(file_exists, 1, mpi_logical, 0, neko_comm, ierr)
212 if (.not. file_exists) exit
213 call this%increment_counter()
214 end do
215 call this%open("w")
216 end subroutine hdf5_file_open_new
217
218 !> Calculate a reusable layout for distributed array writes.
219 !! @param local_count Scalar integer: local number of vector elements or
220 !! matrix columns. For a matrix use mat%get_ncols(), not mat%size().
221 !! Pass only this rank's count, not an array of counts or dimensions.
222 !! @note All ranks in NEKO_COMM must call this routine. A supplied layout
223 !! removes size collectives from subsequent writes; callers must initialise
224 !! it again on all ranks if any local count or the communicator changes.
225 subroutine hdf5_layout_init(this, local_count)
226 class(hdf5_layout_t), intent(inout) :: this
227 integer, intent(in) :: local_count
228 integer :: ierr
229
230 this%local_count = local_count
231 call mpi_scan(local_count, this%offset, 1, mpi_integer, &
232 mpi_sum, neko_comm, ierr)
233 this%offset = this%offset - local_count
234 call mpi_allreduce(local_count, this%total_count, 1, mpi_integer, &
235 mpi_sum, neko_comm, ierr)
236 call mpi_allreduce(local_count, this%max_count, 1, mpi_integer, &
237 mpi_max, neko_comm, ierr)
238 end subroutine hdf5_layout_init
239
240 !> Get the physical file name generated by the next write.
241 function hdf5_file_get_next_output_fname(this) result(fname)
242 class(hdf5_file_t), intent(in) :: this
243 character(len=1024) :: fname
244 character(len=5) :: id_str
245 integer :: counter, suffix_pos
246
247 if (this%overwrite) then
248 fname = this%get_fname()
249 else
250 counter = this%get_counter()
251 if (counter .eq. -1) then
252 counter = this%get_start_counter()
253 else
254 counter = counter + 1
255 end if
256
257 fname = this%get_base_fname()
258 suffix_pos = filename_suffix_pos(fname)
259 write(id_str, '(i5.5)') counter
260 fname = trim(fname(1:suffix_pos-1)) // id_str // fname(suffix_pos:)
261 end if
262
264
265 !> Set the overwrite flag for HDF5 files
266 subroutine hdf5_file_set_overwrite(this, overwrite)
267 class(hdf5_file_t), intent(inout) :: this
268 logical, intent(in) :: overwrite
269 this%overwrite = overwrite
270 end subroutine hdf5_file_set_overwrite
271
272 !> Set the precision for the output (single or double)
273 subroutine hdf5_file_set_precision(this, precision)
274 class(hdf5_file_t), intent(inout) :: this
275 integer, intent(in) :: precision
276 this%precision = precision
277 end subroutine hdf5_file_set_precision
278
279 !> Release the interpolation state and the function space of a layout.
280 !! The mesh is only referenced; it belongs to the case or the checkpoint.
282 class(hdf5_checkpoint_layout_t), intent(inout) :: this
283
284 if (this%mesh2mesh) then
285 call this%global_interp%free()
286 call this%src_dof%free()
287 end if
288 call this%Xh%free()
289 nullify(this%msh)
290 this%mesh2mesh = .false.
291
292 end subroutine hdf5_checkpoint_layout_free
293
294
295#ifdef HAVE_HDF5
296
297 ! ===============
298 ! General methods
299 ! ===============
300
301 !> Write data in HDF5 format.
302 !! @param data Data object to write.
303 !! @param[optional] t Simulation time.
304 subroutine hdf5_file_write(this, data, t)
305 class(hdf5_file_t), intent(inout) :: this
306 class(*), target, intent(in) :: data
307 real(kind=dp), intent(in), optional :: t
308 type(mesh_t), pointer :: msh
309 type(dofmap_t), pointer :: dof
310 type(field_ptr_t), allocatable :: fp(:)
311 type(field_series_ptr_t), allocatable :: fsp(:)
312 real(kind=dp), pointer :: dtlag(:)
313 real(kind=dp), pointer :: tlag(:)
314 integer :: ierr, info, drank, i, j
315 integer(hid_t) :: plist_id, fapl_id
316 integer(hid_t) :: file_id, dset_id, grp_id, attr_id
317 integer(hid_t) :: filespace, dspace_id, memspace
318 integer(hid_t) :: H5T_NEKO_REAL
319 integer(hsize_t), dimension(1) :: ddim, dcount, doffset
320 integer :: suffix_pos
321 character(len=5) :: id_str
322 character(len=1024) :: fname
323 logical :: checkpoint_data
324
325 call hdf5_file_determine_data(data, msh, dof, fp, fsp, dtlag, tlag)
326 checkpoint_data = .false.
327 select type (data)
328 type is (chkp_t)
329 checkpoint_data = .true.
330 end select
331
332 if (.not. this%overwrite) call this%increment_counter()
333 fname = trim(this%get_fname())
334
335 call hdf5_session_init()
336
337 call hdf5_file_determine_real(h5t_neko_real)
338
339 ! The file-access and dataset-transfer property lists are distinct
340 ! objects; reusing one identifier for both would leak the first, and
341 ! HDF5 then refuses to close the library at the end of the session.
342 call h5pcreate_f(h5p_file_access_f, fapl_id, ierr)
343 info = mpi_info_null%mpi_val
344 call h5pset_fapl_mpio_f(fapl_id, neko_comm%mpi_val, info, ierr)
345
346 call h5fcreate_f(fname, h5f_acc_trunc_f, &
347 file_id, ierr, access_prp = fapl_id)
348 call h5pclose_f(fapl_id, ierr)
349
350 call h5pcreate_f(h5p_dataset_xfer_f, plist_id, ierr)
351 call h5pset_dxpl_mpio_f(plist_id, h5fd_mpio_collective_f, ierr)
352
353 call h5screate_f(h5s_scalar_f, filespace, ierr)
354 ddim = 1
355
356 if (present(t)) then
357 call h5acreate_f(file_id, "Time", h5t_native_double, filespace, attr_id, &
358 ierr, h5p_default_f, h5p_default_f)
359 call h5awrite_f(attr_id, h5t_native_double, t, ddim, ierr)
360 call h5aclose_f(attr_id, ierr)
361 end if
362
363 if (associated(dof)) then
364 call h5acreate_f(file_id, "Lx", h5t_native_integer, filespace, attr_id, &
365 ierr, h5p_default_f, h5p_default_f)
366 call h5awrite_f(attr_id, h5t_native_integer, dof%Xh%lx, ddim, ierr)
367 call h5aclose_f(attr_id, ierr)
368 end if
369
370 if (associated(msh)) then
371 call h5gcreate_f(file_id, "Mesh", grp_id, ierr, &
372 lcpl_id = h5p_default_f, gcpl_id = h5p_default_f, &
373 gapl_id = h5p_default_f)
374
375 call h5acreate_f(grp_id, "Elements", h5t_native_integer, filespace, &
376 attr_id, ierr, h5p_default_f, h5p_default_f)
377 call h5awrite_f(attr_id, h5t_native_integer, msh%glb_nelv, ddim, ierr)
378 call h5aclose_f(attr_id, ierr)
379
380 call h5acreate_f(grp_id, "Dimension", h5t_native_integer, filespace, &
381 attr_id, ierr, h5p_default_f, h5p_default_f)
382 call h5awrite_f(attr_id, h5t_native_integer, msh%gdim, ddim, ierr)
383 call h5aclose_f(attr_id, ierr)
384
385 call h5gclose_f(grp_id, ierr)
386 end if
387
388
389 call h5sclose_f(filespace, ierr)
390
391 !
392 ! Write restart group (tlag, dtlag)
393 !
394 if (associated(tlag) .and. associated(dtlag)) then
395 call h5gcreate_f(file_id, "Restart", grp_id, ierr, &
396 lcpl_id = h5p_default_f, gcpl_id = h5p_default_f, &
397 gapl_id = h5p_default_f)
398
399 drank = 1
400 ddim = size(tlag)
401 doffset(1) = 0
402 if (pe_rank .eq. 0) then
403 dcount = size(tlag)
404 else
405 dcount = 0
406 end if
407
408 call h5screate_simple_f(drank, ddim, filespace, ierr)
409
410 call h5dcreate_f(grp_id, 'tlag', h5t_native_double, &
411 filespace, dset_id, ierr)
412 call h5dget_space_f(dset_id, dspace_id, ierr)
413 call h5sselect_hyperslab_f (dspace_id, h5s_select_set_f, &
414 doffset, dcount, ierr)
415 call h5dwrite_f(dset_id, h5t_native_double, tlag, &
416 ddim, ierr, xfer_prp = plist_id)
417 call h5sclose_f(dspace_id, ierr)
418 call h5dclose_f(dset_id, ierr)
419
420 call h5dcreate_f(grp_id, 'dtlag', h5t_native_double, &
421 filespace, dset_id, ierr)
422 call h5dget_space_f(dset_id, dspace_id, ierr)
423 call h5sselect_hyperslab_f (dspace_id, h5s_select_set_f, &
424 doffset, dcount, ierr)
425 call h5dwrite_f(dset_id, h5t_native_double, dtlag, &
426 ddim, ierr, xfer_prp = plist_id)
427 call h5sclose_f(dspace_id, ierr)
428 call h5dclose_f(dset_id, ierr)
429
430 call h5sclose_f(filespace, ierr)
431 call h5gclose_f(grp_id, ierr)
432
433 end if
434
435
436 !
437 ! Write fields group
438 !
439 if (checkpoint_data) then
440 select type (data)
441 type is (chkp_t)
442 call hdf5_checkpoint_write_payloads( &
443 file_id, plist_id, h5t_neko_real, data)
444 end select
445 if (allocated(fp)) deallocate(fp)
446 if (allocated(fsp)) deallocate(fsp)
447 else if (allocated(fp) .or. allocated(fsp)) then
448 call h5gcreate_f(file_id, "Fields", grp_id, ierr, &
449 lcpl_id = h5p_default_f, gcpl_id = h5p_default_f, &
450 gapl_id = h5p_default_f)
451
452 ! `lxyz` rather than `lx**3`: they agree in 3D, but a 2D case has
453 ! `lz = 1`, and the reader derives its offsets the same way.
454 dcount(1) = int(dof%size(), 8)
455 doffset(1) = int(msh%offset_el, 8) * int(dof%Xh%lxyz, 8)
456 ddim = int(dof%size(), 8)
457 drank = 1
458 call mpi_allreduce(mpi_in_place, ddim(1), 1, &
459 mpi_integer8, mpi_sum, neko_comm, ierr)
460
461 call h5screate_simple_f(drank, ddim, filespace, ierr)
462 call h5screate_simple_f(drank, dcount, memspace, ierr)
463
464
465 if (allocated(fp)) then
466 do i = 1, size(fp)
467 call h5dcreate_f(grp_id, fp(i)%ptr%name, h5t_neko_real, &
468 filespace, dset_id, ierr)
469 call h5dget_space_f(dset_id, dspace_id, ierr)
470 call h5sselect_hyperslab_f(dspace_id, h5s_select_set_f, &
471 doffset, dcount, ierr)
472 call h5dwrite_f(dset_id, h5t_neko_real, &
473 fp(i)%ptr%x(1,1,1,1), &
474 ddim, ierr, file_space_id = dspace_id, &
475 mem_space_id = memspace, xfer_prp = plist_id)
476 call h5sclose_f(dspace_id, ierr)
477 call h5dclose_f(dset_id, ierr)
478 end do
479 deallocate(fp)
480 end if
481
482 if (allocated(fsp)) then
483 do i = 1, size(fsp)
484 do j = 1, fsp(i)%ptr%size()
485 call h5dcreate_f(grp_id, fsp(i)%ptr%lf(j)%name, &
486 h5t_neko_real, filespace, dset_id, ierr)
487 call h5dget_space_f(dset_id, dspace_id, ierr)
488 call h5sselect_hyperslab_f(dspace_id, h5s_select_set_f, &
489 doffset, dcount, ierr)
490 call h5dwrite_f(dset_id, h5t_neko_real, &
491 fsp(i)%ptr%lf(j)%x(1,1,1,1), &
492 ddim, ierr, file_space_id = dspace_id, &
493 mem_space_id = memspace, xfer_prp = plist_id)
494 call h5sclose_f(dspace_id, ierr)
495 call h5dclose_f(dset_id, ierr)
496 end do
497 end do
498 deallocate(fsp)
499 end if
500
501 call h5gclose_f(grp_id, ierr)
502 call h5sclose_f(filespace, ierr)
503 call h5sclose_f(memspace, ierr)
504 end if
505
506 call h5pclose_f(plist_id, ierr)
507 call h5fclose_f(file_id, ierr)
509
510 end subroutine hdf5_file_write
511
512 !> Read data in HDF5 format.
513 !! @param data Data object to populate.
514 subroutine hdf5_file_read(this, data)
515 class(hdf5_file_t) :: this
516 class(*), target, intent(inout) :: data
517 integer(hid_t) :: plist_id, fapl_id
518 integer(hid_t) :: file_id, dset_id, grp_id, attr_id
519 integer(hid_t) :: filespace, memspace
520 integer(hid_t) :: H5T_NEKO_REAL
521 integer(hsize_t), dimension(1) :: ddim, dcount, doffset
522 integer :: i,j, ierr, info, glb_nelv, gdim, lx, drank
523 type(mesh_t), pointer :: msh
524 type(dofmap_t), pointer :: dof
525 type(field_ptr_t), allocatable :: fp(:)
526 type(field_series_ptr_t), allocatable :: fsp(:)
527 type(hdf5_checkpoint_layout_t), target :: layout
528 real(kind=dp), pointer :: dtlag(:)
529 real(kind=dp), pointer :: tlag(:)
530 real(kind=dp) :: t
531 character(len=1024) :: fname
532 ! Not LOG_SIZE: these messages are longer than 79 characters, and an
533 ! overflowing internal write is a runtime error, not a truncation.
534 character(len=256) :: err_msg
535 logical :: payloads_exist
536
537 fname = trim(this%get_fname())
538
539 call hdf5_session_init()
540
541 call hdf5_file_determine_data(data, msh, dof, fp, fsp, dtlag, tlag)
542 call hdf5_file_determine_real(h5t_neko_real)
543
544 ! As in the write path, keep the file-access and dataset-transfer
545 ! property lists in separate identifiers so neither is leaked.
546 call h5pcreate_f(h5p_file_access_f, fapl_id, ierr)
547 info = mpi_info_null%mpi_val
548 call h5pset_fapl_mpio_f(fapl_id, neko_comm%mpi_val, info, ierr)
549
550 call h5fopen_f(fname, h5f_acc_rdonly_f, &
551 file_id, ierr, access_prp = fapl_id)
552 call h5pclose_f(fapl_id, ierr)
553
554 call h5lexists_f(file_id, 'Payloads', payloads_exist, ierr)
555
556 call h5pcreate_f(h5p_dataset_xfer_f, plist_id, ierr)
557 call h5pset_dxpl_mpio_f(plist_id, h5fd_mpio_collective_f, ierr)
558
559 ddim = 1
560 call h5aopen_name_f(file_id, 'Time', attr_id, ierr)
561 call h5aread_f(attr_id, h5t_native_double, t, ddim, ierr)
562 call h5aclose_f(attr_id, ierr)
563
564 select type (data)
565 type is (chkp_t)
566 data%t = t
567 end select
568
569 call h5aopen_name_f(file_id, 'Lx', attr_id, ierr)
570 call h5aread_f(attr_id, h5t_native_integer, lx, ddim, ierr)
571 call h5aclose_f(attr_id, ierr)
572
573 call h5gopen_f(file_id, 'Mesh', grp_id, ierr, gapl_id = h5p_default_f)
574
575 call h5aopen_name_f(grp_id, 'Elements', attr_id, ierr)
576 call h5aread_f(attr_id, h5t_native_integer, glb_nelv, ddim, ierr)
577 call h5aclose_f(attr_id, ierr)
578
579 call h5aopen_name_f(grp_id, 'Dimension', attr_id, ierr)
580 call h5aread_f(attr_id, h5t_native_integer, gdim, ddim, ierr)
581 call h5aclose_f(attr_id, ierr)
582 call h5gclose_f(grp_id, ierr)
583
584 ! A mesh supplied through `case.restart_mesh_file` is the one the
585 ! checkpoint was written on, and the file's layout follows it.
586 layout%msh => msh
587 layout%mesh2mesh = .false.
588 select type (data)
589 type is (chkp_t)
590 if (allocated(data%previous_mesh%elements)) then
591 layout%msh => data%previous_mesh
592 layout%mesh2mesh = .true.
593 end if
594 end select
595
596 if (gdim .ne. layout%msh%gdim) then
597 write(err_msg, '(A,I0,A,I0,A)') 'HDF5 checkpoint is for a ', gdim, &
598 'D mesh but this case is ', layout%msh%gdim, 'D'
599 call neko_error(trim(err_msg))
600 end if
601
602 if (glb_nelv .ne. layout%msh%glb_nelv) then
603 write(err_msg, '(A,I0,A,I0,A)') 'HDF5 checkpoint has ', glb_nelv, &
604 ' elements but the mesh it is read on has ', &
605 layout%msh%glb_nelv, '; set case.restart_mesh_file to the ' // &
606 'mesh the checkpoint was written on'
607 call neko_error(trim(err_msg))
608 end if
609
610 if (gdim .eq. 3) then
611 call layout%Xh%init(gll, lx, lx, lx)
612 else
613 call layout%Xh%init(gll, lx, lx)
614 end if
615
616 select type (data)
617 type is (chkp_t)
618 if (gdim .eq. 3) then
619 call data%previous_Xh%init(gll, lx, lx, lx)
620 else
621 call data%previous_Xh%init(gll, lx, lx)
622 end if
623
624 if (layout%mesh2mesh) then
625 call layout%src_dof%init(layout%msh, layout%Xh)
626 call layout%global_interp%init(layout%src_dof, neko_comm, &
627 tol = data%mesh2mesh_tol)
628 call layout%global_interp%find_points(dof%x%x, dof%y%x, dof%z%x, &
629 dof%size())
630 end if
631 end select
632
633 if (associated(tlag) .and. associated(dtlag)) then
634 drank = 1
635 ddim = size(tlag)
636 doffset(1) = 0
637 if (pe_rank .eq. 0) then
638 dcount = size(tlag)
639 else
640 dcount = 0
641 end if
642
643 call h5gopen_f(file_id, 'Restart', grp_id, ierr, &
644 gapl_id = h5p_default_f)
645 call h5dopen_f(grp_id, 'tlag', dset_id, ierr)
646 call h5dget_space_f(dset_id, filespace, ierr)
647 call h5sselect_hyperslab_f (filespace, h5s_select_set_f, &
648 doffset, dcount, ierr)
649 call h5dread_f(dset_id, h5t_native_double, tlag, ddim, ierr, &
650 xfer_prp = plist_id)
651 call h5dclose_f(dset_id, ierr)
652 call h5sclose_f(filespace, ierr)
653
654 call h5dopen_f(grp_id, 'dtlag', dset_id, ierr)
655 call h5dget_space_f(dset_id, filespace, ierr)
656 call h5sselect_hyperslab_f (filespace, h5s_select_set_f, &
657 doffset, dcount, ierr)
658 call h5dread_f(dset_id, h5t_native_double, dtlag, ddim, ierr, &
659 xfer_prp = plist_id)
660 call h5dclose_f(dset_id, ierr)
661 call h5sclose_f(filespace, ierr)
662
663 call h5gclose_f(grp_id, ierr)
664 end if
665
666 if (payloads_exist) then
667 select type (data)
668 type is (chkp_t)
669 call hdf5_checkpoint_read_payloads( &
670 file_id, plist_id, h5t_neko_real, data, layout)
671 class default
672 call neko_error( &
673 "HDF5 payload checkpoints require a checkpoint object")
674 end select
675 if (allocated(fp)) deallocate(fp)
676 if (allocated(fsp)) deallocate(fsp)
677 else if (allocated(fp) .or. allocated(fsp)) then
678 call h5gopen_f(file_id, 'Fields', grp_id, ierr, gapl_id = h5p_default_f)
679
680 if (allocated(fp)) then
681 do i = 1, size(fp)
682 call hdf5_checkpoint_read_field(grp_id, plist_id, &
683 h5t_neko_real, fp(i)%ptr, layout)
684 end do
685 end if
686
687 if (allocated(fsp)) then
688 do i = 1, size(fsp)
689 do j = 1, fsp(i)%ptr%size()
690 call hdf5_checkpoint_read_field(grp_id, plist_id, &
691 h5t_neko_real, fsp(i)%ptr%lf(j), layout)
692 end do
693 end do
694 end if
695 call h5gclose_f(grp_id, ierr)
696 end if
697
698 call h5pclose_f(plist_id, ierr)
699 call h5fclose_f(file_id, ierr)
700
701 call layout%free()
702
704
705 end subroutine hdf5_file_read
706
707 !> Write the format-independent checkpoint payload hierarchy.
708 !! @param file_id Open HDF5 checkpoint file.
709 !! @param plist_id Collective dataset transfer property list.
710 !! @param h5_neko_real HDF5 datatype corresponding to `rp`.
711 !! @param chkp Registered checkpoint payloads to write.
712 subroutine hdf5_checkpoint_write_payloads(file_id, plist_id, &
713 h5_neko_real, chkp)
714 integer(hid_t), intent(in) :: file_id, plist_id, h5_neko_real
715 type(chkp_t), intent(in) :: chkp
716 integer(hid_t) :: payloads_id, payload_id
717 integer :: i, j, k, ierr
718
719 call h5gcreate_f(file_id, "Payloads", payloads_id, ierr)
720
721 do i = 1, chkp%payload_count()
722 call hdf5_checkpoint_open_group(payloads_id, &
723 chkp%payloads(i)%ptr%name, .true., payload_id)
724
725 do j = 1, chkp%payloads(i)%ptr%field_count()
726 call hdf5_checkpoint_write_field(payload_id, plist_id, &
727 h5_neko_real, chkp%payloads(i)%ptr%fields(j)%ptr)
728 end do
729
730 do j = 1, chkp%payloads(i)%ptr%series_count()
731 do k = 1, chkp%payloads(i)%ptr%series(j)%ptr%size()
732 call hdf5_checkpoint_write_field(payload_id, plist_id, &
733 h5_neko_real, &
734 chkp%payloads(i)%ptr%series(j)%ptr%lf(k))
735 end do
736 end do
737
738 do j = 1, chkp%payloads(i)%ptr%mesh_array_count()
739 call hdf5_checkpoint_write_mesh_array(payload_id, plist_id, &
740 h5_neko_real, chkp%payloads(i)%ptr%mesh_arrays(j)%ptr)
741 end do
742
743 do j = 1, chkp%payloads(i)%ptr%array_count()
744 call hdf5_checkpoint_write_array(payload_id, plist_id, &
745 h5_neko_real, chkp%payloads(i)%ptr%arrays(j)%ptr)
746 end do
747
748 call h5gclose_f(payload_id, ierr)
749 end do
750
751 call h5gclose_f(payloads_id, ierr)
752
753 end subroutine hdf5_checkpoint_write_payloads
754
755 !> Read the format-independent checkpoint payload hierarchy.
756 !! @param file_id Open HDF5 checkpoint file.
757 !! @param plist_id Collective dataset transfer property list.
758 !! @param h5_neko_real HDF5 datatype corresponding to `rp`.
759 !! @param chkp Registered checkpoint payloads to populate.
760 !! @param layout Layout of the file and its mapping onto the running case.
761 subroutine hdf5_checkpoint_read_payloads(file_id, plist_id, &
762 h5_neko_real, chkp, layout)
763 integer(hid_t), intent(in) :: file_id, plist_id, h5_neko_real
764 type(chkp_t), intent(inout) :: chkp
765 type(hdf5_checkpoint_layout_t), target, intent(inout) :: layout
766 integer(hid_t) :: payloads_id, payload_id
767 integer :: i, j, k, ierr
768
769 call h5gopen_f(file_id, "Payloads", payloads_id, ierr)
770
771 do i = 1, chkp%payload_count()
772 call hdf5_checkpoint_open_group(payloads_id, &
773 chkp%payloads(i)%ptr%name, .false., payload_id)
774
775 do j = 1, chkp%payloads(i)%ptr%field_count()
776 call hdf5_checkpoint_read_field(payload_id, plist_id, &
777 h5_neko_real, chkp%payloads(i)%ptr%fields(j)%ptr, &
778 layout)
779 end do
780
781 do j = 1, chkp%payloads(i)%ptr%series_count()
782 do k = 1, chkp%payloads(i)%ptr%series(j)%ptr%size()
783 call hdf5_checkpoint_read_field(payload_id, plist_id, &
784 h5_neko_real, &
785 chkp%payloads(i)%ptr%series(j)%ptr%lf(k), layout)
786 end do
787 end do
788
789 do j = 1, chkp%payloads(i)%ptr%mesh_array_count()
790 call hdf5_checkpoint_read_mesh_array(payload_id, plist_id, &
791 h5_neko_real, chkp%payloads(i)%ptr%mesh_arrays(j)%ptr, &
792 layout)
793 end do
794
795 do j = 1, chkp%payloads(i)%ptr%array_count()
796 call hdf5_checkpoint_read_array(payload_id, plist_id, &
797 h5_neko_real, chkp%payloads(i)%ptr%arrays(j)%ptr)
798 end do
799
800 call h5gclose_f(payload_id, ierr)
801 end do
802
803 call h5gclose_f(payloads_id, ierr)
804
805 end subroutine hdf5_checkpoint_read_payloads
806
807 !> Open a nested group path, optionally creating missing groups.
808 !! @param root_id HDF5 group from which the path is resolved.
809 !! @param path Slash-separated relative group path.
810 !! @param create Whether missing groups should be created.
811 !! @param group_id Open HDF5 group at `path`.
812 subroutine hdf5_checkpoint_open_group(root_id, path, create, group_id)
813 integer(hid_t), intent(in) :: root_id
814 character(len=*), intent(in) :: path
815 logical, intent(in) :: create
816 integer(hid_t), intent(out) :: group_id
817 integer(hid_t) :: current_id, next_id
818 integer :: first, last, slash, path_len, ierr
819 logical :: group_exists
820 character(len=:), allocatable :: group_name
821
822 current_id = root_id
823 first = 1
824 path_len = len_trim(path)
825
826 do
827 slash = index(path(first:path_len), "/")
828 if (slash .eq. 0) then
829 last = path_len
830 else
831 last = first + slash - 2
832 end if
833
834 group_name = path(first:last)
835 call h5lexists_f(current_id, group_name, group_exists, ierr)
836 if (group_exists) then
837 call h5gopen_f(current_id, group_name, next_id, ierr)
838 else if (create) then
839 call h5gcreate_f(current_id, group_name, next_id, ierr)
840 else
841 call neko_error("Checkpoint payload group '" // trim(path) // &
842 "' is missing")
843 end if
844
845 if (current_id .ne. root_id) call h5gclose_f(current_id, ierr)
846 current_id = next_id
847
848 if (slash .eq. 0) exit
849 first = last + 2
850 end do
851
852 group_id = current_id
853
854 end subroutine hdf5_checkpoint_open_group
855
856 !> Write one checkpoint field under its native field name.
857 !! @param group_id Open HDF5 payload group.
858 !! @param plist_id Collective dataset transfer property list.
859 !! @param h5_neko_real HDF5 datatype corresponding to `rp`.
860 !! @param fld Field to write.
861 subroutine hdf5_checkpoint_write_field(group_id, plist_id, &
862 h5_neko_real, fld)
863 integer(hid_t), intent(in) :: group_id, plist_id, h5_neko_real
864 type(field_t), intent(in) :: fld
865 integer(hid_t) :: dset_id, filespace, memspace
866 integer(hsize_t), dimension(1) :: ddim, dcount, doffset
867 integer :: ierr
868
869 dcount(1) = int(fld%dof%size(), 8)
870 doffset(1) = int(fld%msh%offset_el, 8) * int(fld%Xh%lxyz, 8)
871 ddim = dcount
872 call mpi_allreduce(mpi_in_place, ddim(1), 1, &
873 mpi_integer8, mpi_sum, neko_comm, ierr)
874
875 call h5screate_simple_f(1, ddim, filespace, ierr)
876 call h5screate_simple_f(1, dcount, memspace, ierr)
877 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
878 doffset, dcount, ierr)
879 call h5dcreate_f(group_id, trim(fld%name), h5_neko_real, &
880 filespace, dset_id, ierr)
881 call h5dwrite_f(dset_id, h5_neko_real, fld%x(1,1,1,1), &
882 dcount, ierr, file_space_id = filespace, mem_space_id = memspace, &
883 xfer_prp = plist_id)
884
885 call h5dclose_f(dset_id, ierr)
886 call h5sclose_f(filespace, ierr)
887 call h5sclose_f(memspace, ierr)
888
889 end subroutine hdf5_checkpoint_write_field
890
891 !> Read one checkpoint field by its native field name.
892 !! @param group_id Open HDF5 payload group.
893 !! @param plist_id Collective dataset transfer property list.
894 !! @param h5_neko_real HDF5 datatype corresponding to `rp`.
895 !! @param fld Field to populate.
896 !! @param layout Layout of the file and its mapping onto the running case.
897 subroutine hdf5_checkpoint_read_field(group_id, plist_id, &
898 h5_neko_real, fld, layout)
899 integer(hid_t), intent(in) :: group_id, plist_id, h5_neko_real
900 type(field_t), intent(inout) :: fld
901 type(hdf5_checkpoint_layout_t), target, intent(inout) :: layout
902 integer(hid_t) :: dset_id, filespace, memspace
903 integer(hsize_t), dimension(1) :: dcount, doffset
904 real(kind=rp), allocatable :: checkpoint_data(:)
905 type(interpolator_t) :: space_interp
906 integer :: ierr
907 logical :: dataset_exists
908
909 call h5lexists_f(group_id, trim(fld%name), dataset_exists, ierr)
910 if (.not. dataset_exists) then
911 call neko_error("Checkpoint field '" // trim(fld%name) // &
912 "' is missing")
913 end if
914
915 call h5dopen_f(group_id, trim(fld%name), dset_id, ierr)
916 call h5dget_space_f(dset_id, filespace, ierr)
917
918 ! The hyperslab follows the file's own layout, which coincides with
919 ! the running case's only when neither the mesh nor the order changed.
920 dcount(1) = int(layout%msh%nelv, hsize_t) * int(layout%Xh%lxyz, hsize_t)
921 doffset(1) = int(layout%msh%offset_el, hsize_t) * &
922 int(layout%Xh%lxyz, hsize_t)
923 call h5screate_simple_f(1, dcount, memspace, ierr)
924 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
925 doffset, dcount, ierr)
926
927 if (layout%mesh2mesh) then
928 allocate(checkpoint_data(int(dcount(1))))
929 call h5dread_f(dset_id, h5_neko_real, checkpoint_data, dcount, ierr, &
930 file_space_id = filespace, mem_space_id = memspace, &
931 xfer_prp = plist_id)
932 call rzero(fld%x, fld%dof%size())
933 call layout%global_interp%evaluate(fld%x, checkpoint_data, .true.)
934 deallocate(checkpoint_data)
935 else if (layout%Xh%lxyz .ne. fld%Xh%lxyz) then
936 allocate(checkpoint_data(int(dcount(1))))
937 call h5dread_f(dset_id, h5_neko_real, checkpoint_data, dcount, ierr, &
938 file_space_id = filespace, mem_space_id = memspace, &
939 xfer_prp = plist_id)
940 call space_interp%init(fld%Xh, layout%Xh)
941 call space_interp%map_host(fld%x, checkpoint_data, fld%msh%nelv, &
942 fld%Xh)
943 call space_interp%free()
944 deallocate(checkpoint_data)
945 else
946 call h5dread_f(dset_id, h5_neko_real, fld%x(1,1,1,1), &
947 dcount, ierr, file_space_id = filespace, &
948 mem_space_id = memspace, xfer_prp = plist_id)
949 end if
950
951 call h5dclose_f(dset_id, ierr)
952 call h5sclose_f(filespace, ierr)
953 call h5sclose_f(memspace, ierr)
954
955 end subroutine hdf5_checkpoint_read_field
956
957 !> Write one nodal mesh array using its mesh distribution.
958 !! @param group_id Open HDF5 payload group.
959 !! @param plist_id Collective dataset transfer property list.
960 !! @param h5_neko_real HDF5 datatype corresponding to `rp`.
961 !! @param array Mesh-array descriptor to write.
962 subroutine hdf5_checkpoint_write_mesh_array(group_id, plist_id, &
963 h5_neko_real, array)
964 integer(hid_t), intent(in) :: group_id, plist_id, h5_neko_real
965 type(checkpoint_mesh_array_t), intent(in) :: array
966 integer(hid_t) :: dset_id, filespace, memspace, attr_id, attr_space
967 integer(hsize_t), dimension(1) :: ddim, dcount, doffset, attr_dims
968 integer :: nodal_shape(3)
969 integer :: ierr
970
971 dcount(1) = int(size(array%x), hsize_t)
972 doffset(1) = int(array%msh%offset_el, hsize_t) * &
973 int(array%Xh%lxyz, hsize_t)
974 ddim(1) = int(array%msh%glb_nelv, hsize_t) * &
975 int(array%Xh%lxyz, hsize_t)
976
977 call h5screate_simple_f(1, ddim, filespace, ierr)
978 call h5screate_simple_f(1, dcount, memspace, ierr)
979 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
980 doffset, dcount, ierr)
981 call h5dcreate_f(group_id, trim(array%name), h5_neko_real, &
982 filespace, dset_id, ierr)
983
984 nodal_shape = [array%Xh%lx, array%Xh%ly, array%Xh%lz]
985 attr_dims(1) = size(nodal_shape)
986 call h5screate_simple_f(1, attr_dims, attr_space, ierr)
987 call h5acreate_f(dset_id, "NodalShape", h5t_native_integer, &
988 attr_space, attr_id, ierr)
989 call h5awrite_f(attr_id, h5t_native_integer, nodal_shape, &
990 attr_dims, ierr)
991 call h5aclose_f(attr_id, ierr)
992 call h5sclose_f(attr_space, ierr)
993
994 call h5dwrite_f(dset_id, h5_neko_real, array%x, dcount, ierr, &
995 file_space_id = filespace, mem_space_id = memspace, &
996 xfer_prp = plist_id)
997
998 call h5dclose_f(dset_id, ierr)
999 call h5sclose_f(filespace, ierr)
1000 call h5sclose_f(memspace, ierr)
1001
1002 end subroutine hdf5_checkpoint_write_mesh_array
1003
1004 !> Read and optionally interpolate one nodal mesh array.
1005 !! @param group_id Open HDF5 payload group.
1006 !! @param plist_id Collective dataset transfer property list.
1007 !! @param h5_neko_real HDF5 datatype corresponding to `rp`.
1008 !! @param array Mesh-array descriptor to populate.
1009 !! @param layout Layout of the file and its mapping onto the running case.
1010 subroutine hdf5_checkpoint_read_mesh_array(group_id, plist_id, &
1011 h5_neko_real, array, layout)
1012 integer(hid_t), intent(in) :: group_id, plist_id, h5_neko_real
1013 type(checkpoint_mesh_array_t), intent(inout) :: array
1014 type(hdf5_checkpoint_layout_t), target, intent(inout) :: layout
1015 integer(hid_t) :: dset_id, filespace, memspace, attr_id, attr_space
1016 integer(hsize_t), dimension(1) :: dcount, doffset
1017 integer(hsize_t), dimension(1) :: dataset_dims, dataset_maxdims
1018 integer(hsize_t), dimension(1) :: attr_dims, attr_maxdims
1019 integer :: stored_shape(3)
1020 real(kind=rp), allocatable :: stored_data(:)
1021 type(neko_space_t), target :: stored_xh
1022 type(interpolator_t) :: space_interp
1023 integer :: ierr
1024 logical :: dataset_exists, shape_exists
1025
1026 ! Mesh arrays hold ALE state (mesh coordinates, lagged mass matrices),
1027 ! which has no meaning on a different mesh. Same check and message as
1028 ! chkp_file.
1029 if (layout%mesh2mesh) then
1030 call neko_error('ALE does not yet support mesh2mesh ' // &
1031 'interpolation for restart!')
1032 end if
1033
1034 call h5lexists_f(group_id, trim(array%name), dataset_exists, ierr)
1035 if (.not. dataset_exists) then
1036 call neko_error("Checkpoint mesh array '" // trim(array%name) // &
1037 "' is missing")
1038 end if
1039
1040 call h5dopen_f(group_id, trim(array%name), dset_id, ierr)
1041 call h5dget_space_f(dset_id, filespace, ierr)
1042 call h5sget_simple_extent_dims_f(filespace, dataset_dims, &
1043 dataset_maxdims, ierr)
1044
1045 stored_shape = [layout%Xh%lx, layout%Xh%ly, layout%Xh%lz]
1046 call h5aexists_f(dset_id, "NodalShape", shape_exists, ierr)
1047 if (shape_exists) then
1048 call h5aopen_f(dset_id, "NodalShape", attr_id, ierr)
1049 else
1050 ! Compatibility with development checkpoints using generic blocks.
1051 call h5aexists_f(dset_id, "BlockShape", shape_exists, ierr)
1052 if (shape_exists) then
1053 call h5aopen_f(dset_id, "BlockShape", attr_id, ierr)
1054 end if
1055 end if
1056 if (shape_exists) then
1057 call h5aget_space_f(attr_id, attr_space, ierr)
1058 call h5sget_simple_extent_dims_f(attr_space, attr_dims, &
1059 attr_maxdims, ierr)
1060 if (attr_dims(1) .ne. 3) then
1061 call neko_error("Checkpoint mesh-array nodal shape must have " // &
1062 "three dimensions")
1063 end if
1064 call h5aread_f(attr_id, h5t_native_integer, stored_shape, &
1065 attr_dims, ierr)
1066 call h5sclose_f(attr_space, ierr)
1067 call h5aclose_f(attr_id, ierr)
1068 end if
1069
1070 if (any(stored_shape .le. 0)) then
1071 call neko_error("Checkpoint mesh-array nodal shape must be positive")
1072 end if
1073 if (array%msh%gdim .eq. 3) then
1074 call stored_xh%init(gll, stored_shape(1), stored_shape(2), &
1075 stored_shape(3))
1076 else
1077 if (stored_shape(3) .ne. 1) then
1078 call neko_error("Two-dimensional checkpoint mesh arrays must " // &
1079 "have one nodal plane")
1080 end if
1081 call stored_xh%init(gll, stored_shape(1), stored_shape(2))
1082 end if
1083
1084 if (int(dataset_dims(1), i8) .ne. &
1085 int(layout%msh%glb_nelv, i8) * int(stored_xh%lxyz, i8)) then
1086 call neko_error("Checkpoint mesh array '" // trim(array%name) // &
1087 "' does not match the current mesh")
1088 end if
1089
1090 dcount(1) = int(layout%msh%nelv, hsize_t) * &
1091 int(stored_xh%lxyz, hsize_t)
1092 doffset(1) = int(layout%msh%offset_el, hsize_t) * &
1093 int(stored_xh%lxyz, hsize_t)
1094 call h5screate_simple_f(1, dcount, memspace, ierr)
1095 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1096 doffset, dcount, ierr)
1097
1098 if (stored_xh%lxyz .eq. array%Xh%lxyz) then
1099 call h5dread_f(dset_id, h5_neko_real, array%x, dcount, ierr, &
1100 file_space_id = filespace, mem_space_id = memspace, &
1101 xfer_prp = plist_id)
1102 else
1103 allocate(stored_data(int(dcount(1))))
1104 call h5dread_f(dset_id, h5_neko_real, stored_data, dcount, ierr, &
1105 file_space_id = filespace, mem_space_id = memspace, &
1106 xfer_prp = plist_id)
1107 call space_interp%init(array%Xh, stored_xh)
1108 call space_interp%map_host(array%x, stored_data, array%msh%nelv, &
1109 array%Xh)
1110 call space_interp%free()
1111 deallocate(stored_data)
1112 end if
1113
1114 call stored_xh%free()
1115 call h5dclose_f(dset_id, ierr)
1116 call h5sclose_f(filespace, ierr)
1117 call h5sclose_f(memspace, ierr)
1118
1119 end subroutine hdf5_checkpoint_read_mesh_array
1120
1121 !> Write one generic real array using its caller-provided distribution.
1122 !! @param group_id Open HDF5 payload group.
1123 !! @param plist_id Collective dataset transfer property list.
1124 !! @param h5_neko_real HDF5 datatype corresponding to `rp`.
1125 !! @param array Array descriptor to write.
1126 subroutine hdf5_checkpoint_write_array(group_id, plist_id, &
1127 h5_neko_real, array)
1128 integer(hid_t), intent(in) :: group_id, plist_id, h5_neko_real
1129 type(checkpoint_array_t), intent(in) :: array
1130 integer(hid_t) :: dset_id, filespace, memspace, h5_type
1131 integer(hsize_t), dimension(1) :: ddim, dcount, doffset
1132 integer :: ierr
1133 logical :: stored_dp
1134
1135 ! Arrays registered through `add_array_dp` keep double precision in the
1136 ! file whatever `rp` is, so that the time history survives a restart into
1137 ! a build of the other precision.
1138 stored_dp = associated(array%x_dp)
1139 if (stored_dp) then
1140 h5_type = h5t_native_double
1141 dcount(1) = int(size(array%x_dp), hsize_t)
1142 else
1143 h5_type = h5_neko_real
1144 dcount(1) = int(size(array%x), hsize_t)
1145 end if
1146 ddim(1) = int(array%global_count, hsize_t)
1147 doffset(1) = int(array%offset, hsize_t)
1148
1149 call h5screate_simple_f(1, ddim, filespace, ierr)
1150 call h5screate_simple_f(1, dcount, memspace, ierr)
1151 call h5dcreate_f(group_id, trim(array%name), h5_type, &
1152 filespace, dset_id, ierr)
1153
1154 if (array%replicated .and. pe_rank .ne. 0) then
1155 call h5sselect_none_f(filespace, ierr)
1156 call h5sselect_none_f(memspace, ierr)
1157 else
1158 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1159 doffset, dcount, ierr)
1160 end if
1161
1162 if (stored_dp) then
1163 call h5dwrite_f(dset_id, h5_type, array%x_dp, dcount, ierr, &
1164 file_space_id = filespace, mem_space_id = memspace, &
1165 xfer_prp = plist_id)
1166 else
1167 call h5dwrite_f(dset_id, h5_type, array%x, dcount, ierr, &
1168 file_space_id = filespace, mem_space_id = memspace, &
1169 xfer_prp = plist_id)
1170 end if
1171
1172 call h5dclose_f(dset_id, ierr)
1173 call h5sclose_f(filespace, ierr)
1174 call h5sclose_f(memspace, ierr)
1175
1176 end subroutine hdf5_checkpoint_write_array
1177
1178 !> Read one generic real array using its caller-provided distribution.
1179 !! @param group_id Open HDF5 payload group.
1180 !! @param plist_id Collective dataset transfer property list.
1181 !! @param h5_neko_real HDF5 datatype corresponding to `rp`.
1182 !! @param array Array descriptor to populate.
1183 subroutine hdf5_checkpoint_read_array(group_id, plist_id, &
1184 h5_neko_real, array)
1185 integer(hid_t), intent(in) :: group_id, plist_id, h5_neko_real
1186 type(checkpoint_array_t), intent(inout) :: array
1187 integer(hid_t) :: dset_id, filespace, memspace, h5_type
1188 integer(hsize_t), dimension(1) :: dcount, doffset
1189 integer(hsize_t), dimension(1) :: dataset_dims, dataset_maxdims
1190 integer :: ierr
1191 logical :: dataset_exists, stored_dp
1192
1193 call h5lexists_f(group_id, trim(array%name), dataset_exists, ierr)
1194 if (.not. dataset_exists) then
1195 call neko_error("Checkpoint array '" // trim(array%name) // &
1196 "' is missing")
1197 end if
1198
1199 call h5dopen_f(group_id, trim(array%name), dset_id, ierr)
1200 call h5dget_space_f(dset_id, filespace, ierr)
1201 call h5sget_simple_extent_dims_f(filespace, dataset_dims, &
1202 dataset_maxdims, ierr)
1203
1204 if (int(dataset_dims(1), i8) .ne. array%global_count) then
1205 call neko_error("Checkpoint array '" // trim(array%name) // &
1206 "' has an incompatible global extent")
1207 end if
1208
1209 stored_dp = associated(array%x_dp)
1210 if (stored_dp) then
1211 h5_type = h5t_native_double
1212 dcount(1) = int(size(array%x_dp), hsize_t)
1213 else
1214 h5_type = h5_neko_real
1215 dcount(1) = int(size(array%x), hsize_t)
1216 end if
1217 doffset(1) = int(array%offset, hsize_t)
1218 call h5screate_simple_f(1, dcount, memspace, ierr)
1219
1220 if (array%replicated .and. pe_rank .ne. 0) then
1221 call h5sselect_none_f(filespace, ierr)
1222 call h5sselect_none_f(memspace, ierr)
1223 else
1224 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1225 doffset, dcount, ierr)
1226 end if
1227 if (stored_dp) then
1228 call h5dread_f(dset_id, h5_type, array%x_dp, dcount, ierr, &
1229 file_space_id = filespace, mem_space_id = memspace, &
1230 xfer_prp = plist_id)
1231 else
1232 call h5dread_f(dset_id, h5_type, array%x, dcount, ierr, &
1233 file_space_id = filespace, mem_space_id = memspace, &
1234 xfer_prp = plist_id)
1235 end if
1236
1237 call h5dclose_f(dset_id, ierr)
1238 call h5sclose_f(filespace, ierr)
1239 call h5sclose_f(memspace, ierr)
1240
1241 ! Only rank zero selected the dataset above, so the copy every rank holds
1242 ! has to come from there.
1243 if (array%replicated) then
1244 if (stored_dp) then
1245 call mpi_bcast(array%x_dp, size(array%x_dp), &
1246 mpi_double_precision, 0, neko_comm, ierr)
1247 else
1248 call mpi_bcast(array%x, size(array%x), mpi_real_precision, 0, &
1249 neko_comm, ierr)
1250 end if
1251 end if
1252
1253 end subroutine hdf5_checkpoint_read_array
1254
1255 !> Determine mesh, fields, and histories represented by a data object.
1256 !! @param data Data object to inspect.
1257 !! @param msh Associated mesh, if available.
1258 !! @param dof Associated degree-of-freedom map, if available.
1259 !! @param fp Individual fields represented by `data`.
1260 !! @param fsp Field series represented by `data`.
1261 !! @param dtlag Previous time-step sizes, if available.
1262 !! @param tlag Previous simulation times, if available.
1263 subroutine hdf5_file_determine_data(data, msh, dof, fp, fsp, dtlag, tlag)
1264 class(*), target, intent(in) :: data
1265 type(mesh_t), pointer, intent(inout) :: msh
1266 type(dofmap_t), pointer, intent(inout) :: dof
1267 type(field_ptr_t), allocatable, intent(inout) :: fp(:)
1268 type(field_series_ptr_t), allocatable, intent(inout) :: fsp(:)
1269 real(kind=dp), pointer, intent(inout) :: dtlag(:)
1270 real(kind=dp), pointer, intent(inout) :: tlag(:)
1271 integer :: i, j, fp_size, fp_cur, fsp_size, fsp_cur
1272
1273 select type (data)
1274 type is (field_t)
1275 dof => data%dof
1276 msh => data%msh
1277 fp_size = 1
1278 allocate(fp(fp_size))
1279 fp(1)%ptr => data
1280
1281 nullify(dtlag)
1282 nullify(tlag)
1283
1284 type is (field_list_t)
1285
1286 if (data%size() .gt. 0) then
1287 allocate(fp(data%size()))
1288
1289 dof => data%dof(1)
1290 msh => data%msh(1)
1291
1292 do i = 1, data%size()
1293 fp(i)%ptr => data%items(i)%ptr
1294 end do
1295 else
1296 call neko_error('Empty field list')
1297 end if
1298
1299 nullify(dtlag)
1300 nullify(tlag)
1301
1302 type is (chkp_t)
1303 block
1304 type(checkpoint_payload_t), pointer :: fluid
1305 type(field_t), pointer :: u
1306
1307 fluid => data%get_payload("fluid")
1308 u => fluid%find_field("u")
1309 if (.not. associated(u) .or. &
1310 .not. associated(fluid%find_field("v")) .or. &
1311 .not. associated(fluid%find_field("w")) .or. &
1312 .not. associated(fluid%find_field("p"))) then
1313 call neko_error("Checkpoint not initialized")
1314 end if
1315 dof => u%dof
1316 msh => u%msh
1317
1318 fp_size = 0
1319 fsp_size = 0
1320 do i = 1, data%payload_count()
1321 fp_size = fp_size + data%payloads(i)%ptr%field_count()
1322 fsp_size = fsp_size + data%payloads(i)%ptr%series_count()
1323 end do
1324
1325 if (fp_size .gt. 0) allocate(fp(fp_size))
1326 if (fsp_size .gt. 0) allocate(fsp(fsp_size))
1327
1328 fp_cur = 1
1329 fsp_cur = 1
1330 do i = 1, data%payload_count()
1331 do j = 1, data%payloads(i)%ptr%field_count()
1332 fp(fp_cur)%ptr => data%payloads(i)%ptr%fields(j)%ptr
1333 fp_cur = fp_cur + 1
1334 end do
1335 do j = 1, data%payloads(i)%ptr%series_count()
1336 fsp(fsp_cur)%ptr => data%payloads(i)%ptr%series(j)%ptr
1337 fsp_cur = fsp_cur + 1
1338 end do
1339 end do
1340
1341 call data%get_time_history(tlag, dtlag)
1342 end block
1343
1344 class default
1345 call neko_log%error('Invalid data')
1346 end select
1347
1348 end subroutine hdf5_file_determine_data
1349
1350 !> Determine hdf5 real type corresponding to NEKO_REAL
1351 !! @note This must be called after h5open_f, otherwise
1352 !! the H5T_NATIVE_XYZ types has a value of 0
1353 subroutine hdf5_file_determine_real(H5T_NEKO_REAL)
1354 integer(hid_t), intent(inout) :: H5T_NEKO_REAL
1355 select case (rp)
1356 case (dp)
1357 h5t_neko_real = h5t_native_double
1358 case (sp)
1359 h5t_neko_real = h5t_native_real
1360 case default
1361 call neko_error("Unsupported real type")
1362 end select
1363 end subroutine hdf5_file_determine_real
1364
1365 ! ================
1366 ! Granular methods
1367 ! ================
1368
1369 !> Open a HDF5 file in a given mode
1370 subroutine hdf5_file_open(this, mode)
1371 class(hdf5_file_t), intent(inout) :: this
1372 character(len=1), intent(in) :: mode
1373 integer :: ierr, mpi_info, mpi_comm, i, n_fields, counter
1374 logical :: file_exists
1375 character(len=1024) :: fname
1376 character(len=LOG_SIZE) :: log_buf
1377
1378 call this%free()
1379
1380 ! Set the mode for the file
1381 this%mode = mode
1382
1383 ! Ensure precision is set and are valid.
1384 if (this%precision .gt. rp) then
1385 this%precision = rp
1386 call neko_warning('Requested precision is higher than working precision')
1387 else if (this%precision .eq. -1) then
1388 this%precision = rp
1389 end if
1390
1391 fname = trim(this%get_fname())
1392 counter = this%get_counter() - this%get_start_counter()
1393
1394 ! Set the configuration for MPI IO
1395 call hdf5_session_init()
1396
1397 mpi_info = mpi_info_null%mpi_val
1398 mpi_comm = neko_comm%mpi_val
1399 call h5pcreate_f(h5p_file_access_f, this%plist_id, ierr)
1400 call h5pset_fapl_mpio_f(this%plist_id, mpi_comm, mpi_info, ierr)
1401
1402 ! Open the file
1403 inquire(file = fname, exist = file_exists)
1404 if (file_exists) then
1405 call h5fopen_f(fname, h5f_acc_rdwr_f, this%file_id, ierr, &
1406 access_prp = this%plist_id)
1407 else
1408 call h5fcreate_f(fname, h5f_acc_trunc_f, &
1409 this%file_id, ierr, access_prp = this%plist_id)
1410 end if
1411
1412 ! All dataset writes share these collective transfer properties.
1413 call h5pcreate_f(h5p_dataset_xfer_f, this%xfer_id, ierr)
1414 call h5pset_dxpl_mpio_f(this%xfer_id, h5fd_mpio_collective_f, ierr)
1415
1416 ! Set the active group to the root of the file
1417 call this%set_active_group()
1418
1419 write (log_buf, *) "Opened HDF5 file: ", trim(fname), " with counter: ", &
1420 counter
1421 call neko_log%message(log_buf, lvl = neko_log_debug)
1422
1423 end subroutine hdf5_file_open
1424
1425 !> Close the file
1426 subroutine hdf5_file_close(this)
1427 class(hdf5_file_t), intent(inout) :: this
1428 integer :: ierr
1429
1430 call this%clear_datasets()
1431 call h5pclose_f(this%xfer_id, ierr)
1432 this%xfer_id = -1_hid_t
1433
1434 if (this%active_group_id .ne. -1_hid_t .and. &
1435 this%active_group_id .ne. this%file_id) then
1436 call h5gclose_f(this%active_group_id, ierr)
1437 end if
1438 this%active_group_id = -1_hid_t
1439
1440 call h5pclose_f(this%plist_id, ierr)
1441 this%plist_id = -1_hid_t
1442 call h5fclose_f(this%file_id, ierr)
1443 this%file_id = -1_hid_t
1444 call hdf5_session_finalize()
1445
1446 call neko_log%message("Closed HDF5 file: " // trim(this%get_fname()), &
1447 lvl = neko_log_debug)
1448
1449 end subroutine hdf5_file_close
1450
1451
1452 !> Set the active group for HDF5 files from an input string
1453 !! @param this The HDF5 file object
1454 !! @param A string indicating the path to the group to create or open.
1455 subroutine hdf5_file_set_group(this, group_name_path)
1456 class(hdf5_file_t), intent(inout) :: this
1457 character(len=*), intent(in), optional :: group_name_path
1458 character(len=1000), allocatable :: group_name(:)
1459
1460 integer(hid_t) :: current_id, group_id
1461 integer :: ierr, i, j, num_groups, name_len, group_loc
1462 logical :: group_exists
1463
1464
1465 ! Dataset names in the cache are relative to the group being closed.
1466 call this%clear_datasets()
1467
1468 ! Close previous active group if one is open
1469 if (this%active_group_id .ne. -1_hid_t .and. this%active_group_id .ne. &
1470 this%file_id) then
1471 call h5gclose_f(this%active_group_id, ierr)
1472 end if
1473 this%active_group_id = -1_hid_t
1474
1475 ! Start from root location = file
1476 current_id = this%file_id
1477 ! Return the root directory if no group name is given
1478 if (.not. present(group_name_path)) then
1479 this%active_group_id = current_id
1480 return
1481 end if
1482
1483 ! Split the input string into group names using "/" as a delimiter
1484 name_len = len(trim(group_name_path))
1485 ! Count how many groups
1486 num_groups = 1 ! There is at least one group if this was passed
1487 do i = 1, name_len
1488 if (group_name_path .eq. "/") then
1489 num_groups = num_groups + 1
1490 end if
1491 end do
1492
1493 ! Allocate the group array and populate it
1494 allocate(group_name(num_groups))
1495 j = 1
1496 group_loc = 1
1497 do i = 1, name_len
1498 if (group_name_path .eq. "/") then
1499 group_name(group_loc) = group_name_path(j:i-1)
1500 group_loc = group_loc + 1
1501 j = i + 1
1502 end if
1503 end do
1504 if (j .ne. name_len) then
1505 group_name(group_loc) = group_name_path(j:name_len)
1506 end if
1507
1508 ! Iterate over the groups in the path
1509 do i = 1, num_groups
1510 call h5lexists_f(current_id, trim(group_name(i)), group_exists, ierr)
1511
1512 ! Only create groups if they dont exist and we are in write mode "w"
1513 if (group_exists) then
1514 call h5gopen_f(current_id, trim(group_name(i)), group_id, ierr)
1515 else
1516 if (this%mode == "r") then
1517 call neko_error("Group " // trim(group_name(i)) // &
1518 " does not exist in file " // trim(this%get_fname()))
1519 end if
1520 call h5gcreate_f(current_id, trim(group_name(i)), group_id, ierr)
1521 end if
1522
1523 ! Close previous location only if it was an opened group, not the file
1524 if (i > 1) then
1525 call h5gclose_f(current_id, ierr)
1526 end if
1527
1528 current_id = group_id
1529 end do
1530
1531 this%active_group_id = current_id
1532 end subroutine hdf5_file_set_group
1533
1534
1535 !> Write a supported object into the active group.
1536 !! @param data Object whose host values are written.
1537 !! @param name Dataset name, required only for host_array_t.
1538 !! @param layout Optional reusable vector or matrix column distribution.
1539 subroutine hdf5_file_write_dataset(this, data, name, layout)
1540 class(hdf5_file_t), intent(inout) :: this
1541 class(*), intent(inout) :: data
1542 character(len=*), intent(in), optional :: name
1543 type(hdf5_layout_t), intent(in), optional :: layout
1544
1545 select type (d => data)
1546 type is (host_array_t)
1547 if (.not. present(name)) then
1548 call neko_error("A dataset name is required for host arrays")
1549 return
1550 end if
1551 call this%write_array(d%x, name, layout)
1552 type is (vector_t)
1553 call this%write_vector(d, layout)
1554 type is (matrix_t)
1555 call this%write_matrix(d, layout)
1556 type is (field_t)
1557 if (present(layout)) then
1558 call neko_error("A supplied layout is only supported for arrays")
1559 return
1560 end if
1561 call this%write_field(d)
1562 class default
1563 call neko_error("write_dataset not implemented for this data type")
1564 end select
1565 end subroutine hdf5_file_write_dataset
1566
1567 subroutine hdf5_file_read_dataset(this, data_name, data, strategy)
1568 class(hdf5_file_t), intent(inout) :: this
1569 character(len=*), intent(in) :: data_name
1570 class(*), intent(inout) :: data
1571 character(len=*), intent(in), optional :: strategy
1572
1573 select type (d => data)
1574 type is (vector_t)
1575 call this%read_vector(data_name, d, strategy)
1576 type is (matrix_t)
1577 call this%read_matrix(data_name, d, strategy)
1578 type is (field_t)
1579 call neko_error("Reading a field_t is not supported yet")
1580 class default
1581 call neko_error("read_dataset not implemented for this data type")
1582 end select
1583 end subroutine hdf5_file_read_dataset
1584
1585 subroutine hdf5_file_write_attribute(this, data_name, data)
1586 class(hdf5_file_t), intent(inout) :: this
1587 character(len=*), intent(in) :: data_name
1588 class(*), intent(inout) :: data
1589
1590 select type (d => data)
1591 type is (integer)
1592 call this%write_int_attribute(data_name, d)
1593 type is (real(kind=rp))
1594 call this%write_rp_attribute(data_name, d)
1595 class default
1596 call neko_error("write_attribute not implemented for this data type")
1597 end select
1598 end subroutine hdf5_file_write_attribute
1599
1600 subroutine hdf5_file_read_attribute(this, data_name, data, exist)
1601 class(hdf5_file_t), intent(inout) :: this
1602 character(len=*), intent(in) :: data_name
1603 class(*), intent(inout) :: data
1604 logical, intent(inout) :: exist
1605
1606 select type (d => data)
1607 type is (integer)
1608 call this%read_int_attribute(data_name, d, exist)
1609 type is (real(kind=rp))
1610 call this%read_rp_attribute(data_name, d, exist)
1611 class default
1612 call neko_error("read_attribute not implemented for this data type")
1613 end select
1614 end subroutine hdf5_file_read_attribute
1615
1616
1617 !> Release the handles owned by one cached dataset.
1618 subroutine hdf5_dataset_free(this)
1619 class(hdf5_dataset_t), intent(inout) :: this
1620 integer :: ierr
1621
1622 if (this%memspace /= -1_hid_t) call h5sclose_f(this%memspace, ierr)
1623 if (this%id /= -1_hid_t) call h5dclose_f(this%id, ierr)
1624 this%memspace = -1_hid_t
1625 this%id = -1_hid_t
1626 this%dset_rank = 0
1627 this%extent = 0_hsize_t
1628 this%memory_shape = 0_hsize_t
1629 if (allocated(this%name)) deallocate(this%name)
1630 end subroutine hdf5_dataset_free
1631
1632 !> Release cached datasets before closing their group or file.
1633 subroutine hdf5_file_clear_datasets(this)
1634 class(hdf5_file_t), intent(inout) :: this
1635 integer :: i
1636
1637 if (.not. allocated(this%datasets)) return
1638 do i = 1, size(this%datasets)
1639 call this%datasets(i)%free()
1640 end do
1641 deallocate(this%datasets)
1642 end subroutine hdf5_file_clear_datasets
1643
1644 !> Open or reuse a dataset and prepare one sample for writing.
1645 !! @param name Dataset name relative to the active group.
1646 !! @param dims Global shape of this sample before appending.
1647 !! @param chunks Chunk shape used only when creating the dataset.
1648 !! @param count Local shape of this sample.
1649 !! @param offset MPI rank's array offset, including previous samples.
1650 !! @param dataset_id Borrowed dataset handle, owned by this file.
1651 !! @param memspace Borrowed memory dataspace, owned by this file.
1652 !! @param filespace Fresh file dataspace; the caller must close it.
1653 subroutine hdf5_file_prepare_dataset(this, name, dims, chunks, count, &
1654 offset, dataset_id, memspace, filespace)
1655 class(hdf5_file_t), intent(inout) :: this
1656 character(len=*), intent(in) :: name
1657 integer(hsize_t), intent(in) :: dims(:), chunks(:), count(:)
1658 integer(hsize_t), intent(inout) :: offset(:)
1659 integer(hid_t), intent(out) :: dataset_id, memspace, filespace
1660 integer(hsize_t) :: new_extent(size(dims)), max_extent(size(dims))
1661 integer(hid_t) :: dcpl_id, precision_hdf
1662 integer :: i, index, dset_rank, ierr
1663 logical :: exists
1664
1665 ! Array rank is the number of dimensions, not the MPI rank.
1666 dset_rank = size(dims)
1667 if (.not. allocated(this%datasets)) allocate(this%datasets(0))
1668 index = 0
1669 do i = 1, size(this%datasets)
1670 if (this%datasets(i)%name == name) then
1671 index = i
1672 exit
1673 end if
1674 end do
1675
1676 exists = .true.
1677 if (index == 0) then
1678 ! Grow only on first use of a name. This copies handle values;
1679 ! only free closes the resources, not the array reallocation.
1680 this%datasets = [this%datasets, hdf5_dataset_t()]
1681 index = size(this%datasets)
1682 associate(dataset => this%datasets(index))
1683 dataset%name = name
1684 dataset%dset_rank = dset_rank
1685 call h5lexists_f(this%active_group_id, name, exists, ierr)
1686 if (exists) then
1687 ! retrieve the dset id for the existing data set
1688 call h5dopen_f(this%active_group_id, name, dataset%id, ierr)
1689 if (ierr < 0) then
1690 call neko_error("Could not open HDF5 dataset: " // name)
1691 return
1692 end if
1693 call h5dget_space_f(dataset%id, filespace, ierr)
1694 call h5sget_simple_extent_dims_f(filespace, &
1695 dataset%extent(1:dset_rank), max_extent, ierr)
1696 call h5sclose_f(filespace, ierr)
1697 else
1698 max_extent = dims
1699 max_extent(dset_rank) = h5s_unlimited_f
1700 ! create file space of this shape
1701 call h5screate_simple_f(dset_rank, dims, filespace, ierr, max_extent)
1702 call h5pcreate_f(h5p_dataset_create_f, dcpl_id, ierr)
1703 call h5pset_chunk_f(dcpl_id, dset_rank, chunks, ierr)
1704 precision_hdf = h5kind_to_type(this%precision, h5_real_kind)
1705 ! create the data set with the given shape
1706 call h5dcreate_f(this%active_group_id, name, precision_hdf, &
1707 filespace, dataset%id, ierr, dcpl_id = dcpl_id)
1708 if (ierr < 0) then
1709 call h5pclose_f(dcpl_id, ierr)
1710 call h5sclose_f(filespace, ierr)
1711 call neko_error("Could not create HDF5 dataset: " // name)
1712 return
1713 end if
1714 call h5pclose_f(dcpl_id, ierr)
1715 call h5sclose_f(filespace, ierr)
1716 dataset%extent(1:dset_rank) = dims
1717 end if
1718 end associate
1719 end if
1720
1721 associate(dataset => this%datasets(index))
1722 if (dataset%dset_rank /= dset_rank) then
1723 call neko_error("HDF5 dataset rank changed: " // name)
1724 return
1725 end if
1726 if (exists .and. this%overwrite .and. pe_rank == 0) then
1727 select case (dset_rank)
1728 case (2)
1729 call neko_warning("Dataset " // name // &
1730 " already exists and wil be overwritten")
1731 case (4)
1732 call neko_warning("Overwriting dataset: " // name)
1733 end select
1734 end if
1735 if (exists .and. .not. this%overwrite) then
1736 ! The extent is queried only on opening an existing dataset.
1737 ! Each extension updates our copy, without querying it again.
1738 new_extent = dims
1739 offset(dset_rank) = offset(dset_rank) + dataset%extent(dset_rank)
1740 new_extent(dset_rank) = new_extent(dset_rank) + dataset%extent(dset_rank)
1741 call h5dset_extent_f(dataset%id, new_extent, ierr)
1742 if (ierr < 0) then
1743 call neko_error("Could not extend HDF5 dataset: " // name)
1744 return
1745 end if
1746 dataset%extent(1:dset_rank) = new_extent
1747 end if
1748
1749 ! Local shapes can change between writes. Only recreate the memory
1750 ! dataspace when that happens; no sample values are retained here.
1751 if (dataset%memspace /= -1_hid_t) then
1752 if (any(dataset%memory_shape(1:dset_rank) /= count)) then
1753 call h5sclose_f(dataset%memspace, ierr)
1754 dataset%memspace = -1_hid_t
1755 end if
1756 end if
1757 if (dataset%memspace == -1_hid_t) then
1758 ! Create the corresponding memory space (buffer) for my local data
1759 call h5screate_simple_f(dset_rank, count, dataset%memspace, ierr)
1760 if (any(count == 0_hsize_t)) then
1761 call h5sselect_none_f(dataset%memspace, ierr)
1762 end if
1763 dataset%memory_shape(1:dset_rank) = count
1764 end if
1765
1766 ! Extending invalidates previously obtained file dataspaces.
1767 ! Get the total file space (shape) of the data set
1768 call h5dget_space_f(dataset%id, filespace, ierr)
1769 if (any(count == 0_hsize_t)) then
1770 call h5sselect_none_f(filespace, ierr)
1771 else
1772 ! Get only the slice where my rank writes
1773 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, offset, &
1774 count, ierr)
1775 end if
1776 dataset_id = dataset%id
1777 memspace = dataset%memspace
1778 end associate
1779 end subroutine hdf5_file_prepare_dataset
1780
1781 !> Write a vector through the host-array path.
1782 !! @param vec Vector whose host values are written.
1783 !! @param layout Optional distribution to reuse between writes.
1784 subroutine hdf5_file_write_vector(this, vec, layout)
1785 class(hdf5_file_t), intent(inout) :: this
1786 type(vector_t), intent(inout) :: vec
1787 type(hdf5_layout_t), intent(in), optional :: layout
1788 real(kind=rp) :: empty(0)
1789
1790 ! vector_allocate returns without allocating for an initial size of zero.
1791 ! Supply an empty array rather than passing its unallocated x component.
1792 if (vec%size() == 0) then
1793 call this%write_array(empty, trim(vec%name), layout)
1794 else
1795 call this%write_array(vec%x, trim(vec%name), layout)
1796 end if
1797 end subroutine hdf5_file_write_vector
1798
1799 !> Write host values without allocating a device-backed temporary.
1800 !! @param values Local values in rank order.
1801 !! @param name Dataset name within the active group.
1802 !! @param layout Optional distribution to reuse between writes.
1803 subroutine hdf5_file_write_array(this, values, name, layout)
1804 class(hdf5_file_t), intent(inout) :: this
1805 real(kind=rp), intent(in) :: values(:)
1806 character(len=*), intent(in) :: name
1807 type(hdf5_layout_t), intent(in), optional :: layout
1808 type(hdf5_layout_t) :: distribution
1809 integer :: ierr, counts, offset, total_count, max_count
1810 integer(hid_t) :: precision_hdf
1811 integer(hid_t) :: filespace, dset_id, memspace
1812 integer(hsize_t), dimension(1) :: dcount, doffset
1813 integer(hsize_t), dimension(1) :: ddims, chunkdims
1814 real(kind=sp), allocatable :: write_buffer_sp(:) ! Write buffer single
1815 real(kind=dp), allocatable :: write_buffer_dp(:) ! Write buffer double
1816
1817 ! ===============
1818 ! Get host array info
1819 ! ===============
1820 counts = size(values)
1821 ! Reuse an explicitly supplied distribution. A local size check catches
1822 ! accidental misuse without adding a collective to every write.
1823 if (present(layout)) then
1824 if (counts /= layout%local_count) then
1825 call neko_error("HDF5 layout does not match the local array size")
1826 return
1827 end if
1828 distribution = layout
1829 else
1830 call distribution%init(counts)
1831 end if
1832 offset = distribution%offset
1833 total_count = distribution%total_count
1834 max_count = distribution%max_count
1835
1836 ! ===============
1837 ! Configure MPIIO
1838 ! ===============
1839 precision_hdf = h5kind_to_type(this%precision, h5_real_kind)
1840
1841 ! ===================
1842 ! Create the data set
1843 ! ===================
1844 ddims = [int(total_count, hsize_t)] ! global size of the vector
1845
1846 ! Enable chunking to be able to append
1847 chunkdims = [max(int(max_count, hsize_t), 1_hsize_t)]
1848
1849 ! ===========================
1850 ! Set up writing the data set
1851 ! ===========================
1852 dcount = [int(counts, hsize_t)] ! local size of the vector
1853
1854 ! offset for this rank in the global vector
1855 doffset = [int(offset, hsize_t)]
1856 ! Retain the dataset and memory space; refresh the file space after
1857 ! extending it to include this sample.
1858 call this%prepare_dataset(trim(name), ddims, chunkdims, &
1859 dcount, doffset, dset_id, memspace, filespace)
1860
1861 ! =======================
1862 ! Cast and write the data
1863 ! =======================
1864 if (this%precision == rp) then
1865 ! The host values already have the requested precision.
1866 call h5dwrite_f(dset_id, precision_hdf, values, dcount, ierr, &
1867 file_space_id = filespace, mem_space_id = memspace, &
1868 xfer_prp = this%xfer_id)
1869 else if (this%precision == sp) then
1870 allocate(write_buffer_sp(size(values)))
1871 if (size(values) > 0) write_buffer_sp = real(values, kind=sp)
1872 ! Write the data
1873 call h5dwrite_f(dset_id, precision_hdf, write_buffer_sp, dcount, ierr, &
1874 file_space_id = filespace, mem_space_id = memspace, &
1875 xfer_prp = this%xfer_id)
1876 deallocate(write_buffer_sp)
1877 else if (this%precision == dp) then
1878 allocate(write_buffer_dp(size(values)))
1879 if (size(values) > 0) write_buffer_dp = real(values, kind=dp)
1880 ! Write the data
1881 call h5dwrite_f(dset_id, precision_hdf, write_buffer_dp, dcount, ierr, &
1882 file_space_id = filespace, mem_space_id = memspace, &
1883 xfer_prp = this%xfer_id)
1884 deallocate(write_buffer_dp)
1885 else
1886 call neko_error("Unsupported precision")
1887 end if
1888
1889 ! =======================
1890 ! Clean up
1891 ! =======================
1892 call h5sclose_f(filespace, ierr)
1893
1894 end subroutine hdf5_file_write_array
1895
1896 !> Write a matrix, distributing its columns over NEKO_COMM.
1897 !! @param mat Matrix whose host values are written.
1898 !! @param layout Optional reusable distribution of the matrix columns.
1899 subroutine hdf5_file_write_matrix(this, mat, layout)
1900 class(hdf5_file_t), intent(inout) :: this
1901 type(matrix_t), intent(inout) :: mat
1902 type(hdf5_layout_t), intent(in), optional :: layout
1903 integer :: ierr, counts, offset, total_count, strides, max_count
1904 type(hdf5_layout_t) :: distribution
1905 integer(hid_t) :: precision_hdf
1906 integer(hid_t) :: filespace, dset_id, memspace
1907 integer(hsize_t), dimension(2) :: dcount, doffset
1908 integer(hsize_t), dimension(2) :: ddims, chunkdims
1909 real(kind=sp), allocatable :: write_buffer_sp(:,:) ! Write buffer single
1910 real(kind=dp), allocatable :: write_buffer_dp(:,:) ! Write buffer double
1911
1912 ! ===============
1913 ! Get Matrix info
1914 ! ===============
1915 strides = mat%get_nrows()
1916 counts = mat%get_ncols()
1917 ! Matrices distribute columns; the layout is independent of row count.
1918 if (present(layout)) then
1919 if (counts /= layout%local_count) then
1920 call neko_error("HDF5 layout does not match the local column count")
1921 return
1922 end if
1923 distribution = layout
1924 else
1925 call distribution%init(counts)
1926 end if
1927 offset = distribution%offset
1928 total_count = distribution%total_count
1929 max_count = distribution%max_count
1930
1931 ! ===============
1932 ! Configure MPIIO
1933 ! ===============
1934 precision_hdf = h5kind_to_type(this%precision, h5_real_kind)
1935
1936 ! ===================
1937 ! Create the data set
1938 ! ===================
1939 ! global size of the matrix
1940 ddims = [int(strides, hsize_t), int(total_count, hsize_t)]
1941 chunkdims = [int(strides, hsize_t), max(int(max_count, hsize_t), 1_hsize_t)]
1942
1943 ! ===========================
1944 ! Set up writing the data set
1945 ! ===========================
1946 ! local size of the matrix
1947 dcount = [int(strides, hsize_t), int(counts, hsize_t)]
1948 ! offset for this rank in the global matrix
1949 doffset = [0_hsize_t, int(offset, hsize_t)]
1950 ! Retain the dataset and memory space; refresh the file space after
1951 ! extending it to include this sample.
1952 call this%prepare_dataset(trim(mat%name), ddims, chunkdims, &
1953 dcount, doffset, dset_id, memspace, filespace)
1954
1955 ! =======================
1956 ! Cast and write the data
1957 ! =======================
1958 if (this%precision == rp) then
1959 ! The host values already have the requested precision.
1960 call h5dwrite_f(dset_id, precision_hdf, mat%x, dcount, ierr, &
1961 file_space_id = filespace, mem_space_id = memspace, &
1962 xfer_prp = this%xfer_id)
1963 else if (this%precision == sp) then
1964 allocate(write_buffer_sp(mat%get_nrows(), mat%get_ncols()))
1965 if (mat%size() > 0) write_buffer_sp = real(mat%x, kind=sp)
1966 ! Write the data
1967 call h5dwrite_f(dset_id, precision_hdf, write_buffer_sp, dcount, ierr, &
1968 file_space_id = filespace, mem_space_id = memspace, &
1969 xfer_prp = this%xfer_id)
1970 deallocate(write_buffer_sp)
1971 else if (this%precision == dp) then
1972 allocate(write_buffer_dp(mat%get_nrows(), mat%get_ncols()))
1973 if (mat%size() > 0) write_buffer_dp = real(mat%x, kind=dp)
1974 ! Write the data
1975 call h5dwrite_f(dset_id, precision_hdf, write_buffer_dp, dcount, ierr, &
1976 file_space_id = filespace, mem_space_id = memspace, &
1977 xfer_prp = this%xfer_id)
1978 deallocate(write_buffer_dp)
1979 else
1980 call neko_error("Unsupported precision")
1981 end if
1982
1983 ! =======================
1984 ! Clean up
1985 ! =======================
1986 call h5sclose_f(filespace, ierr)
1987
1988 end subroutine hdf5_file_write_matrix
1989
1990 subroutine hdf5_file_write_field(this, field)
1991 class(hdf5_file_t), intent(inout) :: this
1992 type(field_t), intent(inout) :: field
1993 integer :: ierr, counts, offset, total_count, max_count
1994 integer :: stride_ax_1, stride_ax_2, stride_ax_3
1995 integer(hid_t) :: precision_hdf
1996 integer(hid_t) :: filespace, dset_id, memspace
1997 integer(hsize_t), dimension(4) :: dcount, doffset
1998 integer(hsize_t), dimension(4) :: ddims, chunkdims
1999 real(kind=sp), allocatable :: write_buffer_sp(:,:,:,:) ! Write buffer single
2000 real(kind=dp), allocatable :: write_buffer_dp(:,:,:,:) ! Write buffer double
2001
2002 ! ==============
2003 ! Get Field info
2004 ! ==============
2005 stride_ax_1 = field%Xh%lx
2006 stride_ax_2 = field%Xh%ly
2007 stride_ax_3 = field%Xh%lz
2008 counts = field%msh%nelv
2009 total_count = field%msh%glb_nelv
2010 max_count = 0
2011 offset = field%msh%offset_el
2012 call mpi_allreduce(counts, max_count, 1, mpi_integer, &
2013 mpi_max, neko_comm, ierr)
2014
2015 ! ===============
2016 ! Configure MPIIO
2017 ! ===============
2018 precision_hdf = h5kind_to_type(this%precision, h5_real_kind)
2019
2020 ! ===================
2021 ! Create the data set
2022 ! ===================
2023 ddims = [int(stride_ax_1, hsize_t), &
2024 int(stride_ax_2, hsize_t), &
2025 int(stride_ax_3, hsize_t), &
2026 int(total_count, hsize_t)] ! global size of the tensor
2027 chunkdims = [int(stride_ax_1, hsize_t), &
2028 int(stride_ax_2, hsize_t), &
2029 int(stride_ax_3, hsize_t), &
2030 max(int(max_count, hsize_t), 1_hsize_t)]
2031
2032 ! ===========================
2033 ! Set up writing the data set
2034 ! ===========================
2035 dcount = [int(stride_ax_1, hsize_t), &
2036 int(stride_ax_2, hsize_t), &
2037 int(stride_ax_3, hsize_t), &
2038 int(counts, hsize_t)] ! local size of the tensor
2039 doffset = [0_hsize_t, 0_hsize_t, 0_hsize_t, &
2040 int(offset, hsize_t)] ! offset in the global tensor
2041 ! Retain the dataset and memory space; refresh the file space after
2042 ! extending it to include this sample.
2043 call this%prepare_dataset(trim(field%name), ddims, chunkdims, &
2044 dcount, doffset, dset_id, memspace, filespace)
2045
2046 ! =======================
2047 ! Cast and write the data
2048 ! =======================
2049 if (this%precision == sp) then
2050 allocate(write_buffer_sp(field%Xh%lx, field%Xh%ly, field%Xh%lz, &
2051 field%msh%nelv))
2052 if (field%msh%nelv > 0) write_buffer_sp = real(field%x, kind=sp)
2053 ! Write the data
2054 call h5dwrite_f(dset_id, precision_hdf, write_buffer_sp, dcount, ierr, &
2055 file_space_id = filespace, mem_space_id = memspace, &
2056 xfer_prp = this%xfer_id)
2057 deallocate(write_buffer_sp)
2058 else if (this%precision == dp) then
2059 allocate(write_buffer_dp(field%Xh%lx, field%Xh%ly, field%Xh%lz, &
2060 field%msh%nelv))
2061 if (field%msh%nelv > 0) write_buffer_dp = real(field%x, kind=dp)
2062 ! Write the data
2063 call h5dwrite_f(dset_id, precision_hdf, write_buffer_dp, dcount, ierr, &
2064 file_space_id = filespace, mem_space_id = memspace, &
2065 xfer_prp = this%xfer_id)
2066 deallocate(write_buffer_dp)
2067 else
2068 call neko_error("Unsupported precision")
2069 end if
2070
2071 ! =======================
2072 ! Clean up
2073 ! =======================
2074 call h5sclose_f(filespace, ierr)
2075
2076 end subroutine hdf5_file_write_field
2077
2078 !> Read a vector
2079 subroutine hdf5_file_read_vector(this, data_name, vec, strategy)
2080 class(hdf5_file_t) :: this
2081 character(len=*), intent(in) :: data_name
2082 type(vector_t), intent(inout) :: vec
2083 character(len=*), intent(in), optional :: strategy
2084 character(len=1000) :: strategy_
2085 integer :: ierr, counts, offset, total_count, dset_rank
2086 integer(hid_t) :: precision_hdf
2087 integer(hid_t) :: xf_id, filespace, dset_id, memspace
2088 integer(hsize_t), dimension(1) :: dcount, doffset
2089 integer(hsize_t), dimension(1) :: tempddims, tempmaxddims
2090 integer :: temprank
2091 logical :: dset_exists
2092 type(linear_dist_t) :: dist
2093
2094 ! Set up strategy
2095 if (present(strategy)) then
2096 if (trim(strategy) .eq. "linear" .or. &
2097 trim(strategy) .eq. "rank_0") then
2098 strategy_ = strategy
2099 else
2100 call neko_error("Unsupported strategy: " // trim(strategy))
2101 end if
2102 else
2103 strategy_ = "linear"
2104 end if
2105
2106 ! Free the input
2107 call vec%free()
2108
2109 ! ===============
2110 ! Configure MPIIO
2111 ! ===============
2112 call h5pcreate_f(h5p_dataset_xfer_f, xf_id, ierr)
2113 call h5pset_dxpl_mpio_f(xf_id, h5fd_mpio_collective_f, ierr)
2114 precision_hdf = h5kind_to_type(rp, h5_real_kind)
2115
2116 ! ===================
2117 ! Get the data set info
2118 ! ===================
2119 call h5lexists_f(this%active_group_id, trim(data_name), dset_exists, ierr)
2120 if (dset_exists) then
2121 ! Open the data set
2122 call h5dopen_f(this%active_group_id, trim(data_name), dset_id, ierr)
2123 ! Get the current rank of the dataset
2124 call h5dget_space_f(dset_id, filespace, ierr)
2125 call h5sget_simple_extent_ndims_f(filespace, temprank, ierr)
2126 if (temprank .ne. 1) then
2127 call neko_error("Dataset " // trim(data_name) // &
2128 " is not a rank 1 vector in file " // trim(this%get_fname()))
2129 end if
2130 ! Get the current shape and close the filespace
2131 call h5sget_simple_extent_dims_f(filespace, tempddims, tempmaxddims, &
2132 ierr)
2133 call h5sclose_f(filespace, ierr)
2134 else
2135 call neko_error("Dataset " // trim(data_name) // &
2136 " does not exist in current group " // trim(this%get_fname()))
2137 end if
2138
2139 ! =============================
2140 ! Perform the data distribution
2141 ! =============================
2142 total_count = int(tempddims(1))
2143 if (strategy_ .eq. "linear") then
2144 dist = linear_dist_t(total_count, pe_rank, pe_size, neko_comm)
2145 counts = dist%num_local()
2146 offset = 0
2147 else if (strategy_ .eq. "rank_0") then
2148 if (pe_rank .eq. 0) then
2149 counts = total_count
2150 else
2151 counts = 0
2152 end if
2153 offset = 0
2154 end if
2155 call mpi_exscan(counts, offset, 1, mpi_integer, &
2156 mpi_sum, neko_comm, ierr)
2157
2158 ! ===========================
2159 ! Set up reading the data set
2160 ! ===========================
2161 dset_rank = 1 ! rank 1 array, i.e. a vector
2162 dcount = [int(counts, hsize_t)] ! local size of the vector
2163 doffset = [int(offset, hsize_t)] ! offset for this rank in the global vector
2164 ! Get the total file space (shape) of the data set
2165 call h5dget_space_f(dset_id, filespace, ierr)
2166 ! Get only the slice where my rank reads
2167 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, doffset, dcount, &
2168 ierr)
2169 ! Create the corresponding memory space (buffer) for my local data
2170 call h5screate_simple_f(dset_rank, dcount, memspace, ierr)
2171
2172 ! =============================
2173 ! Allocate data. HDF5 will cast
2174 ! =============================
2175 call vec%init(counts, trim(data_name)) ! this is rp
2176 call h5dread_f(dset_id, precision_hdf, vec%x, dcount, ierr, &
2177 file_space_id = filespace, mem_space_id = memspace, &
2178 xfer_prp = xf_id)
2179
2180 ! =======================
2181 ! Clean up
2182 ! =======================
2183 call h5pclose_f(xf_id, ierr)
2184 call h5sclose_f(memspace, ierr)
2185 call h5sclose_f(filespace, ierr)
2186 call h5dclose_f(dset_id, ierr)
2187
2188 end subroutine hdf5_file_read_vector
2189
2190 !> Read a matrix
2191 subroutine hdf5_file_read_matrix(this, data_name, mat, strategy)
2192 class(hdf5_file_t) :: this
2193 character(len=*), intent(in) :: data_name
2194 type(matrix_t), intent(inout) :: mat
2195 character(len=*), intent(in), optional :: strategy
2196 character(len=1000) :: strategy_
2197 integer :: ierr, counts, offset, total_count, dset_rank
2198 integer(hid_t) :: precision_hdf
2199 integer(hid_t) :: xf_id, filespace, dset_id, memspace
2200 integer(hsize_t), dimension(2) :: dcount, doffset
2201 integer(hsize_t), dimension(2) :: tempddims, tempmaxddims
2202 integer :: temprank
2203 logical :: dset_exists
2204 type(linear_dist_t) :: dist
2205
2206 ! Set up strategy
2207 if (present(strategy)) then
2208 if (trim(strategy) .eq. "linear" .or. &
2209 trim(strategy) .eq. "rank_0") then
2210 strategy_ = strategy
2211 else
2212 call neko_error("Unsupported strategy: " // trim(strategy))
2213 end if
2214 else
2215 strategy_ = "linear"
2216 end if
2217
2218 ! Free the input
2219 call mat%free()
2220
2221 ! ===============
2222 ! Configure MPIIO
2223 ! ===============
2224 call h5pcreate_f(h5p_dataset_xfer_f, xf_id, ierr)
2225 call h5pset_dxpl_mpio_f(xf_id, h5fd_mpio_collective_f, ierr)
2226 precision_hdf = h5kind_to_type(rp, h5_real_kind)
2227
2228 ! ===================
2229 ! Get the data set info
2230 ! ===================
2231 call h5lexists_f(this%active_group_id, trim(data_name), dset_exists, ierr)
2232 if (dset_exists) then
2233 ! Openr the data set
2234 call h5dopen_f(this%active_group_id, trim(data_name), dset_id, ierr)
2235 ! Get the current rank of the of the dataset
2236 call h5dget_space_f(dset_id, filespace, ierr)
2237 call h5sget_simple_extent_ndims_f(filespace, temprank, ierr)
2238 if (temprank .ne. 2) then
2239 call neko_error("Dataset " // trim(data_name) // &
2240 " is not a rank 2 matrix in file " // trim(this%get_fname()))
2241 end if
2242 ! Get the current shape and close the filespace
2243 call h5sget_simple_extent_dims_f(filespace, tempddims, tempmaxddims, &
2244 ierr)
2245 call h5sclose_f(filespace, ierr)
2246 else
2247 call neko_error("Dataset " // trim(data_name) &
2248 // " does not exist in current group " // &
2249 trim(this%get_fname()))
2250 end if
2251
2252 ! =============================
2253 ! Perform the data distribution
2254 ! =============================
2255 total_count = int(tempddims(2))
2256 if (strategy_ .eq. "linear") then
2257 dist = linear_dist_t(total_count, pe_rank, pe_size, neko_comm)
2258 counts = dist%num_local()
2259 offset = 0
2260 else if (strategy_ .eq. "rank_0") then
2261 if (pe_rank .eq. 0) then
2262 counts = total_count
2263 else
2264 counts = 0
2265 end if
2266 offset = 0
2267 end if
2268 call mpi_scan(counts, offset, 1, mpi_integer, &
2269 mpi_sum, neko_comm, ierr)
2270 offset = offset - counts ! Not using exclusive scan
2271
2272 ! ===========================
2273 ! Set up reading the data set
2274 ! ===========================
2275 dset_rank = 2 ! rank 2 array, i.e. a matrix
2276 dcount = [int(tempddims(1), hsize_t), int(counts, hsize_t)] ! local size
2277 doffset = [0_hsize_t, int(offset, hsize_t)] ! offset in the global matrix
2278 ! Get the total file space (shape) of the data set
2279 call h5dget_space_f(dset_id, filespace, ierr)
2280 ! Get only the slice where my rank reads
2281 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, doffset, dcount, &
2282 ierr)
2283 ! Create the corresponding memory space (buffer) for my local data
2284 call h5screate_simple_f(dset_rank, dcount, memspace, ierr)
2285
2286 ! =============================
2287 ! Allocate data. HDF5 will cast
2288 ! =============================
2289 call mat%init(int(tempddims(1)), counts, trim(data_name)) ! this is rp
2290 call h5dread_f(dset_id, precision_hdf, mat%x, dcount, ierr, &
2291 file_space_id = filespace, mem_space_id = memspace, &
2292 xfer_prp = xf_id)
2293
2294 ! =======================
2295 ! Clean up
2296 ! =======================
2297 call h5pclose_f(xf_id, ierr)
2298 call h5sclose_f(memspace, ierr)
2299 call h5sclose_f(filespace, ierr)
2300 call h5dclose_f(dset_id, ierr)
2301
2302
2303 end subroutine hdf5_file_read_matrix
2304
2305 !> Write an integer attribute
2306 subroutine hdf5_file_write_int_attribute(this, attr_name, attr)
2307 class(hdf5_file_t), intent(inout) :: this
2308 character(len=*), intent(in) :: attr_name
2309 integer, intent(in) :: attr
2310 integer :: ierr
2311 integer(hid_t) :: filespace, attr_id
2312 integer(hsize_t), dimension(1) :: dcount
2313 logical :: attr_exists
2314
2315 ! ====================
2316 ! Create the attribute
2317 ! ====================
2318 dcount = [int(1, hsize_t)]
2319 call h5aexists_f(this%active_group_id, trim(attr_name), attr_exists, ierr)
2320 if (attr_exists) then
2321 ! retrieve the attr id for the existing attribute
2322 call h5aopen_f(this%active_group_id, trim(attr_name), attr_id, ierr)
2323 else
2324 ! create file space of this shape
2325 call h5screate_f(h5s_scalar_f, filespace, ierr)
2326 ! create the data set with the given shape
2327 call h5acreate_f(this%active_group_id, trim(attr_name), &
2328 h5t_native_integer, &
2329 filespace, attr_id, ierr, h5p_default_f, h5p_default_f)
2330 call h5sclose_f(filespace, ierr)
2331 end if
2332
2333 ! ===========================
2334 ! Set up writing the data set
2335 ! ===========================
2336 call h5awrite_f(attr_id, h5t_native_integer, attr, dcount, ierr)
2337
2338 ! =======================
2339 ! Clean up
2340 ! =======================
2341 call h5aclose_f(attr_id, ierr)
2342
2343 end subroutine hdf5_file_write_int_attribute
2344
2345 !> Write a real (kind=rp) attribute
2346 subroutine hdf5_file_write_rp_attribute(this, attr_name, attr)
2347 class(hdf5_file_t), intent(inout) :: this
2348 character(len=*), intent(in) :: attr_name
2349 real(kind=rp), intent(in) :: attr
2350 integer :: ierr
2351 integer(hid_t) :: precision_hdf
2352 integer(hid_t) :: filespace, attr_id
2353 integer(hsize_t), dimension(1) :: dcount
2354 logical :: attr_exists
2355
2356 ! Get the precision
2357 precision_hdf = h5kind_to_type(rp, h5_real_kind)
2358
2359 ! ====================
2360 ! Create the attribute
2361 ! ====================
2362 dcount = [int(1, hsize_t)]
2363 call h5aexists_f(this%active_group_id, trim(attr_name), attr_exists, ierr)
2364 if (attr_exists) then
2365 ! retrieve the attr id for the existing attribute
2366 call h5aopen_f(this%active_group_id, trim(attr_name), attr_id, ierr)
2367 else
2368 ! create file space of this shape
2369 call h5screate_f(h5s_scalar_f, filespace, ierr)
2370 ! create the data set with the given shape
2371 call h5acreate_f(this%active_group_id, trim(attr_name), precision_hdf, &
2372 filespace, attr_id, ierr, h5p_default_f, h5p_default_f)
2373 call h5sclose_f(filespace, ierr)
2374 end if
2375
2376 ! ===========================
2377 ! Set up writing the data set
2378 ! ===========================
2379 call h5awrite_f(attr_id, precision_hdf, attr, dcount, ierr)
2380
2381 ! =======================
2382 ! Clean up
2383 ! =======================
2384 call h5aclose_f(attr_id, ierr)
2385
2386 end subroutine hdf5_file_write_rp_attribute
2387
2388 !> Read an integer attribute
2389 subroutine hdf5_file_read_int_attribute(this, attr_name, attr, attr_exists)
2390 class(hdf5_file_t), intent(inout) :: this
2391 character(len=*), intent(in) :: attr_name
2392 integer, intent(inout) :: attr
2393 logical, intent(inout) :: attr_exists
2394 integer :: ierr
2395 integer(hid_t) :: filespace, attr_id
2396 integer(hsize_t), dimension(1) :: dcount
2397
2398 ! ====================
2399 ! Create the attribute
2400 ! ====================
2401 dcount = [int(1, hsize_t)]
2402 call h5aexists_f(this%active_group_id, trim(attr_name), attr_exists, ierr)
2403 if (attr_exists) then
2404 ! retrieve the attr id for the existing attribute
2405 call h5aopen_f(this%active_group_id, trim(attr_name), attr_id, ierr)
2406 else
2407 return
2408 end if
2409
2410 ! ===========================
2411 ! Set up writing the data set
2412 ! ===========================
2413 call h5aread_f(attr_id, h5t_native_integer, attr, dcount, ierr)
2414
2415 ! =======================
2416 ! Clean up
2417 ! =======================
2418 call h5aclose_f(attr_id, ierr)
2419
2420 end subroutine hdf5_file_read_int_attribute
2421
2422 !> Read a real (kind=rp) attribute
2423 subroutine hdf5_file_read_rp_attribute(this, attr_name, attr, attr_exists)
2424 class(hdf5_file_t), intent(inout) :: this
2425 character(len=*), intent(in) :: attr_name
2426 real(kind=rp), intent(inout) :: attr
2427 logical, intent(inout) :: attr_exists
2428 integer :: ierr
2429 integer(hid_t) :: precision_hdf
2430 integer(hid_t) :: filespace, attr_id
2431 integer(hsize_t), dimension(1) :: dcount
2432
2433 ! Get the precision
2434 precision_hdf = h5kind_to_type(rp, h5_real_kind)
2435
2436 ! ====================
2437 ! Create the attribute
2438 ! ====================
2439 dcount = [int(1, hsize_t)]
2440 call h5aexists_f(this%active_group_id, trim(attr_name), attr_exists, ierr)
2441 if (attr_exists) then
2442 ! retrieve the attr id for the existing attribute
2443 call h5aopen_f(this%active_group_id, trim(attr_name), attr_id, ierr)
2444 else
2445 return
2446 end if
2447
2448 ! ===========================
2449 ! Set up writing the data set
2450 ! ===========================
2451 call h5aread_f(attr_id, precision_hdf, attr, dcount, ierr)
2452
2453 ! =======================
2454 ! Clean up
2455 ! =======================
2456 call h5aclose_f(attr_id, ierr)
2457
2458 end subroutine hdf5_file_read_rp_attribute
2459
2460#else
2461
2462 !> Open a HDF5 file in a given mode
2463 subroutine hdf5_file_open(this, mode)
2464 class(hdf5_file_t), intent(inout) :: this
2465 character(len=1), intent(in) :: mode
2466 call neko_error('Neko needs to be built with HDF5 support')
2467 end subroutine hdf5_file_open
2468
2469 !> Close the file
2470 subroutine hdf5_file_close(this)
2471 class(hdf5_file_t), intent(inout) :: this
2472 call neko_error('Neko needs to be built with HDF5 support')
2473 end subroutine hdf5_file_close
2474
2475 !> Set the active group for HDF5 files
2476 subroutine hdf5_file_set_group(this, group_name_path)
2477 class(hdf5_file_t), intent(inout) :: this
2478 character(len=*), intent(in), optional :: group_name_path
2479 call neko_error('Neko needs to be built with HDF5 support')
2480 end subroutine hdf5_file_set_group
2481
2482 !> Write data in HDF5 format.
2483 !! @param data Data object to write.
2484 !! @param[optional] t Simulation time.
2485 subroutine hdf5_file_write(this, data, t)
2486 class(hdf5_file_t), intent(inout) :: this
2487 class(*), target, intent(in) :: data
2488 real(kind=dp), intent(in), optional :: t
2489 call neko_error('Neko needs to be built with HDF5 support')
2490 end subroutine hdf5_file_write
2491
2492 !> Read data in HDF5 format.
2493 !! @param data Data object to populate.
2494 subroutine hdf5_file_read(this, data)
2495 class(hdf5_file_t) :: this
2496 class(*), target, intent(inout) :: data
2497 call neko_error('Neko needs to be built with HDF5 support')
2498 end subroutine hdf5_file_read
2499
2500 subroutine hdf5_file_write_dataset(this, data, name, layout)
2501 class(hdf5_file_t), intent(inout) :: this
2502 class(*), intent(inout) :: data
2503 character(len=*), intent(in), optional :: name
2504 type(hdf5_layout_t), intent(in), optional :: layout
2505 call neko_error('Neko needs to be built with HDF5 support')
2506 end subroutine hdf5_file_write_dataset
2507
2508 subroutine hdf5_file_read_dataset(this, data_name, data, strategy)
2509 class(hdf5_file_t), intent(inout) :: this
2510 character(len=*), intent(in) :: data_name
2511 class(*), intent(inout) :: data
2512 character(len=*), intent(in), optional :: strategy
2513 call neko_error('Neko needs to be built with HDF5 support')
2514 end subroutine hdf5_file_read_dataset
2515
2516 subroutine hdf5_file_write_attribute(this, data_name, data)
2517 class(hdf5_file_t), intent(inout) :: this
2518 character(len=*), intent(in) :: data_name
2519 class(*), intent(inout) :: data
2520 call neko_error('Neko needs to be built with HDF5 support')
2521 end subroutine hdf5_file_write_attribute
2522
2523 subroutine hdf5_file_read_attribute(this, data_name, data, exist)
2524 class(hdf5_file_t), intent(inout) :: this
2525 character(len=*), intent(in) :: data_name
2526 class(*), intent(inout) :: data
2527 logical, intent(inout) :: exist
2528 call neko_error('Neko needs to be built with HDF5 support')
2529 end subroutine hdf5_file_read_attribute
2530
2531 subroutine hdf5_file_write_array(this, values, name, layout)
2532 class(hdf5_file_t), intent(inout) :: this
2533 real(kind=rp), intent(in) :: values(:)
2534 character(len=*), intent(in) :: name
2535 type(hdf5_layout_t), intent(in), optional :: layout
2536 call neko_error('Neko needs to be built with HDF5 support')
2537 end subroutine hdf5_file_write_array
2538
2539 subroutine hdf5_file_write_vector(this, vec, layout)
2540 class(hdf5_file_t), intent(inout) :: this
2541 type(vector_t), intent(inout) :: vec
2542 type(hdf5_layout_t), intent(in), optional :: layout
2543 call neko_error('Neko needs to be built with HDF5 support')
2544 end subroutine hdf5_file_write_vector
2545
2546 subroutine hdf5_file_write_matrix(this, mat, layout)
2547 class(hdf5_file_t), intent(inout) :: this
2548 type(matrix_t), intent(inout) :: mat
2549 type(hdf5_layout_t), intent(in), optional :: layout
2550 call neko_error('Neko needs to be built with HDF5 support')
2551 end subroutine hdf5_file_write_matrix
2552
2553 subroutine hdf5_file_write_field(this, fld)
2554 class(hdf5_file_t), intent(inout) :: this
2555 type(field_t), intent(inout) :: fld
2556 call neko_error('Neko needs to be built with HDF5 support')
2557 end subroutine hdf5_file_write_field
2558
2559 subroutine hdf5_file_read_vector(this, data_name, vec, strategy)
2560 class(hdf5_file_t) :: this
2561 character(len=*), intent(in) :: data_name
2562 type(vector_t), intent(inout) :: vec
2563 character(len=*), intent(in), optional :: strategy
2564 call neko_error('Neko needs to be built with HDF5 support')
2565 end subroutine hdf5_file_read_vector
2566
2567 subroutine hdf5_file_read_matrix(this, data_name, mat, strategy)
2568 class(hdf5_file_t) :: this
2569 character(len=*), intent(in) :: data_name
2570 type(matrix_t), intent(inout) :: mat
2571 character(len=*), intent(in), optional :: strategy
2572 call neko_error('Neko needs to be built with HDF5 support')
2573 end subroutine hdf5_file_read_matrix
2574
2575 subroutine hdf5_file_write_int_attribute(this, attr_name, attr)
2576 class(hdf5_file_t), intent(inout) :: this
2577 character(len=*), intent(in) :: attr_name
2578 integer, intent(in) :: attr
2579 call neko_error('Neko needs to be built with HDF5 support')
2580 end subroutine hdf5_file_write_int_attribute
2581
2582 subroutine hdf5_file_write_rp_attribute(this, attr_name, attr)
2583 class(hdf5_file_t), intent(inout) :: this
2584 character(len=*), intent(in) :: attr_name
2585 real(kind=rp), intent(in) :: attr
2586 call neko_error('Neko needs to be built with HDF5 support')
2587 end subroutine hdf5_file_write_rp_attribute
2588
2589 subroutine hdf5_file_read_int_attribute(this, attr_name, attr, attr_exists)
2590 class(hdf5_file_t), intent(inout) :: this
2591 character(len=*), intent(in) :: attr_name
2592 integer, intent(inout) :: attr
2593 logical, intent(inout) :: attr_exists
2594 call neko_error('Neko needs to be built with HDF5 support')
2595 end subroutine hdf5_file_read_int_attribute
2596
2597 subroutine hdf5_file_read_rp_attribute(this, attr_name, attr, attr_exists)
2598 class(hdf5_file_t), intent(inout) :: this
2599 character(len=*), intent(in) :: attr_name
2600 real(kind=rp), intent(inout) :: attr
2601 logical, intent(inout) :: attr_exists
2602 call neko_error('Neko needs to be built with HDF5 support')
2603 end subroutine hdf5_file_read_rp_attribute
2604
2605#endif
2606
2607end module hdf5_file
double real
Format-independent checkpoint payloads.
Defines format-independent checkpoint registration and restart state.
Definition comm.F90:1
type(mpi_datatype), public mpi_real_precision
MPI type for working precision of REAL types.
Definition comm.F90:54
integer, public pe_size
MPI size of communicator.
Definition comm.F90:62
integer, public pe_rank
MPI rank.
Definition comm.F90:59
type(mpi_comm), public neko_comm
MPI communicator.
Definition comm.F90:46
Defines practical data distributions.
Definition datadist.f90:34
Defines a mapping of the degrees of freedom.
Definition dofmap.f90:35
Contains the field_serties_t type.
Defines a field.
Definition field.f90:34
Module for file I/O operations.
Definition file.f90:34
Implements global_interpolation given a dofmap.
HDF5 file format.
Definition hdf5_file.F90:34
subroutine hdf5_file_open(this, mode)
Open a HDF5 file in a given mode.
subroutine hdf5_file_write_vector(this, vec, layout)
subroutine hdf5_file_free(this)
Release an open file and its retained dataset resources.
subroutine hdf5_file_write_field(this, fld)
subroutine hdf5_file_read(this, data)
Read data in HDF5 format.
subroutine hdf5_file_write(this, data, t)
Write data in HDF5 format.
subroutine hdf5_file_read_dataset(this, data_name, data, strategy)
subroutine hdf5_file_write_int_attribute(this, attr_name, attr)
subroutine hdf5_file_write_attribute(this, data_name, data)
subroutine hdf5_file_read_vector(this, data_name, vec, strategy)
subroutine hdf5_file_read_matrix(this, data_name, mat, strategy)
subroutine hdf5_file_read_int_attribute(this, attr_name, attr, attr_exists)
subroutine hdf5_file_write_rp_attribute(this, attr_name, attr)
subroutine hdf5_file_set_precision(this, precision)
Set the precision for the output (single or double).
character(len=1024) function hdf5_file_get_next_output_fname(this)
Get the physical file name generated by the next write.
subroutine hdf5_file_read_rp_attribute(this, attr_name, attr, attr_exists)
subroutine hdf5_file_read_attribute(this, data_name, data, exist)
subroutine hdf5_file_close(this)
Close the file.
subroutine hdf5_file_write_matrix(this, mat, layout)
subroutine hdf5_file_write_dataset(this, data, name, layout)
subroutine hdf5_file_set_group(this, group_name_path)
Set the active group for HDF5 files.
subroutine hdf5_layout_init(this, local_count)
Calculate a reusable layout for distributed array writes.
subroutine hdf5_file_write_array(this, values, name, layout)
subroutine hdf5_checkpoint_layout_free(this)
Release the interpolation state and the function space of a layout. The mesh is only referenced; it b...
subroutine hdf5_file_open_new(this)
Open a new numbered file without modifying any existing file. Uses the inherited filename and counter...
subroutine hdf5_file_set_overwrite(this, overwrite)
Set the overwrite flag for HDF5 files.
HDF5 session management.
subroutine, public hdf5_session_init
Initialise the global HDF5 session HDF5 does not maintain a global reference count,...
subroutine, public hdf5_session_finalize
Finalize the global HDF5 session.
Module containing host-only array type.
Routines to interpolate between different spaces.
Logging routines.
Definition log.f90:34
integer, parameter, public neko_log_debug
Debug.
Definition log.f90:56
type(log_t), public neko_log
Global log stream.
Definition log.f90:92
integer, parameter, public log_size
Definition log.f90:46
Definition math.F90:60
subroutine, public rzero(a, n)
Zero a real vector.
Definition math.F90:249
Defines a matrix.
Definition matrix.f90:34
Defines a mesh.
Definition mesh.f90:34
integer, parameter, public i8
Definition num_types.f90:7
integer, parameter, public dp
Definition num_types.f90:10
integer, parameter, public sp
Definition num_types.f90:8
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Defines a function space.
Definition space.f90:34
integer, parameter, public gll
Definition space.f90:50
Utilities.
Definition utils.f90:35
subroutine, public neko_warning(warning_msg)
Reports a warning to standard output.
Definition utils.f90:454
pure integer function, public filename_suffix_pos(fname)
Find position (in the string) of a filename's suffix.
Definition utils.f90:113
Defines a vector.
Definition vector.f90:34
Collection of live simulation data registered for checkpointing.
A named real array and its selection in a global checkpoint dataset.
A named nodal real array distributed over mesh elements.
A named collection of live fields to checkpoint together.
Load-balanced linear distribution .
Definition datadist.f90:50
field_ptr_t, To easily obtain a pointer to a field
Definition field.f90:83
field_list_t, To be able to group fields together
A wrapper for a pointer to a field_series_t.
Stores a series (sequence) of fields, logically connected to a base field, and arranged according to ...
A generic file handler.
Implements global interpolation for arbitrary points in the domain.
Where the data in a checkpoint file sits and how it maps onto the running case. Built once per read a...
Dataset resources retained while the active group remains open.
Definition hdf5_file.F90:89
Interface for HDF5 files.
Reusable distribution of a rank-ordered host array over NEKO_COMM. Counts are vector elements or matr...
Definition hdf5_file.F90:73
Host-only temporary array.
Interpolation between two space::space_t.
The function space for the SEM solution fields.
Definition space.f90:64
#define max(a, b)
Definition tensor.cu:40