Skip to content

Commit

Permalink
more systematic h5 serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
philippwindischhofer committed Nov 14, 2023
1 parent 887d28e commit 00b69cb
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 14 deletions.
27 changes: 24 additions & 3 deletions include/Eisvogel/H5Serialization.hh
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
#ifndef __H5SERIALIZATION_HH
#define __H5SERIALIZATION_HH

#include "hdf5.h"
#include <string>
#include "hdf5.h"
#include "Eisvogel/H5Utils.hh"

namespace h5stor {

template <typename T>
struct Traits;

template <std::size_t n>
struct Traits<std::array<float, n>> {
using type = std::array<float, n>;

static void serialize(hid_t m_file_id, const type& val, std::string name) {
hsize_t shape[1] = {n};
H5Utils::make_and_write_dataset(m_file_id, name.c_str(), 1, shape, H5T_NATIVE_FLOAT, val.data());
}

static type deserialize(hid_t m_file_id, std::string name) {
std::array<float, n> val;
H5Utils::read_dataset(m_file_id, name.c_str(), H5T_NATIVE_FLOAT, val.data());
return val;
}
};

class H5Serializer {

public:
H5Serializer(std::string filepath) {
m_file_id = H5Fcreate(filepath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
m_file_id = H5Utils::open_or_create_file(filepath);
}

~H5Serializer() {
H5Utils::close_file(m_file_id);
}

template <typename T>
void serialize(const T& value, std::string name) {
Traits<T>::serialize(m_file_id, value, name);
Expand All @@ -24,7 +45,7 @@ namespace h5stor {
template <typename T>
T deserialize(std::string name) {
return Traits<T>::deserialize(m_file_id, name);
}
}

private:
hid_t m_file_id;
Expand Down
16 changes: 16 additions & 0 deletions include/Eisvogel/H5Utils.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __H5UTILS_HH
#define __H5UTILS_HH

#include "hdf5.h"
#include <string>
#include <filesystem>

namespace H5Utils {

hid_t open_or_create_file(std::string filepath);
herr_t close_file(hid_t file_id);
herr_t make_and_write_dataset(hid_t file_id, const char* name, int rank, const hsize_t* shape, hid_t dtype, const void* buffer);
herr_t read_dataset(hid_t file_id, const char* name, hid_t dtype, void* buffer);
}

#endif
27 changes: 23 additions & 4 deletions include/Eisvogel/NDArray.hh
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,32 @@ namespace h5stor {

static void serialize(hid_t m_file_id, const type& val, std::string name) {
std::cout << "serializing now" << std::endl;
const hsize_t* shape = val.shape().data();
hid_t dataspace_id = H5Screate_simple(dims, shape, NULL);
hid_t dataset_id = H5Dcreate2(m_file_id, name.c_str(), H5T_NATIVE_FLOAT, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
herr_t status = H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, val.m_data.data());
status = H5Sclose(dataspace_id);
status = H5Dclose(dataset_id);
}


static type deserialize(hid_t m_file_id, std::string name) {
std::cout << "deserializing now" << std::endl;
hid_t dataset_id = H5Dopen2(m_file_id, name.c_str(), H5P_DEFAULT);
hid_t dataspace_id = H5Dget_space(dataset_id);
const int ndims = H5Sget_simple_extent_ndims(dataspace_id);
std::cout << "found dataspace with " << ndims << " dimensions" << std::endl;
hsize_t read_shape[ndims];
H5Sget_simple_extent_dims(dataspace_id, read_shape, NULL);
std::cout << "have dataspace with the following shape" << std::endl;
std::cout << read_shape[0] << std::endl;
std::cout << read_shape[1] << std::endl;
// herr_t status = H5Dread(dataset_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata);
// status = H5Sclose(dataspace_id);
// status = H5Dclose(dataset_id);
}

// static type deserialize(hid_t m_file_id, std::string name) {

// }
private:

};
}

Expand Down
13 changes: 11 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_FLAGS "-O3 -ftree-vectorize -ffast-math -ftree-vectorizer-verbose=2 -funroll-loops -march=native")

find_package(HDF5 REQUIRED)

add_library(greenstor SHARED
H5Utils.cxx)
target_include_directories(greenstor PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(greenstor PUBLIC ${HDF_LIBRARIES})
target_compile_options(greenstor PUBLIC ${HDF_CFLAGS_OTHER})
set_target_properties(greenstor PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/greenstor)

add_library(eisvogel SHARED
WeightingFieldUtils.cxx
Kernels.cxx
Expand All @@ -19,14 +29,13 @@ if(BUILD_WFC)
find_package(MPI REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(MEEP REQUIRED meep)
find_package(HDF5 REQUIRED)

add_library(eisvogel_wfc SHARED
WeightingFieldCalculator.cxx
Geometry.cxx
Antenna.cxx)
target_link_directories(eisvogel_wfc PUBLIC ${MEEP_LIBRARY_DIRS} ${HDF_LIBRARY_DIRS})
target_link_libraries(eisvogel_wfc PUBLIC eisvogel ${MEEP_LIBRARIES} ${HDF_LIBRARIES} MPI::MPI_CXX)
target_link_libraries(eisvogel_wfc PUBLIC eisvogel greenstor ${MEEP_LIBRARIES} ${HDF_LIBRARIES} MPI::MPI_CXX)
target_include_directories(eisvogel_wfc PUBLIC ${PROJECT_SOURCE_DIR}/include ${MEEP_INCLUDE_DIRS} ${HDF_INCLUDE_DIRS})
target_compile_options(eisvogel_wfc PUBLIC ${MEEP_CFLAGS_OTHER} ${HDF_CFLAGS_OTHER})

Expand Down
38 changes: 38 additions & 0 deletions src/H5Utils.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "Eisvogel/H5Utils.hh"

namespace H5Utils {

hid_t open_or_create_file(std::string filepath) {
hid_t file_id;

if(std::filesystem::exists(filepath)) {
file_id = H5Fopen(filepath.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
} else {
file_id = H5Fcreate(filepath.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
}

return file_id;
}

herr_t close_file(hid_t file_id) {
return H5Fclose(file_id);
}

herr_t make_and_write_dataset(hid_t file_id, const char* name, int rank, const hsize_t* shape, hid_t dtype, const void* buffer) {
hid_t dataspace_id = H5Screate_simple(rank, shape, NULL);
hid_t dataset_id = H5Dcreate2(file_id, name, dtype, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
herr_t status = H5Dwrite(dataset_id, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buffer);
status = H5Sclose(dataspace_id);
status = H5Dclose(dataset_id);
return status;
}

herr_t read_dataset(hid_t file_id, const char* name, hid_t dtype, void* buffer) {
hid_t dataset_id = H5Dopen2(file_id, name, H5P_DEFAULT);
hid_t dataspace_id = H5Dget_space(dataset_id);
herr_t status = H5Dread(dataset_id, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buffer);
status = H5Sclose(dataspace_id);
status = H5Dclose(dataset_id);
return status;
}
}
27 changes: 22 additions & 5 deletions tests/io/testHDF5.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,31 @@
int main(void) {

std::string outpath = "test.h5";
h5stor::H5Serializer ser(outpath);

// Build a simple array for testing purposes
DenseNDArray<float, 2> testarr({2, 2}, 1.0);
{
h5stor::H5Serializer ser(outpath);

// Test (de)serialization of std::array
std::array<float, 3> testarr = {1, 2, 3};
ser.serialize(testarr, "testarr");
}

std::cout << testarr(1,1) << std::endl;
{
h5stor::H5Serializer ser(outpath);
std::array<float, 3> read_arr = ser.deserialize<std::array<float, 3>>("testarr");
for(auto cur: read_arr) {
std::cout << cur << std::endl;
}
}

// // Build a simple array for testing purposes
// DenseNDArray<float, 2> testarr({2, 2}, 1.0);

ser.serialize(testarr, "testarr");
// std::cout << testarr(1,1) << std::endl;

// ser.serialize(testarr, "testarr");

// ser.deserialize<DenseNDArray<float, 2>>("testarr");

std::cout << "Done" << std::endl;
return 0;
Expand Down

0 comments on commit 00b69cb

Please sign in to comment.