1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-09 21:50:45 +01:00

NamedTensor changes done

This commit is contained in:
Michael Marshall 2019-11-12 17:31:42 +00:00
parent b0f24ec302
commit 6d7043e0c2
12 changed files with 77 additions and 74 deletions

View File

@ -65,13 +65,12 @@
#include <Hadrons/Modules/MScalarSUN/StochFreeField.hpp> #include <Hadrons/Modules/MScalarSUN/StochFreeField.hpp>
#include <Hadrons/Modules/MScalarSUN/Div.hpp> #include <Hadrons/Modules/MScalarSUN/Div.hpp>
#include <Hadrons/Modules/MScalarSUN/TrKinetic.hpp> #include <Hadrons/Modules/MScalarSUN/TrKinetic.hpp>
#include <Hadrons/Modules/MDistil/PerambFromSolve.hpp>
#include <Hadrons/Modules/MDistil/DistilVectors.hpp>
#include <Hadrons/Modules/MDistil/Noises.hpp>
#include <Hadrons/Modules/MDistil/LapEvec.hpp>
#include <Hadrons/Modules/MDistil/Perambulator.hpp>
#include <Hadrons/Modules/MDistil/DistilPar.hpp> #include <Hadrons/Modules/MDistil/DistilPar.hpp>
#include <Hadrons/Modules/MDistil/DistilCommon.hpp> #include <Hadrons/Modules/MDistil/DistilVectors.hpp>
#include <Hadrons/Modules/MDistil/LapEvec.hpp>
#include <Hadrons/Modules/MDistil/Noises.hpp>
#include <Hadrons/Modules/MDistil/PerambFromSolve.hpp>
#include <Hadrons/Modules/MDistil/Perambulator.hpp>
#include <Hadrons/Modules/MAction/ZMobiusDWF.hpp> #include <Hadrons/Modules/MAction/ZMobiusDWF.hpp>
#include <Hadrons/Modules/MAction/ScaledDWF.hpp> #include <Hadrons/Modules/MAction/ScaledDWF.hpp>
#include <Hadrons/Modules/MAction/WilsonClover.hpp> #include <Hadrons/Modules/MAction/WilsonClover.hpp>

View File

