diff --git a/libsrc/core/archive.hpp b/libsrc/core/archive.hpp index fc318e55..201305b3 100644 --- a/libsrc/core/archive.hpp +++ b/libsrc/core/archive.hpp @@ -308,6 +308,59 @@ namespace ngcore val.DoArchive(*this); return *this; } + // pack elements to binary + template + 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 + constexpr size_t TotSize (T & first, Trest & ...rest) const + { + return sizeof(first) + TotSize(rest...); + } + constexpr size_t TotSize () const { return 0; } + + + template + 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 + 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 Archive& operator & (std::shared_ptr& 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(d), n*sizeof(std::byte)); return *this; + } private: template diff --git a/libsrc/meshing/meshtype.hpp b/libsrc/meshing/meshtype.hpp index 42448af1..20a34477 100644 --- a/libsrc/meshing/meshtype.hpp +++ b/libsrc/meshing/meshtype.hpp @@ -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); }