Dana Vrajitoru
B424 Parallel and Distributed Programming

More on MPI

Barrier and Broadcast

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, sendsize, 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, recvsize, MPI_INT, 0, MPI_COMM_WORLD);
if (proc_id == 0)
  Print_data(recvbuf, recvsize);

Receive Any Source

Non-Blocking Send and Receive

Solving the Send - Recv

  • The process can later wait for the send or receive to complete with a call to
    MPI_Wait(&request, &status);
  • It can also test if the send or receive has completed with
    int flag;
    MPI_Test(&request, &flag, &status);
  • The flag will be set to true by the call if the send or receive has completed, and to false otherwise.