packed archiving

This commit is contained in:
Joachim Schoeberl 2021-06-27 12:32:51 +02:00
parent 54db7941d0
commit 31d5ce8be9
2 changed files with 75 additions and 23 deletions

View File

@ -308,6 +308,59 @@ namespace ngcore
val.DoArchive(*this); return *this;
}
// pack elements to binary
template <typename ... Types>
Archive & DoPacked (Types & ... args)
{
if (true) // (isbinary)
{
if (is_output)
{
std::byte mem[TotSize(args...)];
CopyToBin (&mem[0], args...);
Do(&mem[0], sizeof(mem));
}
else
{
std::byte mem[TotSize(args...)];
Do(&mem[0], sizeof(mem));
CopyFromBin (&mem[0], args...);
}
}
// else
// cout << "DoPacked of non-binary called --> individual pickling" << endl;
return *this;
}
template <typename T, typename ... Trest>
constexpr size_t TotSize (T & first, Trest & ...rest) const
{
return sizeof(first) + TotSize(rest...);
}
constexpr size_t TotSize () const { return 0; }
template <typename T, typename ... Trest>
constexpr void CopyToBin (std::byte * ptr, T & first, Trest & ...rest) const
{
memcpy (ptr, &first, sizeof(first));
CopyToBin(ptr+sizeof(first), rest...);
}
constexpr void CopyToBin (std::byte * ptr) const { }
template <typename T, typename ... Trest>
constexpr void CopyFromBin (std::byte * ptr, T & first, Trest & ...rest) const
{
memcpy (&first, ptr, sizeof(first));
CopyFromBin(ptr+sizeof(first), rest...);
}
constexpr void CopyFromBin (std::byte * ptr) const { }
// Archive shared_ptrs =================================================
template <typename T>
Archive& operator & (std::shared_ptr<T>& ptr)
@ -706,6 +759,7 @@ namespace ngcore
{ return Write(i); }
Archive & operator & (bool & b) override
{ return Write(b); }
Archive & operator & (std::string & str) override
{
int len = str.length();
@ -732,6 +786,11 @@ namespace ngcore
ptr = 0;
}
}
Archive & Do (std::byte * d, size_t n) override
{
FlushBuffer();
stream->write(reinterpret_cast<char*>(d), n*sizeof(std::byte)); return *this;
}
private:
template <typename T>

View File

@ -378,9 +378,10 @@ namespace netgen
void DoArchive (Archive & ar)
{
// ar & x[0] & x[1] & x[2] & layer & singular;
ar.Do(&x[0], 3);
ar & layer & singular;
ar & (unsigned char&)(type);
// ar.Do(&x[0], 3);
// ar & layer & singular;
// ar & (unsigned char&)(type);
ar.DoPacked (x[0], x[1], x[2], layer, singular, (unsigned char&)(type));
}
};
@ -558,13 +559,18 @@ namespace netgen
if (ar.Output())
{ _np = np; _typ = typ; _curved = is_curved;
_vis = visible; _deleted = deleted; }
ar & _np & _typ & index & _curved & _vis & _deleted;
// ar & _np & _typ & index & _curved & _vis & _deleted;
ar.DoPacked (_np, _typ, index, _curved, _vis, _deleted);
// ar & next; don't need
if (ar.Input())
{ np = _np; typ = ELEMENT_TYPE(_typ); is_curved = _curved;
visible = _vis; deleted = _deleted; }
/*
for (size_t i = 0; i < np; i++)
ar & pnum[i];
*/
static_assert(sizeof(int) == sizeof (PointIndex));
ar.Do( (int*)&pnum[0], np);
}
#ifdef PARALLEL
@ -840,32 +846,19 @@ namespace netgen
void DoArchive (Archive & ar)
{
/*
short _np, _typ;
bool _curved;
if (ar.Output())
{ _np = np; _typ = typ; _curved = is_curved; }
ar & _np & _typ & index & _curved;
// ar & _np & _typ & index & _curved;
ar.DoPacked (_np, _typ, index, _curved);
if (ar.Input())
{ np = _np; typ = ELEMENT_TYPE(_typ); is_curved = _curved; }
*/
if (ar.Output())
{
short _np, _typ;
bool _curved;
_np = np; _typ = typ; _curved = is_curved;
ar & _np & _typ & index & _curved;
}
else
{
alignas (4) std::byte tmp[9];
ar.Do (&tmp[0], 9);
np = *(short*)(void*)&tmp[0];
typ = ELEMENT_TYPE(*(short*)(void*)&tmp[2]);
index = *(int*)(void*)&tmp[4];
is_curved = *(bool*)(void*)&tmp[8];
}
/*
for (size_t i = 0; i < np; i++)
ar & pnum[i];
*/
static_assert(sizeof(int) == sizeof (PointIndex));
ar.Do( (int*)&pnum[0], np);
}