MPI_Cart_rank

MPI_Cart_rank finds the corresponding process rank of the cartesian coordinates of a cartesian communicator.

Fortran Syntax

Subroutine MPI_Cart_rank(comm, coords, rank, ierr)

C Syntax

int MPI_Cart_rank(MPI_Comm comm, int *coords, int *rank)


Example in Fortran


      call MPI_Cart_create(old_comm,ndims,dim_size,
     &       periods,reorder,new_comm,ierr)

      if(Iam .eq. root) then    !! only want to do this on one process
        do i=0,nv-1
          do j=0,mv-1
            coords(0) = i
            coords(1) = j
            call MPI_Cart_rank(new_comm,coords,rank,ierr)
            write(*,*)coords,rank
          enddo
        enddo
      endif

Once a cartesian communicator has been established, repeated applications of MPI_Cart_rank for all possible values of the cartesian coordinates produce a correlation table of the cartesian coordinates and their corresponding process ranks.

Shown in Figure a below is the resulting cartesian topology (grid) where the index pair “i,j” represent row “i” and column “j”. The number in parentheses represents the rank number associated with the cartesian coordinates.

Figure a. Cartesian Grid
0,0 (0) 0,1 (1)
1,0 (2) 1,1 (3)
2,0 (4) 2,1 (5)

Here is the fortran code used to generate the above tables.


Note that:

  • the rank number of the cartesian grid (i.e., number in parentheses) need not be the same as those corresponding to MPI_COMM_WORLD.
  • this routine is the reciprocal of MPI_Cart_coords.
  • the return rank of the routine could be negative (with the value MPI_PROC_NULL), indicating that the specified coords is out of bound.