#include #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: * - the function MPI_Dims_create may be used to compute dimensions in both directions * - MPI_Cart_create must be used to create the new topology * - cart_grid will be the handle of the new topology */ --- /* Local world_rank initialization and comparison to global world_rank: * - how can you get cart_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 * - get process cartesian coordinates 'coords' with function MPI_Cart_coords */ --- /* Communication south: * - use MPI_Cart_shift and MPI_Sendrecv properly to send local rank to the neighbours * - receive neighbour rank in 'temp' variable and store sum in 'sum' */ --- /* Communication north */ --- /*Communication east */ --- /*Communication west */ --- /*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: * - how would you create a "row sub-communicator" ? * - use the function MPI_Cart_sub to get 'cart_row' communicator handle * - use the proper functions to get 'row_size' and 'row_rank' */ sum = 0; sub_coords[0] = 0; sub_coords[1] = 1; --- /* Use MPI_Reduce to sum up ranks in row sub-communicator */ --- if (row_rank == 0) { avg = (float) sum / row_size; printf("Row %d, row average: %.2f\n", coords[0], avg); } /*Column sub-communicator creation: * - repeate to obtain 'cart-col' comm-handle and 'col_size' and 'col_rank' */ sum = 0; sub_coords[0] = 1; sub_coords[1] = 0; --- /* Use MPI_Reduce to sum up ranks in row sub-communicator */ --- 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; }