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.
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)
Parameters
inbuf
The input buffer, i.e. the data to be packed into contigous memory.
incount
Number of input data items.
datatype
The datatype of each item to be packed.
outbuf
Starting address of the output buffer.
outsize
The size, in bytes, of the output buffer.
position
This is an input/output parameter that describes locations within
outbuf
. The data atinbuf
will be copied tooutbuf + *position
. After the function returns, the value*position
indicates the first position inoutbuf
that follows the data just copied. This is useful to pass asposition
to the next call toMPI_Pack
.comm
The 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)
Parameters
inbuf
The input buffer, i.e. the data to be unpacked.
insize
The size, in bytes, of the input buffer.
position
This is an input/output parameter that describes locations within
inbuf
. The data atinbuf + *position
will be copied tooutbuf
. After the function returns, the value*position
indicates the first position ininbuf
that follows the data just copied. This is useful to pass asposition
to the next call toMPI_Unpack
.outbuf
Starting address of the output buffer.
outcount
Number of output data items.
datatype
The datatype of each item to be unpacked.
comm
The 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.