Skip to content

AlynJ/HDF5_Fortran_API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HDF5 Fortran API

A Fortran library which handles the interface to HDF5 library to give simpler I/O proceedures usable within Fortran coding projects.

The modhdf5.f90 module contains subroutines which interface with the HDF5 library so that you do not need to use it directly in your code. You only need to call upon this module to perform the I/O to the .h5 file.

You can look at examples of how this module is used within the ./tests/testio.f90 code.

Author: Alyn D. N. James

Version: 0.1.1

How to use

To add this to your Fortran code, add this module .f90 file to the compilation (cmake, make and so on) and load the public subroutines module in your Fortran code via

use modhdf5

The .inc files will needed to be in the same directory as the .f90 file, but these will automatically be included during the compilation. (These .inc files are used to reduce the amount of repetative code.)

This module uses macro preprocessors to be compatible with both the serial and parallel HDF5 library. However, to enable the (MPI) parallel HDF5 capabilities in modhdf5, you need to add the -DMPI_modhdf5 flag to the make compilation step. This is done within cmake as seen in the Tests section below.

Dependencies

Here is the list of libraries required for the modhdf5 module (with the version it was tested with):

  • HDF5 library (1.14.6)
  • Fortran 2018+ (gfortran 15.1.0 and ifx 2025.3.0)
  • MPI (for parallel enabled version) (Openmpi 5.0.8)

For the repository, the following additional packages (with the version it was tested with) are needed:

  • cmake (3.22.1)

Documentation

This API has been written as a single module to be added to Fortran code bases to concentrate the h5 I/O in one (relatively compact) module (modhdf5). Within modhdf5, the public subroutines to be used in your code are given at the top of the file. All the global variables are commented, and the inputs and outputs of all of the subroutines are labelled. The subroutines are named to be self-explanatory (some have short doc-strings for clarity).

The modhdf5 module has the following capabilities:

  • Read and write hdf5 datasets and hyperslice/hyperslab datasets for double, real, integer and character variables of ranks 0 to 7.
  • Read and write hdf5 double, real, integer and character attributes of ranks 0 to 7.
  • Compatible with serial and (MPI) parallel HDF5 configured libraries (i.e., this module is capable to read/write h5 files in serial and parallel).
  • Writes datasets and/or their attributes to either the file or to a group.

Tests

This repository uses the naturalFRUIT v0.7.4 .f90 code for constructing the test runner for testing the functionality of the code base. Credit goes to the developers for naturalfruit.f90.

Please look at the .github/workflows/ci.yml to see how modhdf5 has been compiled in different setups.

Serial Tests

Compile the test runner (within GCC+Openmpi) within test/, run

cmake -S . -DCMAKE_Fortran_COMPILER=mpif90 -B build

For conda environment, use

cmake -S . -DCMAKE_Fortran_COMPILER=mpif90 -DCMAKE_PREFIX_PATH=$CONDA_PREFIX -B build

run the test suite within build via

./build/h5testrunner

(MPI) Parallel Tests

Compile the test runner (within GCC+Openmpi) within test/, run

cmake -S . -DCMAKE_Fortran_COMPILER=mpif90 -DMPI_VERSION=ON -B build

For conda environment, use

cmake -S . -DCMAKE_Fortran_COMPILER=mpif90 -DMPI_VERSION=ON -DCMAKE_PREFIX_PATH=$CONDA_PREFIX -B build

run the test suite via

mpirun -np 2 ./build/h5testrunner_mpi

Developer notes

This modhdf5 has been written for HDF5 version 1.x.y. The additional functionality of HDF5 2.x.y has not been included.

modhdf5 uses polymorphic, assumed-rank variable inputs to keep the interface generic. The variable rank and type are resolved within the read/write subroutines to interface with the HDF5 library Fortran APIs. modhdf5 currently sticks to the standard H5 native types available within the HDF5 library. These are:

  • H5T_NATIVE_INTEGER
  • H5T_NATIVE_REAL
  • H5T_NATIVE_DOUBLE
  • H5T_FORTRAN_S1 (modhdf5 extends this to a custom n-sized character type to handle aribitary sized character variable.)

It is suggested for users to resolve their data types to conform with these native types and add the Fortran data type it was originally to the fortran_type attribute (via fdtype_name subroutine input/output) so that other users know its origin.

About

A Fortran library which handles the interface to HDF5 library to give simpler I/O proceedures usable within Fortran coding projects

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors