MPI_Allgather Examples

 

program allgather
	include "mpif.h"
	integer a(2, 8), b(8), cpart(2), ctotal(8)
	integer rank,size,ierr
	
	 call MPI_INIT(ierr)
	 call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
	 call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)
	 if(size.ne.4)then 
             write(*,*)'Error!:# of processors must be equal to 4'
             write(*,*)'Programm aborting....'
	     call MPI_ABORT(ierr)
         endif

	 do i=1,2
	  do k=1,8   
	     a(i,k) = rank*k
          enddo
         enddo

	 do i = 1,2
	 write(*,*)(a(i,k),k=1,8)
         enddo

	 do k=1,8
            b(k) = k
	 enddo

	 do i=1, 2
	     cpart(i) = 0
	     DO k=1, 8 
	          cpart(i) = cpart(i) + a(i, k) * b(k)
	     enddo 
	 enddo

	 write(*,*)cpart

	 call MPI_ALLGATHER(cpart, 2, MPI_INTEGER, ctotal, 2, 
     6		MPI_INTEGER, MPI_COMM_WORLD, ierr) 

         write(*,*)ctotal

	 call MPI_FINALIZE(ierr)
	 end