Skip to content

Commit

Permalink
Working implementation for HDF5 writer
Browse files Browse the repository at this point in the history
- Developed a singular interface for IO writers
- Wrappers for HDF5 library based on the interface
- Defined wavefield format - working implementation for HDF5 format
  • Loading branch information
Rohit-Kakodkar committed Feb 28, 2024
1 parent b3def59 commit 304ba07
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 199 deletions.
6 changes: 3 additions & 3 deletions include/IO/HDF5/impl/dataset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#define SPECFEM_IO_HDF5_IMPL_DATASET_HPP

#include "H5Cpp.h"
#include "file.hpp"
#include "group.hpp"
#include "native_type.hpp"
#include "native_type.tpp"
#include <memory>
#include <string>

Expand All @@ -23,7 +22,7 @@ template <typename ViewType> class Dataset {

const static int rank;
using value_type = typename ViewType::non_const_value_type;
const static H5::PredType native_type;
using native_type = typename specfem::IO::impl::HDF5::native_type<value_type>;
using MemSpace = typename ViewType::memory_space;

Dataset(std::unique_ptr<H5::H5File> &file, const std::string &name,
Expand All @@ -37,6 +36,7 @@ template <typename ViewType> class Dataset {

private:
ViewType data;
// native_type datatype;
std::unique_ptr<H5::DataSet> dataset;
std::unique_ptr<H5::DataSpace> dataspace;
};
Expand Down
26 changes: 11 additions & 15 deletions include/IO/HDF5/impl/dataset.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
#include "native_type.hpp"
#include <string>

template <typename ViewType>
const H5::PredType specfem::IO::impl::HDF5::Dataset<ViewType>::native_type =
specfem::IO::impl::HDF5::native_type<
typename ViewType::non_const_value_type>::type;

// check if Kokkos version is < 4.1
#if KOKKOS_VERSION < 40100
template <typename ViewType>
Expand All @@ -32,10 +27,11 @@ specfem::IO::impl::HDF5::Dataset<ViewType>::Dataset(
dims[i] = data.extent(i);
}
return dims;
}())),
dataset(std::make_unique<H5::DataSet>(
file->createDataSet(name, native_type, *dataspace))) {
ASSERT(data.span_is_contiguous() == true, "ViewType must be contiguous");
}())) {

this->dataset = std::make_unique<H5::DataSet>(
file->createDataSet(name, native_type::type(), *dataspace));
ASSERT(data.span_is_contiguous(), "ViewType must be contiguous");
}

template <typename ViewType>
Expand All @@ -50,22 +46,22 @@ specfem::IO::impl::HDF5::Dataset<ViewType>::Dataset(
dims[i] = data.extent(i);
}
return dims;
}())),
dataset(std::make_unique<H5::DataSet>(
group->createDataSet(name, native_type, *dataspace))) {
}())) {

ASSERT(data.span_is_contiguous() == true, "ViewType must be contiguous");
this->dataset = std::make_unique<H5::DataSet>(
group->createDataSet(name, native_type::type(), *dataspace));
ASSERT(data.span_is_contiguous(), "ViewType must be contiguous");
}

