10.2. Memory Allocation

PreviousUpNext
Up: MPI Environmental Management Next: Error Handling Previous: Inquire Hardware Resource Information

In some systems, message-passing and remote-memory-access ( RMA) operations run faster when accessing specially allocated memory (e.g., memory that is shared by the other processes in the communicating group on an SMP). MPI provides a mechanism for allocating and freeing such special memory. The use of such memory for message-passing or RMA is not mandatory, and this memory can be used without restrictions as any other dynamically allocated memory. However, implementations may restrict the use of some RMA functionality as defined in Section Lock.

MPI_ALLOC_MEM(size, info, baseptr)
IN sizesize of memory segment in bytes (non-negative integer)
IN infoinfo argument (handle)
OUT baseptrpointer to beginning of memory segment allocated
C binding
int MPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr)
Fortran 2008 binding
MPI_Alloc_mem(size, info, baseptr, ierror)

USE, INTRINSIC :: ISO_C_BINDING, ONLY : C_PTR
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
Fortran binding
MPI_ALLOC_MEM(SIZE, INFO, BASEPTR, IERROR)

INTEGER(KIND=MPI_ADDRESS_KIND) SIZE, BASEPTR
INTEGER INFO, IERROR

If the Fortran compiler provides TYPE(C_PTR), then the following generic interface must be provided in the mpi module and should be provided in the (deprecated) mpif.h include file through overloading, i.e., with the same routine name as the routine with INTEGER(KIND=MPI_ADDRESS_KIND) BASEPTR, but with a different specific procedure name:


INTERFACE MPI_ALLOC_MEM 
    SUBROUTINE MPI_ALLOC_MEM(SIZE, INFO, BASEPTR, IERROR) 
        IMPORT :: MPI_ADDRESS_KIND 
        INTEGER :: INFO, IERROR 
        INTEGER(KIND=MPI_ADDRESS_KIND) :: SIZE, BASEPTR 
    END SUBROUTINE 
    SUBROUTINE MPI_ALLOC_MEM_CPTR(SIZE, INFO, BASEPTR, IERROR) 
        USE, INTRINSIC :: ISO_C_BINDING, ONLY : C_PTR 
        IMPORT :: MPI_ADDRESS_KIND 
        INTEGER :: INFO, IERROR 
        INTEGER(KIND=MPI_ADDRESS_KIND) :: SIZE 
        TYPE(C_PTR) :: BASEPTR 
    END SUBROUTINE 
END INTERFACE 
The base procedure name of this overloaded function is MPI_ALLOC_MEM_CPTR. The implied specific procedure names are described in Section Interface Specifications, Procedure Names, and the Profiling Interface.

By default, the allocated memory shall be aligned to at least the alignment required for load/store accesses of any datatype corresponding to a predefined MPI datatype. The info argument may be used to specify a desired alternative minimum alignment in bytes for the allocated memory by setting the value of the key mpi_minimum_memory_alignment to an integral number equal to a power of two. An implementation may ignore values smaller than the default required alignment. The info argument can also be used to provide directives that control the desired location of the allocated memory. Such a directive does not affect the semantics of the call. The corresponding info values are implementation-dependent. A null directive value of info = MPI_INFO_NULL is always valid.

The function MPI_ALLOC_MEM may raise an error of class MPI_ERR_NO_MEM to indicate it failed because memory is exhausted.

MPI_FREE_MEM(base)
IN baseinitial address of memory segment allocated by MPI_ALLOC_MEM (choice)
C binding
int MPI_Free_mem(void *base)
Fortran 2008 binding
MPI_Free_mem(base, ierror)

TYPE(*), DIMENSION(..), INTENT(IN), ASYNCHRONOUS :: base
INTEGER, OPTIONAL, INTENT(OUT) :: ierror
Fortran binding
MPI_FREE_MEM(BASE, IERROR)

<type> BASE(*)
INTEGER IERROR

The function MPI_FREE_MEM may raise an error of class MPI_ERR_BASE to indicate an invalid base argument.


Rationale.

The C bindings of MPI_ALLOC_MEM and MPI_FREE_MEM are similar to the bindings for the malloc and free C library calls: a call to MPI_Alloc_mem(..., &base) should be paired with a call to MPI_Free_mem(base) (one less level of indirection). Both arguments are declared to be of same type void* so as to facilitate type casting. The Fortran binding is consistent with the C bindings: the Fortran MPI_ALLOC_MEM call returns in baseptr the TYPE(C_PTR) pointer or the (integer valued) address of the allocated memory. The base argument of MPI_FREE_MEM is a choice argument, which passes (a reference to) the variable stored at that location. ( End of rationale.)

Advice to implementors.

If MPI_ALLOC_MEM allocates special memory, then a design similar to the design of C malloc and free functions has to be used, in order to find out the size of a memory segment, when the segment is freed. If no special memory is used, MPI_ALLOC_MEM simply invokes malloc, and MPI_FREE_MEM invokes free.

A call to MPI_ALLOC_MEM can be used in shared memory systems to allocate memory in a shared memory segment. ( End of advice to implementors.)

Example Example of use of MPI_ALLOC_MEM, in Fortran with TYPE(C_PTR) pointers. We assume 4-byte REALs.

Image file


Example Example of use of MPI_ALLOC_MEM, in Fortran with nonstandard Cray-pointers. We assume 4-byte REALs, and assume that these pointers are address-sized.

Image file

This code is not Fortran 77 or Fortran 90 code. Some compilers may not support this code or need a special option, e.g., the GNU gFortran compiler needs -fcray-pointer.


Advice to implementors.

Some compilers map Cray-pointers to address-sized integers, some to TYPE(C_PTR) pointers (e.g., Cray Fortran, version 7.3.3). From the user's viewpoint, this mapping is irrelevant because Examples Memory Allocation should work correctly with an MPI-3.0 (or later) library if Cray-pointers are available. ( End of advice to implementors.)


Example Same example, in C.

Image file


PreviousUpNext
Up: MPI Environmental Management Next: Error Handling Previous: Inquire Hardware Resource Information


Return to MPI-4.1 Standard Index
Return to MPI Forum Home Page

(Unofficial) MPI-4.1 of November 2, 2023
HTML Generated on November 19, 2023