分散

根程序使用 MPI_Scatter 操作將 sendbuf 中的內容分散到所有程序(包括其自身)。

int rank;
int size;
int sendcount = 1;
int recvcount = sendcount;
int sendbuf[3];
int recvbuf;
int root = 0;

MPI_Comm_size (MPI_COMM_WORLD, &size);

if (size != 3)
{
    fprintf (stderr, "Number of processes must be 3\n");
    exit (0);
}

MPI_Comm_rank (MPI_COMM_WORLD, &rank);

if (rank == 0)
{
    sendbuf[0] = 3;
    sendbuf[1] = 5;
    sendbuf[2] = 7;
}

MPI_Scatter (sendbuf, sendcount, MPI_INT, &recvbuf, recvcount, MPI_INT, root, MPI_COMM_WORLD);

printf ("rank: %i, value: %i\n", rank, recvbuf);

/* Output:
 * rank: 0, value: 3
 * rank: 1, value: 5
 * rank: 2, value: 7
 */