MPI_Cart_create creates a new communicator using a cartesian topology.
Example in Fortran
|
In the above example we use MPI_Cart_create to map (or rename) 6 processes
from a linear ordering (
With processes renamed in a 2D grid topology, we will be able to
assign or distribute work, or distinguish among the processes by
their grid topology rather than by their linear process ranks.
Additionally, we imposed periodicity along the first
dimension ( periods(0)=.true. ) which means that any reference beyond the first
or last entry of any row will be wrapped around cyclically. For example,
row index i=-1, due to periodicity, is mapped into i=2; similarly, i=-2 is
mapped to i=1. Likewise, i=3 is the same as i=0.
There is no periodicity imposed on the second dimension ( periods(1)=.false. ).
Any reference to the column index outside of its defined range ( in this
case 0 to 1) will result in a negative process rank (equal to
MPI_PROC_NULL which is -1) which signifies that
it is out of range.
Similarly, if periodicity is defined only for the column index
(i.e., periods(0)=.false.; periods(1)=.true.), each row will wrap around itself. The effects of periodic columns and periodic rows are depicted in figures b and c, respectively. The tan-colored cells indicate cyclic boundary condition in effect.
While having the processes laid out in the cartesian topology help the programmer
to write code that's conceivably more readable, many MPI routines only
recognize rank number and hence knowing the relationship between ranks
and cartesian coordinates (as shown in the tables above) is key to the
opportunity to exploit the topology for computational expediency.
In the following sections, we will discuss two subroutines that will provide us with
these informations. They are:
MPI_Cart_coords and
MPI_Cart_rank.
Here is the fortran code used to generate
the above tables.
0,0 (0) 0,1 (1) 1,0 (2) 1,1 (3) 2,0 (4) 2,1 (5)
-1,0 (4)
-1,1 (5)
0,-1(-1)
0,0 (0)
0,1 (1)
0,2(-1)
1,-1(-1)
1,0
(2)
1,1
(3)
1,2
(-1)
2,-1(-1)
2,0 (4)
2,1 (5)
2,2 (-1)
3,0 (0)
3,1 (1)
-1,0 (-1)
-1,1 (-1)
0,-1 (1)
0, 0 (0)
0, 1 (1)
0, 2 (0)
1,-1 (3)
1, 0 (2)
1, 1 (3)
1, 2 (2)
2,-1 (5)
2, 0 (4)
2, 1 (5)
2, 2 (4)
3,0 (-1)
3,1 (-1)
Example in C
#include "mpi.h";
MPI_Comm old_comm, new_comm;
int ndims, reorder, ierr;
int dim_size[2], periods[2];
old_comm = MPI_COMM_WORLD;
ndims = 2;
dim_size[0] = 3;
dim_size[1] = 2;
periods[0] = 1;
periods[1] = 0;
reorder = 1;
MPI_Cart_create(old_comm, ndims, dim_size,
periods, reorder, &new_comm);