OGS
fileIO.cpp
Go to the documentation of this file.
1
15
16#include <hdf5.h>
17#include <mpi.h>
18
19#include "BaseLib/Logging.h"
20#include "getCommunicator.h"
21
22namespace MeshLib::IO
23{
24std::filesystem::path partitionFilename(
25 std::filesystem::path const& basic_filepath, int const file_group)
26{
27 std::string const filename = (file_group > 0)
28 ? basic_filepath.stem().string() + '_' +
29 std::to_string(file_group) +
30 basic_filepath.extension().string()
31 : basic_filepath.filename().string();
32 std::filesystem::path const filepathwithextension =
33 basic_filepath.parent_path() / filename;
34 DBUG("HDF Filepath: {:s}.", filepathwithextension.string());
35 return filepathwithextension;
36};
37
38hid_t createFile(std::filesystem::path const& filepath,
39 unsigned int const n_files)
40{
41 auto const communicator = getCommunicator(n_files);
42 MPI_Comm const comm = communicator.mpi_communicator;
43 MPI_Info const info = MPI_INFO_NULL;
44 hid_t const plist_id = H5Pcreate(H5P_FILE_ACCESS);
45
46 H5Pset_fapl_mpio(plist_id, comm, info);
47 H5Pset_coll_metadata_write(plist_id, true);
48
49 std::filesystem::path const partition_filename =
50 partitionFilename(filepath, communicator.color);
51 hid_t file = H5Fcreate(partition_filename.string().c_str(), H5F_ACC_TRUNC,
52 H5P_DEFAULT, plist_id);
53 H5Pclose(plist_id);
54
55 return file;
56}
57
58hid_t openHDF5File(std::filesystem::path const& filepath,
59 unsigned int const n_files)
60{
61 MPI_Comm const comm = getCommunicator(n_files).mpi_communicator;
62 MPI_Info info = MPI_INFO_NULL;
63 hid_t const plist_id = H5Pcreate(H5P_FILE_ACCESS);
64 H5Pset_fapl_mpio(plist_id, comm, info);
65 hid_t file = H5Fopen(filepath.string().c_str(), H5F_ACC_RDWR, plist_id);
66 H5Pclose(plist_id);
67 return file;
68}
69
71{
72 // property list for collective dataset write
73 hid_t io_transfer_property = H5Pcreate(H5P_DATASET_XFER);
74 H5Pset_dxpl_mpio(io_transfer_property, H5FD_MPIO_COLLECTIVE);
75 return io_transfer_property;
76}
77
78} // namespace MeshLib::IO
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Dispatches HDF5 functions specific to execution platform (w/o MPI). There are multiple implementation...
Assigns to each MPI communicator an output file name by attribute color There are multiple implementa...
std::filesystem::path partitionFilename(std::filesystem::path const &basic_filepath, int const file_group)
Definition fileIO.cpp:24
FileCommunicator getCommunicator(unsigned int const n_files)
int64_t createHDF5TransferPolicy()
Definition fileIO.cpp:70
int64_t openHDF5File(std::filesystem::path const &filepath, unsigned int n_files)
Definition fileIO.cpp:58
int64_t createFile(std::filesystem::path const &filepath, unsigned int n_files)
Definition fileIO.cpp:38