diff --git a/Grid/serialisation/BinaryIO.h b/Grid/serialisation/BinaryIO.h index 757753c7..bf011454 100644 --- a/Grid/serialisation/BinaryIO.h +++ b/Grid/serialisation/BinaryIO.h @@ -51,6 +51,8 @@ namespace Grid { template void writeDefault(const std::string &s, const std::vector &x); void writeDefault(const std::string &s, const char *x); + template + void writeMultiDim(const std::string &s, const std::vector & Dimensions, const U * pDataRowMajor, size_t NumElements); private: std::ofstream file_; }; @@ -66,6 +68,8 @@ namespace Grid { void readDefault(const std::string &s, U &output); template void readDefault(const std::string &s, std::vector &output); + template + void readMultiDim(const std::string &s, std::vector &buf, std::vector &dim); private: std::ifstream file_; }; @@ -92,6 +96,27 @@ namespace Grid { } } + template + void BinaryWriter::writeMultiDim(const std::string &s, const std::vector & Dimensions, const U * pDataRowMajor, size_t NumElements) + { + uint64_t rank = static_cast( Dimensions.size() ); + uint64_t tmp = 1; + for( auto i = 0 ; i < rank ; i++ ) + tmp *= Dimensions[i]; + assert( tmp == NumElements && "Dimensions don't match size of data being written" ); + // Total number of elements + write("", tmp); + // Number of dimensions + write("", rank); + // Followed by each dimension + for( auto i = 0 ; i < rank ; i++ ) { + tmp = Dimensions[i]; + write("", tmp); + } + for( auto i = 0; i < NumElements; ++i) + write("", pDataRowMajor[i]); + } + // Reader template implementation //////////////////////////////////////////// template void BinaryReader::readDefault(const std::string &s, U &output) @@ -114,6 +139,30 @@ namespace Grid { read("", output[i]); } } + + template + void BinaryReader::readMultiDim(const std::string &s, std::vector &buf, std::vector &dim) + { + // Number of elements + uint64_t NumElements; + read("", NumElements); + // Number of dimensions + uint64_t rank; + read("", rank); + // Followed by each dimension + uint64_t count = 1; + dim.resize(rank); + uint64_t tmp; + for( auto i = 0 ; i < rank ; i++ ) { + read("", tmp); + dim[i] = tmp; + count *= tmp; + } + assert( count == NumElements && "Dimensions don't match size of data being read" ); + buf.resize(count); + for( auto i = 0; i < count; ++i) + read("", buf[i]); + } } #endif diff --git a/tests/IO/Test_serialisation.cc b/tests/IO/Test_serialisation.cc index cb4f65d5..01672e8b 100644 --- a/tests/IO/Test_serialisation.cc +++ b/tests/IO/Test_serialisation.cc @@ -159,9 +159,11 @@ public: } }; +#define RDR_ Hdf5Reader +#define WTR_ Hdf5Writer #define TensorWriteReadInnerNoInit( T ) \ filename = "iotest_"s + std::to_string(++TestNum) + "_" #T ".h5"; \ - ioTest(filename, t, #T, #T); + ioTest(filename, t, #T, #T); #define TensorWriteReadInner( T ) SequentialInit( t ); TensorWriteReadInnerNoInit( T ) #define TensorWriteRead( T ) { T t ; TensorWriteReadInner( T ) } #define TensorWriteReadV(T, ... ) { T t( __VA_ARGS__ ); TensorWriteReadInner( T ) } @@ -183,7 +185,7 @@ void EigenHdf5IOTest(void) TensorWriteReadInner ( TensorRank5UShort ); std::cout << " Testing alternate memory order read ... "; TensorRank5UShortAlt t2; - Hdf5Reader reader(filename); + RDR_ reader(filename); read(reader, "TensorRank5UShort", t2); bool good = true; for_all( t2, [&](unsigned short c, unsigned short n,