      program gather
c
      include 'mpif.h'
      integer ierr,myrank,tnode
      integer a(16)
c
      call mpi_init(ierr)
      call mpi_comm_size(MPI_COMM_WORLD,tnode,ierr)
      call mpi_comm_rank(MPI_COMM_WORLD,myrank,ierr)
c
      lb = 16 /tnode
      lc = mod(16,tnode)
c
      if(myrank+1 .le. lc) then
         is = myrank *lb +myrank +1
         ie = is +lb
      else
         is = myrank *lb +lc +1
         ie = is +lb -1
      end if
c
      do 10 i = is, ie
         a(i) = i **2
 10   continue
c
      if(myrank+1 .le. lc) then
         call mpi_gather(a(is),lb+1,MPI_INTEGER,
     &            a(is),lb+1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
      else
         call mpi_gather(a(is),lb,MPI_INTEGER,
     &            a(is),lb,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
      end if
c
 100  format(a3,i3,a5,i3)
c
      do 20 j = 1,16
         if(myrank.EQ.0)then
            write(*,100)'a(',j,') = ',a(j)
         end if
 20   continue
c
      call mpi_finalize(ierr)
      stop
      end
c
c
ccc   Result
c
c  $ mpirun -np 4 ./a.out
c  a(  1 ) =   1
c  a(  2 ) =   4
c  a(  3 ) =   9
c  a(  4 ) =  16
c  a(  5 ) =  25
c  a(  6 ) =  36
c  a(  7 ) =  49
c  a(  8 ) =  64
c  a(  9 ) =  81
c  a( 10 ) = 100
c  a( 11 ) = 121
c  a( 12 ) = 144
c  a( 13 ) = 169
c  a( 14 ) = 196
c  a( 15 ) = 225
c  a( 16 ) = 256
