From 23a211a5431c9aaf4a019248c6dbe11853a6913d Mon Sep 17 00:00:00 2001 From: Sebastian Eibl <sebastian.eibl@fau.de> Date: Fri, 23 Jun 2017 12:31:29 +0200 Subject: [PATCH] extended SendBuffer with low level memory functions --- src/core/mpi/SendBuffer.h | 45 +++++++++++++++++++++++++++++++---- tests/core/mpi/BufferTest.cpp | 28 ++++++++++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/core/mpi/SendBuffer.h b/src/core/mpi/SendBuffer.h index f68af2370..6b3209aae 100644 --- a/src/core/mpi/SendBuffer.h +++ b/src/core/mpi/SendBuffer.h @@ -140,11 +140,13 @@ public: //**Utility functions************************************************************************************************ /*!\name Utility functions */ //@{ - inline T* ptr () const; - inline void reserve( size_t newCapacity ); - inline void clear (); - inline void reset (); - inline void addDebugMarker( const char * marker ); + inline T* ptr () const; + inline std::ptrdiff_t getOffset() const; + inline T* getMemoryLocation( const std::ptrdiff_t offset); + inline void reserve( size_t newCapacity ); + inline void clear (); + inline void reset (); + inline void addDebugMarker( const char * marker ); //@} //******************************************************************************************************************* @@ -490,6 +492,39 @@ inline T* GenericSendBuffer<T,G>::ptr() const } //********************************************************************************************************************** +/** + * Returns the offset from the beginning to the current position inside the buffer in bytes. + * + * Example: + * \snippet BufferTest.cpp SendBuffer Overwrite Test + * The buffer now contains 3, 2, 3 + * \attention This is a low level function. Use with care! + * \see getMemoryLocation() + */ +template< typename T // Element type + , typename G > // Growth policy +inline std::ptrdiff_t GenericSendBuffer<T,G>::getOffset() const +{ + return cur_ - begin_; +} + + +/** + * Returns the memory address corresponding to the offset. Offset is measured in bytes from the beginning of the buffer. + * + * Example: + * \snippet BufferTest.cpp SendBuffer Overwrite Test + * The buffer now contains 3, 2, 3 + * \attention This is a low level function. Use with care! + * \see getOffset() + */ +template< typename T // Element type + , typename G > // Growth policy +inline T* GenericSendBuffer<T,G>::getMemoryLocation( const std::ptrdiff_t offset) +{ + return begin_ + offset; +} + //********************************************************************************************************************** /*!\brief Setting the minimum capacity of the send buffer. diff --git a/tests/core/mpi/BufferTest.cpp b/tests/core/mpi/BufferTest.cpp index b02255e16..05265695b 100644 --- a/tests/core/mpi/BufferTest.cpp +++ b/tests/core/mpi/BufferTest.cpp @@ -299,6 +299,32 @@ void bufferTestUInt8() } +void bufferOverwriteTest() +{ + //! [SendBuffer Overwrite Test] + int a = 1; + int b = 2; + int c = 3; + SendBuffer sb; + auto offset = sb.getOffset(); + sb << a << b << c; + *reinterpret_cast<int*>(sb.getMemoryLocation(offset)) = c; + //! [SendBuffer Overwrite Test] + + // Copying + RecvBuffer rb( sb ); + + int recv; + + rb >> recv; + WALBERLA_CHECK_EQUAL(c, recv); + rb >> recv; + WALBERLA_CHECK_EQUAL(b, recv); + rb >> recv; + WALBERLA_CHECK_EQUAL(c, recv); +} + + int main() { debug::enterTestMode(); @@ -313,6 +339,8 @@ int main() // than the type that is stored //bufferTest<double>(); + bufferOverwriteTest(); + return 0; } -- GitLab