diff --git a/src/core/mpi/SendBuffer.h b/src/core/mpi/SendBuffer.h index f68af2370b2e8dc175c2158d12dd4fdb5715c2d3..6b3209aaea40c3289524e1ca0fc571afa43e1e3a 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 b02255e160d2974fae410d56f5e27a6bb7c12408..05265695b5aeaf8b98fedb170b7bf4d38320674b 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; }