MPI_CART_SHIFT(3)

πŸŽ€ MPI_CART_SHIFT(3) πŸŽ€

okay bestie, welcome to the Open MPI corner of the manual β€” this one's MPI_Cart_shift, and it's genuinely one of the cutest little routines in the whole cartesian-topology friend group. it hands you back the shifted source and destination ranks for a given shift direction and amount. no cap. πŸ’…


✨ NAME ✨

MPI_Cart_shift β€” returns the shifted source and destination ranks, given a shift direction and amount.


🌸 SYNTAX 🌸

πŸ’– C Syntax

#include <mpi.h>

int MPI_Cart_shift(MPI_Comm comm, int direction, int disp,
    int *rank_source, int *rank_dest)

πŸ’– Fortran Syntax

USE MPI
! or the older form: INCLUDE 'mpif.h'

MPI_CART_SHIFT(COMM, DIRECTION, DISP, RANK_SOURCE,
        RANK_DEST, IERROR)
    INTEGER COMM, DIRECTION, DISP, RANK_SOURCE
    INTEGER RANK_DEST, IERROR

πŸ’– Fortran 2008 Syntax

USE mpi_f08

MPI_Cart_shift(comm, direction, disp, rank_source, rank_dest, ierror)
    TYPE(MPI_Comm), INTENT(IN) :: comm
    INTEGER, INTENT(IN) :: direction, disp
    INTEGER, INTENT(OUT) :: rank_source, rank_dest
    INTEGER, OPTIONAL, INTENT(OUT) :: ierror

πŸ¦‹ INPUT PARAMETERS πŸ¦‹

  • comm β€” the communicator that's got that cartesian structure going on (handle). πŸ—ΊοΈ
  • direction β€” the coordinate dimension you're shifting along (integer).
  • disp β€” the displacement, bestie. > 0 means an upward shift, < 0 means a downward shift (integer). ⬆️⬇️

🌈 OUTPUT PARAMETERS 🌈

  • rank_source β€” rank of the source process (integer). 🫢
  • rank_dest β€” rank of the destination process (integer). 🎯
  • ierror β€” Fortran only: error status (integer).

πŸ‘‘ DESCRIPTION πŸ‘‘

so here's the vibe: if your process topology is a cartesian structure, you're probably gonna run an MPI_Sendrecv along some coordinate direction to actually shift the data around. and MPI_Sendrecv wants two things as input β€” the rank of a source process (for the receive) and the rank of a destination process (for the send). that's literally what MPI_Cart_shift gives you. call it on a cartesian process group and it hands the calling process those exact identifiers, which you then pass straight into MPI_Sendrecv. iconic teamwork. 🀝 you tell it the coordinate direction and the size of the step (positive or negative), and heads up β€” the function is local.

the direction argument says which dimension you're shifting along, i.e. the coordinate whose value gets modified by the shift. coordinates are numbered from 0 to ndims-1, where ndims is the number of dimensions. ✨

πŸ“Œ Note: the direction argument is in the range [0, n-1] for an n-dimensional cartesian mesh.

depending on how periodic your cartesian group is in the specified coordinate direction, MPI_Cart_shift gives you identifiers for either a circular shift or an end-off shift. and for an end-off shift? the value MPI_PROC_NULL may come back in rank_source or rank_dest, which is the "yeah that source/destination is out of range, bestie" signal. πŸ’€


🧚 Example 🧚

okay so picture this: the communicator comm has a two-dimensional, periodic, cartesian topology attached to it. a two-dimensional array of REALs is stored one element per process, in variable A. and we wanna skew this array by shifting column i (vertically, i.e. along the column) by i steps. slay. πŸ’–

! find process rank
    CALL MPI_COMM_RANK(comm, rank, ierr)
! find Cartesian coordinates
    CALL MPI_CART_COORDS(comm, rank, maxdims, coords, ierr)
! compute shift source and destination
    CALL MPI_CART_SHIFT(comm, 0, coords(2), source, dest, ierr)
! skew array
    CALL MPI_SENDRECV_REPLACE(A, 1, MPI_REAL, dest, 0, source, 0, comm, status,
                              ierr)

⭐ NOTE ⭐

in Fortran, the dimension indicated by DIRECTION = i has DIMS(i+1) nodes, where DIMS is the array that was used to create the grid. in C, the dimension indicated by direction = i is the dimension specified by dims[i]. different indexing, same energy β€” don't let the off-by-one jump you. πŸ™ˆ


πŸ’œ ERRORS πŸ’œ

almost all MPI routines return an error value β€” C routines as the return result of the function, and Fortran routines in the last argument. πŸ“¬

before the error value is returned, the current MPI error handler associated with the communication object (e.g. communicator, window, file) gets called. if there's no communication object associated with the MPI call, then the call is considered attached to MPI_COMM_SELF and will call the associated MPI error handler. and when MPI_COMM_SELF is not initialized (i.e. before MPI_Init/MPI_Init_thread, after MPI_Finalize, or when using the Sessions Model exclusively) the error raises the initial error handler. the initial error handler can be changed by calling MPI_Comm_set_errhandler on MPI_COMM_SELF when using the World model, or the mpi_initial_errhandler CLI argument to mpiexec or info key to MPI_Comm_spawn/MPI_Comm_spawn_multiple. if no other appropriate error handler has been set, then the MPI_ERRORS_RETURN error handler is called for MPI I/O functions and the MPI_ERRORS_ABORT error handler is called for all other MPI functions. 😭

Open MPI includes three predefined error handlers you can use:

  • MPI_ERRORS_ARE_FATAL β€” causes the program to abort all connected MPI processes. πŸ’€
  • MPI_ERRORS_ABORT β€” an error handler that can be invoked on a communicator, window, file, or session. when called on a communicator, it acts as if MPI_Abort was called on that communicator. if called on a window or file, it acts as if MPI_Abort was called on a communicator containing the group of processes in the corresponding window or file. if called on a session, it aborts only the local process. πŸ”₯
  • MPI_ERRORS_RETURN β€” returns an error code to the application. πŸ“¨

MPI applications can also implement their own error handlers by calling:

  • MPI_Comm_create_errhandler then MPI_Comm_set_errhandler
  • MPI_File_create_errhandler then MPI_File_set_errhandler
  • MPI_Session_create_errhandler then MPI_Session_set_errhandler or at MPI_Session_init
  • MPI_Win_create_errhandler then MPI_Win_set_errhandler
⚠️ note that MPI does not guarantee that an MPI program can continue past an error. like, at all. 🫠

see the MPI man page for a full list of MPI error codes. and see the Error Handling section of the MPI-3.1 standard for more information. πŸ“š


2003-2026, The Open MPI Community


and that's a wrap, bestie!! πŸŽ€βœ¨ written by the Open MPI girlies, dated Sep 11, 2026, and filed under MPI_CART_SHIFT(3) β€” go forth and shift those ranks, you absolute main character. πŸ‘‘πŸ’…πŸ¦‹

πŸ“¦ where this page comes from