1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-09 21:50:45 +01: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,20 +238,26 @@ 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() ) {
// size of this dimension if( dim != 1 ) {
us = htons( static_cast<uint16_t>( dim ) ); // size of this dimension
w.write(reinterpret_cast<const char *>(&us), sizeof(us)); us = htons( static_cast<uint16_t>( dim ) );
// length of this dimension name w.write(reinterpret_cast<const char *>(&us), sizeof(us));
us = htons( static_cast<uint16_t>( IndexNames[d].size() ) ); // length of this dimension name
w.write(reinterpret_cast<const char *>(&us), sizeof(us)); us = htons( static_cast<uint16_t>( IndexNames[d].size() ) );
// dimension name w.write(reinterpret_cast<const char *>(&us), sizeof(us));
w.write(IndexNames[d].c_str(), IndexNames[d].size()); // dimension name
w.write(IndexNames[d].c_str(), IndexNames[d].size());
}
d++; d++;
} }
// Actual data // Actual data
@ -277,24 +283,30 @@ 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() ) {
// size of dimension if( dim != 1 ) {
r.read(reinterpret_cast<char *>(&us), sizeof(us)); // size of dimension
assert( dim == ntohs( us ) && "size of dimension" ); r.read(reinterpret_cast<char *>(&us), sizeof(us));
// length of dimension name assert( dim == ntohs( us ) && "size of dimension" );
r.read(reinterpret_cast<char *>(&us), sizeof(us)); // length of dimension name
size_t l = ntohs( us ); r.read(reinterpret_cast<char *>(&us), sizeof(us));
assert( l == IndexNames[d].size() && "length of dimension name" ); size_t l = ntohs( us );
// dimension name assert( l == IndexNames[d].size() && "length of dimension name" );
std::string s( l, '?' ); // dimension name
r.read(&s[0], l); std::string s( l, '?' );
assert( s == IndexNames[d] && "dimension name" ); r.read(&s[0], l);
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;