MPI_Cart_shift
MPI_Cart_shift finds the resulting source and destination ranks – given a shift direction and amount.
Fortran Syntax
Subroutine MPI_Cart_shift(comm, direction, displ, source, dest, ierr)
C Syntax
int MPI_Cart_shift(MPI_Comm comm, int direction, int displ, int *source, int *dest)
Loosely speaking, MPI_Cart_shift is used to find two “nearby”
neighbors of the calling process along a specific direction of an Ndimensional cartesian topology. This direction is specified by the input argument, direction
, to MPI_Cart_shift
. The two neighbors are called source and destination ranks and the proximity of these two neighbors to the calling process is determined by the input parameter displ
. If displ = 1
, the neighbors are the two adjoining processes along the specified direction and the source is the process with the lower rank number while the destination rank is the process with the higher rank. On the other hand, if displ = 1
, the reverse is true. A simple code fragment and a complete sample code are shown below to demonstrate the usage. A more practical example is shown in the iterative solvers application.
Example in Fortran

Example in C

In the above example, we demonstrate the application of MPI_Cart_shift to obtain the source and destination rank numbers of the calling process, me
, resulting from shifting it along the first direction of the 2D cartesian grid by one.
Shown in Figure a below is a 3×2 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.
0,0 (0)  0,1 (1) 
1,0 (2)  1,1 (3) 
2,0 (4)  2,1 (5) 
With the input as specified above and if the calling process were, say 2, the source and destination ranks as a result of the shift would be 0 and 4, respectively. Similarly, if the calling process were 1, the source rank would be 5 and destination rank would be 3. The source rank of 5 is the consequence of period(0) = .true. More examples are included in this sample code.
Note that:
 direction, the cartesian grid index, has range (0, 1, …, ndim1). For a 2D grid, the 2 choices for direction are 0 and 1.
 the returned rank number resulting from the MPI_Cart_shift call is informational only. It does not result in the calling process rank to change.
 a negative returned value (MPI_UNDEFINED) signifies that the shift is out of bound.
 if cyclic condition is present along the shift direction, out of bound would not result. (see sample code).