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 if MPI_Abort was called on that communicator. if called on a window or file, it acts as if MPI_Abort was 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_errhandler then MPI_Comm_set_errhandler
  • MPI_File_create_errhandler then MPI_File_set_errhandler
  • MPI_Session_create_errhandler then MPI_Session_set_errhandler or at MPI_Session_init
  • MPI_Win_create_errhandler then MPI_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_Unpack
  • MPI_Pack_size

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