From 2547a450d8a080eec99ccfc7fc756a0ce9ddb4a1 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 25 Jan 2023 18:31:03 +0100 Subject: [PATCH 1/2] hdf5: type declaration in separate file + some reorgs in table code --- src/opt/hdf5/include/cnorxz_hdf5.cc.h | 2 +- src/opt/hdf5/include/cnorxz_hdf5.h | 2 +- src/opt/hdf5/include/h5_file.h | 2 +- src/opt/hdf5/include/h5_group.cc.h | 21 ++++++++++ src/opt/hdf5/include/h5_group.h | 8 ++++ src/opt/hdf5/include/h5_table.cc.h | 18 ++------ src/opt/hdf5/include/h5_table.h | 6 ++- .../{h5_types.cc.h => h5_type_id.cc.h} | 4 +- src/opt/hdf5/include/h5_type_id.h | 40 ++++++++++++++++++ src/opt/hdf5/include/h5_types.h | 42 +++++++------------ src/opt/hdf5/lib/h5_group.cc | 23 ++++++++-- src/opt/hdf5/lib/h5_table.cc | 42 +++++++++++++++++++ 12 files changed, 158 insertions(+), 52 deletions(-) rename src/opt/hdf5/include/{h5_types.cc.h => h5_type_id.cc.h} (93%) create mode 100644 src/opt/hdf5/include/h5_type_id.h diff --git a/src/opt/hdf5/include/cnorxz_hdf5.cc.h b/src/opt/hdf5/include/cnorxz_hdf5.cc.h index b4d78b9..bdf3fae 100644 --- a/src/opt/hdf5/include/cnorxz_hdf5.cc.h +++ b/src/opt/hdf5/include/cnorxz_hdf5.cc.h @@ -1,4 +1,4 @@ -#include "h5_types.cc.h" +#include "h5_type_id.cc.h" #include "h5_group.cc.h" #include "h5_table.cc.h" diff --git a/src/opt/hdf5/include/cnorxz_hdf5.h b/src/opt/hdf5/include/cnorxz_hdf5.h index 113ee7c..5a47a23 100644 --- a/src/opt/hdf5/include/cnorxz_hdf5.h +++ b/src/opt/hdf5/include/cnorxz_hdf5.h @@ -3,6 +3,6 @@ #include "h5_file.h" #include "h5_group.h" #include "h5_table.h" -#include "h5_types.h" +#include "h5_type_id.h" #include "cnorxz_hdf5.cc.h" diff --git a/src/opt/hdf5/include/h5_file.h b/src/opt/hdf5/include/h5_file.h index 52c2849..79dfaf5 100644 --- a/src/opt/hdf5/include/h5_file.h +++ b/src/opt/hdf5/include/h5_file.h @@ -2,9 +2,9 @@ #ifndef __cxz_h5_file_h__ #define __cxz_h5_file_h__ +#include "h5_types.h" #include "h5_content_base.h" #include "h5_group.h" -//#include namespace CNORXZ { diff --git a/src/opt/hdf5/include/h5_group.cc.h b/src/opt/hdf5/include/h5_group.cc.h index 554bc44..b9f0bab 100644 --- a/src/opt/hdf5/include/h5_group.cc.h +++ b/src/opt/hdf5/include/h5_group.cc.h @@ -2,10 +2,31 @@ #ifndef __cxz_h5_group_cc_h__ #define __cxz_h5_group_cc_h__ +#include "h5_group.h" + namespace CNORXZ { namespace hdf5 { + template + Sptr> Group::getTable(const String& name, Tuple proto) + { + auto i = this->getIndexTo(name); + auto tab = std::dynamic_pointer_cast( *i ); + if(tab == nullptr){ + auto stab = std::dynamic_pointer_cast>(*i); + CXZ_ASSERT(stab != nullptr, "wrong format for table '" << name << "'"); + return stab; + } + else { + const RangePtr fields = tab->fields(); + (*i)->close(); + *i = std::make_shared>(name, this, fields); + return *i; + } + } + + template Group& Group::addData(const String& name, const ArrayBase& data) { diff --git a/src/opt/hdf5/include/h5_group.h b/src/opt/hdf5/include/h5_group.h index a13cd02..edc1f71 100644 --- a/src/opt/hdf5/include/h5_group.h +++ b/src/opt/hdf5/include/h5_group.h @@ -2,6 +2,7 @@ #ifndef __cxz_h5_group_h__ #define __cxz_h5_group_h__ +#include "h5_types.h" #include "h5_content_base.h" namespace CNORXZ @@ -26,6 +27,11 @@ namespace CNORXZ const ContentPtr& get(const String& name) const; Sptr getGroup(const String& name) const; + Sptr
getTable(const String& name) const; + + template + Sptr> getTable(const String& name, Tuple proto); + const MArray& get() const; Group& addGroup(const String& name); @@ -40,6 +46,8 @@ namespace CNORXZ MArray mCont; void mkCont(); + AIndex getIndexTo(const String& name) const; + BIndex getIndexTo(const String& name); }; } } diff --git a/src/opt/hdf5/include/h5_table.cc.h b/src/opt/hdf5/include/h5_table.cc.h index cc42228..5ae7e30 100644 --- a/src/opt/hdf5/include/h5_table.cc.h +++ b/src/opt/hdf5/include/h5_table.cc.h @@ -3,7 +3,7 @@ #define __cxz_h5_table_cc_h__ #include "h5_table.h" -#include "h5_types.h" +#include "h5_type_id.h" namespace CNORXZ { @@ -60,24 +60,12 @@ namespace CNORXZ template STable& STable::appendRecord(const Tuple& t) { - constexpr hsize_t chunk_size = sizeof(t); - constexpr Int compress = 0; RangePtr appr = CRangeFactory(1).create(); if(mRecords == nullptr){ - mRecords = appr; - Vector fields(mFields->size()); - auto fr = std::dynamic_pointer_cast>(mFields); - for(auto fi = fr->begin(); fi != fr->end(); ++fi){ - fields[fi.lex()] = (*fi).c_str(); - } - H5TBmake_table(mName.c_str(), mParent->id(), mName.c_str(), mFields->size(), - mRecords->size(), sizeof(t), fields.data(), mOffsets.data(), - mTypes.data(), chunk_size, NULL, compress, &t); + initTable(1, &t, sizeof(t), sizeof(t)); } else { - mRecords = mRecords->extend(appr); - H5TBappend_records(mParent->id(), mName.c_str(), 1, sizeof(t), - mOffsets.data(), mSizes.data(), &t); + Table::appendRecord(1, &t, sizeof(t)); } return *this; } diff --git a/src/opt/hdf5/include/h5_table.h b/src/opt/hdf5/include/h5_table.h index 18a6476..910bbd2 100644 --- a/src/opt/hdf5/include/h5_table.h +++ b/src/opt/hdf5/include/h5_table.h @@ -2,6 +2,7 @@ #ifndef __cxz_h5_table_h__ #define __cxz_h5_table_h__ +#include "h5_types.h" #include "h5_content_base.h" namespace CNORXZ @@ -23,8 +24,11 @@ namespace CNORXZ virtual String filename() const override final; Table& initFieldNames(const Vector& fnames); - Table& appendRecord(SizeT n, const char* data); + Table& initTable(SizeT n, const void* data, SizeT dsize, SizeT chunk_size); + Table& appendRecord(SizeT n, const void* data, SizeT dsize); Table& readRecord(SizeT pos, SizeT n, char* data); + const RangePtr& fields() const; + const RangePtr& records() const; protected: RangePtr mRecords; diff --git a/src/opt/hdf5/include/h5_types.cc.h b/src/opt/hdf5/include/h5_type_id.cc.h similarity index 93% rename from src/opt/hdf5/include/h5_types.cc.h rename to src/opt/hdf5/include/h5_type_id.cc.h index b7c93ac..ae646c1 100644 --- a/src/opt/hdf5/include/h5_types.cc.h +++ b/src/opt/hdf5/include/h5_type_id.cc.h @@ -1,6 +1,6 @@ -#ifndef __cxz_h5_types_cc_h__ -#define __cxz_h5_types_cc_h__ +#ifndef __cxz_h5_type_id_cc_h__ +#define __cxz_h5_type_id_cc_h__ #include "base/types.h" diff --git a/src/opt/hdf5/include/h5_type_id.h b/src/opt/hdf5/include/h5_type_id.h new file mode 100644 index 0000000..a517dca --- /dev/null +++ b/src/opt/hdf5/include/h5_type_id.h @@ -0,0 +1,40 @@ + +#ifndef __cxz_h5_type_id_h__ +#define __cxz_h5_type_id_h__ + +namespace CNORXZ +{ + namespace hdf5 + { + template + SizeT getTupleOffset(const Tuple& t, CSizeT i); + + /************** + * TypeId * + **************/ + + template + struct TypeId { static inline hid_t get(); }; + + template <> + struct TypeId { static inline hid_t get(); }; + + template <> + struct TypeId { static inline hid_t get(); }; + + template <> + struct TypeId { static inline hid_t get(); }; + + template + struct TypeId> { static inline hid_t get(); }; + + /***************** + * getTypeId * + *****************/ + + template + hid_t getTypeId(T x); + } +} + +#endif diff --git a/src/opt/hdf5/include/h5_types.h b/src/opt/hdf5/include/h5_types.h index 6036760..744b157 100644 --- a/src/opt/hdf5/include/h5_types.h +++ b/src/opt/hdf5/include/h5_types.h @@ -1,39 +1,27 @@ -#ifndef __cxz_h5_types_h__ -#define __cxz_h5_types_h__ +#ifndef __h5_types_h__ +#define __h5_types_h__ namespace CNORXZ { namespace hdf5 { - template - SizeT getTupleOffset(const Tuple& t, CSizeT i); + // definition -> h5_content_base.h + class ContentBase; - /************** - * TypeId * - **************/ - - template - struct TypeId { static inline hid_t get(); }; + // definition -> h5_group.h + class Group; - template <> - struct TypeId { static inline hid_t get(); }; + // definition -> h5_file.h + class File; + + // definition -> h5_table.h + class Table; + + // definition -> h5_table.h + template + class STable; - template <> - struct TypeId { static inline hid_t get(); }; - - template <> - struct TypeId { static inline hid_t get(); }; - - template - struct TypeId> { static inline hid_t get(); }; - - /***************** - * getTypeId * - *****************/ - - template - hid_t getTypeId(T x); } } diff --git a/src/opt/hdf5/lib/h5_group.cc b/src/opt/hdf5/lib/h5_group.cc index 3690c36..e6d3b92 100644 --- a/src/opt/hdf5/lib/h5_group.cc +++ b/src/opt/hdf5/lib/h5_group.cc @@ -73,10 +73,7 @@ namespace CNORXZ const ContentPtr& Group::get(const String& name) const { - CXZ_ASSERT(this->isOpen(), "tried to get content of closed group"); - auto dvec = [](const String& n) { return Vector({DType(n)}); }; - auto i = mCont.begin(); - i.at(dvec(name)); + auto i = this->getIndexTo(name); return *i; } @@ -191,5 +188,23 @@ namespace CNORXZ initCont, reinterpret_cast(&icd) ); } + AIndex Group::getIndexTo(const String& name) const + { + CXZ_ASSERT(this->isOpen(), "tried to get content of closed group"); + auto dvec = [](const String& n) { return Vector({DType(n)}); }; + auto i = mCont.begin(); + i.at(dvec(name)); + return i; + } + + BIndex Group::getIndexTo(const String& name) + { + CXZ_ASSERT(this->isOpen(), "tried to get content of closed group"); + auto dvec = [](const String& n) { return Vector({DType(n)}); }; + auto i = mCont.begin(); + i.at(dvec(name)); + return i; + } + } } diff --git a/src/opt/hdf5/lib/h5_table.cc b/src/opt/hdf5/lib/h5_table.cc index 8ecd4dd..c2902bb 100644 --- a/src/opt/hdf5/lib/h5_table.cc +++ b/src/opt/hdf5/lib/h5_table.cc @@ -90,5 +90,47 @@ namespace CNORXZ mFields = URangeFactory(fnames).create(); return *this; } + + Table& Table::initTable(SizeT n, const void* data, SizeT dsize, SizeT chunk_size) + { + const Int compress = 0; + mRecords = CRangeFactory(n).create(); + Vector fields(mFields->size()); + auto fr = std::dynamic_pointer_cast>(mFields); + for(auto fi = fr->begin(); fi != fr->end(); ++fi){ + fields[fi.lex()] = (*fi).c_str(); + } + const herr_t err = H5TBmake_table + (mName.c_str(), mParent->id(), mName.c_str(), mFields->size(), mRecords->size(), dsize, + fields.data(), mOffsets.data(), mTypes.data(), chunk_size, NULL, compress, data); + CXZ_ASSERT(err >= 0, "error while initialzing table: error code = " << err); + return *this; + } + + Table& Table::appendRecord(SizeT n, const void* data, SizeT dsize) + { + mRecords = mRecords->extend( CRangeFactory(1).create() ); + const herr_t err = H5TBappend_records(mParent->id(), mName.c_str(), n, dsize, + mOffsets.data(), mSizes.data(), data); + CXZ_ASSERT(err >= 0, "error while appending record to table: error code = " << err); + return *this; + } + + Table& Table::readRecord(SizeT pos, SizeT n, char* data) + { + CXZ_ERROR("not implemented!!!"); + return *this; + } + + const RangePtr& Table::fields() const + { + return mFields; + } + + const RangePtr& Table::records() const + { + return mRecords; + } + } } From 25b5c13608f11f9670842a4284ba30182bdfc468 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 26 Jan 2023 19:55:29 +0100 Subject: [PATCH 2/2] h5_table: remove mCheckedFile --- src/opt/hdf5/include/h5_table.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/opt/hdf5/include/h5_table.h b/src/opt/hdf5/include/h5_table.h index 910bbd2..0d2ee92 100644 --- a/src/opt/hdf5/include/h5_table.h +++ b/src/opt/hdf5/include/h5_table.h @@ -37,7 +37,6 @@ namespace CNORXZ MArray mOffsets; MArray mTypes; hid_t mType = 0; - bool mCheckedFile = false; }; template