User Tools

Site Tools


Site Tools

switch.f90
program switch
	use mpi
	implicit none
	integer :: rank, nb_mpi_processes, ierror, tag, statu(MPI_STATUS_SIZE), n, val, val0
	integer :: niter = 4
 
	! Switch program
 
	tag = 7777
 
	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 /= 2) stop 'This program is design to be run with 2 processes only'
 
	if(rank==0) val = 74
	if(rank==1) val = 32
 
	! check
	print*, 'process',rank,'I have the value',val,'at 2'
 
	! First switch
	if(rank==0) then
		call MPI_SENDRECV ( val , 1 , MPI_INTEGER , 1 , tag , val0 , 1 , MPI_INTEGER , 1 , tag , MPI_COMM_WORLD , statu , ierror )
		val = val0
	end if
	if(rank==1) then
		call MPI_SENDRECV ( val , 1 , MPI_INTEGER , 0 , tag , val0 , 1 , MPI_INTEGER , 0 , tag , MPI_COMM_WORLD , statu , ierror )
		val = val0
	end if
 
	! check
	print*, 'process',rank,'I have the value',val,'at 1'
 
	! Second switch
	if(rank==0) then
		call MPI_SENDRECV ( val , 1 , MPI_INTEGER , 1 , tag , val0 , 1 , MPI_INTEGER , 1 , tag , MPI_COMM_WORLD , statu , ierror )
		val = val0
	end if
	if(rank==1) then
		call MPI_SENDRECV ( val , 1 , MPI_INTEGER , 0 , tag , val0 , 1 , MPI_INTEGER , 0 , tag , MPI_COMM_WORLD , statu , ierror )
		val = val0
	end if
 
	! check
	print*, 'process',rank,'I have the value',val,'at 2'
 
	call MPI_FINALIZE ( ierror ) ! Close MPI
 
end program switch
switch.c
#include <stddef.h>
#include <mpi.h>
 
// Switch program
 
int main(int argc, char** argv) 
{
	MPI_Init(NULL, NULL);
	int nb_mpi_processes;
	MPI_Comm_size(MPI_COMM_WORLD, &nb_mpi_processes);
	int rank;
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
 
	if(nb_mpi_processes != 2) { printf("This program is design to be run with 2 processes only"); return 0;}
 
	int tag = 7777;
	int val;
	int val0;
	if(rank==0) {val = 74;}
	if(rank==1) {val = 32;}
 
	// check
	printf("Process %d I have the value %d at 2\n",rank,val);
 
	// First switch
	if(rank==0) {
		MPI_Sendrecv ( &val , 1 , MPI_INTEGER , 1 , tag , &val0 , 1 , MPI_INTEGER , 1 , tag , MPI_COMM_WORLD , MPI_STATUS_IGNORE  );
		val = val0;
	}
	if(rank==1) {
		MPI_Sendrecv ( &val , 1 , MPI_INTEGER , 0 , tag , &val0 , 1 , MPI_INTEGER , 0 , tag , MPI_COMM_WORLD , MPI_STATUS_IGNORE  );
		val = val0;
	}
 
	// check
	printf("Process %d I have the value %d at 1\n",rank,val);
 
	// Second switch
	if(rank==0) {
		MPI_Sendrecv ( &val , 1 , MPI_INTEGER , 1 , tag , &val0 , 1 , MPI_INTEGER , 1 , tag , MPI_COMM_WORLD , MPI_STATUS_IGNORE  );
		val = val0;
	}
	if(rank==1) {
		MPI_Sendrecv ( &val , 1 , MPI_INTEGER , 0 , tag , &val0 , 1 , MPI_INTEGER , 0 , tag , MPI_COMM_WORLD , MPI_STATUS_IGNORE  );
		val = val0;
	}
 
	// check
	printf("Process %d I have the value %d at 2\n",rank,val);
 
	MPI_Finalize(); // Close MPI
 
	return 0;
}