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.> 0means an upward shift,< 0means 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: thedirectionargument is in the range[0, n-1]for ann-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 ifMPI_Abortwas called on that communicator. if called on a window or file, it acts as ifMPI_Abortwas 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_errhandlerthenMPI_Comm_set_errhandlerMPI_File_create_errhandlerthenMPI_File_set_errhandlerMPI_Session_create_errhandlerthenMPI_Session_set_errhandleror atMPI_Session_initMPI_Win_create_errhandlerthenMPI_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. π
π COPYRIGHT π
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
- arch rolling: openmpi 5.0.11-1 Β· BSD-3-Clause AND LicenseRef-MPICH
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms