Broadcast
Table of contents
- Introduction
- Connection to Modules
- Files Required
- Part 1: Broadcast with Send and Recv
- Part 2: Replacing Send/Recv with Bcast.
Introduction
The MPI_Bcast primitive sends data stored in a buffer at the root to all other ranks in the communicator. The syntax is as follows:
int MPI_Bcast(void *buffer,
int count,
MPI_Datatype datatype,
int root,
MPI_Comm comm)
Connection to Modules
- This module is particularly useful for the communication module.
Files Required
Part 1: Broadcast with Send and Recv
In this exercise, we assume that there are \(p\geq1\) ranks.
- Rank 0 will store in a buffer a value of 100.
- Rank 0 will send this value in the buffer to all other ranks in the communicator.
- Ranks in the range \([1, p-1]\) will receive the value send by rank 0.
- In this exercise, we will only use
MPI_SendandMPI_Recv. - To get started, use the file
broadcast_starter.c.
Figure 1 shows an example with \(p=5\) ranks.

Figure 1: Example of sending the value 100 from rank 0 to ranks 1–4.
A code snippet from broadcast_starter.c is shown below. Write the code to send the buffer storing 100 at rank 0 to the other ranks.
int buffer;
//Initialize the value to be broadcast
if(my_rank==0)
{
buffer=100;
}
//Write code here
Part 2: Replacing Send/Recv with Bcast.
Using broadcast_starter.c, write the same program as outlined above, except use MPI_Bcast instead of MPI_Send and MPI_Recv.