From e84d4e90c8ceeb306e06a28deda8e81f9058fae1 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Fri, 25 Jun 2021 18:58:25 +0200 Subject: [PATCH 1/5] add header for std::byte --- libsrc/core/archive.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libsrc/core/archive.hpp b/libsrc/core/archive.hpp index a011b6bf..7c7db7d5 100644 --- a/libsrc/core/archive.hpp +++ b/libsrc/core/archive.hpp @@ -10,6 +10,7 @@ #include // for shared_ptr #include // for string #include // for declval, enable_if_t, false_type, is_co... +#include // for std::byte #include // for type_info #include // for move, swap, pair #include // for vector From 54db7941d088d1043cf2eb4a7e85ec1e9c0c9895 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Sat, 26 Jun 2021 12:14:17 +0200 Subject: [PATCH 2/5] tuning mesh(un)pickling --- libsrc/core/archive.hpp | 4 +++- libsrc/meshing/meshtype.hpp | 21 ++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/libsrc/core/archive.hpp b/libsrc/core/archive.hpp index 7c7db7d5..fc318e55 100644 --- a/libsrc/core/archive.hpp +++ b/libsrc/core/archive.hpp @@ -207,7 +207,7 @@ namespace ngcore Do(&v[0], size); return (*this); } - + // archive implementation for enums template auto operator & (T& val) -> std::enable_if_t::value, Archive&> @@ -809,6 +809,8 @@ namespace ngcore return *this; } + Archive & Do (std::byte * d, size_t n) override + { stream->read(reinterpret_cast(d), n*sizeof(std::byte)); return *this; } // NOLINT Archive & Do (double * d, size_t n) override { stream->read(reinterpret_cast(d), n*sizeof(double)); return *this; } // NOLINT Archive & Do (int * i, size_t n) override diff --git a/libsrc/meshing/meshtype.hpp b/libsrc/meshing/meshtype.hpp index 978e7b29..42448af1 100644 --- a/libsrc/meshing/meshtype.hpp +++ b/libsrc/meshing/meshtype.hpp @@ -840,6 +840,7 @@ namespace netgen void DoArchive (Archive & ar) { + /* short _np, _typ; bool _curved; if (ar.Output()) @@ -847,10 +848,24 @@ namespace netgen ar & _np & _typ & index & _curved; if (ar.Input()) { np = _np; typ = ELEMENT_TYPE(_typ); is_curved = _curved; } - /* - for (size_t i = 0; i < np; i++) - ar & pnum[i]; */ + + 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]; + } static_assert(sizeof(int) == sizeof (PointIndex)); ar.Do( (int*)&pnum[0], np); } From 31d5ce8be968912ed4f562b5e14a783289eb59c3 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Sun, 27 Jun 2021 12:32:51 +0200 Subject: [PATCH 3/5] packed archiving --- libsrc/core/archive.hpp | 59 +++++++++++++++++++++++++++++++++++++ libsrc/meshing/meshtype.hpp | 39 ++++++++++-------------- 2 files changed, 75 insertions(+), 23 deletions(-) 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); } From 91506aa71ab31214180a257c047a453c2971fc85 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Mon, 28 Jun 2021 01:07:03 +0200 Subject: [PATCH 4/5] static constexpr --- libsrc/core/archive.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libsrc/core/archive.hpp b/libsrc/core/archive.hpp index 201305b3..0d86df83 100644 --- a/libsrc/core/archive.hpp +++ b/libsrc/core/archive.hpp @@ -333,11 +333,11 @@ namespace ngcore } template - constexpr size_t TotSize (T & first, Trest & ...rest) const + static constexpr size_t TotSize (T & first, Trest & ...rest) { return sizeof(first) + TotSize(rest...); } - constexpr size_t TotSize () const { return 0; } + static constexpr size_t TotSize () { return 0; } template From fd50131a5bd05ca6c1d612de272001939d2550f8 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Mon, 28 Jun 2021 01:35:23 +0200 Subject: [PATCH 5/5] constexpr function --- libsrc/core/archive.hpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/libsrc/core/archive.hpp b/libsrc/core/archive.hpp index 0d86df83..bddd2533 100644 --- a/libsrc/core/archive.hpp +++ b/libsrc/core/archive.hpp @@ -94,6 +94,17 @@ namespace ngcore template constexpr bool is_archivable = detail::is_Archivable_struct::value; + + template + constexpr size_t TotSize () + { + if constexpr (sizeof...(Trest) == 0) + return sizeof(T); + else + return sizeof(T) + TotSize (); + } + + // Base Archive class class NGCORE_API Archive { @@ -308,22 +319,25 @@ namespace ngcore val.DoArchive(*this); return *this; } + + + // pack elements to binary template Archive & DoPacked (Types & ... args) { if (true) // (isbinary) { + constexpr size_t totsize = TotSize(); // (args...); + std::byte mem[totsize]; if (is_output) { - std::byte mem[TotSize(args...)]; CopyToBin (&mem[0], args...); - Do(&mem[0], sizeof(mem)); + Do(&mem[0], totsize); } else { - std::byte mem[TotSize(args...)]; - Do(&mem[0], sizeof(mem)); + Do(&mem[0], totsize); CopyFromBin (&mem[0], args...); } } @@ -332,13 +346,6 @@ namespace ngcore return *this; } - template - static constexpr size_t TotSize (T & first, Trest & ...rest) - { - return sizeof(first) + TotSize(rest...); - } - static constexpr size_t TotSize () { return 0; } - template constexpr void CopyToBin (std::byte * ptr, T & first, Trest & ...rest) const