MPI_Gatherv and MPI_Scatterv are the variable-message-size versions of MPI_Gather and MPI_Scatter. MPI_Gatherv extends the functionality of MPI_Gather to permit a varying count of data from each process and to allow some flexibility in where the gathered data is placed on the root process. It does this by changing the count argument from a single integer to an integer array and providing a new argument displs (an array). MPI_Scatterv extends MPI_Scatter in a similar manner. More information on the use of these routines will be presented in an Application Example later in this roadmap.

Gatherv and Scatterv Syntax

int MPI_Gatherv(void *sbuf, int scount, \
     MPI_Datatype stype, void *rbuf, int *rcounts, \
     int *displs, MPI_Datatype rtype, \
     int root, MPI_Comm comm)

int MPI_Scatterv(void *sbuf, int *scounts, \
     int *displs, MPI_Datatype stype, void *rbuf, \
     int rcount, MPI_Datatype rtype, \
     int root, MPI_Comm comm)
     

MPI_GATHERV(sbuf, scount, stype, rbuf, rcounts, displs, rtype,
     root, comm, ierr)

MPI_SCATTERV(sbuf, scounts, displs, stype, rbuf, rcount, rtype,
     root, comm, ierr)
Gatherv parameters:
sbuf
is the starting address of the send buffer
scount
is the number of elements in the send buffer
stype
is the data type of send buffer elements
rbuf
is the address of the receive buffer
rcounts
is an array containing the number of elements to be received from each process
displs
is an array specifying the displacement relative to rbuf at which to place the incoming data from the corresponding process
rtype
is the data type of the receive buffer elements
root
is the rank of the receiving process
comm
is the group communicator
Info: Only the root process uses rbuf, rcounts, displs, or rtype.
Scatterv parameters:
sbuf
is the starting address of the send buffer
scounts
is an integer array specifying the number of elements to be received from each process
displs
is an array specifying the displacement relative to sbuf from which to take the data going out to the corresponding process
stype
is the data type of send buffer elements
rbuf
is the address of the receive buffer
rcount
is the number of elements in the receive buffer
rtype
is the data type of the receive buffer elements
root
is the rank of the receiving process
comm
is the group communicator
Info: Only the root process uses sbuf, scounts, displs, or stype.
Examples

For the purpose of illustrating the usage of MPI_Gatherv and MPI_Scatterv, we give two Fortran program fragments below:

Fortran MPI_GATHERV Example
Gatherv example corresponding to code fragment, in which 25 elements are gathered from each process and copied into the root process with a stride of 37.5. The stride variable determines the distance between the starting location of consecutive groups of elements once copied to root. Once gathered, the gap between the elements from each process on root is the width of 12 or 13 elements.
Gatherv
! Gatherv example fragment
real a(25), rbuf(MAX), stride
integer displs(NRANK), rcounts(NRANK)
stride = 37.5
⋮
do i= 1, NRANK
   displs(i) = (i-1) * stride
   rcounts(i) = 25
enddo
call mpi_gatherv(a, 25, MPI_REAL, rbuf, rcounts, displs, &
      MPI_REAL, root, MPI_COMM_WORLD, ierr)
⋮ 

Notice that the effect of setting stride= 37.5 is to cause the separation between the chunks on the root to alternate between 12 and 13 integers.

Fortran MPI_SCATTERV Example
Scatterv example corresponding to code fragment, in which 25 elements are scattered to each process from the root process with a stride of 37.5. The stride variable determines the distance between the starting location of groups of consecutive elements to be copied from root.
Scatterv
! Scatterv example fragment
real a(25), sbuf(MAX), stride
integer displs(NRANK), scounts(NRANK)
stride = 37.5
⋮ 
do i= 1, NRANK
   displs(i) = (i-1) * stride
   scounts(i) = 25
enddo
call mpi_scatterv(sbuf, scounts, displs, MPI_REAL, a, 25, & 
      MPI_REAL, root, MPI_COMM_WORLD, ierr)
⋮

Notice again that the effect of setting stride=37.5 is to cause the separation between the chunks on the root to alternate between 12 and 13 integers.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement