get_mpi_comm_info Subroutine

public subroutine get_mpi_comm_info(comm, rank, nranks, error)

Determine rank and size of a communicator

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: comm

MPI communicator handle, users of mpi_f08 pass comm%mpi_val

integer, intent(out) :: rank

Zero-based rank of the calling process

integer, intent(out) :: nranks

Number of processes in the communicator

type(error_type), intent(out), allocatable :: error

Error handling


Source Code

subroutine get_mpi_comm_info(comm, rank, nranks, error)

   !> MPI communicator handle, users of mpi_f08 pass comm%mpi_val
   integer, intent(in) :: comm

   !> Zero-based rank of the calling process
   integer, intent(out) :: rank

   !> Number of processes in the communicator
   integer, intent(out) :: nranks

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

#ifdef WITH_MPI
   type(MPI_Comm) :: mpi_comm
   integer :: stat
   logical :: initialized

   rank = 0
   nranks = 1

   call MPI_Initialized(initialized, stat)
   if (stat /= MPI_SUCCESS .or. .not.initialized) then
      call fatal_error(error, "MPI is not initialized")
      return
   end if

   ! the integer handle is the portable currency between the language bindings
   mpi_comm%mpi_val = comm

   call MPI_Comm_rank(mpi_comm, rank, stat)
   if (stat /= MPI_SUCCESS) then
      call fatal_error(error, "Could not determine rank of MPI communicator")
      return
   end if

   call MPI_Comm_size(mpi_comm, nranks, stat)
   if (stat /= MPI_SUCCESS) then
      call fatal_error(error, "Could not determine size of MPI communicator")
      return
   end if
#else
   rank = 0
   nranks = 1
   call fatal_error(error, unavailable)
#endif

end subroutine get_mpi_comm_info