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

      real,allocatable,dimension(:) :: A,B
      double precision time_begin,time_end

      integer xb,yb,i,j,nitems,root

      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))
      do i=1,xmax
        A(i) = i*9999.0
      enddo

      nitems = xmax/nnodes
!start timer .....
      root = 0
      time_begin =  MPI_Wtime()
      do iter = 1,niters
! Do all-to-all personilized communication of matrix blocks
         call MPI_Scatter(A,nitems,MPI_REAL,B,nitems,MPI_REAL,root,comm,ierr)
      enddo
! Stop timer

      time_end =  MPI_Wtime()

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

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

      end


      


