MPI_PACK(3)
π MPI_PACK(3) β an Open MPI moment π
okay so bestie, you know Open MPI? yeah, this whole page lives in that world. and today's main character is MPI_Pack, which is literally the "squish all your lil data bits into one contiguous memory blob" girlie. no cap, she's iconic. π
β¨ NAME β¨
MPI_Pack β packs data of a given datatype into contiguous memory.
β¨ SYNTAX β¨
πΈ C Syntax
#include <mpi.h>
int MPI_Pack(const void *inbuf, int incount, MPI_Datatype datatype,
void *outbuf, int outsize, int *position, MPI_Comm comm)
πΈ Fortran Syntax
USE MPI
! or the older form: INCLUDE 'mpif.h'
MPI_PACK(INBUF, INCOUNT, DATATYPE, OUTBUF,OUTSIZE, POSITION,
COMM, IERROR)
<type> INBUF(*), OUTBUF(*)
INTEGER INCOUNT, DATATYPE, OUTSIZE, POSITION, COMM, IERROR
πΈ Fortran 2008 Syntax
USE mpi_f08
MPI_Pack(inbuf, incount, datatype, outbuf, outsize, position, comm, ierror)
TYPE(*), DIMENSION(..), INTENT(IN) :: inbuf
TYPE(*), DIMENSION(..) :: outbuf
INTEGER, INTENT(IN) :: incount, outsize
TYPE(MPI_Datatype), INTENT(IN) :: datatype
INTEGER, INTENT(INOUT) :: position
TYPE(MPI_Comm), INTENT(IN) :: comm
INTEGER, OPTIONAL, INTENT(OUT) :: ierror
π INPUT PARAMETERS π
inbufβ where your input buffer starts (choice). yes bestie, the choice type, she's flexible like that πincountβ how many input data items you're packing (integer).datatypeβ the datatype of each one of those input data items (handle).outsizeβ how big your output buffer is, in bytes (integer).commβ the communicator for the packed message (handle). she's the one who's gonna send this blob later, so keep her in the loop π«Ά
π INPUT/OUTPUT PARAMETER π
positionβ the current position in the buffer, in bytes (integer). she comes in with a value, she leaves with a value, she's booked and busy π
π OUTPUT PARAMETERS π
outbufβ where your output buffer starts (choice).ierrorβ Fortran only, it's the error status (integer).
π¦ DESCRIPTION π¦
so what she does: she packs the message sitting in the send buffer you described with inbuf, incount, and datatype into the buffer space you gave her with outbuf and outsize. π«
the input buffer can be any communication buffer allowed in MPI_Send β like, anything you'd be allowed to send normally, she'll take it. the output buffer is a contiguous storage area holding outsize bytes, starting at the address outbuf. and bestie, note this: the length is counted in bytes, not elements β like it's a communication buffer for a message of type MPI_Packed. iykyk. β¨
the input value of position is the first location in the output buffer that gets used for packing. then position gets incremented by the size of the packed message, so the output value of position is the first location in the output buffer following the spots the packed message took up. and the comm argument is the communicator that'll be used afterward for sending that packed message. π
Example: an example using MPI_Pack:
int myrank, position, i, j, a[2];
char buff[1000];
// ...
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
if (myrank == 0) {
/* SENDER CODE */
position = 0;
MPI_Pack(&i, 1, MPI_INT, buff, 1000, &position, MPI_COMM_WORLD);
MPI_Pack(&j, 1, MPI_INT, buff, 1000, &position, MPI_COMM_WORLD);
MPI_Send(buff, position, MPI_PACKED, 1, 0, MPI_COMM_WORLD);
} else {
/* RECEIVER CODE */
MPI_Recv(a, 2, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
π₯ ERRORS π₯
kk so almost all MPI routines return an error value β C routines hand it back as the return result of the function, and Fortran routines put it in the last argument. π
before that error value comes back to you, the current MPI error handler associated with the communication object (like, a communicator, window, or file β you know the vibes) gets called. if there's no communication object associated with the MPI call, then the call counts as attached to MPI_COMM_SELF and it'll call the associated MPI error handler. when MPI_COMM_SELF isn't initialized (i.e., before MPI_Init/MPI_Init_thread, after MPI_Finalize, or when you're using the Sessions Model exclusively) the error raises the initial error handler. and bestie, that initial error handler can be changed by calling MPI_Comm_set_errhandler on MPI_COMM_SELF when you're using the World model, or via the mpi_initial_errhandler CLI argument to mpiexec, or the info key to MPI_Comm_spawn/MPI_Comm_spawn_multiple. if no other appropriate error handler has been set, then MPI_ERRORS_RETURN gets called for MPI I/O functions, and MPI_ERRORS_ABORT gets called for all the other MPI functions. π
Open MPI includes three predefined error handlers you can use:
MPI_ERRORS_ARE_FATALβ causes the program to abort all connected MPI processes. like, everybody. πMPI_ERRORS_ABORTβ an error handler that can be invoked on a communicator, window, file, or session. when called on a communicator, it acts as ifMPI_Abortwas called on that communicator. if called on a window or file, it acts as ifMPI_Abortwas called on a communicator containing the group of processes in the corresponding window or file. if called on a session, it aborts only the local process. so specific, we love her πMPI_ERRORS_RETURNβ returns an error code to the application.
MPI applications can also implement their own error handlers by calling:
MPI_Comm_create_errhandlerthenMPI_Comm_set_errhandlerMPI_File_create_errhandlerthenMPI_File_set_errhandlerMPI_Session_create_errhandlerthenMPI_Session_set_errhandleror atMPI_Session_initMPI_Win_create_errhandlerthenMPI_Win_set_errhandler
β οΈ note that MPI does not guarantee that an MPI program can continue past an error. like, fr fr, don't count on it.
see the MPI man page for a full list of MPI error codes. π
see the Error Handling section of the MPI-3.1 standard for more information. π
π§ SEE ALSO π§
MPI_UnpackMPI_Pack_size
π COPYRIGHT π
2003-2026, The Open MPI Community
and that's the whole page, bestie β mwah π this lil fairy godmother of a manpage is brought to you by the Open MPI girlies and dated Sep 11, 2026, so go forth and pack your data into contiguous memory like the main character you are π¦πβ¨
π¦ where this page comes from
- arch rolling: openmpi 5.0.11-1 Β· BSD-3-Clause AND LicenseRef-MPICH
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms