walberla::mpi::GenericSendBuffer< T, G > Class Template Reference

Detailed Description

template<typename T = unsigned char, typename G = OptimalGrowth>
class walberla::mpi::GenericSendBuffer< T, G >

Implementation of a MPI send buffer.

The SendBuffer class is a special purpose implementation for the MPI communication functionality. It offers a convenient and safe setup of MPI messages even for mixed-type communication. The following example gives an impression of the usage of the SendBuffer class:

using namespace mpi;
// Adding a double and an integer to a send buffer
SendBuffer buffer;
double d( 2.1 );
Vec3 v( 3.5, -2.1, 0.7 );
buffer << d << v;
// Sending a MPI message to process 0 with the contents of the send buffer
MPI_Send( buffer.ptr(), buffer.size(), MPI_BYTE, 0, 0, MPI_COMM_WORLD );

For another example see also the Buffer Unit Test in File BufferTest.cpp

Note that the order of data values in the send buffer is depending on the order the elements are added to the buffer. This order is not changed during the MPI communication and the same order has to be used during the extraction of the sent data elements. See also the RecvBuffer class description for the receiver side of the MPI communication.

#include <SendBuffer.h>

Classes

class  Ptr
 

Public Types

using ElementType = T
 Type of the receive buffer elements. More...
 

Public Member Functions

Constructors
 GenericSendBuffer (size_t initCapacity=0)
 Standard constructor for SendBuffer. More...
 
 GenericSendBuffer (const GenericSendBuffer &sb)
 Copy constructor for SendBuffer. More...
 
Destructor
 ~GenericSendBuffer ()
 Destructor for SendBuffer. More...
 
Assignment operator
GenericSendBufferoperator= (const GenericSendBuffer &sb)
 Copy assignment operator for SendBuffer. More...
 
Get functions
size_t maxSize () const
 Returns the maximum possible size of the send buffer. More...
 
size_t size () const
 Returns the current size of the send buffer. More...
 
size_t capacity () const
 Returns the capacity of the send buffer. More...
 
bool isEmpty () const
 Returns true if the send buffer is empty. More...
 
Operators
template<typename V >
std::enable_if< std::is_arithmetic< V >::value||std::is_enum< V >::value, GenericSendBuffer & >::type operator<< (V value)
 Adds a built-in data value to the send buffer. More...
 
Repositioning
T * forward (uint_t elements)
 Forward the given number of elements. More...
 
void rewind (const size_t &size)
 Rewinds the stream to a previous position. More...
 

Utility functions

template<typename VT >
Ptr< VT > allocate (const size_t length=1, const VT &v=VT())
 Returns a special pointer class to modify the allocated memory location later. More...
 
T * ptr () const
 Returns a pointer to the first element of the send buffer. More...
 
void reserve (size_t newCapacity)
 Setting the minimum capacity of the send buffer. More...
 
void clear ()
 Clearing the send buffer. More...
 
void reset ()
 Clearing the send buffer. More...
 
void addDebugMarker (const char *marker)
 Adds a debug marker if buffer debugging is enabled. More...
 
void extendMemory (size_t newCapacity)
 Extending the internal memory of the send buffer. More...
 
template<typename V >
std::enable_if< std::is_arithmetic< V >::value||std::is_enum< V >::value, GenericSendBuffer & >::type put (V value)
 Implements operator<< without debugging system. More...
 
std::ptrdiff_t getOffset () const
 Returns the offset from the beginning to the current position inside the buffer in bytes. More...
 
T * getMemoryLocation (const std::ptrdiff_t offset)
 Returns the memory address corresponding to the offset. More...
 

Member variables

T * begin_
 Pointer to the first element of the send buffer. More...
 
T * cur_
 Pointer to the current/last element of the send buffer. More...
 
T * end_
 Pointer to the end of the storage of the send buffer. More...
 
template<typename U >
class GenericRecvBuffer
 

Member Typedef Documentation

◆ ElementType

template<typename T = unsigned char, typename G = OptimalGrowth>
using walberla::mpi::GenericSendBuffer< T, G >::ElementType = T

Type of the receive buffer elements.

Constructor & Destructor Documentation

