MPI_Cart_shift finds the resulting source and destination ranks - given a shift direction and amount.
Subroutine MPI_Cart_shift(comm, direction, displ, source, dest, ierr)
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 N-dimensional 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 3x2 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.