#include #include typedef enum neighbor { UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3 } neighbor_t; int main(int argc, char **argv) { int world_size; int rank, cart_rank; int dims[2] = {0, 0}; int periods[2] ={0, 0}; int reorder = 0; int coords[2]; int neighbors[4]; MPI_Comm cart_comm; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &world_size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Dims_create(world_size, 2, dims); MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 0, &cart_comm); MPI_Comm_rank(cart_comm, &cart_rank); MPI_Cart_coords(cart_comm, cart_rank, 2, coords); MPI_Cart_shift(cart_comm, 0, 1, &neighbors[ UP], &neighbors[DOWN]); MPI_Cart_shift(cart_comm, 1, 1, &neighbors[LEFT], &neighbors[RIGHT]); printf("Rank = %4d - Coords = (%3d, %3d) - Neighbors (up, down, left, right) = (%3d, %3d, %3d, %3d)\n", rank, coords[0], coords[1], neighbors[UP], neighbors[DOWN], neighbors[LEFT], neighbors[RIGHT]); MPI_Finalize(); return 0; }