20.3.10. Interlanguage Communication

PreviousUpNext
Up: Language Interoperability Next: Application Binary Interface (ABI) Previous: Constants

The type matching rules for communication in MPI are not changed: the datatype specification for each item sent should match, in type signature, the datatype specification used to receive this item (unless one of the types is MPI_PACKED). Also, the type of a message item should match the type declaration for the corresponding communication buffer location, unless the type is MPI_BYTE or MPI_PACKED. Interlanguage communication is allowed if it complies with these rules.


Example In the example below, a Fortran array is sent from Fortran and received in C.


! FORTRAN CODE 
SUBROUTINE MYEXAMPLE() 
USE mpi_f08 
REAL :: R(5) 
INTEGER :: IERR, MYRANK, AOBLEN(1) 
TYPE(MPI_Datatype) :: DTYPE, AOTYPE(1) 
INTEGER(KIND=MPI_ADDRESS_KIND) :: AODISP(1) 
 
! create an absolute datatype for array R 
AOBLEN(1) = 5 
CALL MPI_GET_ADDRESS(R, AODISP(1), IERR) 
AOTYPE(1) = MPI_REAL 
CALL MPI_TYPE_CREATE_STRUCT(1, AOBLEN, AODISP, AOTYPE, DTYPE, IERR) 
CALL MPI_TYPE_COMMIT(DTYPE, IERR) 
 
CALL MPI_COMM_RANK(MPI_COMM_WORLD, MYRANK, IERR) 
IF (MYRANK .EQ. 0) THEN 
   CALL MPI_SEND(MPI_BOTTOM, 1, DTYPE, 1, 0, MPI_COMM_WORLD, IERR) 
ELSE 
   CALL C_ROUTINE(DTYPE%MPI_VAL) 
END IF 
END SUBROUTINE 


/* C code */ 
 
void C_ROUTINE(MPI_Fint *fhandle) 
{ 
   MPI_Datatype type; 
   MPI_Status status; 
 
   type = MPI_Type_f2c(*fhandle); 
 
   MPI_Recv(MPI_BOTTOM, 1, type, 0, 0, MPI_COMM_WORLD, &status); 
} 

MPI implementors may weaken these type matching rules, and allow messages to be sent with Fortran types and received with C types, and vice versa, when those types match. I.e., if the Fortran type INTEGER is identical to the C type int, then an MPI implementation may allow data to be sent with datatype MPI_INTEGER and be received with datatype MPI_INT. However, such code is not portable.


PreviousUpNext
Up: Language Interoperability Next: Application Binary Interface (ABI) Previous: Constants


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

(Unofficial) MPI-5.0 of June 9, 2025
HTML Generated on March 2, 2025