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