From 6ebb32ffbf22f6ae845bd28d4ae4c3c2fca1e464 Mon Sep 17 00:00:00 2001 From: Michael Marshall Date: Mon, 18 Feb 2019 21:40:53 +0000 Subject: [PATCH] Rationalised Test_serialisation --- Grid/serialisation/BaseIO.h | 20 ++++++++++++++- tests/IO/Test_serialisation.cc | 46 +++++++++++++++++----------------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/Grid/serialisation/BaseIO.h b/Grid/serialisation/BaseIO.h index 11dfee0e..6adb87d3 100644 --- a/Grid/serialisation/BaseIO.h +++ b/Grid/serialisation/BaseIO.h @@ -230,6 +230,24 @@ namespace Grid { } } + // Used for sequential initialisations + template constexpr T Flag = 1; + template constexpr std::complex Flag> {1, -1}; + // Returns the type of the real part of an arithmetic type + template struct RealType { using type = T; }; + template struct RealType> { using type = T; }; + + template + typename std::enable_if::value, void>::type + Sequential_Init( ETensor &ET, typename EigenIO::Traits::scalar_type Inc = Flag::scalar_type> ) + { + using Traits = EigenIO::Traits; + using scalar_type = typename Traits::scalar_type; + for_all( ET, [&](scalar_type &c, typename ETensor::Index n, const std::array &Dims ) { + c = Inc * static_cast::type>(n); + } ); + } + // Helper to dump a tensor #ifdef DEBUG #define dump_tensor(args...) dump_tensor_func(args) @@ -774,7 +792,7 @@ namespace Grid { typename std::enable_if::value, void>::type Reader::Reshape(ETensor &t, const std::array &dims ) { - //assert( 0 && "EigenIO: Fixed tensor dimensions can't be changed" ); + assert( 0 && "EigenIO: Fixed tensor dimensions can't be changed" ); } template diff --git a/tests/IO/Test_serialisation.cc b/tests/IO/Test_serialisation.cc index 73045155..ae61eebe 100644 --- a/tests/IO/Test_serialisation.cc +++ b/tests/IO/Test_serialisation.cc @@ -105,7 +105,8 @@ void ioTest(const std::string &filename, const O &object, const std::string &nam std::cout << " done." << std::endl; } -#ifdef DEBUG +#ifdef HAVE_HDF5 +typedef Eigen::Tensor ShortRank5Tensor; //typedef int TestScalar; typedef std::complex TestScalar; typedef Eigen::Tensor TestTensor; @@ -118,24 +119,32 @@ class PerambIOTestClass: Serializable { public: using PerambTensor = Eigen::Tensor; GRID_SERIALIZABLE_CLASS_MEMBERS(PerambIOTestClass - //, SpinColourVector, scv - //, SpinColourMatrix, scm - , PerambTensor, Perambulator + , ShortRank5Tensor, shortRank5Tensor + , PerambTensor, Perambulator , std::vector, DistilParameterNames , std::vector, DistilParameterValues + , PerambTensor, Perambulator2 + , SpinColourVector, scv + , SpinColourMatrix, scm //, TestTensor, Critter //, TestTensorFixed, FixedCritter //, aTestTensorFixed, aFixedCritter //, LSCTensor, MyLSCTensor //, LCMTensor, MyLCMTensor ); - PerambIOTestClass() : Perambulator(2,3,1,4,5,1), + PerambIOTestClass() : Perambulator(2,3,1,4,5,1), Perambulator2(7,1,6,1,5,1), DistilParameterNames {"alpha", "beta", "gamma", "delta", "epsilon", "what's f?"}, - DistilParameterValues{2,3,1,4,5,1}//, Critter(7,3,2), aFixedCritter(3) - {} + DistilParameterValues{2,3,1,4,5,1} + , shortRank5Tensor{5,4,3,2,1} + //, Critter(7,3,2)//, aFixedCritter(3) + { + Sequential_Init(Perambulator); + Sequential_Init(Perambulator2, {-3.1415927,7}); + Sequential_Init(shortRank5Tensor); + } }; -bool EigenIOTest(void) { +void EigenHdf5IOTest(void) { SpinColourVector scv, scv2; scv2 = scv; ioTest("iotest_vector.h5", scv, "SpinColourVector"); @@ -182,11 +191,8 @@ bool EigenIOTest(void) { //dump_tensor(tf, "tf"); PerambIOTestClass o; - for_all( o.Perambulator, [&](TestScalar &c, float f, const std::array::rank_non_trivial> &Dims ){ - c = TestScalar{f,-f}; - //std::cout << " a(" << Dims[0] << "," << Dims[1] << "," << Dims[2] << ")=" << c; - } ); - dump_tensor(o.Perambulator, "PerambIOTestClass" ); + //dump_tensor(o.Perambulator, "Perambulator" ); + dump_tensor(o.shortRank5Tensor, "shortRank5Tensor"); /*for_all( o.FixedCritter, [&](TestScalar &c, float f, const std::array &Dims ){ c = TestScalar{f,-f}; //std::cout << " a(" << Dims[0] << "," << Dims[1] << "," << Dims[2] << ")=" << c; @@ -229,10 +235,6 @@ bool EigenIOTest(void) { } ioTest("iotest_LCMTensor.h5", l2, "LCMTensor_object_instance_name"); //dump_tensor(l2, "l2"); - - std::cout << "Wow!" << std::endl; - - return true; } #endif @@ -259,7 +261,6 @@ int main(int argc,char **argv) Grid_init(&argc,&argv); std::cout << std::boolalpha << "==== basic IO" << std::endl; // display true / false for boolean -#ifndef DEBUG GridSerialRNG rng; rng.SeedFixedIntegers(std::vector({42,10,81,9})); @@ -283,7 +284,6 @@ int main(int argc,char **argv) // test serializable class writing myclass obj(1234); // non-trivial constructor std::vector vec; - std::pair pair; std::cout << "-- serialisable class writing to 'bother.xml'..." << std::endl; write(WR,"obj",obj); @@ -291,7 +291,6 @@ int main(int argc,char **argv) vec.push_back(obj); vec.push_back(myclass(5678)); vec.push_back(myclass(3838)); - pair = std::make_pair(myenum::red, myenum::blue); write(WR, "objvec", vec); std::cout << "-- serialisable class writing to std::cout:" << std::endl; @@ -300,6 +299,7 @@ int main(int argc,char **argv) std::cout << "vec[0] == obj: " << (vec[0] == obj) << std::endl; std::cout << "vec[1] == obj: " << (vec[1] == obj) << std::endl; std::cout << "-- pair writing to std::cout:" << std::endl; + std::pair pair = std::make_pair(myenum::red, myenum::blue); std::cout << pair << std::endl; // read tests @@ -321,6 +321,8 @@ int main(int argc,char **argv) #ifdef HAVE_HDF5 ioTest("iotest.h5", obj, "HDF5 (object) "); ioTest("iotest.h5", vec, "HDF5 (vector of objects)"); + std::cout << "\n==== detailed Hdf5 tensor tests (Grid::EigenIO)" << std::endl; + EigenHdf5IOTest(); #endif std::cout << "\n==== vector flattening/reconstruction" << std::endl; @@ -364,8 +366,6 @@ int main(int argc,char **argv) tensorConvTest(rng, ColourVector); tensorConvTest(rng, SpinMatrix); tensorConvTest(rng, SpinVector); -#elif HAVE_HDF5 - if(! EigenIOTest() ) exit(EXIT_FAILURE); -#endif + Grid_finalize(); }