Derived datatypes: pack and unpack
Questions
How can you reduce the number of messages sent and received?
Objectives
Learn how to pack heterogeneous data into a single message.
MPI supports many of the basic datatypes recognized by the C and Fortran standards. However, often one needs to represent data that requires more complex programming structures than just the fundamental datatypes.
MPI |
C |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The MPI standard defines functions to extend the datatypes that can be used in MPI messages. In this episode, we will discuss how to collect heterogeneous data into a single MPI message, leaving the definition of your own types to next episode Derived datatypes: MPI_Datatype.
Packing and unpacking
MPI offers the possibility to pack and unpack data of known datatype into a
single contiguous memory buffer, without first having to define a
corresponding datatype.
This can be an extremely useful technique to reduce messaging traffic and could
help with the readability and portability of the code.
The resulting packed buffer will be of type MPI_PACKED and can contain any
sort of heterogeneous collection of basic datatypes recognized by MPI.
MPI allows the programmer to communicate heterogeneous collections into a
single message, without defining a full-fledged custom datatype. The data is
packed into a buffer of type MPI_PACKED. On the receiving end, the buffer
will be unpacked into its constituent components.
Pack data in noncontiguous memory to a contiguous memory buffer.
int MPI_Pack(const void *inbuf,
int incount,
MPI_Datatype datatype,
void *outbuf,
int outsize,
int *position,
MPI_Comm comm)
The relation of inbuf, outbuf, and position when calling
MPI_Pack. In this figure, outbuf already holds some data (the red
shaded area). The data in inbuf is copied to outbuf starting at the
address outbuf+*position. When the function returns, the position
parameter will have been updated to refer to the first position in outbuf
following the data copied by this call.
Parameters
inbufThe input buffer, i.e. the data to be packed into contigous memory.
incountNumber of input data items.
datatypeThe datatype of each item to be packed.
outbufStarting address of the output buffer.
outsizeThe size, in bytes, of the output buffer.
positionThis is an input/output parameter that describes locations within
outbuf. The data atinbufwill be copied tooutbuf + *position. After the function returns, the value*positionindicates the first position inoutbufthat follows the data just copied. This is useful to pass aspositionto the next call toMPI_Pack.commThe communicator.
Unpack a contiguous memory buffer into noncontiguous memory locations.
int MPI_Unpack(const void *inbuf,
int insize,
int *position,
void *outbuf,
int outcount,
MPI_Datatype datatype,
MPI_Comm comm)
The relation of inbuf, outbuf, and position when calling
MPI_Unpack. In this figure, inbuf holds some data. The data
in inbuf is copied to outbuf starting at the address given with
position. When the function returns, the position parameter will
have been updated to the first position in inbuf following the just
copied data.
Parameters
inbufThe input buffer, i.e. the data to be unpacked.
insizeThe size, in bytes, of the input buffer.
positionThis is an input/output parameter that describes locations within
inbuf. The data atinbuf + *positionwill be copied tooutbuf. After the function returns, the value*positionindicates the first position ininbufthat follows the data just copied. This is useful to pass aspositionto the next call toMPI_Unpack.outbufStarting address of the output buffer.
outcountNumber of output data items.
datatypeThe datatype of each item to be unpacked.
commThe communicator.
See also
The lecture covering MPI datatypes from EPCC is available on GitHub
Chapter 5 of the Using MPI book by William Gropp et al. [GLS14]
Chapter 6 of the Parallel Programming with MPI book by Peter Pacheco. [Pac97]
Keypoints
You can reduce message traffic by packing (unpacking) heterogeneous data together.
Packing/unpacking are straightforward to use, but might lead to less readable programs.