50 likes | 161 Views
MPI_Bcast. Bcast stands for broadcast, and is used to send data from one process to all other processes. The format for this function is: MPI_Bcast(&msg_address,#_elements,MPI_Type, ID, Communicator). MPI_Bcast (&k, 1, MPI_INT, 0, MPI_COMM_WORLD) Process 0 sends k to all other processes.
E N D
MPI_Bcast • Bcast stands for broadcast, and is used to send data from one process to all other processes. • The format for this function is: MPI_Bcast(&msg_address,#_elements,MPI_Type, ID, Communicator). • MPI_Bcast (&k, 1, MPI_INT, 0, MPI_COMM_WORLD) • Process 0 sends k to all other processes
MPI_Bcast • This function is best used when a some data that a process was using is changed, and it needs to get updated by all other processes in that communicator (ie: the next homework). • Note: A call to MPI_Bcast must be placed where all processes can see it, if not the sending process will block and hang, waiting for an acknowledgement.
Data Empty Write data to all processes Data Empty Data Empty Data written, unblock Data Present Data Present Data Present MPI_Bcast Process 1 Process 2 Process 3 Process 0 Data Present
Simple Program that Demonstrates MPI_Bcast: #include <mpi.h> #include <stdio.h> int main (int argc, char *argv[]){ int k,id,p,size; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &id); MPI_Comm_size(MPI_COMM_WORLD, &size); if(id == 0) k = 20; else k = 10; for(p=0; p<size; p++){ if(id == p) printf("Process %d: k= %d before\n",id,k); } //note MPI_Bcast must be put where all other processes //can see it. MPI_Bcast(&k,1,MPI_INT,0,MPI_COMM_WORLD); for(p=0; p<size; p++){ if(id == p) printf("Process %d: k= %d after\n",id,k); } MPI_Finalize(); return 0
The Output would look like: Process 0: k= 20 before Process 0: k= 20 after Process 3: k= 10 before Process 3: k= 20 after Process 2: k= 10 before Process 2: k= 20 after Process 1: k= 10 before Process 1: k= 20 after