2019-01-22 17:59:55 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
2019-10-20 14:22:45 +01:00
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
2019-01-22 17:59:55 +00:00
|
|
|
|
2019-11-12 17:31:42 +00:00
|
|
|
Source file: Hadrons/NamedTensor.hpp
|
2019-11-02 14:58:32 +00:00
|
|
|
|
2019-01-22 17:59:55 +00:00
|
|
|
Copyright (C) 2015-2019
|
|
|
|
|
|
|
|
Author: Felix Erben <ferben@ed.ac.uk>
|
|
|
|
Author: Michael Marshall <Michael.Marshall@ed.ac.uk>
|
2019-11-02 14:58:32 +00:00
|
|
|
|
2019-01-22 17:59:55 +00:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
|
|
|
|
2019-11-12 17:31:42 +00:00
|
|
|
#ifndef Hadrons_NamedTensor_hpp_
|
|
|
|
#define Hadrons_NamedTensor_hpp_
|
2019-01-22 17:59:55 +00:00
|
|
|
|
|
|
|
#include <Hadrons/Global.hpp>
|
2019-04-30 23:53:57 +01:00
|
|
|
#include <Hadrons/EigenPack.hpp>
|
2019-02-01 15:20:35 +00:00
|
|
|
|
2019-11-01 20:10:51 +00:00
|
|
|
BEGIN_HADRONS_NAMESPACE
|
2019-04-28 20:23:44 +01:00
|
|
|
|
2019-01-22 17:59:55 +00:00
|
|
|
/******************************************************************************
|
2019-11-12 17:31:42 +00:00
|
|
|
NamedTensor contains:
|
|
|
|
1) Name of the tensor. By default, this is the tag name used for save / load
|
|
|
|
2) Eigen::Tensor of type Scalar_ and rank NumIndices_ (row-major order)
|
|
|
|
3) Name for each index
|
|
|
|
They can be persisted to / restored from disk. During restore, these validations are performed:
|
2019-11-08 11:55:00 +00:00
|
|
|
1) Tensor dimensionality must match
|
|
|
|
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
|
2019-01-22 17:59:55 +00:00
|
|
|
******************************************************************************/
|
|
|
|
|
2019-11-02 14:58:32 +00:00
|
|
|
extern const std::string NamedTensorFileExtension;
|
|
|
|
|
2019-11-08 11:55:00 +00:00
|
|
|
template<typename Scalar_, int NumIndices_>
|
2019-04-28 17:53:42 +01:00
|
|
|
class NamedTensor : Serializable
|
2019-01-31 15:06:52 +00:00
|
|
|
{
|
2019-02-01 15:20:35 +00:00
|
|
|
public:
|
2019-11-02 14:58:32 +00:00
|
|
|
using Scalar = Scalar_;
|
|
|
|
static constexpr int NumIndices = NumIndices_;
|
|
|
|
using ET = Eigen::Tensor<Scalar_, NumIndices_, Eigen::RowMajor>;
|
|
|
|
using Index = typename ET::Index;
|
|
|
|
GRID_SERIALIZABLE_CLASS_MEMBERS(NamedTensor,
|
|
|
|
ET, tensor,
|
|
|
|
std::vector<std::string>, IndexNames );
|
2019-11-08 11:55:00 +00:00
|
|
|
|
|
|
|
// Name of the object and Index names as set in the constructor
|
2019-11-12 17:31:42 +00:00
|
|
|
const std::string &Name_;
|
|
|
|
const std::array<std::string, NumIndices_> &DefaultIndexNames_;
|
2019-11-08 11:55:00 +00:00
|
|
|
|
2019-11-02 14:58:32 +00:00
|
|
|
// Default constructor (assumes tensor will be loaded from file)
|
2019-11-12 17:31:42 +00:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NamedTensor(const std::string &Name,
|
|
|
|
const std::array<std::string, NumIndices_> &indexNames)
|
|
|
|
: IndexNames{indexNames.begin(), indexNames.end()}, Name_{Name}, DefaultIndexNames_{indexNames} {}
|
2019-11-02 14:58:32 +00:00
|
|
|
|
|
|
|
// Construct a named tensor explicitly specifying size of each dimension
|
|
|
|
template<typename... IndexTypes>
|
2019-11-12 17:31:42 +00:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NamedTensor(const std::string &Name,
|
2019-11-13 00:02:51 +00:00
|
|
|
const std::array<std::string, NumIndices_> &indexNames,
|
2019-11-08 11:55:00 +00:00
|
|
|
Eigen::Index firstDimension, IndexTypes... otherDimensions)
|
2019-11-12 17:31:42 +00:00
|
|
|
: tensor(firstDimension, otherDimensions...),
|
|
|
|
IndexNames{indexNames.begin(), indexNames.end()}, Name_{Name}, DefaultIndexNames_{indexNames}
|
2019-11-02 14:58:32 +00:00
|
|
|
{
|
2019-11-13 00:02:51 +00:00
|
|
|
if(sizeof...(otherDimensions) + 1 != NumIndices_)
|
2019-11-12 17:31:42 +00:00
|
|
|
HADRONS_ERROR(Argument, "NamedTensor: dimensions != tensor rank");
|
2019-11-02 14:58:32 +00:00
|
|
|
}
|
2019-11-08 11:55:00 +00:00
|
|
|
|
2019-11-02 14:58:32 +00:00
|
|
|
// Do my index names match the default for my type?
|
2019-11-12 17:31:42 +00:00
|
|
|
template<typename array_or_vector_of_string>
|
|
|
|
bool ValidateIndexNames( const array_or_vector_of_string &CheckNames ) const
|
2019-11-02 14:58:32 +00:00
|
|
|
{
|
2019-11-12 17:31:42 +00:00
|
|
|
return IndexNames.size() == CheckNames.size() && std::equal( IndexNames.begin(), IndexNames.end(), CheckNames.begin(),
|
|
|
|
[](const std::string &s1, const std::string &s2)
|
|
|
|
{
|
|
|
|
return s1.size() == s2.size() && std::equal( s1.begin(), s1.end(), s2.begin(),
|
|
|
|
[](const char & c1, const char & c2)
|
|
|
|
{ return c1 == c2 || std::toupper(c1) == std::toupper(c2); }); // case insensitive
|
|
|
|
});
|
2019-11-02 14:58:32 +00:00
|
|
|
}
|
2019-11-12 17:31:42 +00:00
|
|
|
bool ValidateIndexNames() const { return ValidateIndexNames(DefaultIndexNames_); }
|
2019-11-08 11:55:00 +00:00
|
|
|
|
2019-11-02 14:58:32 +00:00
|
|
|
#ifdef HAVE_HDF5
|
|
|
|
using Default_Reader = Grid::Hdf5Reader;
|
|
|
|
using Default_Writer = Grid::Hdf5Writer;
|
|
|
|
#else
|
|
|
|
using Default_Reader = Grid::BinaryReader;
|
|
|
|
using Default_Writer = Grid::BinaryWriter;
|
|
|
|
#endif
|
|
|
|
|
2019-11-08 11:55:00 +00:00
|
|
|
void write(const std::string &FileName, const std::string &Tag) const
|
2019-11-02 14:58:32 +00:00
|
|
|
{
|
2019-11-08 11:55:00 +00:00
|
|
|
std::string FileName_{FileName};
|
|
|
|
FileName_.append( NamedTensorFileExtension );
|
2019-11-12 17:31:42 +00:00
|
|
|
LOG(Message) << "Writing " << Name_ << " to file " << FileName_ << " tag " << Tag << std::endl;
|
2019-11-08 11:55:00 +00:00
|
|
|
Default_Writer w( FileName_ );
|
|
|
|
write( w, Tag, *this );
|
2019-11-02 14:58:32 +00:00
|
|
|
}
|
2019-11-12 17:31:42 +00:00
|
|
|
void write(const std::string &FileName) const { return write(FileName, Name_); }
|
2019-11-08 11:55:00 +00:00
|
|
|
|
|
|
|
// Read tensor.
|
|
|
|
// Validate:
|
|
|
|
// 1) index names (if requested)
|
|
|
|
// 2) index dimensions (if they are non-zero when called)
|
|
|
|
template<typename Reader> void read(Reader &r, bool bValidate, const std::string &Tag)
|
2019-11-02 14:58:32 +00:00
|
|
|
{
|
|
|
|
// Grab index names and dimensions
|
|
|
|
std::vector<std::string> OldIndexNames{std::move(IndexNames)};
|
2019-11-08 11:55:00 +00:00
|
|
|
const typename ET::Dimensions OldDimensions{tensor.dimensions()};
|
2019-11-02 14:58:32 +00:00
|
|
|
read(r, Tag, *this);
|
|
|
|
const typename ET::Dimensions & NewDimensions{tensor.dimensions()};
|
|
|
|
for (int i = 0; i < NumIndices_; i++)
|
2019-11-12 17:31:42 +00:00
|
|
|
if(OldDimensions[i] && OldDimensions[i] != NewDimensions[i])
|
|
|
|
HADRONS_ERROR(Size,"NamedTensor::read dimension size");
|
|
|
|
if (bValidate && !ValidateIndexNames(OldIndexNames))
|
|
|
|
HADRONS_ERROR(Definition,"NamedTensor::read dimension name");
|
2019-11-02 14:58:32 +00:00
|
|
|
}
|
2019-11-12 17:31:42 +00:00
|
|
|
template<typename Reader> void read(Reader &r, bool bValidate = true) { read(r, bValidate, Name_); }
|
2019-11-08 11:55:00 +00:00
|
|
|
|
|
|
|
inline void read (const std::string &FileName, bool bValidate, const std::string &Tag)
|
2019-11-02 14:58:32 +00:00
|
|
|
{
|
2019-11-08 11:55:00 +00:00
|
|
|
Default_Reader r(FileName + NamedTensorFileExtension);
|
2019-11-02 14:58:32 +00:00
|
|
|
read(r, bValidate, Tag);
|
|
|
|
}
|
2019-11-12 17:31:42 +00:00
|
|
|
inline void read (const std::string &FileName, bool bValidate= true) { return read(FileName, bValidate, Name_); }
|
2019-01-22 17:59:55 +00:00
|
|
|
};
|
|
|
|
|
2019-02-03 17:05:19 +00:00
|
|
|
/******************************************************************************
|
2019-11-02 14:58:32 +00:00
|
|
|
Common elements for distillation
|
2019-02-03 17:05:19 +00:00
|
|
|
******************************************************************************/
|
|
|
|
|
2019-11-02 14:58:32 +00:00
|
|
|
BEGIN_MODULE_NAMESPACE(MDistil)
|
2019-02-03 17:05:19 +00:00
|
|
|
|
2019-11-02 14:58:32 +00:00
|
|
|
//Eigenvectors of the Laplacian
|
|
|
|
using LapEvecs = Grid::Hadrons::EigenPack<LatticeColourVector>;
|
2019-04-29 18:40:38 +01:00
|
|
|
|
2019-11-02 14:58:32 +00:00
|
|
|
// Noise vector (index order: nnoise, nt, nvec, ns)
|
2019-01-24 12:44:06 +00:00
|
|
|
|
2019-11-08 11:55:00 +00:00
|
|
|
class NoiseTensor : public NamedTensor<Complex, 4>
|
|
|
|
{
|
2019-11-12 17:31:42 +00:00
|
|
|
static const std::string Name__;
|
|
|
|
static const std::array<std::string, 4> DefaultIndexNames__;
|
2019-11-08 11:55:00 +00:00
|
|
|
public:
|
|
|
|
// Default constructor (assumes tensor will be loaded from file)
|
2019-11-12 17:31:42 +00:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NoiseTensor() : NamedTensor{Name__, DefaultIndexNames__} {}
|
2019-11-08 11:55:00 +00:00
|
|
|
|
|
|
|
// Construct a named tensor explicitly specifying size of each dimension
|
|
|
|
template<typename... IndexTypes>
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE NoiseTensor(Eigen::Index nNoise, Eigen::Index nT, Eigen::Index nVec, Eigen::Index nS)
|
2019-11-12 17:31:42 +00:00
|
|
|
: NamedTensor{Name__, DefaultIndexNames__, nNoise, nT, nVec, nS} {}
|
2019-11-08 11:55:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class PerambTensor : public NamedTensor<SpinVector, 6>
|
|
|
|
{
|
2019-11-12 17:31:42 +00:00
|
|
|
static const std::string Name__;
|
|
|
|
static const std::array<std::string, 6> DefaultIndexNames__;
|
2019-11-08 11:55:00 +00:00
|
|
|
public:
|
|
|
|
// Default constructor (assumes tensor will be loaded from file)
|
2019-11-12 17:31:42 +00:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PerambTensor() : NamedTensor{Name__, DefaultIndexNames__} {}
|
2019-11-08 11:55:00 +00:00
|
|
|
|
|
|
|
// Construct a named tensor explicitly specifying size of each dimension
|
|
|
|
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)
|
2019-11-12 17:31:42 +00:00
|
|
|
: NamedTensor{Name__, DefaultIndexNames__, nT, nVec, LI, nNoise, nT_inv, SI} {}
|
2019-11-08 11:55:00 +00:00
|
|
|
};
|
2019-01-24 12:44:06 +00:00
|
|
|
|
2019-01-22 17:59:55 +00:00
|
|
|
END_MODULE_NAMESPACE
|
|
|
|
END_HADRONS_NAMESPACE
|
2019-11-12 17:31:42 +00:00
|
|
|
#endif // Hadrons_NamedTensor_hpp_
|