B424 Parallel and Distributed Programming

MPI Scatter and Gather

Prototypes

int MPI_Scatter ( void *sendbuf, int sendcnt, MPI_Datatype sendtype, void *recvbuf, int recvcnt, MPI_Datatype recvtype, int root, MPI_Comm comm ) ;

The value of the parameter sendcnt is the size of the data to be sent to each process and not the total size of the sendbuf array. If the type of the send and receive data is the same, then sendcnt and recvcnt should have the same value. The array sendbuf is only used by the root process.

int MPI_Gather ( void *sendbuf, int sendcnt, MPI_Datatype sendtype, void *recvbuf, int recvcnt, MPI_Datatype recvtype, int root, MPI_Comm comm ) ;

Same remark: the value of the parameter recvcnt is the size of the data received by the root from each process, and not the size of the total array recvbuf. The value of this parameter is in general equal to the value of the sendsize, if the type of the data is the same. The array recvbuf is only used by the root process.

Example Using Scatter

const int recvsize = 50;
int *sendbuf, recvbuf[recvsize];
int sendsize = nb_proc*recvsize;
sendbuf = new int[sendsize];
if (proc_id == 0)
  Generate_data(sendbuf, sendsize);
MPI_Scatter(sendbuf, recvsize, MPI_INT, recvbuf, recvsize, MPI_INT, 0, MPI_COMM_WORLD);
for (i=0; i<nb_proc; i++)
  Print_data(recvbuf, recvsize);

Example Using Gather

const int sendsize = 50;
int sendbuf[sendsize], *recvbuf;
int recvsize = nb_proc*sendsize; 
if (proc_id == 0)
  recvbuf = new int[recvsize];
for (i=0; i<nb_proc; i++)
  Generate_data(sendbuf, sendsize);
MPI_Gather(sendbuf, sendsize, MPI_INT, recvbuf, sendsize, MPI_INT, 0, MPI_COMM_WORLD);
if (proc_id == 0)
  Print_data(recvbuf, recvsize);