you are required to create a 2D grid using MPI Cartesian topology functions. Figure 1…
- User
you are required to create a 2D grid using MPI Cartesian topology functions. Figure 1 illustrates an example of a 4 by 5 Cartesian grid. For this task, each MPI process (in the grid) is required to print the following: a) b) c) d) Current rank Cartesian rank Coordinates List of immediate adjacent processes (left, right, top and bottom). Users must also have an option to specify the grid size by command line argument(s). Sample code has been provided for you. You are required to fill in 5 lines of code in highlighted areas (in red). Hint: Look for Open MPI functions starting with “MPI_Cart”. Submit your Open MPI solution for task 1 in “task1.c”. #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <mpi.h> #define SHIFT_ROW 0 #define SHIFT_COL 1 #define DISP 1 int main(int argc, char argv[]) { int ndims=2, size, my_rank, reorder, my_cart_rank, ierr; int nrows, ncols; int nbr_i_lo, nbr_i_hi; int nbr_j_lo, nbr_j_hi; MPI_Comm comm2D; int dims[ndims],coord[ndims]; int wrap_around[ndims]; / start up initial MPI environment / MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); / process command line arguments*/ if (argc == 3) { nrows = atoi (argv[1]); ncols = atoi (argv[2]); dims[0] = nrows; /* number of rows / dims[1] = ncols; / number of columns / if( (nrowsncols) != size) { if( my_rank == 0) printf("ERROR: nrowsncols)=%d * %d = %d != %d\n", nrows, ncols, nrowsncols,size); MPI_Finalize(); return 0; } } else { nrows=ncols=(int)sqrt(size); dims[0]=dims[1]=0; } /************************************************************ / / create cartesian topology for processes / /*********************************************************** / MPI_Dims_create(size, ndims, dims); if(my_rank==0) printf("Root Rank: %d. Comm Size: %d: Grid Dimension = [%d x %d] \n",my_rank,size,dims[0],dims[1]); / create cartesian mapping / wrap_around[0] = wrap_around[1] = 0; / periodic shift is .false. / reorder = 1; ierr =0; / ierr = ... / if(ierr != 0) printf("ERROR[%d] creating CART\n",ierr); / find my coordinates in the cartesian communicator group / / . . ./ / use my cartesian coordinates to find my rank in cartesian group*/ /* . . . / / get my neighbors; axis is coordinate dimension of shift / / axis=0 ==> shift along the rows: P[my_row-1]: P[me] : P[my_row+1] / / axis=1 ==> shift along the columns P[my_col-1]: P[me] : P[my_col+1] / / . . . / / . . . */ printf("Global rank: %d. Cart rank: %d. Coord: (%d, %d). Left: %d. Right: %d. Top: %d. Bottom: %d\n", my_rank, my_cart_rank, coord[0], coord[1], nbr_j_lo, nbr_j_hi, nbr_i_lo, nbr_i_hi); fflush(stdout); MPI_Comm_free( &comm2D ); MPI_Finalize(); return 0; }
- Assistant
The missing code lines in your MPI program involve creating the Cartesian communicator, obtaining your rank and coordinates in that communicator, and finding your neighbors using MPI's Cartesian topology functions.
Here's how you can fill in those lines:
/* Create the Cartesian communicator */ ierr = MPI_Cart_create(MPI_COMM_WORLD, ndims, dims, wrap_around, reorder, &comm2D); /* Get my rank in the new Cartesian communicator */ MPI_Comm_rank(comm2D, &my_cart_rank); /* Find my coordinates in the Cartesian communicator */ MPI_Cart_coords(comm2D, my_cart_rank, ndims, coord); /* Get my neighbors in the grid */ MPI_Cart_shift(comm2D, SHIFT_ROW, DISP, &nbr_i_lo, &nbr_i_hi); MPI_Cart_shift(comm2D, SHIFT_COL, DISP, &nbr_j_lo, &nbr_j_hi);This code uses the following MPI functions:
MPI_Cart_createto create a new communicator with a Cartesian topology.MPI_Comm_rankto get your rank in the new communicator.MPI_Cart_coordsto get your coordinates in the Cartesian grid.MPI_Cart_shiftto find your neighboring processes in the grid in both the row and column dimensions.
Your final code should look like this:
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <mpi.h> #define SHIFT_ROW 0 #define SHIFT_COL 1 #define DISP 1 int main(int argc, char *argv[]) { int ndims=2, size, my_rank, reorder, my_cart_rank, ierr; int nrows, ncols; int nbr_i_lo, nbr_i_hi; int nbr_j_lo, nbr_j_hi; MPI_Comm comm2D; int dims[ndims], coord[ndims]; int wrap_around[ndims]; /* start up initial MPI environment */ MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); /* process command line arguments*/ if (argc == 3) { nrows = atoi (argv[1]); ncols = atoi (argv[2]); dims[0] = nrows; /* number of rows */ dims[1] = ncols; /* number of columns */ if( (nrows*ncols) != size) { if( my_rank == 0) printf("ERROR: nrows*ncols)=%d * %d = %d != %d\n", nrows, ncols, nrows*ncols, size); MPI_Finalize(); return 0; } } else { nrows=ncols=(int)sqrt(size); dims[0]=dims[1]=0; } /************************************************************/ /* create cartesian topology for processes */ /************************************************************/ MPI_Dims_create(size, ndims, dims); if(my_rank==0) printf("Root Rank: %d. Comm Size: %d: Grid Dimension = [%d x %d] \n", my_rank, size, dims[0], dims[1]); /* create cartesian mapping */ wrap_around[0] = wrap_around[1] = 0; /* periodic shift is false */ reorder = 1; ierr = 0; ierr = MPI_Cart_create(MPI_COMM_WORLD, ndims, dims, wrap_around, reorder, &comm2D); if(ierr != 0) printf("ERROR[%d] creating CART\n",ierr); /* find my coordinates in the cartesian communicator group */ MPI_Comm_rank(comm2D, &my_cart_rank); MPI_Cart_coords(comm2D, my_cart_rank, ndims, coord); /* get my neighbors; axis is coordinate dimension of shift */ MPI_Cart_shift(comm2D, SHIFT_ROW, DISP, &nbr_i_lo, &nbr_i_hi); MPI_Cart_shift(comm2D, SHIFT_COL, DISP, &nbr_j_lo, &nbr_j_hi); printf("Global rank: %d. Cart rank: %d. Coord: (%d, %d). Left: %d. Right: %d. Top: %d. Bottom: %d\n", my_rank, my_cart_rank, coord[0], coord[1], nbr_j_lo, nbr_j_hi, nbr_i_lo, nbr_i_hi); fflush(stdout); MPI_Comm_free( &comm2D ); MPI_Finalize(); return 0; }Save this code in a file named
task1.c. When you compile and run your program with the appropriate number of processes and grid dimensions, each MPI process will print its global rank, Cartesian rank, coordinates, and the ranks of its immediate adjacent processes (left, right, top, and bottom).