Skip to content

Commit

Permalink
Add a very simple hdf5 exercise for exploration
Browse files Browse the repository at this point in the history
  • Loading branch information
hokkanen committed Apr 15, 2024
1 parent d4c9b64 commit db93618
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 1 deletion.
3 changes: 2 additions & 1 deletion parallel-io/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Parallel I/O exercises

- [Parallel I/O with Posix](posix)
- [HDF5 example](hdf5)
- [HDF5-writerank](hdf5-writerank)
- [HDF5-write-read](hdf5-writeread)

File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added parallel-io/hdf5-write-read/solution/data.h5
Binary file not shown.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions parallel-io/hdf5-writerank/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## HDF5-writerank exercise

Explore the HDF5 codes ([hdf5-writerank.c](hdf5-writerank.c) or [hdf5-writerank.f90](hdf5-writerank.f90)) and try to compile them.

On Lumi, you will need to load the module `cray-hdf5-parallel` before you
can compile the code:

```
module load cray-hdf5-parallel
```

After compiling, try running the program with some number of MPI ranks. After this, try using the `h5dump` and `h5ls` commands to check the values in the HDF5 file that is produced by running the example. Do you understand what the program does and what are the values in the file (as shown by `h5dump`)?
42 changes: 42 additions & 0 deletions parallel-io/hdf5-writerank/hdf5-writerank.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
program hdf5_writerank
use mpi
use hdf5
implicit none
! Declare vars
integer :: err, myproc, numprocs
integer(hid_t) :: plist, file, dataspace, dataset, memspace
integer(hsize_t) :: counts(1)
! Initialize MPI
call mpi_init(err)
call mpi_comm_rank(MPI_COMM_WORLD, myproc, err)
call mpi_comm_size(MPI_COMM_WORLD, numprocs, err)

! Initialize Fortran HDF5 interface
call h5open_f(err)
! Create a new property list for file acccess
call h5pcreate_f(H5P_FILE_ACCESS_F, plist, err)
! Store MPI IO communicator info to the file access property list
call h5pset_fapl_mpio_f(plist, MPI_COMM_WORLD, MPI_INFO_NULL, err)
! Create a new HDF5 file named "parallel_out.h5"
call h5fcreate_f("parallel_out.h5", H5F_ACC_TRUNC_F, file, err, access_prp=plist)
! Create a new simple dataspace for the file and open for access
call h5screate_simple_f(1, int([numprocs], hsize_t), dataspace, err)
! creates a new dataset named "MPI_RANKS" for 'file'
call h5dcreate_f(file, "MPI_RANKS", H5T_NATIVE_INTEGER, dataspace, dataset, err)
! Number of blocks to be included in the hyperslab region
counts(1) = 1
! Select a hyperslab region of the file dataspace
call h5sselect_hyperslab_f(dataspace, H5S_SELECT_SET_F, int([myproc], hsize_t), counts, err)
! Create a new simple dataspace for the memory buffer and open for access
call h5screate_simple_f(1, counts, memspace, err)
! Each rank writes its own rank number (partially constructing 'dataset') into a file
call h5dwrite_f(dataset, H5T_NATIVE_INTEGER, [myproc], int([numprocs], hsize_t), err, memspace, dataspace, H5P_DEFAULT_F)

! Close all handles
call h5dclose_f(dataset, err)
call h5sclose_f(dataspace, err)
call h5fclose_f(file, err)
call h5pclose_f(plist, err)
call h5close_f(err)
call mpi_finalize(err)
end program hdf5_writerank
38 changes: 38 additions & 0 deletions parallel-io/hdf5-writerank/hdf5-writerank.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "hdf5.h"
#include <mpi.h>

int main(int argc, char **argv) {

// Initialize MPI
int myproc, numprocs;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myproc);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

// Create a new property list for file acccess
hid_t plist = H5Pcreate(H5P_FILE_ACCESS);
// Store MPI IO communicator info to the file access property list
H5Pset_fapl_mpio(plist, MPI_COMM_WORLD, MPI_INFO_NULL);
// Create a new HDF5 file named "parallel_out.h5"
hid_t file = H5Fcreate("parallel_out.h5", H5F_ACC_TRUNC, H5P_DEFAULT, plist);
// Create a new simple dataspace for the file and open for access
hid_t dataspace = H5Screate_simple(1, (const hsize_t[]){numprocs}, NULL);
// creates a new dataset named "MPI_RANKS" for 'file'
hid_t dataset = H5Dcreate(file, "MPI_RANKS", H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
// Number of blocks to be included in the hyperslab region
hsize_t count[] = {1};
// Select a hyperslab region of the file dataspace
H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, (const hsize_t[]){myproc}, NULL, count, NULL);
// Create a new simple dataspace for the memory buffer and open for access
hid_t memspace = H5Screate_simple(1, count, NULL);
// Each rank writes its own rank number (partially constructing 'dataset') into a file
H5Dwrite(dataset, H5T_NATIVE_INT, memspace, dataspace, H5P_DEFAULT, &myproc);

// Close all handles and return
H5Dclose(dataset);
H5Sclose(dataspace);
H5Fclose(file);
H5Pclose(plist);
MPI_Finalize();
return 0;
}
Binary file added parallel-io/hdf5-writerank/parallel_out.h5
Binary file not shown.

0 comments on commit db93618

Please sign in to comment.