This page imported from: /afs/bu.edu/cwis/webuser/web/s/c/scv/documentation/tutorials/MPI/alliance/virtual_topology/codes/cart_shift_example.html

MPI_Cart_shift Example

MPI_Cart_shift Example

In this example, we demonstrate the usage of
MPI_Cart_shift. With six (6) active processes, we
proceed to create a 2D cartesian topology for these 6 processes.
This results in a 3×2 cartesian topology representation for the
6 processes. Furthermore, we impose a cyclic boundary condition
down the rows — but not the columns – of this 2D grid.
Given the calling process rank number in the cartesian grid
communicator, upon calling MPI_Cart_shift, the source and destination
ranks of the calling process rank are returned:

  1. along the rows and a displacement of +1, the source is the
    rank above the calling rank and the destination is the rank below it.

  2. along the rows and a displacement of -2, the source is two
    ranks above the calling rank and the destination is two ranks below it.

  3. across the columns and a displacement of +1, the source is the
    rank to the left of the calling rank and the destination is the rank
    to the right.

  4. across the columns and a displacement of -1, the source is the rank
    to the right of the calling rank and the destination is the rank
    to the left.



Output

Note that some returned ranks in the last four columns are negative
because they are out of bound and are assigned the value MPI_UNDEFINED.
The value of MPI_UNDEFINED is implementation dependent. In this case, it
is -1.


tonka:virtual_topology/codes % mpirun -np 6 cart_shift_example                

                  (   along the rows     )(   across columns     )
                         
    2D   Row   Col  From    To  From    To  From    To  From    To
  Rank     i     j   Src  Dest   Src  Dest   Src  Dest   Src  Dest
     0     0     0     4     2     4     2    -1     1     1    -1
     1     0     1     5     3     5     3     0    -1    -1     0
     2     1     0     0     4     0     4    -1     3     3    -1
     3     1     1     1     5     1     5     2    -1    -1     2
     4     2     0     2     0     2     0    -1     5     5    -1
     5     2     1     3     1     3     1     4    -1    -1     4

For your convenience in seeing what MPI_Cart_shift does under the four
different sets of input parameters, shown in Figure a below is the 3×2
cartesian topology grid.
Here, 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. periods(0)=.true.;periods(1)=.false.
-2,0 (2) -1,1 (3)
-1,0 (4) -1,1 (5)
0,-1(-1) 0,0 (0) 0,1 (1) 0,2(-1)
1,-1(-1) 1,0 (2) 1,1 (3)

1,2 (-1)
2,-1(-1) 2,0 (4) 2,1 (5) 2,2 (-1)
3,0 (0) 3,1 (1)
4,0 (2) 4,1 (3)