From 59932895e93e0edbe3a623aaf0aa23a448412b24 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 22 Jan 2023 23:34:30 +0100 Subject: [PATCH] add h5_types + class STabel : public Table ( field types known by compile time) --- src/opt/hdf5/include/h5_table.cc.h | 46 ++++++++++++++++++++++- src/opt/hdf5/include/h5_table.h | 19 +++++++--- src/opt/hdf5/include/h5_types.cc.h | 30 +++++++++++++++ src/opt/hdf5/include/h5_types.h | 60 ++++++++++++++++++++++++++++++ src/opt/hdf5/lib/h5_table.cc | 7 ++++ 5 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 src/opt/hdf5/include/h5_types.cc.h create mode 100644 src/opt/hdf5/include/h5_types.h diff --git a/src/opt/hdf5/include/h5_table.cc.h b/src/opt/hdf5/include/h5_table.cc.h index bba1265..784b886 100644 --- a/src/opt/hdf5/include/h5_table.cc.h +++ b/src/opt/hdf5/include/h5_table.cc.h @@ -3,6 +3,7 @@ #define __cxz_h5_table_cc_h__ #include "h5_table.h" +#include "h5_types.h" namespace CNORXZ { @@ -15,9 +16,52 @@ namespace CNORXZ } template - Table& appendRecord(const Tuple& t) const + STabel::STabel(const String& name, const ContentBase* _parent) : + Table(name, _parent) { + // checks..!!!! + } + + template + STabel& STabel::appendRecord(const Tuple& t) + { + if(not mCheckedFile){ + this->open(); + } + this->close(); // H5TB does not require an open dataset + CXZ_ASSERT(mFields != nullptr, + "field names have to be initialized before creating table"); + CXZ_ASSERT(mFields->size() == sizeof...(Ts), + "expected tuple of size = " << mFields->size() + << ", got: " << sizeof...(Ts)); + RangePtr appr = CRangeFactory(1).create(); + auto offsets = iter<0,sizeof...(Ts)> + ( [&](auto i) { return &std::get(t) - &t; }, + [](const auto&... e) { return Vector({e...}); }); + auto sizes = iter<0,sizeof...(Ts)> + ( [&](auto i) { return sizeof(std::get(t)); }, + [](const auto&... e) { return Vector({e...}); }); + auto types = iter<0,sizeof...(Ts)> + ( [&](auto i) { return getTypeId(std::get(t)); }, + [](const auto&... e) { return Vector({e...}); }); + if(mRecords == nullptr){ + mRecords = appr; + mSizes = MArray(std::move(sizes)); + mOffsets = MArray(std::move(offsets)); + mTypes = MArray(std::move(types)); + + // init + } + else { + // check consistency + + mRecords = mRecords->extend(appr); + } + // append + H5TBappend_records(mParent->id(), mName, 1, sizeof(t), + mOffsets.data(), mSizes.data(), &t); + return *this; } } } diff --git a/src/opt/hdf5/include/h5_table.h b/src/opt/hdf5/include/h5_table.h index 8ed657a..707a77c 100644 --- a/src/opt/hdf5/include/h5_table.h +++ b/src/opt/hdf5/include/h5_table.h @@ -23,15 +23,11 @@ namespace CNORXZ virtual String filename() const override final; Table& openDSet(); + Table& initFieldNames(const Vector& fnames); template decltype(auto) iterRecords(F&& f) const; - template - Table& appendRecord(const Tuple& t) const; - - template - Table& appendRecord(const MArray>& t) const; private: RangePtr mRecords; @@ -40,6 +36,19 @@ namespace CNORXZ MArray mOffsets; MArray mTypes; hid_t mType; + bool mCheckedFile = false; + }; + + template + class STabel : public Table + { + public: + DEFAULT_MEMBERS(STabel); + STabel(const String& name, const ContentBase* _parent); + + Table& appendRecord(const Tuple& t); + Table& appendRecord(const MArray>& t); + }; } } diff --git a/src/opt/hdf5/include/h5_types.cc.h b/src/opt/hdf5/include/h5_types.cc.h new file mode 100644 index 0000000..ca02f4b --- /dev/null +++ b/src/opt/hdf5/include/h5_types.cc.h @@ -0,0 +1,30 @@ + +#ifndef __cxz_h5_types_cc_h__ +#define __cxz_h5_types_cc_h__ + +namespace CNORXZ +{ + namespace hdf5 + { + template + inline hid_t TypeId>::get(Arr x) + { + static hid_t arrtype = H5Tarray_create2(getTypeId(x[0]), 1, N); + return arrtype; + } + + template + hid_t getTypeId(T x) + { + if constexpr (TypeIsNative::value){ + return NativeTypeId::value; + } + else { + return TypeId::get(x); + } + } + + } +} + +#endif diff --git a/src/opt/hdf5/include/h5_types.h b/src/opt/hdf5/include/h5_types.h new file mode 100644 index 0000000..14b69cf --- /dev/null +++ b/src/opt/hdf5/include/h5_types.h @@ -0,0 +1,60 @@ + +#ifndef __cxz_h5_types_h__ +#define __cxz_h5_types_h__ + +namespace CNORXZ +{ + namespace hdf5 + { + /******************** + * TypeIsNative * + ********************/ + + template + struct TypeIsNative { static constexpr bool value = false; }; + + template <> + struct TypeIsNative { static constexpr bool value = true; }; + + template + struct TypeIsNative { static constexpr bool value = true; }; + + template + struct TypeIsNative { static constexpr bool value = true; }; + + /******************** + * NativeTypeId * + ********************/ + + template + struct NativeTypeId { static constexpr hid_t value = 0; }; + + template <> + struct NativeTypeId { static constexpr hid_t value = H5T_NATIVE_ULONG; }; + + template <> + struct NativeTypeId { static constexpr hid_t value = H5T_NATIVE_INT; }; + + template <> + struct NativeTypeId { static constexpr hid_t value = H5T_NATIVE_DOUBLE; }; + + /************** + * TypeId * + **************/ + + template + struct TypeId { static inline hid_t get() { return 0; } }; + + template + struct TypeId> { static inline hid_t get(); }; + + /***************** + * getTypeId * + *****************/ + + template + hid_t getTypeId(const T& x); + } +} + +#endif diff --git a/src/opt/hdf5/lib/h5_table.cc b/src/opt/hdf5/lib/h5_table.cc index 27e91c8..045cfa7 100644 --- a/src/opt/hdf5/lib/h5_table.cc +++ b/src/opt/hdf5/lib/h5_table.cc @@ -56,6 +56,7 @@ namespace CNORXZ } mTypes = MArray(mFields, std::move(types)); } + mCheckedFile = true; // now an empty mRecords etc indicates that there is currently no table at the requested location return *this; } @@ -87,5 +88,11 @@ namespace CNORXZ return *this; } + Table& Table::initFieldNames(const Vector& fnames) + { + CXZ_ASSERT(mFields == nullptr, "fields already initialized"); + mFields = URangeFactory(fnames).create(); + return *this; + } } }