mpi Collectives Broadcast


Example

The following code broadcasts the contents in buffer among all the processes belonging to the MPI_COMM_WORLD communicator (i.e. all the processes running in parallel) using the MPI_Bcast operation.

int rank;
int res;

res = MPI_Comm_rank (MPI_COMM_WORLD, &rank);
if (res != MPI_SUCCESS)
{
    fprintf (stderr, "MPI_Comm_rank failed\n");
    exit (0);
}

int buffer[100];
if (rank == 0)
{
    // Process with rank id 0 should fill the buffer structure
    // with the data it wants to share.
}

res = MPI_Bcast (buffer, 100, MPI_INT, 0, MPI_COMM_WORLD);
if (res != MPI_SUCCESS)
{
    fprintf (stderr, "MPI_Bcast failed\n");
    exit (0);
}