@ -2,7 +2,7 @@
Grid physics library, www.github.com/paboyle/Grid Grid physics library, www.github.com/paboyle/Grid
Source file: Hadrons/Modules/MDistil/DistilCommon.hpp Source file: Hadrons/Modules/MDistil/Distil.hpp
Copyright (C) 2015-2019 Copyright (C) 2015-2019
@ -27,10 +27,10 @@
*************************************************************************************/ *************************************************************************************/
/* END LEGAL */ /* END LEGAL */
#ifndef Hadrons_MDistil_DistilCommon_hpp_ #ifndef Hadrons_MDistil_Distil_hpp_
#define Hadrons_MDistil_DistilCommon_hpp_ #define Hadrons_MDistil_Distil_hpp_
#include <Hadrons/Distil.hpp> #include <Hadrons/NamedTensor.hpp>
#include <Hadrons/Module.hpp> #include <Hadrons/Module.hpp>
#include <Hadrons/ModuleFactory.hpp> #include <Hadrons/ModuleFactory.hpp>
#include <Hadrons/Solver.hpp> #include <Hadrons/Solver.hpp>
@ -105,14 +105,18 @@ inline void RotateEigen(std::vector<LatticeColourVector> & evec)
Grid::Complex cplx0 = cv0()()(0); Grid::Complex cplx0 = cv0()()(0);
if( cplx0.imag() == 0 ) if( cplx0.imag() == 0 )
std::cout << GridLogMessage << "RotateEigen() : Site 0 : " << cplx0 << " => already meets phase convention" << std::endl; std::cout << GridLogMessage << "RotateEigen() : Site 0 : " << cplx0 << " => already meets phase convention" << std::endl;
else { else
{
const Real cplx0_mag = Grid::sqrt(cplx0.real()*cplx0.real()+cplx0.imag()*cplx0.imag());
Grid::Complex phase{cplx0 / Grid::Complex(cplx0_mag, 0) };
phase.imag(-phase.imag());
#ifdef GRID_NVCC #ifdef GRID_NVCC
const Real cplx0_mag = thrust::abs(cplx0); //const Real cplx0_mag = thrust::abs(cplx0);
const Grid::Complex phase = thrust::conj(cplx0 / cplx0_mag); //const Grid::Complex phase = thrust::conj(cplx0 / cplx0_mag);
const Real argphase = thrust::arg(phase); const Real argphase = thrust::arg(phase);
#else #else
const Real cplx0_mag = std::abs(cplx0); //const Real cplx0_mag = std::abs(cplx0);
const Grid::Complex phase = std::conj(cplx0 / cplx0_mag); //const Grid::Complex phase = std::conj(cplx0 / cplx0_mag);
const Real argphase = std::arg(phase); const Real argphase = std::arg(phase);
#endif #endif
std::cout << GridLogMessage << "RotateEigen() : Site 0 : |" << cplx0 << "|=" << cplx0_mag << " => phase=" << (argphase / 3.14159265) << " pi" << std::endl; std::cout << GridLogMessage << "RotateEigen() : Site 0 : |" << cplx0 << "|=" << cplx0_mag << " => phase=" << (argphase / 3.14159265) << " pi" << std::endl;
@ -133,4 +137,4 @@ inline void RotateEigen(std::vector<LatticeColourVector> & evec)
END_MODULE_NAMESPACE END_MODULE_NAMESPACE
END_HADRONS_NAMESPACE END_HADRONS_NAMESPACE
#endif // Hadrons_MDistil_DistilCommon_hpp_ #endif // Hadrons_MDistil_Distil_hpp_

View File

@ -30,10 +30,7 @@
#ifndef Hadrons_MDistil_DistilPar_hpp_ #ifndef Hadrons_MDistil_DistilPar_hpp_
#define Hadrons_MDistil_DistilPar_hpp_ #define Hadrons_MDistil_DistilPar_hpp_
#include <Hadrons/Global.hpp> #include <Hadrons/Modules/MDistil/Distil.hpp>
#include <Hadrons/Module.hpp>
#include <Hadrons/ModuleFactory.hpp>
#include <Hadrons/Modules/MDistil/DistilCommon.hpp>
BEGIN_HADRONS_NAMESPACE BEGIN_HADRONS_NAMESPACE

View File

@ -30,7 +30,7 @@
#ifndef Hadrons_MDistil_DistilVectors_hpp_ #ifndef Hadrons_MDistil_DistilVectors_hpp_
#define Hadrons_MDistil_DistilVectors_hpp_ #define Hadrons_MDistil_DistilVectors_hpp_
#include <Hadrons/Modules/MDistil/DistilCommon.hpp> #include <Hadrons/Modules/MDistil/Distil.hpp>
BEGIN_HADRONS_NAMESPACE BEGIN_HADRONS_NAMESPACE
BEGIN_MODULE_NAMESPACE(MDistil) BEGIN_MODULE_NAMESPACE(MDistil)

View File

@ -30,7 +30,7 @@
#ifndef Hadrons_MDistil_LapEvec_hpp_ #ifndef Hadrons_MDistil_LapEvec_hpp_
#define Hadrons_MDistil_LapEvec_hpp_ #define Hadrons_MDistil_LapEvec_hpp_
#include <Hadrons/Modules/MDistil/DistilCommon.hpp> #include <Hadrons/Modules/MDistil/Distil.hpp>
BEGIN_HADRONS_NAMESPACE BEGIN_HADRONS_NAMESPACE
BEGIN_MODULE_NAMESPACE(MDistil) BEGIN_MODULE_NAMESPACE(MDistil)

View File

@ -30,7 +30,7 @@
#ifndef Hadrons_MDistil_Noises_hpp_ #ifndef Hadrons_MDistil_Noises_hpp_
#define Hadrons_MDistil_Noises_hpp_ #define Hadrons_MDistil_Noises_hpp_
#include <Hadrons/Modules/MDistil/DistilCommon.hpp> #include <Hadrons/Modules/MDistil/Distil.hpp>
BEGIN_HADRONS_NAMESPACE BEGIN_HADRONS_NAMESPACE
BEGIN_MODULE_NAMESPACE(MDistil) BEGIN_MODULE_NAMESPACE(MDistil)

View File

@ -30,7 +30,7 @@
#ifndef Hadrons_MDistil_PerambFromSolve_hpp_ #ifndef Hadrons_MDistil_PerambFromSolve_hpp_
#define Hadrons_MDistil_PerambFromSolve_hpp_ #define Hadrons_MDistil_PerambFromSolve_hpp_
#include <Hadrons/Modules/MDistil/DistilCommon.hpp> #include <Hadrons/Modules/MDistil/Distil.hpp>
BEGIN_HADRONS_NAMESPACE BEGIN_HADRONS_NAMESPACE
BEGIN_MODULE_NAMESPACE(MDistil) BEGIN_MODULE_NAMESPACE(MDistil)

View File

@ -47,11 +47,11 @@ extern const std::string NamedTensorFileExtension{".dat"};
BEGIN_MODULE_NAMESPACE(MDistil) BEGIN_MODULE_NAMESPACE(MDistil)
const std::string NoiseTensor::Name_{"Noises"}; const std::string NoiseTensor::Name__{"Noises"};
const std::vector<std::string> NoiseTensor::DefaultIndexNames_{"nNoise", "nT", "nVec", "nS"}; const std::array<std::string, 4> NoiseTensor::DefaultIndexNames__{"nNoise", "nT", "nVec", "nS"};
const std::string PerambTensor::Name_{"Perambulator"}; const std::string PerambTensor::Name__{"Perambulator"};
const std::vector<std::string> PerambTensor::DefaultIndexNames_{"nT", "nVec", "LI", "nNoise", "nT_inv", "SI"}; const std::array<std::string, 6> PerambTensor::DefaultIndexNames__{"nT", "nVec", "LI", "nNoise", "nT_inv", "SI"};
END_MODULE_NAMESPACE END_MODULE_NAMESPACE
END_HADRONS_NAMESPACE END_HADRONS_NAMESPACE

View File

@ -30,7 +30,7 @@
#ifndef Hadrons_MDistil_Perambulator_hpp_ #ifndef Hadrons_MDistil_Perambulator_hpp_
#define Hadrons_MDistil_Perambulator_hpp_ #define Hadrons_MDistil_Perambulator_hpp_
#include <Hadrons/Modules/MDistil/DistilCommon.hpp> #include <Hadrons/Modules/MDistil/Distil.hpp>
BEGIN_HADRONS_NAMESPACE BEGIN_HADRONS_NAMESPACE
BEGIN_MODULE_NAMESPACE(MDistil) BEGIN_MODULE_NAMESPACE(MDistil)

View File

@ -30,7 +30,7 @@
#ifndef Hadrons_MIO_LoadDistilNoise_hpp_ #ifndef Hadrons_MIO_LoadDistilNoise_hpp_
#define Hadrons_MIO_LoadDistilNoise_hpp_ #define Hadrons_MIO_LoadDistilNoise_hpp_
#include <Hadrons/Modules/MDistil/DistilCommon.hpp> #include <Hadrons/Modules/MDistil/Distil.hpp>
BEGIN_HADRONS_NAMESPACE BEGIN_HADRONS_NAMESPACE
BEGIN_MODULE_NAMESPACE(MIO) BEGIN_MODULE_NAMESPACE(MIO)

View File

@ -30,7 +30,7 @@
#ifndef Hadrons_MIO_LoadPerambulator_hpp_ #ifndef Hadrons_MIO_LoadPerambulator_hpp_
#define Hadrons_MIO_LoadPerambulator_hpp_ #define Hadrons_MIO_LoadPerambulator_hpp_
#include <Hadrons/Modules/MDistil/DistilCommon.hpp> #include <Hadrons/Modules/MDistil/Distil.hpp>
BEGIN_HADRONS_NAMESPACE BEGIN_HADRONS_NAMESPACE
BEGIN_MODULE_NAMESPACE(MIO) BEGIN_MODULE_NAMESPACE(MIO)

View File

@ -2,7 +2,7 @@
Grid physics library, www.github.com/paboyle/Grid Grid physics library, www.github.com/paboyle/Grid
Source file: Hadrons/Distil.hpp Source file: Hadrons/NamedTensor.hpp
Copyright (C) 2015-2019 Copyright (C) 2015-2019
@ -27,8 +27,8 @@
*************************************************************************************/ *************************************************************************************/
/* END LEGAL */ /* END LEGAL */
#ifndef Hadrons_Distil_hpp_ #ifndef Hadrons_NamedTensor_hpp_
#define Hadrons_Distil_hpp_ #define Hadrons_NamedTensor_hpp_
#include <Hadrons/Global.hpp> #include <Hadrons/Global.hpp>
#include <Hadrons/EigenPack.hpp> #include <Hadrons/EigenPack.hpp>
@ -36,11 +36,11 @@
BEGIN_HADRONS_NAMESPACE BEGIN_HADRONS_NAMESPACE
/****************************************************************************** /******************************************************************************
NamedTensor NamedTensor contains:
Eigen::Tensor of type Scalar_ and rank NumIndices_ (row-major order), together with a name for each index. 1) Name of the tensor. By default, this is the tag name used for save / load
Index names are mutable, but tensor dimensionality is not (size of each dimension is mutable). 2) Eigen::Tensor of type Scalar_ and rank NumIndices_ (row-major order)
They can be persisted to / restored from disk, by default using tag Name. 3) Name for each index
During restore from disk, these validations are performed: They can be persisted to / restored from disk. During restore, these validations are performed:
1) Tensor dimensionality must match 1) Tensor dimensionality must match
2) IndexNames are validated against current values 2) IndexNames are validated against current values
3) If the tensor has non-zero size, the tensor being loaded must have same extent in each dimension 3) If the tensor has non-zero size, the tensor being loaded must have same extent in each dimension
@ -61,37 +61,39 @@ public:
std::vector<std::string>, IndexNames ); std::vector<std::string>, IndexNames );
// Name of the object and Index names as set in the constructor // Name of the object and Index names as set in the constructor
const std::string &Name; const std::string &Name_;
const std::vector<std::string> &DefaultIndexNames; const std::array<std::string, NumIndices_> &DefaultIndexNames_;
virtual ~NamedTensor(){};
// Default constructor (assumes tensor will be loaded from file) // Default constructor (assumes tensor will be loaded from file)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NamedTensor(const std::string &Name_, const std::vector<std::string> &IndexNames_) EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NamedTensor(const std::string &Name,
: IndexNames{IndexNames_}, Name{Name_}, DefaultIndexNames{IndexNames_} {} const std::array<std::string, NumIndices_> &indexNames)
: IndexNames{indexNames.begin(), indexNames.end()}, Name_{Name}, DefaultIndexNames_{indexNames} {}
// Construct a named tensor explicitly specifying size of each dimension // Construct a named tensor explicitly specifying size of each dimension
template<typename... IndexTypes> template<typename... IndexTypes>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NamedTensor(const std::string &Name_, const std::vector<std::string> &IndexNames_, EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NamedTensor(const std::string &Name,
const std::array<std::string, NumIndices> &indexNames,
Eigen::Index firstDimension, IndexTypes... otherDimensions) Eigen::Index firstDimension, IndexTypes... otherDimensions)
: tensor(firstDimension, otherDimensions...), IndexNames{IndexNames_}, Name{Name_}, DefaultIndexNames{IndexNames_} : tensor(firstDimension, otherDimensions...),
IndexNames{indexNames.begin(), indexNames.end()}, Name_{Name}, DefaultIndexNames_{indexNames}
{ {
assert(sizeof...(otherDimensions) + 1 == NumIndices_ && "NamedTensor: dimensions != tensor rank"); if(sizeof...(otherDimensions) + 1 != NumIndices)
HADRONS_ERROR(Argument, "NamedTensor: dimensions != tensor rank");
} }
// Do my index names match the default for my type? // Do my index names match the default for my type?
bool ValidateIndexNames( const std::vector<std::string> &CheckNames ) const template<typename array_or_vector_of_string>
bool ValidateIndexNames( const array_or_vector_of_string &CheckNames ) const
{ {
assert( CheckNames.size() == NumIndices_ && "Bug: CheckNames don't match NumIndices_" ); return IndexNames.size() == CheckNames.size() && std::equal( IndexNames.begin(), IndexNames.end(), CheckNames.begin(),
bool bSame{ IndexNames.size() == NumIndices_ }; [](const std::string &s1, const std::string &s2)
for( std::size_t i = 0; bSame && i < NumIndices_; i++ ) {
{ return s1.size() == s2.size() && std::equal( s1.begin(), s1.end(), s2.begin(),
bSame = IndexNames[i].size() == CheckNames[i].size() [](const char & c1, const char & c2)
&& std::equal( IndexNames[i].begin(), IndexNames[i].end(), CheckNames[i].begin(), { return c1 == c2 || std::toupper(c1) == std::toupper(c2); }); // case insensitive
[](const char & c1, const char & c2){ return c1 == c2 || std::toupper(c1) == std::toupper(c2); }); });
}
return bSame;
} }
bool ValidateIndexNames() const { return ValidateIndexNames(DefaultIndexNames); } bool ValidateIndexNames() const { return ValidateIndexNames(DefaultIndexNames_); }
#ifdef HAVE_HDF5 #ifdef HAVE_HDF5
using Default_Reader = Grid::Hdf5Reader; using Default_Reader = Grid::Hdf5Reader;
@ -105,11 +107,11 @@ public:
{ {
std::string FileName_{FileName}; std::string FileName_{FileName};
FileName_.append( NamedTensorFileExtension ); FileName_.append( NamedTensorFileExtension );
LOG(Message) << "Writing " << Name << " to file " << FileName_ << " tag " << Tag << std::endl; LOG(Message) << "Writing " << Name_ << " to file " << FileName_ << " tag " << Tag << std::endl;
Default_Writer w( FileName_ ); Default_Writer w( FileName_ );
write( w, Tag, *this ); write( w, Tag, *this );
} }
void write(const std::string &FileName) const { return write(FileName, Name); } void write(const std::string &FileName) const { return write(FileName, Name_); }
// Read tensor. // Read tensor.
// Validate: // Validate:
@ -123,18 +125,19 @@ public:
read(r, Tag, *this); read(r, Tag, *this);
const typename ET::Dimensions & NewDimensions{tensor.dimensions()}; const typename ET::Dimensions & NewDimensions{tensor.dimensions()};
for (int i = 0; i < NumIndices_; i++) for (int i = 0; i < NumIndices_; i++)
assert(OldDimensions[i] == 0 || OldDimensions[i] == NewDimensions[i] && "NamedTensor::read dimension size"); if(OldDimensions[i] && OldDimensions[i] != NewDimensions[i])
if (bValidate) HADRONS_ERROR(Size,"NamedTensor::read dimension size");
assert(ValidateIndexNames(OldIndexNames) && "NamedTensor::read dimension name"); if (bValidate && !ValidateIndexNames(OldIndexNames))
HADRONS_ERROR(Definition,"NamedTensor::read dimension name");
} }
template<typename Reader> void read(Reader &r, bool bValidate = true) { read(r, bValidate, Name); } template<typename Reader> void read(Reader &r, bool bValidate = true) { read(r, bValidate, Name_); }
inline void read (const std::string &FileName, bool bValidate, const std::string &Tag) inline void read (const std::string &FileName, bool bValidate, const std::string &Tag)
{ {
Default_Reader r(FileName + NamedTensorFileExtension); Default_Reader r(FileName + NamedTensorFileExtension);
read(r, bValidate, Tag); read(r, bValidate, Tag);
} }
inline void read (const std::string &FileName, bool bValidate= true) { return read(FileName, bValidate, Name); } inline void read (const std::string &FileName, bool bValidate= true) { return read(FileName, bValidate, Name_); }
}; };
/****************************************************************************** /******************************************************************************
@ -150,32 +153,32 @@ using LapEvecs = Grid::Hadrons::EigenPack<LatticeColourVector>;
class NoiseTensor : public NamedTensor<Complex, 4> class NoiseTensor : public NamedTensor<Complex, 4>
{ {
static const std::string Name_; static const std::string Name__;
static const std::vector<std::string> DefaultIndexNames_; static const std::array<std::string, 4> DefaultIndexNames__;
public: public:
// Default constructor (assumes tensor will be loaded from file) // Default constructor (assumes tensor will be loaded from file)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NoiseTensor() : NamedTensor{Name_, DefaultIndexNames_} {} EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NoiseTensor() : NamedTensor{Name__, DefaultIndexNames__} {}
// Construct a named tensor explicitly specifying size of each dimension // Construct a named tensor explicitly specifying size of each dimension
template<typename... IndexTypes> template<typename... IndexTypes>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NoiseTensor(Eigen::Index nNoise, Eigen::Index nT, Eigen::Index nVec, Eigen::Index nS) EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NoiseTensor(Eigen::Index nNoise, Eigen::Index nT, Eigen::Index nVec, Eigen::Index nS)
: NamedTensor{Name_, DefaultIndexNames_, nNoise, nT, nVec, nS} {} : NamedTensor{Name__, DefaultIndexNames__, nNoise, nT, nVec, nS} {}
}; };
class PerambTensor : public NamedTensor<SpinVector, 6> class PerambTensor : public NamedTensor<SpinVector, 6>
{ {
static const std::string Name_; static const std::string Name__;
static const std::vector<std::string> DefaultIndexNames_; static const std::array<std::string, 6> DefaultIndexNames__;
public: public:
// Default constructor (assumes tensor will be loaded from file) // Default constructor (assumes tensor will be loaded from file)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PerambTensor() : NamedTensor{Name_, DefaultIndexNames_} {} EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PerambTensor() : NamedTensor{Name__, DefaultIndexNames__} {}
// Construct a named tensor explicitly specifying size of each dimension // Construct a named tensor explicitly specifying size of each dimension
template<typename... IndexTypes> template<typename... IndexTypes>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PerambTensor(Eigen::Index nT, Eigen::Index nVec, Eigen::Index LI, Eigen::Index nNoise, Eigen::Index nT_inv, Eigen::Index SI) EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PerambTensor(Eigen::Index nT, Eigen::Index nVec, Eigen::Index LI, Eigen::Index nNoise, Eigen::Index nT_inv, Eigen::Index SI)
: NamedTensor{Name_, DefaultIndexNames_, nT, nVec, LI, nNoise, nT_inv, SI} {} : NamedTensor{Name__, DefaultIndexNames__, nT, nVec, LI, nNoise, nT_inv, SI} {}
}; };
END_MODULE_NAMESPACE END_MODULE_NAMESPACE
END_HADRONS_NAMESPACE END_HADRONS_NAMESPACE
#endif // Hadrons_Distil_hpp_ #endif // Hadrons_NamedTensor_hpp_