parallel_hdf5.cpp (1282B)
1 // File : parallel_hdf5.cpp 2 // Description: Example parallel I/O using HDF5 and MPI 3 // To compile this code on the academic cluster load the following: 4 // 5 // module load gcc/10.2.0-fasrc01 6 // openmpi/4.1.1-fasrc01 7 // hdf5/1.12.1-fasrc01 8 // 9 // Copyright 2023 Harvard University. All Rights Reserved. 10 #include "hdf5IO.h" 11 #include "xdmf_wrapper.h" 12 #include <mpi.h> 13 #include <vector> 14 15 int main(int argc, char *argv[]) 16 { 17 MPI_Init(&argc, &argv); 18 19 int size; 20 MPI_Comm_size(MPI_COMM_WORLD, &size); 21 if (8 != size) { // need p x p x p = 8 ranks for this demo code 22 MPI_Abort(MPI_COMM_WORLD, MPI_ERR_TOPOLOGY); 23 } 24 25 // create Cartesian communicator 26 const int dims[] = {2, 2, 2}; 27 const int periods[] = {false, false, false}; 28 MPI_Comm cart_comm; 29 MPI_Cart_create(MPI_COMM_WORLD, 3, dims, periods, false, &cart_comm); 30 31 // subdomain size and data u 32 int rank; 33 MPI_Comm_rank(cart_comm, &rank); 34 constexpr int Nx = 4; 35 constexpr int Ny = 8; 36 constexpr int Nz = 16; 37 std::vector<float> u(Nx * Ny * Nz, static_cast<float>(rank)); 38 39 write_hdf5_mpi("data", u, Nx, Ny, Nz, cart_comm); 40 41 MPI_Comm_free(&cart_comm); 42 MPI_Finalize(); 43 return 0; 44 }