1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-09-20 01:05:38 +01:00

Serialisation of an object containing an Eigen::Tensor works for Hdf5. Still quite a lot of tidying up to do.

This commit is contained in:
Michael Marshall 2019-02-10 23:19:20 +00:00
parent d5024bd07e
commit 6f2663edf6
3 changed files with 51 additions and 14 deletions

View File

@ -378,8 +378,20 @@ namespace Grid {
{
return os;
}
template <typename T>
static inline bool CompareMember(const T &lhs, const T &rhs) {
return lhs == rhs;
}
template <typename Scalar_, int NumIndices_, int Options_, typename Index_>
static inline bool CompareMember(const Eigen::Tensor<Scalar_, NumIndices_, Options_, Index_> &lhs,
const Eigen::Tensor<Scalar_, NumIndices_, Options_, Index_> &rhs) {
Eigen::Tensor<bool, 0, Options_, Index_> bResult = (lhs == rhs).all();
return bResult(0);
}
};
// Generic writer interface //////////////////////////////////////////////////
template <typename T>
inline void push(Writer<T> &w, const std::string &s) {

View File

@ -109,7 +109,7 @@ THE SOFTWARE.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define GRID_MACRO_MEMBER(A,B) A B;
#define GRID_MACRO_COMP_MEMBER(A,B) result = (result and (lhs. B == rhs. B));
#define GRID_MACRO_COMP_MEMBER(A,B) result = (result and CompareMember(lhs. B, rhs. B));
#define GRID_MACRO_OS_WRITE_MEMBER(A,B) os<< #A <<" " #B << " = " << obj. B << " ; " <<std::endl;
#define GRID_MACRO_READ_MEMBER(A,B) Grid::read(RD,#B,obj. B);
#define GRID_MACRO_WRITE_MEMBER(A,B) Grid::write(WR,#B,obj. B);

View File

@ -441,21 +441,21 @@ bool DebugEigenTest()
}
typedef iMatrix<Complex,7> OddBall;
typedef Eigen::Tensor<OddBall, 3, Eigen::RowMajor> TensorOddBall;
typedef Eigen::Tensor<OddBall, 3> TensorOddBall;
typedef int TestScalar;
//typedef std::complex<double> TestScalar;
typedef Eigen::Tensor<TestScalar, 3, Eigen::RowMajor> TestTensor;
//typedef int TestScalar;
typedef std::complex<double> TestScalar;
typedef Eigen::Tensor<TestScalar, 3> TestTensor;
// From Test_serialisation.cc
class myclass: Serializable {
public:
GRID_SERIALIZABLE_CLASS_MEMBERS(myclass
//, TestTensor, critter
, TestTensor, critter
, SpinColourVector, scv
, SpinColourMatrix, scm
);
//myclass() : critter(7,3,2) {}
myclass() : critter(7,3,2) {}
};
template <typename W, typename R, typename O>
@ -479,6 +479,30 @@ bool ioTest(const std::string &filename, const O &object, const std::string &nam
return true;
}
/*
template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
eval Eigen::TensorForcedEvalOp<const Eigen::TensorCwiseBinaryOp<Eigen::internal::scalar_cmp_op<Scalar_, Scalar_, Eigen::internal::cmp_EQ>, const Eigen::Tensor<Scalar_, NumIndices_, Options_, IndexType_>, const Eigen::Tensor<Scalar_, NumIndices_, Options_, IndexType_> >, Eigen::MakePointer>
template <typename T>
std::ostream& operator << (std::ostream& os, const TensorBase<T, ReadOnlyAccessors>& expr) {
typedef TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice> Evaluator;
typedef typename Evaluator::Dimensions Dimensions;
// Evaluate the expression if needed
TensorForcedEvalOp<const T> eval = expr.eval();
Evaluator tensor(eval, DefaultDevice());
tensor.evalSubExprsIfNeeded(NULL);
// Print the result
static const int rank = internal::array_size<Dimensions>::value;
internal::TensorPrinter<Evaluator, rank>::run(os, tensor);
// Cleanup.
tensor.cleanup();
return os;
}
*/
bool DebugIOTest(void) {
OddBall critter;
ioTest<Hdf5Writer, Hdf5Reader, OddBall>("iotest_oddball.h5", critter, "OddBall");
@ -499,12 +523,13 @@ bool DebugIOTest(void) {
ioTest<Hdf5Writer, Hdf5Reader, TestTensor>("iotest_tensor.h5", t, "eigen_tensor_instance_name");
TestTensor t2(t);
auto bEqual = (t == t2).all();
std::cout << "(t2 == t) = " << bEqual << std::endl;
//if( * bEqual.data() != 0 )
//std::cout << "t2 == t" << std::endl;
//else
//std::cout << "t2 != t" << std::endl;
t2(1,1,1) += Inc;
Eigen::Tensor<bool, 0> rResult = (t == t2).all();
if( rResult(0) )
std::cout << "t2 == t" << std::endl;
else
std::cout << "t2 != t" << std::endl;
//std::cout << "(t == t2) : " << (t == t2).all()(0) << std::endl;
myclass o;
ioTest<Hdf5Writer, Hdf5Reader, myclass>("iotest_object.h5", o, "myclass_object_instance_name");