program Cart use mpi implicit none integer :: rank, nb_mpi_processes, ierror, tag, statu(MPI_STATUS_SIZE), n,i,niter character(len=4) :: charbuff integer :: ndim = 1 ! number of dimmensions of cartesian topology integer, dimension(1) :: nb_process_axe ! number of processes per axe of cartesian topology logical, dimension(1) :: cart_boundaries ! Type of boundaries : .true. -> periodic, .false. -> non periodic integer :: MPI_COMM_CART ! our new communicator integer, dimension(1) :: cart_position, pos ! position of process on the cartesian topology ( and pos a buffer ) integer, dimension(-1:1) :: cart_neigh ! neigbours rank on the cartesian topology ! CART program ! This program show the organisation and use of the Cartesian topology call MPI_INIT( ierror ) call MPI_COMM_SIZE( MPI_COMM_WORLD , nb_mpi_processes , ierror ) call MPI_COMM_RANK( MPI_COMM_WORLD , rank , ierror ) if(nb_mpi_processes /= 4) stop 'This program is design to be run with 4 processes only' nb_process_axe(1) = 4 cart_boundaries(1) = .false. call MPI_CART_CREATE( MPI_COMM_WORLD , ndim , nb_process_axe(1:ndim) , & & cart_boundaries(1:ndim) , .true. , MPI_COMM_CART , ierror ) call MPI_CART_COORDS( MPI_COMM_CART , rank , ndim , cart_position(1:ndim) , ierror ) call MPI_CART_SHIFT (MPI_COMM_CART, 0, 1, cart_neigh(-1), cart_neigh(+1), ierror) print *,'Good way to get neigh, Rank :',rank,'Position :',cart_position(1),'Neighbours :',cart_neigh(-1),cart_neigh(1) do i=-1,1 if((cart_boundaries(1) .eqv. .false.) .AND. & & ((cart_position(1) == 0 .AND. i==-1) .OR. (cart_position(1) == nb_process_axe(1)-1) .AND. i==1)) then cart_neigh(i) = MPI_PROC_NULL else pos(1) = cart_position(1) + i call MPI_CART_RANK( MPI_COMM_CART , pos(1:ndim) , cart_neigh(i) , ierror ) end if end do print *,'2nd way to get neigh, Rank :',rank,'Position :',cart_position(1),'Neighbours :',cart_neigh(-1),cart_neigh(1) call MPI_FINALIZE ( ierror ) ! Close MPI end program Cart