5
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

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: MPI_Bcast

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

Page 2: MPI_Bcast

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.

Page 3: MPI_Bcast

MPI_Bcast

Process 0

Process 1

Process 2

Process 3

Write data to all processes

Data written, unblockData Present

Data Empty

Data Empty

Data Empty

Data Present

Data Present

Data Present

Page 4: MPI_Bcast

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

Page 5: MPI_Bcast

• The Output would look like:Process 0: k= 20 beforeProcess 0: k= 20 afterProcess 3: k= 10 beforeProcess 3: k= 20 afterProcess 2: k= 10 beforeProcess 2: k= 20 afterProcess 1: k= 10 beforeProcess 1: k= 20 after