mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-09 21:50:45 +01:00
NamedTensor writes to tag NamedTensor by default (not filename) - so still usable in case user renames file.
Also tweaked tensor index name checking (which is used to ensure tensor is correct type)
This commit is contained in:
parent
8dc0587621
commit
a3fe57f430
@ -347,11 +347,15 @@ public:
|
|||||||
Grid::SliceShare( gridLowDim, gridHighDim, tensor.data(), (int) (tensor.size() * sizeof(Scalar_)));
|
Grid::SliceShare( gridLowDim, gridHighDim, tensor.data(), (int) (tensor.size() * sizeof(Scalar_)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// load and save - not virtual - probably all changes
|
bool ValidateIndexNames( int iNumNames, const std::string * MatchNames ) const;
|
||||||
|
|
||||||
|
// Read/Write in any format
|
||||||
template<typename Reader> inline void read (Reader &r, const char * pszTag = nullptr);
|
template<typename Reader> inline void read (Reader &r, const char * pszTag = nullptr);
|
||||||
template<typename Writer> inline void write(Writer &w, const char * pszTag = nullptr) const;
|
template<typename Writer> inline void write(Writer &w, const char * pszTag = nullptr) const;
|
||||||
|
// Read/Write in default format, i.e. HDF5 if present, else binary
|
||||||
inline void read (const char * filename, const char * pszTag = nullptr);
|
inline void read (const char * filename, const char * pszTag = nullptr);
|
||||||
inline void write(const char * filename, const char * pszTag = nullptr) const;
|
inline void write(const char * filename, const char * pszTag = nullptr) const;
|
||||||
|
// Original I/O implementation. This will be removed when we're sure it's no longer needed
|
||||||
EIGEN_DEPRECATED inline void ReadBinary (const std::string filename); // To be removed
|
EIGEN_DEPRECATED inline void ReadBinary (const std::string filename); // To be removed
|
||||||
EIGEN_DEPRECATED inline void WriteBinary(const std::string filename); // To be removed
|
EIGEN_DEPRECATED inline void WriteBinary(const std::string filename); // To be removed
|
||||||
};
|
};
|
||||||
@ -563,18 +567,43 @@ template<typename Scalar_, int NumIndices_, uint16_t Endian_Scalar_Size>
|
|||||||
template<typename Writer>
|
template<typename Writer>
|
||||||
void NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::write(Writer &w, const char * pszTag)const{
|
void NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::write(Writer &w, const char * pszTag)const{
|
||||||
if( pszTag == nullptr )
|
if( pszTag == nullptr )
|
||||||
pszTag = "tensor";
|
pszTag = "NamedTensor";
|
||||||
|
LOG(Message) << "Writing NamedTensor to tag " << pszTag << std::endl;
|
||||||
write(w, pszTag, *this);
|
write(w, pszTag, *this);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Scalar_, int NumIndices_, uint16_t Endian_Scalar_Size>
|
template<typename Scalar_, int NumIndices_, uint16_t Endian_Scalar_Size>
|
||||||
void NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::write(const char * filename, const char * pszTag)const{
|
void NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::write(const char * filename, const char * pszTag)const{
|
||||||
const std::string sTag{pszTag == nullptr ? filename : pszTag};
|
|
||||||
std::string sFileName{filename};
|
std::string sFileName{filename};
|
||||||
sFileName.append( MDistil::FileExtension );
|
sFileName.append( MDistil::FileExtension );
|
||||||
LOG(Message) << "Writing NamedTensor to " << sFileName << ", tag " << sTag << std::endl;
|
LOG(Message) << "Writing NamedTensor to file " << sFileName << std::endl;
|
||||||
MDistil::Default_Writer w(sFileName);
|
MDistil::Default_Writer w( sFileName );
|
||||||
write(w, sTag.c_str());
|
write( w, pszTag );
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
Validate named tensor index names
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
template<typename Scalar_, int NumIndices_, uint16_t Endian_Scalar_Size>
|
||||||
|
bool NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::ValidateIndexNames( int iNumNames, const std::string * MatchNames ) const {
|
||||||
|
bool bSame{ iNumNames == NumIndices_ && IndexNames.size() == NumIndices_ };
|
||||||
|
for( int i = 0; bSame && i < NumIndices_; i++ ) {
|
||||||
|
// Case insensitive name compare
|
||||||
|
const int iMatchLen{ static_cast<int>( MatchNames[i].size() ) };
|
||||||
|
const int iNewLen { static_cast<int>( IndexNames[i].size() ) };
|
||||||
|
bSame = ( iMatchLen == iNewLen );
|
||||||
|
for( int j = 0; bSame && j < iMatchLen; j++ ) {
|
||||||
|
wchar_t c1 = MatchNames[i][j];
|
||||||
|
if( c1 >= 'a' && c1 <= 'z' )
|
||||||
|
c1 -= 'a' - 'A';
|
||||||
|
wchar_t c2 = IndexNames[i][j];
|
||||||
|
if( c2 >= 'a' && c1 <= 'z' )
|
||||||
|
c2 -= 'a' - 'A';
|
||||||
|
bSame = ( c1 == c2 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bSame;
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
@ -584,40 +613,26 @@ void NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::write(const char * f
|
|||||||
template<typename Scalar_, int NumIndices_, uint16_t Endian_Scalar_Size>
|
template<typename Scalar_, int NumIndices_, uint16_t Endian_Scalar_Size>
|
||||||
template<typename Reader>
|
template<typename Reader>
|
||||||
void NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::read(Reader &r, const char * pszTag) {
|
void NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::read(Reader &r, const char * pszTag) {
|
||||||
// Grab index names and dimensions
|
|
||||||
if( pszTag == nullptr )
|
if( pszTag == nullptr )
|
||||||
pszTag = "tensor";
|
pszTag = "NamedTensor";
|
||||||
|
// Grab index names and dimensions
|
||||||
std::vector<std::string> OldIndexNames{std::move(IndexNames)};
|
std::vector<std::string> OldIndexNames{std::move(IndexNames)};
|
||||||
typename ET::Dimensions OldDimensions{tensor.dimensions()};
|
typename ET::Dimensions OldDimensions{tensor.dimensions()};
|
||||||
|
LOG(Message) << "Reading NamedTensor from tag " << pszTag << std::endl;
|
||||||
read(r, pszTag, *this);
|
read(r, pszTag, *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::load dimension size");
|
assert(OldDimensions[i] == 0 || OldDimensions[i] == NewDimensions[i] && "NamedTensor::load dimension size");
|
||||||
// Case insensitive name compare - TODO std:: c++ way of doing this
|
assert( ValidateIndexNames( OldIndexNames.size(), &OldIndexNames[0] ) && "NamedTensor::load dimension name" );
|
||||||
const int iOldLen{ static_cast<int>( OldIndexNames[i].size() ) };
|
|
||||||
const int iNewLen{ static_cast<int>( IndexNames[i].size() ) };
|
|
||||||
bool bSame{ iOldLen == 0 || iOldLen == iNewLen };
|
|
||||||
for( int j = 0; j < iOldLen && bSame; j++ ) {
|
|
||||||
wchar_t c1 = OldIndexNames[i][j];
|
|
||||||
if( c1 >= 'a' && c1 <= 'z' )
|
|
||||||
c1 -= 'a' - 'A';
|
|
||||||
wchar_t c2 = IndexNames[i][j];
|
|
||||||
if( c2 >= 'a' && c1 <= 'z' )
|
|
||||||
c2 -= 'a' - 'A';
|
|
||||||
bSame = ( c1 == c2 );
|
|
||||||
}
|
|
||||||
assert(bSame && "NamedTensor::load dimension name");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Scalar_, int NumIndices_, uint16_t Endian_Scalar_Size>
|
template<typename Scalar_, int NumIndices_, uint16_t Endian_Scalar_Size>
|
||||||
void NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::read(const char * filename, const char * pszTag) {
|
void NamedTensor<Scalar_, NumIndices_, Endian_Scalar_Size>::read(const char * filename, const char * pszTag) {
|
||||||
const std::string sTag{pszTag == nullptr ? filename : pszTag};
|
|
||||||
std::string sFileName{filename};
|
std::string sFileName{filename};
|
||||||
sFileName.append( MDistil::FileExtension );
|
sFileName.append( MDistil::FileExtension );
|
||||||
LOG(Message) << "Reading NamedTensor from " << sFileName << ", tag " << sTag << std::endl;
|
LOG(Message) << "Reading NamedTensor from file " << sFileName << std::endl;
|
||||||
MDistil::Default_Reader r(sFileName);
|
MDistil::Default_Reader r( sFileName );
|
||||||
read(r, sTag.c_str());
|
read( r, pszTag );
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
|
@ -166,8 +166,7 @@ void TDistilVectors<FImpl>::setup(void)
|
|||||||
auto &perambulator = envGet(PerambTensor, PerambulatorName);
|
auto &perambulator = envGet(PerambTensor, PerambulatorName);
|
||||||
|
|
||||||
// We expect the perambulator to have been created with these indices
|
// We expect the perambulator to have been created with these indices
|
||||||
for(int i = 0; i < PerambTensor::NumIndices; i++ )
|
assert( perambulator.ValidateIndexNames( PerambIndexNames.size(), &PerambIndexNames[0] ) && "Perambulator index names bad" );
|
||||||
assert( PerambIndexNames[i] == perambulator.IndexNames[i] && "PerambTensor indices bad" );
|
|
||||||
|
|
||||||
const int Nt{ env().getDim(Tdir) };
|
const int Nt{ env().getDim(Tdir) };
|
||||||
assert( Nt == static_cast<int>( perambulator.tensor.dimension(0) ) && "PerambTensor time dimensionality bad" );
|
assert( Nt == static_cast<int>( perambulator.tensor.dimension(0) ) && "PerambTensor time dimensionality bad" );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user