1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-14 01:35:36 +00:00

Only write indices with dimesion!=1

This commit is contained in:
Michael Marshall 2019-02-03 20:58:58 +00:00
parent bd75b843fa
commit 7eda54bb87
2 changed files with 38 additions and 25 deletions

View File

@ -238,12 +238,17 @@ void NamedTensor<Scalar_, NumIndices_>::WriteTemporary(const std::string filenam
// total number of elements // total number of elements
uint32_t ul = htonl( static_cast<uint32_t>( this->size() ) ); uint32_t ul = htonl( static_cast<uint32_t>( this->size() ) );
w.write(reinterpret_cast<const char *>(&ul), sizeof(ul)); w.write(reinterpret_cast<const char *>(&ul), sizeof(ul));
// number of dimensions // number of dimensions which aren't 1
uint16_t us = htons( static_cast<uint16_t>( this->NumIndices ) ); uint16_t us = static_cast<uint16_t>( this->NumIndices );
for( auto dim : this->dimensions() )
if( dim == 1 )
us--;
us = htons( us );
w.write(reinterpret_cast<const char *>(&us), sizeof(us)); w.write(reinterpret_cast<const char *>(&us), sizeof(us));
// dimensions together with names // dimensions together with names
int d = 0; int d = 0;
for( auto dim : this->dimensions() ) { for( auto dim : this->dimensions() ) {
if( dim != 1 ) {
// size of this dimension // size of this dimension
us = htons( static_cast<uint16_t>( dim ) ); us = htons( static_cast<uint16_t>( dim ) );
w.write(reinterpret_cast<const char *>(&us), sizeof(us)); w.write(reinterpret_cast<const char *>(&us), sizeof(us));
@ -252,6 +257,7 @@ void NamedTensor<Scalar_, NumIndices_>::WriteTemporary(const std::string filenam
w.write(reinterpret_cast<const char *>(&us), sizeof(us)); w.write(reinterpret_cast<const char *>(&us), sizeof(us));
// dimension name // dimension name
w.write(IndexNames[d].c_str(), IndexNames[d].size()); w.write(IndexNames[d].c_str(), IndexNames[d].size());
}
d++; d++;
} }
// Actual data // Actual data
@ -277,13 +283,18 @@ void NamedTensor<Scalar_, NumIndices_>::ReadTemporary(const std::string filename
uint32_t ul; uint32_t ul;
r.read(reinterpret_cast<char *>(&ul), sizeof(ul)); r.read(reinterpret_cast<char *>(&ul), sizeof(ul));
assert( this->size() == ntohl( ul ) && "Error: total number of elements" ); assert( this->size() == ntohl( ul ) && "Error: total number of elements" );
// number of dimensions // number of dimensions which aren't 1
uint16_t us; uint16_t us;
r.read(reinterpret_cast<char *>(&us), sizeof(us)); r.read(reinterpret_cast<char *>(&us), sizeof(us));
assert( this->NumIndices == ntohs( us ) && "Error: number of dimensions" ); us = ntohs( us );
for( auto dim : this->dimensions() )
if( dim == 1 )
us++;
assert( this->NumIndices == us && "Error: number of dimensions which aren't 1" );
// dimensions together with names // dimensions together with names
int d = 0; int d = 0;
for( auto dim : this->dimensions() ) { for( auto dim : this->dimensions() ) {
if( dim != 1 ) {
// size of dimension // size of dimension
r.read(reinterpret_cast<char *>(&us), sizeof(us)); r.read(reinterpret_cast<char *>(&us), sizeof(us));
assert( dim == ntohs( us ) && "size of dimension" ); assert( dim == ntohs( us ) && "size of dimension" );
@ -295,6 +306,7 @@ void NamedTensor<Scalar_, NumIndices_>::ReadTemporary(const std::string filename
std::string s( l, '?' ); std::string s( l, '?' );
r.read(&s[0], l); r.read(&s[0], l);
assert( s == IndexNames[d] && "dimension name" ); assert( s == IndexNames[d] && "dimension name" );
}
d++; d++;
} }
// Actual data // Actual data

View File

@ -296,7 +296,7 @@ bool DebugEigenTest()
{ {
const char pszTestFileName[] = "test_tensor.bin"; const char pszTestFileName[] = "test_tensor.bin";
std::array<std::string,3> as={"Alpha", "Beta", "Gamma"}; std::array<std::string,3> as={"Alpha", "Beta", "Gamma"};
MyTensor x(as, 2,3,4); MyTensor x(as, 2,1,4);
DebugShowTensor(x, "x"); DebugShowTensor(x, "x");
x.WriteTemporary(pszTestFileName); x.WriteTemporary(pszTestFileName);
// Test initialisation of an array of strings // Test initialisation of an array of strings
@ -308,7 +308,8 @@ bool DebugEigenTest()
for( auto a : p.IndexNames ) for( auto a : p.IndexNames )
std::cout << a << std::endl; std::cout << a << std::endl;
// Now see whether we can read a tensor back // Now see whether we can read a tensor back
MyTensor y(as, 2,3,4); std::array<std::string,3> a2={"Alpha", "Delta", "Gamma"};
MyTensor y(a2, 2,1,4);
y.ReadTemporary(pszTestFileName); y.ReadTemporary(pszTestFileName);
DebugShowTensor(y, "y"); DebugShowTensor(y, "y");
return true; return true;