#include #include int main(int argc, char *argv[]) { int world_rank, row_rank, col_rank, cart_rank; int nprocs, row_size, col_size; int coords[2], sub_coords[2]; int dims[2] = { 0, 0}, period[2] = { 1, 1}; int src_rank, dst_rank; int sum, temp; float avg; MPI_Comm cart_grid, cart_row, cart_col; MPI_Status status; /* MPI Initialization */ MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); /* Cartesian grid creation */ ... call MPI_Dims_create to get grid dimensions ... call MPI_Cart_create to create new topology /*Local world_rank initialization and comparison to global world_rank */ ... call MPI_Comm_rank to get cartesian rank printf ("I am world_rank %d in MPI_COMM_WORLD and world_rank %d in the cartesian communicator\n", world_rank, cart_rank); /* Coordinates creation and neighbour communication */ ... call MPI_Cart_coords to get process coordinates /* Communication south */ sum = world_rank; ... call MPI_Cart_shift to prepare south-ward communication ... call MPI_Sendrecv to send world_rank and get temp from neighbour sum += temp; /* Communication north */ ... call MPI_Cart_shift to prepare north-ward communication ... call MPI_Sendrecv to send world_rank and get temp from neighbour sum += temp; /*Communication east */ ... call MPI_Cart_shift to prepare east-ward communication ... call MPI_Sendrecv to send world_rank and get temp from neighbour sum += temp; /*Communication west */ ... call MPI_Cart_shift to prepare west-ward communication ... call MPI_Sendrecv to send world_rank and get temp from neighbour sum += temp; /*Neighbour's average */ avg = (float) sum / 5; printf("Cart rank %d (%d, %d), neighbours average: %.2f\n", cart_rank, coords[0], coords[1], avg); /*Row sub-communicator creation */ sum = 0; ... call MPI_Cart_sub to create row sub-spaces ... call MPI_Comm_size and MPI_Comm_rank to get size and rank in sub-space /*Row sub-communicator's average calculation */ ... call MPI_Reduce to sum-up world ranks in sub-space if (row_rank == 0) { avg = (float) sum / row_size; printf("Row %d, row average: %.2f\n", coords[0], avg); } /*Column sub-communicator creation */ sum = 0; ... call MPI_Cart_sub to create column sub-spaces ... call MPI_Comm_size and MPI_Comm_rank to get size and rank in sub-space /*Column sub-communicator's average calculation */ ... call MPI_Reduce to sum-up world ranks in sub-space if (col_rank == 0) { avg = (float) sum / col_size; printf("Column %d, column average: %.2f\n", coords[1], avg); } /* Finalization operations */ MPI_Comm_free(&cart_grid); MPI_Comm_free(&cart_col); MPI_Comm_free(&cart_row); MPI_Finalize(); return 0; }