Site Tools


virtual-topologies-mpi

Table of Contents

Virtual topologies (MPI)

In a 2D stencil on a P×Q process grid, each process needs the ranks of its north, south, east, and west neighbors. Computing those ranks manually requires knowing the grid dimensions and handling wraparound explicitly:

int row = rank / Q,  col = rank % Q;
int north = ((row - 1 + P) % P) * Q + col;
int south = ((row + 1)     % P) * Q + col;

This is rather fragile. Changing the process count or grid shape breaks the formula. MPI_Cart_create registers a Cartesian grid topology with MPI so that MPI_Cart_shift can compute neighbor ranks for you, including periodic wraparound.

int dims[2] = {4, 4};
int periods[2] = {1, 1};   // periodic in both dimensions
MPI_Comm cart;
MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 1, &cart);
 
int north, south;
MPI_Cart_shift(cart, 0, 1, &north, &south);  // neighbors along row dimension

Topologies do not change the underlying communication; they are a convenience layer for structured grids and stencil codes where the natural pattern is nearest-neighbor exchange. MPI_Cart_coords converts a rank to its grid coordinates and MPI_Cart_rank does the reverse.

virtual-topologies-mpi.txt · Last modified: by Ivan Janevski