Skip to content

Commit b62545e

Browse files
authored
Merge pull request #7921 from jsquyres/pr/fix-fortran8integer-vs-c4int-issue
mpi_f08: fix Fortran-8-byte-INTEGER vs. C-4-byte-int issue
2 parents 62d1f89 + 98bc7af commit b62545e

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

config/ompi_setup_mpi_fortran.m4

+21-5
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,36 @@ AC_DEFUN([OMPI_SETUP_MPI_FORTRAN],[
244244

245245
# How big should MPI_STATUS_SIZE be? (i.e., the size of
246246
# MPI_STATUS, expressed in units of Fortran INTEGERs). The C
247-
# equivalent of MPI_Status contains 4 C ints and a size_t.
247+
# MPI_Status struct contains 4 C ints and a size_t.
248248
OMPI_FORTRAN_STATUS_SIZE=0
249-
AC_MSG_CHECKING([for the value of MPI_STATUS_SIZE])
249+
250+
# Calculate how many C int's can fit in sizeof(MPI_Status). Yes,
251+
# I do mean C ints -- not Fortran INTEGERS. The reason is because
252+
# an mpif.h MPI_Status is an array of INTEGERS. But these
253+
# sizeof(INTEGER) may be larger than sizeof(int). Hence,
254+
# MPI_Status_ctof() basically does this:
255+
#
256+
# MPI_Fint *f_status = ...;
257+
# int *s = (int*) &c_status;
258+
# for i=0..sizeof(MPI_Status)/sizeof(int)
259+
# f_status[i] = c_status[i];
260+
#
261+
# Meaning: we have to have as many Fortran INTEGERs in the array
262+
# as int's will fit in a C MPI_Status (vs. just having a Fortran
263+
# array of INTEGERs that has enough bytes to hold a C MPI_Status).
250264
bytes=`expr 4 \* $ac_cv_sizeof_int + $ac_cv_sizeof_size_t`
251-
num_integers=`expr $bytes / $ac_cv_sizeof_int`
252-
sanity=`expr $num_integers \* $ac_cv_sizeof_int`
265+
AC_MSG_NOTICE([C MPI_Status is $bytes bytes long])
266+
AC_MSG_CHECKING([for the value of MPI_STATUS_SIZE])
267+
num_ints=`expr $bytes / $ac_cv_sizeof_int`
268+
sanity=`expr $num_ints \* $ac_cv_sizeof_int`
253269
AS_IF([test "$sanity" != "$bytes"],
254270
[AC_MSG_RESULT([unknown!])
255271
AC_MSG_WARN([WARNING: Size of C int: $ac_cv_sizeof_int])
256272
AC_MSG_WARN([WARNING: Size of C size_t: $ac_cv_sizeof_size_t])
257273
AC_MSG_WARN([WARNING: Size of Fortran INTEGER: $OMPI_SIZEOF_FORTRAN_INTEGER])
258274
AC_MSG_WARN([Could not make this work out evenly...!])
259275
AC_MSG_ERROR([Cannot continue])])
260-
OMPI_FORTRAN_STATUS_SIZE=$num_integers
276+
OMPI_FORTRAN_STATUS_SIZE=$num_ints
261277
AC_MSG_RESULT([$OMPI_FORTRAN_STATUS_SIZE Fortran INTEGERs])
262278
AC_SUBST(OMPI_FORTRAN_STATUS_SIZE)
263279

ompi/mpi/fortran/use-mpi-f08/mod/mpi-f08-types.F90

+12-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,18 @@ module mpi_f08_types
7474
integer :: MPI_SOURCE
7575
integer :: MPI_TAG
7676
integer :: MPI_ERROR
77-
integer(C_INT) OMPI_PRIVATE :: c_cancelled
78-
integer(C_SIZE_T) OMPI_PRIVATE :: c_count
77+
! The mpif.h interface uses MPI_STATUS_SIZE to know how long of
78+
! an array of INTEGERs is necessary to hold a C MPI_Status.
79+
! Effectively do the same thing here: pad out this datatype with
80+
! as many INTEGERs as there are C int's can fit in
81+
! sizeof(MPI_Status) bytes -- see MPI_Status_ctof() for an
82+
! explanation why.
83+
!
84+
! This padding makes this F08 Type(MPI_Status) be the same size
85+
! as the mpif.h status (i.e., an array of MPI_STATUS_SIZE
86+
! INTEGERs), which is critical for MPI_Status_ctof() to not
87+
! overwrite memory.
88+
integer OMPI_PRIVATE :: internal(MPI_STATUS_SIZE - 3)
7989
end type MPI_Status
8090

8191
!

0 commit comments

Comments
 (0)