# 4.11. Memory Allocation

Up: Miscellany Next: Language Interoperability Previous: The Info Object

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 the MPI_WIN_LOCK and MPI_WIN_UNLOCK functions to windows allocated in such memory (see Section Lock .)

MPI_ALLOC_MEM(size, info, baseptr)
IN size | size of memory segment in bytes (nonnegative integer)
IN info | info argument (handle)
OUT baseptr | pointer to beginning of memory segment allocated

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

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

`void* MPI::Alloc_mem(MPI::Aint size, const MPI::Info& info)
`

The info argument can 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. Valid info values are implementation-dependent; a null directive value of info = MPI_INFO_NULL is always valid.

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

MPI_FREE_MEM(base)
IN base | initial address of memory segment allocated by MPI_ALLOC_MEM (choice)

`int MPI_Free_mem(void *base)
`

`MPI_FREE_MEM(BASE, IERROR)
<type> BASE(*)
INTEGER IERROR
`

`void MPI::Free_mem(void *base)
`

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

Rationale.

The C and 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 and C++ bindings: the Fortran MPI_ALLOC_MEM call returns in baseptr 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 pointer support. We assume 4-byte REALs, and assume that pointers are address-sized.

    REAL A
    POINTER (P, A(100,100))   ! no memory is allocated
    CALL MPI_ALLOC_MEM(4*100*100, MPI_INFO_NULL, P, IERR)
    ! memory is allocated
    ...
    A(3,5) = 2.71;
    ...
    CALL MPI_FREE_MEM(A, IERR) ! memory is freed

Since standard Fortran does not support (C-like) pointers, this code is not Fortran 77 or Fortran 90 code. Some compilers (in particular, at the time of writing, g77 and Fortran compilers for Intel) do not support this code.

Example Same example, in C

    float  (* f)[100][100] ;
    MPI_Alloc_mem(sizeof(float)*100*100, MPI_INFO_NULL, &f);
    ...
    (*f)[5][3] = 2.71;
    ...
    MPI_Free_mem(f);

Up: Miscellany Next: Language Interoperability Previous: The Info Object

Return to MPI-2 Standard Index
Return to MPI 1.1 Standard Index
Return to MPI Forum Home Page

MPI-2.0 of July 18, 1997
HTML Generated on September 10, 2001