      program transpose_matrix
      include 'mpif.h'
      integer ndims,xmax,ymax,nnodes,myid
      parameter(ndims=2)
      parameter(xmax=1000)
       parameter(niters=1000)
      integer comm,ierr
      integer status(MPI_STATUS_SIZE)

      double precision ,allocatable,dimension(:) :: A,B

      integer xb,yb,i,j,nitems

      comm = MPI_COMM_WORLD
      call MPI_init(ierr)
      call MPI_COMM_RANK(comm,myid,ierr)
      call MPI_COMM_SIZE(comm,nnodes,ierr)
  
      allocate(A(xmax))
      allocate(B(xmax*nnodes))
      A = 9999.0
      
      nitems = xmax
!start timer .....
    
      time_begin =  MPI_Wtime()
      
      do iter = 1,niters
! Do all-to-all personilized communication of matrix blocks
         call MPI_ALLTOALL(A,xmax,MPI_DOUBLE_PRECISION,B,xmax,MPI_DOUBLE_PRECISION,comm,ierr)
      enddo
! Stop timer

      time_end =  MPI_Wtime()

      if (myid == 0) then
        print *,'Elapsed time ',niters,'iterations for all2allperson'
        print *,'For matrix with dimensions',xmax,ymax ,'is'
        print *,time_end-time_begin ,'seconds'   
       endif

      deallocate(a)
      deallocate(b)
     
      call MPI_FINALIZE(ierr)

      end


      


