MPI_Gather and MPI_Scatter

 

MPI_Gather

      program gather
      include 'mpif.h'
      integer irecv(3), rank, size, isend

      call MPI_INIT(ierr)
      call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
      call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)

      isend = rank + 1

      call MPI_Gather(isend, 1, MPI_INTEGER,
     &                irecv, 1, MPI_INTEGER, 0,
     &                MPI_COMM_WORLD, ierr)

      if(rank .eq. 0) then
        print *, 'irecv = ', irecv
      endif

      call MPI_FINALIZE(ierr)
         end

 

MPI_Scatter

      program scatter
      include 'mpif.h'
      integer isend(3), recv, rank, size

      call MPI_INIT(ierr)
      call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
      call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)

      if(rank .eq. 0) then
        do i=1,size
           isend(i) = i
        enddo
      endif

      call MPI_Scatter(isend, 1, MPI_INTEGER,
     &                irecv, 1, MPI_INTEGER, 0,
     &                MPI_COMM_WORLD, ierr)

      print *, 'rank = ', rank , ': ','irecv= ', irecv

      call MPI_FINALIZE(ierr)
      end