Skip to content

Commit

Permalink
[cpp] Cleanup HDF5 reader & writer
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Sep 25, 2024
1 parent 9f0ec5b commit 678f5ce
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ auto main(int argc, char** argv) -> int {
auto const sim = pffdtd::loadSimulation2D(simDir);
auto const out = engine(sim);

auto results = pffdtd::H5FWriter{simDir / args.sim2d.out};
auto results = pffdtd::HDF5Writer{simDir / args.sim2d.out};
results.write("out", out);

auto const stop = pffdtd::getTime();
Expand Down
21 changes: 13 additions & 8 deletions src/cpp/pffdtd/hdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ inline constexpr auto isStdVector = false;
template<typename T>
inline constexpr auto isStdVector<std::vector<T>> = true;

struct H5FReader {
explicit H5FReader(std::filesystem::path const& path)
: _handle{H5Fopen(path.string().c_str(), H5F_ACC_RDONLY, H5P_DEFAULT)} {}
struct HDF5Reader {
explicit HDF5Reader(std::filesystem::path const& path)
: _handle{[&] {
if (not std::filesystem::exists(path)) {
raisef<std::invalid_argument>("file '{}' does not exist", path.string());
}
return H5Fopen(path.string().c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
}()} {}

~H5FReader() { H5Fclose(_handle); }
~HDF5Reader() { H5Fclose(_handle); }

[[nodiscard]] auto handle() const noexcept -> hid_t { return _handle; }

Expand Down Expand Up @@ -131,11 +136,11 @@ struct H5FReader {
hid_t _handle;
};

struct H5FWriter {
explicit H5FWriter(std::filesystem::path const& path)
struct HDF5Writer {
explicit HDF5Writer(std::filesystem::path const& path)
: _handle{H5Fcreate(path.string().c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)} {}

~H5FWriter() { H5Fclose(_handle); }
~HDF5Writer() { H5Fclose(_handle); }

auto write(char const* name, stdex::mdspan<double const, stdex::dextents<size_t, 2>> buf) -> void {
hsize_t dims[2]{
Expand Down Expand Up @@ -170,7 +175,7 @@ struct H5FWriter {
};

template<typename T>
[[nodiscard]] auto read(H5FReader& reader, char const* dset_str, int ndims, hsize_t* dims) -> std::unique_ptr<T[]> {
[[nodiscard]] auto read(HDF5Reader& reader, char const* dset_str, int ndims, hsize_t* dims) -> std::unique_ptr<T[]> {
auto dset = H5Dopen(reader.handle(), dset_str, H5P_DEFAULT);
auto dspace = H5Dget_space(dset);
PFFDTD_ASSERT(H5Sget_simple_extent_ndims(dspace) == ndims);
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/pffdtd/simulation_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace pffdtd {

auto loadSimulation2D(std::filesystem::path const& dir) -> Simulation2D {
auto sim = H5FReader{dir / "sim.h5"};
auto sim = HDF5Reader{dir / "sim.h5"};

auto const Nx = sim.read<int64_t>("Nx");
auto const Ny = sim.read<int64_t>("Ny");
Expand Down
30 changes: 5 additions & 25 deletions src/cpp/pffdtd/simulation_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ template<typename Real>
////////////////////////////////////////////////////////////////////////
// Read constants HDF5 dataset
////////////////////////////////////////////////////////////////////////
auto filename = simDir / "constants.h5";
if (not std::filesystem::exists(filename)) {
raisef<std::invalid_argument>("file '{}' does not exist", filename.string());
}

auto constants = H5FReader{filename};
auto constants = HDF5Reader{simDir / "constants.h5"};

//////////////////
// constants
Expand Down Expand Up @@ -147,12 +142,7 @@ template<typename Real>
////////////////////////////////////////////////////////////////////////
// Read vox HDF5 dataset
////////////////////////////////////////////////////////////////////////
filename = simDir / "vox_out.h5";
if (not std::filesystem::exists(filename)) {
raisef<std::invalid_argument>("file '{}' does not exist", filename.string());
}

auto vox_out = H5FReader{filename};
auto vox_out = HDF5Reader{simDir / "vox_out.h5"};

//////////////////
// integers
Expand Down Expand Up @@ -206,12 +196,7 @@ template<typename Real>
////////////////////////////////////////////////////////////////////////
// Read signals HDF5 dataset
////////////////////////////////////////////////////////////////////////
filename = simDir / "signals.h5";
if (not std::filesystem::exists(filename)) {
raisef<std::invalid_argument>("file '{}' does not exist", filename.string());
}

auto signals = H5FReader{filename};
auto signals = HDF5Reader{simDir / "signals.h5"};

//////////////////
// integers
Expand Down Expand Up @@ -254,12 +239,7 @@ template<typename Real>
////////////////////////////////////////////////////////////////////////
// Read materials HDF5 dataset
////////////////////////////////////////////////////////////////////////
filename = simDir / "materials.h5";
if (not std::filesystem::exists(filename)) {
raisef<std::invalid_argument>("file '{}' does not exist", filename.string());
}

auto materials = H5FReader{filename};
auto materials = HDF5Reader{simDir / "materials.h5"};

//////////////////
// integers
Expand Down Expand Up @@ -539,7 +519,7 @@ void writeOutputs_impl(Simulation3D<Real> const& sim, std::filesystem::path cons
}
}

auto writer = H5FWriter{simDir / "sim_outs.h5"};
auto writer = HDF5Writer{simDir / "sim_outs.h5"};
writer.write("u_out", u_out);
std::puts("wrote output dataset");
}
Expand Down

0 comments on commit 678f5ce

Please sign in to comment.