[MPI] MPI_Scatter, MPI_Gather

Gather Purpose:

If an array is scattered across all processes in the group and one wants to collect each piece of the array into a specified array on a single process, the call to use is GATHER.

Scatter Purpose:

On the other hand, if one wants to distribute the data into n segments, where the ith segment is sent to the ith process in the group which has n processes, use SCATTER. Think of it as the inverse to GATHER.

We will first consider the basic form of these MPI gather/scatter operations, in which the number of data items collected from or sent to processes is the same for all processes, and the data items are arranged contiguously in order of process rank. The syntax is given below:

C

int MPI_Gather(const void* sbuf, int scount, \
     MPI_Datatype stype, void* rbuf, int rcount, \
     MPI_Datatype rtype, int root, MPI_Comm comm )

int MPI_Scatter(const void* sbuf, int scount, \
     MPI_Datatype stype, void* rbuf, int rcount, \
     MPI_Datatype rtype, int root, MPI_Comm comm)


FORTRAN

MPI_GATHER(sbuf, scount, stype, rbuf, rcount, rtype,
     root, comm, ierr)

MPI_SCATTER(sbuf, scount, stype, rbuf, rcount, rtype,
     root, comm, ierr)


where, for the Gather routines:
sbufis the starting address of the send buffer,
scountis the number of elements to be sent,
stypeis the data type of send buffer elements,
rbufis the starting address of the receive buffer,
rcountis the number of elements for any single receive,
rtypeis the data type of the receive buffer elements,
rootis the rank of receiving process, and
commis the communicator.


Note: rbuf, rcount, rtype are significant for the root process only.

and for the Scatter routines:
sbufis the address of the send buffer,
scountis the number of elements to be sent to each process,
stypeis the data type of the send buffer elements,
rbufis the address of the receive buffer,
rcountis the number of elements in the receive buffer,
rtypeis the data type of the receive buffer elements,
rootis the rank of the sending process, and
commis the communicator.


Note: sbuf, scount, stype are significant for the root process only.

In the gather operation, each process (root process included) sends scount elements of type stype of sbuf to the root process. The root process receives the messages and stores them in rank order in the rbuf. For scatter, the reverse holds. The root process sends a buffer of N chunks of data (N = number of processes in the group) so that process 1 gets the first element, process 2 gets the second element, etc.

Here we give two Fortran program fragments further showing the use of MPI_GATHER and MPI_SCATTER.
MPI_GATHER Example

Gather2


MPI_SCATTER Example

Gather3 

- Fortran Source Code Scatter Example
program hello
    use mpi
    integer :: sbuf(10), rbuf(2)
    integer :: status(MPI_STATUS_SIZE)
    integer :: rank, src=0, dest=1, ierr, i
   
    call mpi_init(ierr) 
    call MPI_Comm_rank( MPI_COMM_WORLD, rank,ierr);
   
    !array preparation from src processor
    if (rank == src) then 
        do i = 1, 10
            sbuf(i) = i
        end do
    end if
   
    !scattering array data
    call mpi_scatter(sbuf, 2, MPI_INT, rbuf, 2, MPI_INT, src, MPI_COMM_WORLD, ierr)
    print *, rbuf
    call mpi_finalize(ierr)
end program hello


- Fortran Source Code Scatter Example Output
>mpiexec -n 3 mpiscatter.exe
           3           4
           1           2
           5           6


- Fortran Source Code Gather Example
program hello
    use mpi
    integer :: rbuf(10), sbuf(2)
    integer :: status(MPI_STATUS_SIZE)
    integer :: rank, src=0, dest=1, ierr, i
   
    call mpi_init(ierr) 
    call MPI_Comm_rank( MPI_COMM_WORLD, rank,ierr);
   
    !array preparation from sending processors
    do i = 1, 2
        sbuf(i) = 10 * rank + i
    end do
    !gathering array data
    call mpi_gather(sbuf, 2, MPI_INT, rbuf, 2, MPI_INT, src, MPI_COMM_WORLD, ierr)
    print *, rbuf
    call mpi_finalize(ierr)
end program hello


- Fortran Source Code Gather Example Output
>mpiexec -n 3 mpigather.exe
           0           0           0           0           0           0
           1           2          11          12          21          22
           0           0           0           0           0           0
           0           0           0           0
           0           0           0           0
           0           0           0           0


댓글

  1. A scatterplot is a data visualization that shows the values ​​of two different variables as points. The data for each point is represented by its horizontal (x) and vertical (y) position in the visualization. Additional variables can be encoded using labels, markers, color, transparency, size (bubbles), and by creating 'small multiples' of scatterplots. Scatter charts are also known as scatter charts, scatter charts, scatter charts, scatter plots, and scatter charts.
    https://ppcexpo.com/blog/scatter-plot-examples

    답글삭제

댓글 쓰기

이 블로그의 인기 게시물

[Linux, AIX] 사용자 계정 생성 및 설정

[AIX] rpm 설치와 rpm 으로 패키지 설치 및 삭제

Ubuntu 에서 Fortran 시작하기