Using MPI_Alltoall to perform matrix transpose

 

program alltoall
	include "mpif.h"
	integer send(4),recv(3)
	integer rank,size,ierr
	
	 call MPI_INIT(ierr)
	 call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
	 call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)
	 if(size.ne.4)then 
             write(*,*)'Error!:# of processors must be equal to 4'
             write(*,*)'Programm aborting....'
	     call MPI_ABORT(ierr)
         endif

	  do k=1,size   
	     send(k) = k + rank*size
          enddo

	 write(*,*))rank, ': ', 'send = ',send

	 call MPI_ALLTOALL(send, 2, MPI_REAL, recv, 1, 
     6		MPI_INTEGER, MPI_COMM_WORLD, ierr) 

	 write(*,*)rank,': ','recv = ',recv

	 call MPI_FINALIZE(ierr)
	 end