


c*****************************************************************
c
c                          Example 9 ( allgather.f ) 
c
c    Objective           : To gather data from all the processors.
c                          This example demonstrates the use of
c                          MPI_Init
c                          MPI_Comm_rank
c                          MPI_Comm_size
c                          MPI_Allgather
c                          MPI_Barrier
c                          MPI_Finalize
c
c    Input               : Data from files (gdata0,gdata1,gdata2,gdata3,gdata4,
c                          gdata5,gdata6,gdata7) is available on respective 
c                          processor.
c
c    Output              : Gathered data is printed on each processor.
c
c    Necessary Condition : Number of Processes should be 
c                          less than or equal to 8.
c
c******************************************************************

	  
	  
	  program main
  
	  include "mpif.h"


C  	   .......Variables Initialisation ......
	  parameter 	(MAX_SIZE = 1000) 
	  integer 	n_size, Numprocs, MyRank
	  integer 	i
	  integer 	Input_A(MAX_SIZE), Output(MAX_SIZE)


C	   ........MPI Initialisation .......
	  call MPI_INIT(ierror) 
	  call MPI_COMM_RANK(MPI_COMM_WORLD, MyRank,ierror)
	  call MPI_COMM_SIZE(MPI_COMM_WORLD, Numprocs,ierror)

C     .......Read the Input file ......

	  if(Numprocs .gt. 8) then
		 if(MyRank .eq. Root)
     *           print*,"Number of processor should be less than 9"
		 goto 100
	  endif

	  if( MyRank .eq. 0) then
    	      open (unit= 15, FILE = "./data/gdata0")
	  endif

	  if( MyRank .eq. 1) then
    	      open (unit= 16, FILE = "./data/gdata1")
	  endif

	  if( MyRank .eq. 2) then
    	      open (unit= 17, FILE = "./data/gdata2")
	  endif

	  if( MyRank .eq. 3) then
    	      open (unit= 18, FILE = "./data/gdata3")
	  endif

	  if( MyRank .eq. 4) then
    	      open (unit= 19, FILE = "./data/gdata4")
	  endif

	  if( MyRank .eq. 5) then
    	      open (unit= 20, FILE = "./data/gdata5")
	  endif

	  if( MyRank .eq. 6) then
    	      open (unit= 21, FILE = "./data/gdata6")
	  endif

	  if( MyRank .eq. 7) then
    	      open (unit= 22, FILE = "./data/gdata7")
	  endif

    	  read(unit=15 + MyRank,*) n_size
          read(unit = 15+MyRank,*)(Input_A(i), i=1,n_size)     

    	  do i=1,n_size,1   
	    print*,MyRank, i, Input_A(i)
    	  enddo 

    	  call MPI_ALLGATHER (Input_A, n_size, MPI_INTEGER, Output, 
     &		n_size, MPI_INTEGER, MPI_COMM_WORLD,ierror) 

    	  write(6,*) "Results of Gathering data on processor", MyRank,
     &		 n_size*Numprocs



    	  do i=1,n_size*Numprocs,1
	  	     write(20+MyRank, *) I, Output(i)
          enddo

 100    call MPI_FINALIZE(ierror) 
   	  stop
   	  end




