      program gather
c
      implicit real*8(a-h,p-z)
c
      include 'mpif.h'
      parameter (n = 8)
      integer ierr,myrank,tnode
      dimension a(n,n), b(n,n)
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 = n /tnode
      lc = mod(n,tnode)
c
      if(lc .ne. 0)then
         if(myrank.EQ.0)then
            write(*,*)'np=1,2,4,8$B$G<B9T$7$F2<$5$$(B'
         end if
      else
c
         is = myrank *lb +lc +1
         ie = is +lb -1
c
      do 10 i = is, ie
         do 15 j = 1, n
            a(j,i) = j *100 +i
 15      continue
 10   continue
c
         call mpi_gather(a(1,is),n*lb,MPI_DOUBLE_PRECISION ,
     &       b(1,1),n*lb,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
c
      open(60, file='b.dat' ,status='unknown')
 100  format(a3,i2.0,a1,i2.0,a5,f4.0)
c
      do 20 k = 1,n
         do 25 l = 1,n
            if(myrank.EQ.0)then
               write(60,100)'b(',l,',',k,') = ',b(l,k)
            end if
 25      continue
 20   continue      
c
      close(60)
c
      end if
c
      call mpi_finalize(ierr)
      stop
      end