◆ GenericSendBuffer() [1/2]

template<typename T , typename G >
walberla::mpi::GenericSendBuffer< T, G >::GenericSendBuffer ( size_t  initCapacity = 0)
inlineexplicit

Standard constructor for SendBuffer.

Parameters
initCapacityThe initial capacity of the send buffer.

The default initial capacity of the send buffer is specified by the selected growth policy.

◆ GenericSendBuffer() [2/2]

template<typename T , typename G >
walberla::mpi::GenericSendBuffer< T, G >::GenericSendBuffer ( const GenericSendBuffer< T, G > &  sb)
inline

Copy constructor for SendBuffer.

Parameters
sbThe send buffer to be copied.

◆ ~GenericSendBuffer()

template<typename T , typename G >
walberla::mpi::GenericSendBuffer< T, G >::~GenericSendBuffer
inline

Destructor for SendBuffer.

Member Function Documentation

◆ addDebugMarker()

template<typename T , typename G >
void walberla::mpi::GenericSendBuffer< T, G >::addDebugMarker ( const char *  marker)
inline

Adds a debug marker if buffer debugging is enabled.

See corresponding function GenericRecvBuffer::readDebugMarker()

◆ allocate()

template<typename T = unsigned char, typename G = OptimalGrowth>
template<typename VT >
Ptr<VT> walberla::mpi::GenericSendBuffer< T, G >::allocate ( const size_t  length = 1,
const VT &  v = VT() 
)
inline

Returns a special pointer class to modify the allocated memory location later.

Example:

int a = 1;
int b = 2;
int c = 3;
auto ptr = sb.allocate<int>(2);
sb << b << c;
*ptr = a;
ptr[1] = c;
//ptr[2] = c; // will fail in debug: out of bounds

The buffer now contains 1, 3, 2, 3

◆ capacity()

template<typename T , typename G >
size_t walberla::mpi::GenericSendBuffer< T, G >::capacity
inline

Returns the capacity of the send buffer.

Returns
The capacity.

◆ clear()

template<typename T , typename G >
void walberla::mpi::GenericSendBuffer< T, G >::clear
inline

Clearing the send buffer.

Returns
void

This function performs a complete reset of the send buffer.

◆ extendMemory()

template<typename T , typename G >
void walberla::mpi::GenericSendBuffer< T, G >::extendMemory ( size_t  newCapacity)
private

Extending the internal memory of the send buffer.

Parameters
newCapacityThe new minimum capacity of the send buffer.
Returns
void

◆ forward()

template<typename T , typename G >
T * walberla::mpi::GenericSendBuffer< T, G >::forward ( uint_t  elements)
inline

Forward the given number of elements.

Parameters
elementsThe number of elements to be advanced.
Returns
Previous position.

This function forwards element send buffer elements of type T and returns the previous buffer position.

◆ getMemoryLocation()

template<typename T , typename G >
T * walberla::mpi::GenericSendBuffer< T, G >::getMemoryLocation ( const std::ptrdiff_t  offset)
inlineprivate

Returns the memory address corresponding to the offset.

Offset is measured in bytes from the beginning of the buffer.

Attention
This is a low level function. Use with care!
See also
getOffset()

◆ getOffset()

template<typename T , typename G >
std::ptrdiff_t walberla::mpi::GenericSendBuffer< T, G >::getOffset
inlineprivate

Returns the offset from the beginning to the current position inside the buffer in bytes.

Attention
This is a low level function. Use with care!
See also
getMemoryLocation()

◆ isEmpty()

template<typename T , typename G >
bool walberla::mpi::GenericSendBuffer< T, G >::isEmpty
inline

Returns true if the send buffer is empty.

Returns
true if the send buffer is empty, false if it is not.

◆ maxSize()

template<typename T , typename G >
size_t walberla::mpi::GenericSendBuffer< T, G >::maxSize
inline

Returns the maximum possible size of the send buffer.

Returns
The maximum possible size.

◆ operator<<()

template<typename T , typename G >
template<typename V >
std::enable_if< std::is_arithmetic< V >::value||std::is_enum< V >::value, GenericSendBuffer< T, G > & >::type walberla::mpi::GenericSendBuffer< T, G >::operator<< ( value)

