#include #include #include #include // This program show the organisation and use of the Cartesian topology int main(int argc, char** argv) { int rank, nb_mpi_processes, ierror, tag, n,i,niter; char charbuff[4]; int ndim = 1; // number of dimmensions of cartesian topology int nb_process_axe[1]; // number of processes per axe of cartesian topology int cart_boundaries[1]; // cart_boundaries, type of boundaries : 1 -> periodic, 0 -> non periodic int cart_position[1], pos[1]; // position of process on the cartesian topology ( and pos a buffer ) int cart_neigh[3]; // neigbours rank on the cartesian topology // CART program // This program show the organisation and use of the Cartesian topology MPI_Init(NULL, NULL); MPI_Comm_size( MPI_COMM_WORLD , &nb_mpi_processes ); MPI_Comm_rank( MPI_COMM_WORLD , &rank ); if(nb_mpi_processes != 4) { printf("This program is design to be run with 4 processes only"); return 0;} nb_process_axe[0] = 4; cart_boundaries[0] = 0; MPI_Comm MPI_COMM_CART; // our new communicator MPI_Cart_create( MPI_COMM_WORLD , ndim , &nb_process_axe[0] , &cart_boundaries[0] , 1 , &MPI_COMM_CART ); MPI_Cart_coords( MPI_COMM_CART , rank , ndim , &cart_position[0] ); MPI_Cart_shift (MPI_COMM_CART, 0, 1, &cart_neigh[0], &cart_neigh[2] ); printf("Good way to get neigh, Rank : %d Position : %d Neighbours : %d %d\n",rank,cart_position[0],cart_neigh[0],cart_neigh[2]); for(i=0;i<=2;i++) { if((cart_boundaries[0] == 0) && ((cart_position[0] == 0 && i==0) || (cart_position[0] == nb_process_axe[0]-1) && i==2)) { cart_neigh[i] = MPI_PROC_NULL; } else { pos[0] = cart_position[0] + i - 1; MPI_Cart_rank( MPI_COMM_CART , &pos[0] , &cart_neigh[i] ); } } printf("2nd way to get neigh, Rank : %d Position : %d Neighbours : %d %d\n",rank,cart_position[0],cart_neigh[0],cart_neigh[2]); MPI_Finalize ( ); // Close MPI return 0; }