template <typename ViewType>
void specfem::IO::impl::HDF5::Dataset<ViewType>::write() {
if (std::is_same_v<MemSpace, specfem::kokkos::HostMemSpace>) {
dataset->write(data.data(), native_type);
dataset->write(data.data(), native_type::type());
return;
} else if (std::is_same_v<MemSpace, specfem::kokkos::DevMemSpace>) {
auto host_data = Kokkos::create_mirror_view(data);
Kokkos::deep_copy(host_data, data);
dataset->write(host_data.data(), native_type);
dataset->write(host_data.data(), native_type::type());
return;
} else {
throw std::runtime_error("Unknown memory space");
Expand Down
1 change: 0 additions & 1 deletion include/IO/HDF5/impl/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "H5Cpp.h"
#include "dataset.hpp"
#include "dataset.tpp"
#include "group.hpp"
#include <memory>
#include <string>
Expand Down
2 changes: 0 additions & 2 deletions include/IO/HDF5/impl/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "H5Cpp.h"
#include "dataset.hpp"
#include "dataset.tpp"
#include "file.hpp"
#include <memory>
#include <string>

Expand Down
2 changes: 1 addition & 1 deletion include/IO/HDF5/impl/native_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace IO {
namespace impl {
namespace HDF5 {

template <typename T> struct native_type { const static H5::PredType type; };
template <typename T> struct native_type {};
} // namespace HDF5
} // namespace impl
} // namespace IO
Expand Down
112 changes: 112 additions & 0 deletions include/IO/HDF5/impl/native_type.tpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#ifndef SPECFEM_IO_HDF5_IMPL_NATIVE_TYPE_TPP
#define SPECFEM_IO_HDF5_IMPL_NATIVE_TYPE_TPP

#include "H5Cpp.h"
#include "native_type.hpp"
#include <iostream>

template <> struct specfem::IO::impl::HDF5::native_type<int> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_INT);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<float> {
static H5::FloatType& type() {
static H5::FloatType type(H5::PredType::NATIVE_FLOAT);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<double> {
static H5::FloatType& type() {
static H5::FloatType type(H5::PredType::NATIVE_DOUBLE);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<long> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_LONG);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<long long> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_LLONG);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<unsigned int> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_UINT);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<unsigned long> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_ULONG);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<unsigned long long> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_ULLONG);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<unsigned char> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_UCHAR);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<char> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_CHAR);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<short> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_SHORT);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<unsigned short> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_USHORT);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

template <> struct specfem::IO::impl::HDF5::native_type<bool> {
static H5::IntType& type() {
static H5::IntType type(H5::PredType::NATIVE_HBOOL);
type.setOrder(H5T_ORDER_LE);
return type;
}
};

#endif
56 changes: 16 additions & 40 deletions src/IO/HDF5/native_type.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,31 @@
#include "IO/HDF5/impl/native_type.hpp"
#include "H5Cpp.h"
#include "IO/HDF5/impl/native_type.tpp"

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<float>::type =
H5::PredType::NATIVE_FLOAT;
// Explicit instantiation

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<double>::type =
H5::PredType::NATIVE_DOUBLE;
template struct specfem::IO::impl::HDF5::native_type<float>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<int>::type =
H5::PredType::NATIVE_INT;
template struct specfem::IO::impl::HDF5::native_type<double>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<long>::type =
H5::PredType::NATIVE_LONG;
template struct specfem::IO::impl::HDF5::native_type<int>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<long long>::type =
H5::PredType::NATIVE_LLONG;
template struct specfem::IO::impl::HDF5::native_type<long>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<unsigned int>::type =
H5::PredType::NATIVE_UINT;
template struct specfem::IO::impl::HDF5::native_type<long long>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<unsigned long>::type =
H5::PredType::NATIVE_ULONG;
template struct specfem::IO::impl::HDF5::native_type<unsigned int>;

template <>
const H5::PredType
specfem::IO::impl::HDF5::native_type<unsigned long long>::type =
H5::PredType::NATIVE_ULLONG;
template struct specfem::IO::impl::HDF5::native_type<unsigned long>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<short>::type =
H5::PredType::NATIVE_SHORT;
template struct specfem::IO::impl::HDF5::native_type<unsigned long long>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<unsigned short>::type =
H5::PredType::NATIVE_USHORT;
template struct specfem::IO::impl::HDF5::native_type<short>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<char>::type =
H5::PredType::NATIVE_CHAR;
template struct specfem::IO::impl::HDF5::native_type<unsigned short>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<unsigned char>::type =
H5::PredType::NATIVE_UCHAR;
template struct specfem::IO::impl::HDF5::native_type<char>;

template <>
const H5::PredType specfem::IO::impl::HDF5::native_type<bool>::type =
H5::PredType::NATIVE_HBOOL;
template struct specfem::IO::impl::HDF5::native_type<unsigned char>;

template struct specfem::IO::impl::HDF5::native_type<bool>;
Loading

0 comments on commit 304ba07

Please sign in to comment.