1

I want to create a wrapper procedure for MPI_Size in Fortran. I wrote the function wmpi_size which returns an mpi size given a communicator. I would like the communicator to be an optional input, so if it is not present I set it to mpi_comm_world. The code compiles either way, but when I call wmpi_size without input the code crashes at runtime. One way to fix this is to pass mpi_comm_world to mpi_comm_size directly if the communicator is not present, as shown in the commented function definition, but I would like to know how to fix the original function.

env: gcc-7.3.0 + openmpi-3.1.2

compilation: mpifort -o test.exe test.f08

execution: mpiexec -n 2 ./test.exe

program test

   use mpi_f08

   implicit none 

   type(mpi_comm) :: comm
   integer :: mpisize

   call mpi_init()

   mpisize = wmpi_size()
   print *, mpisize

   call mpi_finalize()

contains 

   integer function wmpi_size(comm)
      type(mpi_comm), optional :: comm
      integer :: ierr
      if ( .not. present(comm) ) then
         comm = mpi_comm_world
      end if
      call mpi_comm_size(comm, wmpi_size, ierr)
      if ( ierr /= 0 ) error stop 'wmpi_size failed'
   end function wmpi_size

   ! integer function wmpi_size(comm)
   !    type(mpi_comm), optional :: comm
   !    integer :: ierr
   !    if ( .not. present(comm) ) then
   !       call mpi_comm_size(mpi_comm_world, wmpi_size, ierr)
   !    else
   !       call mpi_comm_size(comm, wmpi_size, ierr)
   !    end if
   !    if ( ierr /= 0 ) error stop 'wmpi_size failed'
   ! end function wmpi_size

end program test
  • 1
    You need a local variable for the communicator - if comm is not passed how can you store values in it? – Ian Bush Sep 18 '19 at 21:13

0 Answers0