User Tools

Site Tools


Site Tools

cart.f90
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
cart.c
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <mpi.h>
 
// 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;
}