From 74a3a5b825bec459b6912c7ae0c7c7673b084a01 Mon Sep 17 00:00:00 2001 From: Michael Marshall Date: Sat, 16 Feb 2019 18:45:46 +0000 Subject: [PATCH] Fixed existing bug in Hdf5Reader::readDefault for std::vector --- Grid/serialisation/BaseIO.h | 34 ++++++++++++++++++++++++++++++++-- Grid/serialisation/Hdf5IO.h | 2 +- tests/IO/Test_serialisation.cc | 23 +++-------------------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/Grid/serialisation/BaseIO.h b/Grid/serialisation/BaseIO.h index 64e5a12c..bf4eb42a 100644 --- a/Grid/serialisation/BaseIO.h +++ b/Grid/serialisation/BaseIO.h @@ -213,13 +213,42 @@ namespace Grid { } } + // Helper to dump a tensor +#ifdef DEBUG + template + typename std::enable_if::value, void>::type + dump_tensor_func(T &t, const char * pName = nullptr) + { + using Traits = typename EigenIO::Traits; + const auto rank{T::NumIndices}; + const auto &dims = t.dimensions(); + std::cout << "Dumping rank " << rank << ((T::Options & Eigen::RowMajor) ? ", row" : ", column") << "-major tensor "; + if( pName ) + std::cout << pName; + for( auto i = 0 ; i < rank; i++ ) std::cout << "[" << dims[i] << "]"; + std::cout << " in memory order:" << std::endl; + for_all( t, [&](typename Traits::scalar_type &c, typename T::Index index, const std::array Dims ){ + std::cout << " "; + for( auto dim : Dims ) + std::cout << "[" << dim << "]"; + std::cout << " = " << c << std::endl; + } ); + std::cout << "========================================" << std::endl; + } +#define dump_tensor(args...) dump_tensor_func(args) +#else +#define dump_tensor(args...) +#endif + // Helper to dump a tensor in memory order + // Kind of superfluous given the above +#ifdef DEBUG template typename std::enable_if::value, void>::type - DumpMemoryOrder(T t, const char * pName = nullptr) + DumpMemoryOrder(T &t, const char * pName = nullptr) { - const auto dims = t.dimensions(); const auto rank = t.rank(); + const auto &dims = t.dimensions(); std::cout << "Dumping rank " << rank << ((T::Options & Eigen::RowMajor) ? ", row" : ", column") << "-major tensor "; if( pName ) std::cout << pName; @@ -258,6 +287,7 @@ namespace Grid { std::cout << std::endl; } } +#endif // Abstract writer/reader classes //////////////////////////////////////////// // static polymorphism implemented using CRTP idiom diff --git a/Grid/serialisation/Hdf5IO.h b/Grid/serialisation/Hdf5IO.h index c486801a..7de6dc88 100644 --- a/Grid/serialisation/Hdf5IO.h +++ b/Grid/serialisation/Hdf5IO.h @@ -215,7 +215,7 @@ namespace Grid // read the flat vector std::vector buf(size); - if (size > dataSetThres_) + if (size * sizeof(Element) > dataSetThres_) { H5NS::DataSet dataSet; diff --git a/tests/IO/Test_serialisation.cc b/tests/IO/Test_serialisation.cc index d8e02ea0..509241ab 100644 --- a/tests/IO/Test_serialisation.cc +++ b/tests/IO/Test_serialisation.cc @@ -101,20 +101,6 @@ void ioTest(const std::string &filename, const O &object, const std::string &nam } #ifdef DEBUG -template -//typename std::enable_if::value, void>::type -void dump_tensor(T & t) -{ - using Traits = typename EigenIO::Traits; - for_all( t, [&](typename Traits::scalar_type &c, typename T::Index index, const std::size_t * pDims ){ - std::cout << " "; - for( int i = 0 ; i < t.NumDimensions + Traits::rank_non_trivial; i++ ) - std::cout << "[" << pDims[i] << "]"; - std::cout << " = " << c << std::endl; - } ); - std::cout << "========================================" << std::endl; -} - //typedef int TestScalar; typedef std::complex TestScalar; typedef Eigen::Tensor, 6> TestTensorSingle; @@ -159,8 +145,7 @@ bool EigenIOTest(void) { Val += Inc; } ioTest("iotest_tensor.h5", t, "eigen_tensor_instance_name"); - std::cout << "t:"; - dump_tensor(t); + dump_tensor(t, "t"); // Now serialise a fixed size tensor using FixedTensor = Eigen::TensorFixedSize>; @@ -173,8 +158,7 @@ bool EigenIOTest(void) { Val += Inc; } ioTest("iotest_tensor_fixed.h5", tf, "eigen_tensor_fixed_name"); - std::cout << "tf:"; - dump_tensor(tf); + dump_tensor(tf, "tf"); ETSerClass o; ioTest("iotest_object.h5", o, "ETSerClass_object_instance_name"); @@ -192,8 +176,7 @@ bool EigenIOTest(void) { Val += Inc; } ioTest("iotest_LSCTensor.h5", l, "LSCTensor_object_instance_name"); - std::cout << "l:"; - dump_tensor(l); + dump_tensor(l, "l"); // Tensor of spin colour LCMTensor l2;