MPI_ALLOC_MEM(3)

✨ MPI_ALLOC_MEM(3) ✨

okay so we're hanging out in the Open MPI manual today bestie, and the icon of the hour is:

MPI_Alloc_mem β€” Allocates a specified memory segment. πŸ’–

no cap, that's the whole vibe. let's get into it.


πŸ¦‹ SYNTAX πŸ¦‹

C Syntax

#include <mpi.h>

int MPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr)

Fortran Syntax

USE MPI
! or the older form: INCLUDE 'mpif.h'
MPI_ALLOC_MEM(SIZE, INFO, BASEPTR, IERROR)
     INTEGER INFO, IERROR
     INTEGER(KIND=MPI_ADDRESS_KIND) SIZE, BASEPTR

Fortran 2008 Syntax

USE mpi_f08
MPI_Alloc_mem(size, info, baseptr, ierror)
     USE, INTRINSIC :: ISO_C_BINDING, ONLY
     INTEGER(KIND=MPI_ADDRESS_KIND), INTENT(IN) :: size
     TYPE(MPI_Info), INTENT(IN) :: info
     TYPE(C_PTR), INTENT(OUT) :: baseptr
     INTEGER, OPTIONAL, INTENT(OUT) :: ierror

πŸŽ€ INPUT PARAMETERS πŸŽ€

  • size β€” how many bytes of memory we're manifesting. nonnegative integer, so no negative vibes allowed πŸ™…β€β™€οΈ
  • info β€” the Info argument (a handle). yes, handle, like a little handle you hold it by.

πŸ’… OUTPUT PARAMETERS πŸ’…

  • baseptr β€” pointer to the beginning of the memory segment that just got allocated. the starting line of your new memory block, periodt.
  • ierror β€” Fortran only: error status (integer).

🌸 DESCRIPTION 🌸

MPI_Alloc_mem allocates size bytes of memory. the starting address of that memory comes back to you in the variable baseptr. that's it, that's the whole serve. 🫢


πŸ’– C NOTES πŸ’–

the parameter baseptr is of type void * so that literally any pointer object can be passed through it. so the argument you hand over should be a pointer to a pointer of arbitrary type (like void **). yeah, pointer-to-pointer. iykyk. ✨


🧚 FORTRAN NOTES 🧚

lowkey important: the MPI_Alloc_mem calls require you to use the iso_c_binding module, because of the whole TYPE(C_PTR) situation.

use iso_c_binding

type(c_ptr) :: alloc_ptr
integer :: size, ierr
double precision, pointer :: array(:,:)

! A 2D array of 100 elements
size = 10 * 10
call MPI_Alloc_Mem(size * 8, MPI_INFO_NULL, alloc_ptr, ierr)

! Point to the array
call c_f_pointer(alloc_ptr, array, [10, 10])

! ... use the array ...

! Free the memory, no need for the alloc_ptr
call MPI_Free_mem(array, ierr)

look at that, iconic behavior. πŸ’œ


😭 ERRORS 😭

almost all MPI routines return an error value β€” C routines give 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 (e.g., communicator, window, file) 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. and when MPI_COMM_SELF is not 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. you can change that initial error handler by calling MPI_Comm_set_errhandler on MPI_COMM_SELF when you're using the World model, or with the mpi_initial_errhandler CLI argument to mpiexec, or the info key to MPI_Comm_spawn/MPI_Comm_spawn_multiple. and if no other appropriate error handler has been set? then the MPI_ERRORS_RETURN error handler gets called for MPI I/O functions, and the MPI_ERRORS_ABORT error handler gets called for all other MPI functions.

Open MPI ships three predefined error handlers you can use, bestie:

  • MPI_ERRORS_ARE_FATAL β€” causes the program to abort all connected MPI processes. yes, all of them. πŸ’€
  • 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 it's 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 it's called on a session, it aborts only the local process.
  • MPI_ERRORS_RETURN β€” returns an error code to the application. polite. considerate. we love her.

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. so like… don't build your whole personality on surviving one. πŸ™ˆ

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_Free_mem

2003-2026, The Open MPI Community


and that's the whole page, bestie β€” no notes, no missing details, ate and left no crumbs. 🌈⭐ this was MPI_ALLOC_MEM(3) from the Open MPI manual, page dated Sep 10, 2026. go forth and allocate responsibly. πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from