mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-09 21:50:45 +01:00
Can write both fixed and dynamic sized tensors (small tidy)
This commit is contained in:
parent
dff7d9261d
commit
9a225235b6
@ -62,9 +62,6 @@ namespace Grid {
|
|||||||
void write(const std::string &s, const iVector<U, N> &output);
|
void write(const std::string &s, const iVector<U, N> &output);
|
||||||
template <typename U, int N>
|
template <typename U, int N>
|
||||||
void write(const std::string &s, const iMatrix<U, N> &output);
|
void write(const std::string &s, const iMatrix<U, N> &output);
|
||||||
/*template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
|
|
||||||
typename std::enable_if<std::is_arithmetic<Scalar_>::value || Grid::is_complex<Scalar_>::value, void>::type
|
|
||||||
write(const std::string &s, const Eigen::Tensor<Scalar_, NumIndices_, Options_, IndexType_> &output);*/
|
|
||||||
template <typename ETensor>
|
template <typename ETensor>
|
||||||
typename std::enable_if<std::is_base_of<Eigen::TensorBase<ETensor, Eigen::ReadOnlyAccessors>, ETensor>::value && (std::is_arithmetic<typename ETensor::Scalar>::value || Grid::is_complex<typename ETensor::Scalar>::value), void>::type
|
typename std::enable_if<std::is_base_of<Eigen::TensorBase<ETensor, Eigen::ReadOnlyAccessors>, ETensor>::value && (std::is_arithmetic<typename ETensor::Scalar>::value || Grid::is_complex<typename ETensor::Scalar>::value), void>::type
|
||||||
write(const std::string &s, const ETensor &output);
|
write(const std::string &s, const ETensor &output);
|
||||||
@ -181,15 +178,10 @@ namespace Grid {
|
|||||||
|
|
||||||
// Eigen::Tensors of arithmetic/complex base type
|
// Eigen::Tensors of arithmetic/complex base type
|
||||||
template <typename T>
|
template <typename T>
|
||||||
/*template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
|
|
||||||
typename std::enable_if<std::is_arithmetic<Scalar_>::value || Grid::is_complex<Scalar_>::value, void>::type
|
|
||||||
Writer<T>::write(const std::string &s, const Eigen::Tensor<Scalar_, NumIndices_, Options_, IndexType_> &output)*/
|
|
||||||
template <typename ETensor>
|
template <typename ETensor>
|
||||||
typename std::enable_if<std::is_base_of<Eigen::TensorBase<ETensor, Eigen::ReadOnlyAccessors>, ETensor>::value && (std::is_arithmetic<typename ETensor::Scalar>::value || Grid::is_complex<typename ETensor::Scalar>::value), void>::type
|
typename std::enable_if<std::is_base_of<Eigen::TensorBase<ETensor, Eigen::ReadOnlyAccessors>, ETensor>::value && (std::is_arithmetic<typename ETensor::Scalar>::value || Grid::is_complex<typename ETensor::Scalar>::value), void>::type
|
||||||
Writer<T>::write(const std::string &s, const ETensor &output)
|
Writer<T>::write(const std::string &s, const ETensor &output)
|
||||||
{
|
{
|
||||||
//typedef Eigen::Tensor<Scalar_, NumIndices_, Options_, IndexType_> Tensor;
|
|
||||||
//typedef Eigen::TensorBase<ETensor, Eigen::ReadOnlyAccessors> Tensor;
|
|
||||||
const typename ETensor::Index NumElements{output.size()};
|
const typename ETensor::Index NumElements{output.size()};
|
||||||
assert( NumElements > 0 );
|
assert( NumElements > 0 );
|
||||||
if( NumElements == 1 )
|
if( NumElements == 1 )
|
||||||
@ -394,6 +386,18 @@ namespace Grid {
|
|||||||
Eigen::Tensor<bool, 0> bResult = (lhs == rhs).all();
|
Eigen::Tensor<bool, 0> bResult = (lhs == rhs).all();
|
||||||
return bResult(0);
|
return bResult(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static inline typename std::enable_if<std::is_base_of<Eigen::TensorBase<T, Eigen::ReadOnlyAccessors>, T>::value, bool>::type
|
||||||
|
CompareMember(const std::vector<T> &lhs, const std::vector<T> &rhs) {
|
||||||
|
const auto NumElements{lhs.size()};
|
||||||
|
bool bResult = ( NumElements == rhs.size() );
|
||||||
|
for( auto i = 0 ; i < NumElements && bResult ; i++ ) {
|
||||||
|
Eigen::Tensor<bool, 0> b = (lhs[i] == rhs[i]).all();
|
||||||
|
bResult = b(0);
|
||||||
|
}
|
||||||
|
return bResult;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Generic writer interface //////////////////////////////////////////////////
|
// Generic writer interface //////////////////////////////////////////////////
|
||||||
|
@ -440,26 +440,6 @@ bool DebugEigenTest()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef iMatrix<Complex,7> OddBall;
|
|
||||||
typedef Eigen::Tensor<OddBall, 3> TensorOddBall;
|
|
||||||
|
|
||||||
//typedef int TestScalar;
|
|
||||||
typedef std::complex<double> TestScalar;
|
|
||||||
typedef Eigen::Tensor<TestScalar, 3> TestTensor;
|
|
||||||
typedef Eigen::TensorFixedSize<TestScalar, Eigen::Sizes<9,4,2>> TestTensorFixed;
|
|
||||||
|
|
||||||
// From Test_serialisation.cc
|
|
||||||
class myclass: Serializable {
|
|
||||||
public:
|
|
||||||
GRID_SERIALIZABLE_CLASS_MEMBERS(myclass
|
|
||||||
, TestTensor, Critter
|
|
||||||
, TestTensorFixed, FixedCritter
|
|
||||||
, SpinColourVector, scv
|
|
||||||
, SpinColourMatrix, scm
|
|
||||||
);
|
|
||||||
myclass() : Critter(7,3,2) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename W, typename R, typename O>
|
template <typename W, typename R, typename O>
|
||||||
bool ioTest(const std::string &filename, const O &object, const std::string &name)
|
bool ioTest(const std::string &filename, const O &object, const std::string &name)
|
||||||
{
|
{
|
||||||
@ -470,80 +450,62 @@ bool ioTest(const std::string &filename, const O &object, const std::string &nam
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*R reader(filename);
|
/*R reader(filename);
|
||||||
O buf;
|
O buf;
|
||||||
bool good;
|
bool good;
|
||||||
|
|
||||||
read(reader, "testobject", buf);
|
read(reader, "testobject", buf);
|
||||||
good = (object == buf);
|
good = (object == buf);
|
||||||
std::cout << name << " IO test: " << (good ? "success" : "failure");
|
std::cout << name << " IO test: " << (good ? "success" : "failure");
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return good;*/
|
return good;*/
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
//typedef int TestScalar;
|
||||||
template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
|
typedef std::complex<double> TestScalar;
|
||||||
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>
|
typedef Eigen::Tensor<TestScalar, 3> TestTensor;
|
||||||
|
typedef Eigen::TensorFixedSize<TestScalar, Eigen::Sizes<9,4,2>> TestTensorFixed;
|
||||||
template <typename T>
|
typedef std::vector<TestTensorFixed> aTestTensorFixed;
|
||||||
std::ostream& operator << (std::ostream& os, const TensorBase<T, ReadOnlyAccessors>& expr) {
|
// From Test_serialisation.cc
|
||||||
typedef TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice> Evaluator;
|
class myclass: Serializable {
|
||||||
typedef typename Evaluator::Dimensions Dimensions;
|
public:
|
||||||
|
GRID_SERIALIZABLE_CLASS_MEMBERS(myclass
|
||||||
// Evaluate the expression if needed
|
, SpinColourVector, scv
|
||||||
TensorForcedEvalOp<const T> eval = expr.eval();
|
, SpinColourMatrix, scm
|
||||||
Evaluator tensor(eval, DefaultDevice());
|
, TestTensor, Critter
|
||||||
tensor.evalSubExprsIfNeeded(NULL);
|
, TestTensorFixed, FixedCritter
|
||||||
|
, aTestTensorFixed, aFixedCritter
|
||||||
// Print the result
|
);
|
||||||
static const int rank = internal::array_size<Dimensions>::value;
|
myclass() : Critter(7,3,2), aFixedCritter(3) {}
|
||||||
internal::TensorPrinter<Evaluator, rank>::run(os, tensor);
|
};
|
||||||
|
|
||||||
// Cleanup.
|
|
||||||
tensor.cleanup();
|
|
||||||
return os;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
template <typename T> constexpr T Inc{1,-1};
|
|
||||||
//template <> constexpr std::complex<double> Inc< < std::complex<double> > >{1,1};
|
|
||||||
|
|
||||||
bool DebugIOTest(void) {
|
bool DebugIOTest(void) {
|
||||||
OddBall critter;
|
|
||||||
ioTest<Hdf5Writer, Hdf5Reader, OddBall>("iotest_oddball.h5", critter, "OddBall");
|
|
||||||
SpinColourMatrix scm;
|
|
||||||
ioTest<Hdf5Writer, Hdf5Reader, SpinColourMatrix>("iotest_matrix.h5", scm, "SpinColourMatrix");
|
|
||||||
SpinColourVector scv;
|
SpinColourVector scv;
|
||||||
ioTest<Hdf5Writer, Hdf5Reader, SpinColourVector>("iotest_vector.h5", scv, "SpinColourVector");
|
ioTest<Hdf5Writer, Hdf5Reader, SpinColourVector>("iotest_vector.h5", scv, "SpinColourVector");
|
||||||
|
SpinColourMatrix scm;
|
||||||
|
ioTest<Hdf5Writer, Hdf5Reader, SpinColourMatrix>("iotest_matrix.h5", scm, "SpinColourMatrix");
|
||||||
|
|
||||||
|
constexpr TestScalar Inc{1,-1};
|
||||||
|
|
||||||
TestTensor t(3,6,2);
|
TestTensor t(3,6,2);
|
||||||
TestScalar Val{Inc<TestScalar>};
|
TestScalar Val{Inc};
|
||||||
for( int i = 0 ; i < 3 ; i++)
|
for( int i = 0 ; i < t.dimension(0) ; i++)
|
||||||
for( int j = 0 ; j < 6 ; j++)
|
for( int j = 0 ; j < t.dimension(1) ; j++)
|
||||||
for( int k = 0 ; k < 2 ; k++) {
|
for( int k = 0 ; k < t.dimension(2) ; k++) {
|
||||||
t(i,j,k) = Val;
|
t(i,j,k) = Val;
|
||||||
Val += Inc<TestScalar>;
|
Val += Inc;
|
||||||
}
|
}
|
||||||
ioTest<Hdf5Writer, Hdf5Reader, TestTensor>("iotest_tensor.h5", t, "eigen_tensor_instance_name");
|
ioTest<Hdf5Writer, Hdf5Reader, TestTensor>("iotest_tensor.h5", t, "eigen_tensor_instance_name");
|
||||||
|
|
||||||
TestTensor t2(t);
|
|
||||||
t2(1,1,1) += Inc<TestScalar>;
|
|
||||||
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;
|
|
||||||
|
|
||||||
// Now serialise a fixed size tensor
|
// Now serialise a fixed size tensor
|
||||||
using FixedTensor = Eigen::TensorFixedSize<TestScalar, Eigen::Sizes<8,4,3>>;
|
using FixedTensor = Eigen::TensorFixedSize<TestScalar, Eigen::Sizes<8,4,3>>;
|
||||||
FixedTensor tf;
|
FixedTensor tf;
|
||||||
Val = Inc<TestScalar>;
|
Val = Inc;
|
||||||
for( int i = 0 ; i < 8 ; i++)
|
for( int i = 0 ; i < tf.dimension(0) ; i++)
|
||||||
for( int j = 0 ; j < 4 ; j++)
|
for( int j = 0 ; j < tf.dimension(1) ; j++)
|
||||||
for( int k = 0 ; k < 3 ; k++) {
|
for( int k = 0 ; k < tf.dimension(2) ; k++) {
|
||||||
tf(i,j,k) = Val;
|
tf(i,j,k) = Val;
|
||||||
Val += Inc<TestScalar>;
|
Val += Inc;
|
||||||
}
|
}
|
||||||
ioTest<Hdf5Writer, Hdf5Reader, FixedTensor>("iotest_tensor_fixed.h5", tf, "eigen_tensor_fixed_name");
|
ioTest<Hdf5Writer, Hdf5Reader, FixedTensor>("iotest_tensor_fixed.h5", tf, "eigen_tensor_fixed_name");
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user