Adds a built-in data value to the send buffer.

Returns
Reference to the send buffer.

This function adds one data value of built-in data type to the send buffer.

Note: This operator may only be used for built-in data types. The attempt to use a user-defined data type results in a compile time error!

◆ operator=()

template<typename T , typename G >
GenericSendBuffer< T, G > & walberla::mpi::GenericSendBuffer< T, G >::operator= ( const GenericSendBuffer< T, G > &  sb)

Copy assignment operator for SendBuffer.

Parameters
sbThe send buffer to be copied.
Returns
Reference to the assigned send buffer.

◆ ptr()

template<typename T , typename G >
T * walberla::mpi::GenericSendBuffer< T, G >::ptr
inline

Returns a pointer to the first element of the send buffer.

Returns
Pointer to the first element of the send buffer.

This utility function enables the SendBuffer to be used directly as send buffer in MPI send functions:

using namespace communication;
// Filling a send buffer with a double and an integer value
SendBuffer<byte> buffer;
double d = 2.1;
int i= 2;
buffer << d << i;
// Sending a MPI message to process 0 with the contents of the send buffer
MPI_Send( buffer.ptr(), buffer.size(), MPI_BYTE, 0, 0, MPI_COMM_WORLD );

◆ put()

template<typename T , typename G >
template<typename V >
std::enable_if< std::is_arithmetic< V >::value||std::is_enum< V >::value, GenericSendBuffer< T, G > & >::type walberla::mpi::GenericSendBuffer< T, G >::put ( value)
private

Implements operator<< without debugging system.

Returns
Reference to the send buffer.

◆ reserve()

template<typename T , typename G >
void walberla::mpi::GenericSendBuffer< T, G >::reserve ( size_t  newCapacity)
inline

Setting the minimum capacity of the send buffer.

Parameters
newCapacityThe new minimum capacity of the send buffer.
Returns
void

This function reserves at least newCapacity elements of data type T for the send buffer.

◆ reset()

template<typename T , typename G >
void walberla::mpi::GenericSendBuffer< T, G >::reset
inline

Clearing the send buffer.

Returns
void

This function performs a complete reset of the send buffer - including the deletion of allocated memory!

◆ rewind()

template<typename T , typename G >
void walberla::mpi::GenericSendBuffer< T, G >::rewind ( const size_t s)
inline

Rewinds the stream to a previous position.

// Filling a send buffer with a double and an integer value
SendBuffer<byte> buffer;
double d = 2.1;
buffer << d;
size_t memorizedSize = buffer.size();
int i=3;
buffer << i;
buffer.rewind(memorizedSize);
i=5;
buffer << i; //overwrites the 3 in the buffer with the new value 5

◆ size()

template<typename T , typename G >
size_t walberla::mpi::GenericSendBuffer< T, G >::size
inline

Returns the current size of the send buffer.

Returns
The current size.

Friends And Related Function Documentation

◆ GenericRecvBuffer

template<typename T = unsigned char, typename G = OptimalGrowth>
template<typename U >
friend class GenericRecvBuffer
friend

Member Data Documentation

◆ begin_

template<typename T = unsigned char, typename G = OptimalGrowth>
T* walberla::mpi::GenericSendBuffer< T, G >::begin_
private

Pointer to the first element of the send buffer.

◆ cur_

template<typename T = unsigned char, typename G = OptimalGrowth>
T* walberla::mpi::GenericSendBuffer< T, G >::cur_
private

Pointer to the current/last element of the send buffer.

◆ end_

template<typename T = unsigned char, typename G = OptimalGrowth>
T* walberla::mpi::GenericSendBuffer< T, G >::end_
private

Pointer to the end of the storage of the send buffer.


The documentation for this class was generated from the following file:
T * ptr() const
Returns a pointer to the first element of the send buffer.
Definition: SendBuffer.h:592
math::Vector3< real_t > Vec3
Definition: DataTypes.h:40
const int c[3][NR_OF_DIRECTIONS]
The x,y,z component for each direction.
Definition: Directions.h:126
GenericSendBuffer<> SendBuffer
Definition: SendBuffer.h:219