Skip to content

Commit

Permalink
HDF5: Complex Support
Browse files Browse the repository at this point in the history
  • Loading branch information
ax3l committed Dec 19, 2019
1 parent b541cc3 commit 5ff6236
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 88 deletions.
181 changes: 114 additions & 67 deletions include/openPMD/IO/HDF5/HDF5Auxiliary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,82 +27,105 @@

#include <hdf5.h>

#include <complex>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>


namespace openPMD
{
inline hid_t
getH5DataType(Attribute const& att)
struct GetH5DataType
{
using DT = Datatype;
switch( att.dtype )
std::unordered_map< std::string, hid_t > m_userTypes;

GetH5DataType( std::unordered_map< std::string, hid_t > userTypes )
: m_userTypes{ std::move(userTypes) }
{
case DT::CHAR:
case DT::VEC_CHAR:
return H5Tcopy(H5T_NATIVE_CHAR);
case DT::UCHAR:
case DT::VEC_UCHAR:
return H5Tcopy(H5T_NATIVE_UCHAR);
case DT::SHORT:
case DT::VEC_SHORT:
return H5Tcopy(H5T_NATIVE_SHORT);
case DT::INT:
case DT::VEC_INT:
return H5Tcopy(H5T_NATIVE_INT);
case DT::LONG:
case DT::VEC_LONG:
return H5Tcopy(H5T_NATIVE_LONG);
case DT::LONGLONG:
case DT::VEC_LONGLONG:
return H5Tcopy(H5T_NATIVE_LLONG);
case DT::USHORT:
case DT::VEC_USHORT:
return H5Tcopy(H5T_NATIVE_USHORT);
case DT::UINT:
case DT::VEC_UINT:
return H5Tcopy(H5T_NATIVE_UINT);
case DT::ULONG:
case DT::VEC_ULONG:
return H5Tcopy(H5T_NATIVE_ULONG);
case DT::ULONGLONG:
case DT::VEC_ULONGLONG:
return H5Tcopy(H5T_NATIVE_ULLONG);
case DT::FLOAT:
case DT::VEC_FLOAT:
return H5Tcopy(H5T_NATIVE_FLOAT);
case DT::DOUBLE:
case DT::ARR_DBL_7:
case DT::VEC_DOUBLE:
return H5Tcopy(H5T_NATIVE_DOUBLE);
case DT::LONG_DOUBLE:
case DT::VEC_LONG_DOUBLE:
return H5Tcopy(H5T_NATIVE_LDOUBLE);
case DT::STRING:
{
hid_t string_t_id = H5Tcopy(H5T_C_S1);
H5Tset_size(string_t_id, att.get< std::string >().size());
return string_t_id;
}
case DT::VEC_STRING:
{
hid_t string_t_id = H5Tcopy(H5T_C_S1);
size_t max_len = 0;
for( std::string const& s : att.get< std::vector< std::string > >() )
max_len = std::max(max_len, s.size());
H5Tset_size(string_t_id, max_len);
return string_t_id;
}

hid_t
operator()(Attribute const &att)
{
using DT = Datatype;
switch (att.dtype) {
case DT::CHAR:
case DT::VEC_CHAR:
return H5Tcopy(H5T_NATIVE_CHAR);
case DT::UCHAR:
case DT::VEC_UCHAR:
return H5Tcopy(H5T_NATIVE_UCHAR);
case DT::SHORT:
case DT::VEC_SHORT:
return H5Tcopy(H5T_NATIVE_SHORT);
case DT::INT:
case DT::VEC_INT:
return H5Tcopy(H5T_NATIVE_INT);
case DT::LONG:
case DT::VEC_LONG:
return H5Tcopy(H5T_NATIVE_LONG);
case DT::LONGLONG:
case DT::VEC_LONGLONG:
return H5Tcopy(H5T_NATIVE_LLONG);
case DT::USHORT:
case DT::VEC_USHORT:
return H5Tcopy(H5T_NATIVE_USHORT);
case DT::UINT:
case DT::VEC_UINT:
return H5Tcopy(H5T_NATIVE_UINT);
case DT::ULONG:
case DT::VEC_ULONG:
return H5Tcopy(H5T_NATIVE_ULONG);
case DT::ULONGLONG:
case DT::VEC_ULONGLONG:
return H5Tcopy(H5T_NATIVE_ULLONG);
case DT::FLOAT:
case DT::VEC_FLOAT:
return H5Tcopy(H5T_NATIVE_FLOAT);
case DT::DOUBLE:
case DT::ARR_DBL_7:
case DT::VEC_DOUBLE:
return H5Tcopy(H5T_NATIVE_DOUBLE);
case DT::LONG_DOUBLE:
case DT::VEC_LONG_DOUBLE:
return H5Tcopy(H5T_NATIVE_LDOUBLE);

case DT::CFLOAT:
case DT::VEC_CFLOAT:
return H5Tcopy( m_userTypes.at( typeid(std::complex< float >).name() ) );
case DT::CDOUBLE:
case DT::VEC_CDOUBLE:
return H5Tcopy( m_userTypes.at( typeid(std::complex< double >).name() ) );
case DT::CLONG_DOUBLE:
case DT::VEC_CLONG_DOUBLE:
return H5Tcopy( m_userTypes.at( typeid(std::complex< long double >).name() ) );

case DT::STRING: {
hid_t string_t_id = H5Tcopy(H5T_C_S1);
H5Tset_size(string_t_id, att.get<std::string>().size());
return string_t_id;
}
case DT::VEC_STRING: {
hid_t string_t_id = H5Tcopy(H5T_C_S1);
size_t max_len = 0;
for (std::string const &s : att.get<std::vector<std::string> >())
max_len = std::max(max_len, s.size());
H5Tset_size(string_t_id, max_len);
return string_t_id;
}
case DT::BOOL:
return H5Tcopy( m_userTypes.at( typeid(bool).name() ) );
case DT::DATATYPE:
throw std::runtime_error("[HDF5] Meta-Datatype leaked into IO");
case DT::UNDEFINED:
throw std::runtime_error("[HDF5] Unknown Attribute datatype (HDF5 datatype)");
default:
throw std::runtime_error("[HDF5] Datatype not implemented");
}
case DT::BOOL:
return H5Tcopy(H5T_NATIVE_HBOOL);
case DT::DATATYPE:
throw std::runtime_error("Meta-Datatype leaked into IO");
case DT::UNDEFINED:
throw std::runtime_error("Unknown Attribute datatype (HDF5 datatype)");
default:
throw std::runtime_error("Datatype not implemented in HDF5 IO");
}
}
};

inline hid_t
getH5DataSpace(Attribute const& att)
Expand All @@ -123,6 +146,9 @@ getH5DataSpace(Attribute const& att)
case DT::FLOAT:
case DT::DOUBLE:
case DT::LONG_DOUBLE:
case DT::CFLOAT:
case DT::CDOUBLE:
case DT::CLONG_DOUBLE:
case DT::STRING:
case DT::BOOL:
return H5Screate(H5S_SCALAR);
Expand Down Expand Up @@ -217,6 +243,27 @@ getH5DataSpace(Attribute const& att)
H5Sset_extent_simple(vec_t_id, 1, dims, nullptr);
return vec_t_id;
}
case DT::VEC_CFLOAT:
{
hid_t vec_t_id = H5Screate(H5S_SIMPLE);
hsize_t dims[1] = {att.get< std::vector< std::complex< float > > >().size()};
H5Sset_extent_simple(vec_t_id, 1, dims, nullptr);
return vec_t_id;
}
case DT::VEC_CDOUBLE:
{
hid_t vec_t_id = H5Screate(H5S_SIMPLE);
hsize_t dims[1] = {att.get< std::vector< std::complex< double > > >().size()};
H5Sset_extent_simple(vec_t_id, 1, dims, nullptr);
return vec_t_id;
}
case DT::VEC_CLONG_DOUBLE:
{
hid_t vec_t_id = H5Screate(H5S_SIMPLE);
hsize_t dims[1] = {att.get< std::vector< std::complex< long double > > >().size()};
H5Sset_extent_simple(vec_t_id, 1, dims, nullptr);
return vec_t_id;
}
case DT::VEC_STRING:
{
hid_t vec_t_id = H5Screate(H5S_SIMPLE);
Expand Down
4 changes: 4 additions & 0 deletions include/openPMD/IO/HDF5/HDF5IOHandlerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ namespace openPMD
hid_t m_datasetTransferProperty;
hid_t m_fileAccessProperty;

// h5py compatible types for bool and complex
hid_t m_H5T_BOOL_ENUM;
hid_t m_H5T_CFLOAT;
hid_t m_H5T_CDOUBLE;
hid_t m_H5T_CLONG_DOUBLE;
}; // HDF5IOHandlerImpl
#else
class HDF5IOHandlerImpl
Expand Down
Loading

0 comments on commit 5ff6236

Please sign in to comment.