      program one2allbroadcast
      include 'mpif.h'
      integer nnodes,myid,vdim,shift_cnt
      logical reorder
      parameter(niters=1000,ndims=1,reorder=.TRUE.)
      parameter(vdim=10000)
      integer comm,ierr,source,dest
      integer status(MPI_STATUS_SIZE),root
      double precision time_begin,time_end
      double precision buffer(vdim),temp(vdim)
      integer dims(ndims),comm_chart,own_position(ndims)
      logical periods(ndims)
 
      data dims /ndims * 0/ 
      comm = MPI_COMM_WORLD
      call MPI_init(ierr)
      call MPI_COMM_SIZE(comm,nnodes,ierr)
  
      shift_cnt = 2
      buffer  = 9999.000
      source = myid
      dest   = myid

      if (shift_cnt .ne. 0) then
        dest   = mod(myid+1,nnodes)
        source = mod(myid-1+nnodes,nnodes)
      endif
      if (shift_cnt < 0) then
        it = dest
        dest = source
        source = dest
      endif
!start timer .....
      time_begin =  MPI_Wtime()

      call MPI_DIMS_CREATE(nnodes,ndims,dims,ierr)
      periods(1) = .TRUE.
      call MPI_CART_CREATE(comm,ndims,dims,periods,reorder,comm_cart,ierr)
      call MPI_CART_GET(comm_cart,ndims,dims,periods,own_position,ierr)

      nsrcount = vdim/nnodes
      call MPI_CART_SHIFT(comm_cart,0,shift_cnt,source,dest,ierr)
      do iter = 1,niters
!shift trough the first direction
        call MPI_SENDRECV(buffer,nsrcount,MPI_DOUBLE_PRECISION,&
           dest,0,temp,nsrcount,MPI_DOUBLE_PRECISION,source,0,&
           comm_chart,status,ierr)
      enddo
! Stop timer

      time_end =  MPI_Wtime()

      if (myid == 0) then
        print *,'Elapsed time ',niters,'iterations for ',shift_cnt
        print *,'shifts is'
        print *,time_end-time_begin ,'seconds'   
       endif

      call MPI_FINALIZE(ierr)

      end


      


