Skip to content

mpi_f08: fix Fortran-8-byte-INTEGER vs. C-4-byte-int issue #7921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions config/ompi_setup_mpi_fortran.m4
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,36 @@ AC_DEFUN([OMPI_SETUP_MPI_FORTRAN],[

# How big should MPI_STATUS_SIZE be? (i.e., the size of
# MPI_STATUS, expressed in units of Fortran INTEGERs). The C
# equivalent of MPI_Status contains 4 C ints and a size_t.
# MPI_Status struct contains 4 C ints and a size_t.
OMPI_FORTRAN_STATUS_SIZE=0
AC_MSG_CHECKING([for the value of MPI_STATUS_SIZE])

# Calculate how many C int's can fit in sizeof(MPI_Status). Yes,
# I do mean C ints -- not Fortran INTEGERS. The reason is because
# an mpif.h MPI_Status is an array of INTEGERS. But these
# sizeof(INTEGER) may be larger than sizeof(int). Hence,
# MPI_Status_ctof() basically does this:
#
# MPI_Fint *f_status = ...;
# int *s = (int*) &c_status;
# for i=0..sizeof(MPI_Status)/sizeof(int)
# f_status[i] = c_status[i];
#
# Meaning: we have to have as many Fortran INTEGERs in the array
# as int's will fit in a C MPI_Status (vs. just having a Fortran
# array of INTEGERs that has enough bytes to hold a C MPI_Status).
bytes=`expr 4 \* $ac_cv_sizeof_int + $ac_cv_sizeof_size_t`
num_integers=`expr $bytes / $ac_cv_sizeof_int`
sanity=`expr $num_integers \* $ac_cv_sizeof_int`
AC_MSG_NOTICE([C MPI_Status is $bytes bytes long])
AC_MSG_CHECKING([for the value of MPI_STATUS_SIZE])
num_ints=`expr $bytes / $ac_cv_sizeof_int`
sanity=`expr $num_ints \* $ac_cv_sizeof_int`
AS_IF([test "$sanity" != "$bytes"],
[AC_MSG_RESULT([unknown!])
AC_MSG_WARN([WARNING: Size of C int: $ac_cv_sizeof_int])
AC_MSG_WARN([WARNING: Size of C size_t: $ac_cv_sizeof_size_t])
AC_MSG_WARN([WARNING: Size of Fortran INTEGER: $OMPI_SIZEOF_FORTRAN_INTEGER])
AC_MSG_WARN([Could not make this work out evenly...!])
AC_MSG_ERROR([Cannot continue])])
OMPI_FORTRAN_STATUS_SIZE=$num_integers
OMPI_FORTRAN_STATUS_SIZE=$num_ints
AC_MSG_RESULT([$OMPI_FORTRAN_STATUS_SIZE Fortran INTEGERs])
AC_SUBST(OMPI_FORTRAN_STATUS_SIZE)

Expand Down
14 changes: 12 additions & 2 deletions ompi/mpi/fortran/use-mpi-f08/mod/mpi-f08-types.F90
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ module mpi_f08_types
integer :: MPI_SOURCE
integer :: MPI_TAG
integer :: MPI_ERROR
integer(C_INT) OMPI_PRIVATE :: c_cancelled
integer(C_SIZE_T) OMPI_PRIVATE :: c_count
! The mpif.h interface uses MPI_STATUS_SIZE to know how long of
! an array of INTEGERs is necessary to hold a C MPI_Status.
! Effectively do the same thing here: pad out this datatype with
! as many INTEGERs as there are C int's can fit in
! sizeof(MPI_Status) bytes -- see MPI_Status_ctof() for an
! explanation why.
!
! This padding makes this F08 Type(MPI_Status) be the same size
! as the mpif.h status (i.e., an array of MPI_STATUS_SIZE
! INTEGERs), which is critical for MPI_Status_ctof() to not
! overwrite memory.
integer OMPI_PRIVATE :: internal(MPI_STATUS_SIZE - 3)
end type MPI_Status

!
Expand Down