OGS
mpi/fileIO.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <hdf5.h>
7#include <mpi.h>
8
9#include "BaseLib/Logging.h"
10#include "getCommunicator.h"
11
12namespace MeshLib::IO
13{
14std::filesystem::path partitionFilename(
15 std::filesystem::path const& basic_filepath, int const file_group)
16{
17 std::string const filename = (file_group > 0)
18 ? basic_filepath.stem().string() + '_' +
19 std::to_string(file_group) +
20 basic_filepath.extension().string()
21 : basic_filepath.filename().string();
22 std::filesystem::path const filepathwithextension =
23 basic_filepath.parent_path() / filename;
24 DBUG("HDF Filepath: {:s}.", filepathwithextension.string());
25 return filepathwithextension;
26};
27
28hid_t createFile(std::filesystem::path const& filepath,
29 unsigned int const n_files)
30{
31 auto const communicator = getCommunicator(n_files);
32 MPI_Comm const comm = communicator.mpi_communicator;
33 MPI_Info const info = MPI_INFO_NULL;
34 hid_t const plist_id = H5Pcreate(H5P_FILE_ACCESS);
35
36 H5Pset_fapl_mpio(plist_id, comm, info);
37 H5Pset_coll_metadata_write(plist_id, true);
38
39 std::filesystem::path const partition_filename =
40 partitionFilename(filepath, communicator.color);
41 hid_t file = H5Fcreate(partition_filename.string().c_str(), H5F_ACC_TRUNC,
42 H5P_DEFAULT, plist_id);
43 H5Pclose(plist_id);
44
45 return file;
46}
47
48hid_t openHDF5File(std::filesystem::path const& filepath,
49 unsigned int const n_files)
50{
51 MPI_Comm const comm = getCommunicator(n_files).mpi_communicator;
52 MPI_Info info = MPI_INFO_NULL;
53 hid_t const plist_id = H5Pcreate(H5P_FILE_ACCESS);
54 H5Pset_fapl_mpio(plist_id, comm, info);
55 hid_t file = H5Fopen(filepath.string().c_str(), H5F_ACC_RDWR, plist_id);
56 H5Pclose(plist_id);
57 return file;
58}
59
61{
62 // property list for collective dataset write
63 hid_t io_transfer_property = H5Pcreate(H5P_DATASET_XFER);
64 H5Pset_dxpl_mpio(io_transfer_property, H5FD_MPIO_COLLECTIVE);
65 return io_transfer_property;
66}
67
68} // namespace MeshLib::IO
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
std::filesystem::path partitionFilename(std::filesystem::path const &basic_filepath, int const file_group)
FileCommunicator getCommunicator(unsigned int const n_files)
int64_t createHDF5TransferPolicy()
int64_t openHDF5File(std::filesystem::path const &filepath, unsigned int n_files)
int64_t createFile(std::filesystem::path const &filepath, unsigned int n_files)