-----------------------------------------------------------------------------
| 53 | |
| 54 | //----------------------------------------------------------------------------- |
| 55 | hid_t io::hdf5::open_file(MPI_Comm comm, const std::filesystem::path& filename, |
| 56 | const std::string& mode, bool use_mpi_io) |
| 57 | { |
| 58 | // Set parallel access with communicator |
| 59 | const hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS); |
| 60 | |
| 61 | if (use_mpi_io) |
| 62 | { |
| 63 | MPI_Info info; |
| 64 | MPI_Info_create(&info); |
| 65 | if (H5Pset_fapl_mpio(plist_id, comm, info) < 0) |
| 66 | throw std::runtime_error("Call to H5Pset_fapl_mpio unsuccessful"); |
| 67 | MPI_Info_free(&info); |
| 68 | } |
| 69 | |
| 70 | hid_t file_id = -1; |
| 71 | if (mode == "w") // Create file for write, overwriting any existing file |
| 72 | { |
| 73 | if (auto d = filename.parent_path(); !d.empty()) |
| 74 | std::filesystem::create_directories(d); |
| 75 | file_id = H5Fcreate(filename.string().c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, |
| 76 | plist_id); |
| 77 | if (file_id < 0) |
| 78 | throw std::runtime_error("Failed to create HDF5 file."); |
| 79 | } |
| 80 | else if (mode == "a") // Open file to append, creating if does not exist |
| 81 | { |
| 82 | if (std::filesystem::exists(filename)) |
| 83 | file_id = H5Fopen(filename.string().c_str(), H5F_ACC_RDWR, plist_id); |
| 84 | else |
| 85 | { |
| 86 | if (auto d = filename.parent_path(); !d.empty()) |
| 87 | std::filesystem::create_directories(d); |
| 88 | file_id = H5Fcreate(filename.string().c_str(), H5F_ACC_EXCL, H5P_DEFAULT, |
| 89 | plist_id); |
| 90 | } |
| 91 | |
| 92 | if (file_id < 0) |
| 93 | { |
| 94 | throw std::runtime_error( |
| 95 | "Failed to create/open HDF5 file (append mode)."); |
| 96 | } |
| 97 | } |
| 98 | else if (mode == "r") // Open file to read |
| 99 | { |
| 100 | if (std::filesystem::exists(filename)) |
| 101 | { |
| 102 | file_id = H5Fopen(filename.string().c_str(), H5F_ACC_RDONLY, plist_id); |
| 103 | if (file_id < 0) |
| 104 | throw std::runtime_error("Failed to open HDF5 file."); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | throw std::runtime_error("Unable to open HDF5 file. File " |
| 109 | + filename.string() + " does not exist."); |
| 110 | } |
| 111 | } |
| 112 |
nothing calls this directly
no outgoing calls
no test coverage detected