Who are my neighbors?
The routine MPI_Cart_shift finds the neighbors in each direction of the new communicator.
dir = 0; // in C 0 for columns, 1 for rows
// in Fortran, it’s 1 and 2
disp = 1; // specifies first neighbor to the right and left
ierr = MPI_Cart_shift (comm2d, dir, disp, &nbrbottom,
This returns the process numbers (ranks) for a communication of the bottom and top neighbors.
Typically, the neighbors are used with send/recv to exchange data.
If a process in a non-periodic mesh is on the border and has no neighbor, then the value MPI_PROCNULL is returned. This process value can be used in a send/recv, but it will have no effect.