What is the proper way to handle MPI communicators in Fortran?

1.4k Views Asked by At

I read that is recommended to use the MPI module rather than include mpif.h file. However, I get the following error

Error: There is no specific subroutine for the generic ‘mpi_comm_split’

when I run this program

program hello_world
  use mpi_f08
  implicit none
  ! include 'mpif.h'
  integer :: ierr, num_procs, my_id,newcomm
  integer :: color,key

  call MPI_INIT ( ierr )
  color =1; key=0
  call MPI_COMM_RANK (MPI_COMM_WORLD, my_id, ierr)
  call MPI_COMM_SIZE (MPI_COMM_WORLD, num_procs, ierr)

  call MPI_Comm_split(MPI_COMM_WORLD, color,key,newcomm, ierr)

  call MPI_FINALIZE ( ierr )

end

The error disappears if I include 'mpif.h' instead of using the MPI module. Why is that?

1

There are 1 best solutions below

5
On BEST ANSWER

The use mpi_f08 interface introduces different wrapper types for the different MPI handle objects. While in mpif.h and the use mpi interfaces all handles are simply INTEGERs, in the use mpi_f08 interface there are TYPE(MPI_Comm), TYPE(MPI_File), etc. This allows the compiler to perform checks for things like passing a communicator handle where a file handle is expected.

This is a breaking change on the source level as code must be rewritten, e.g.,

INTEGER :: newcomm

becomes

TYPE(MPI_Comm) :: newcomm

On the binary level there is no change since all those MPI_Xyz types are simply an INTEGER wrapped in a TYPE specifier, which makes them layout compatible. Old Fortran code can still exchange MPI handles with modern Fortran code and vice versa - the INTEGER handle value can be set or extracted via newcomm%MPI_VAL.