In the following codes:
if (rank==0) master();
else slave();
...
void master()
{
int i=0;
}
...
void slave()
{
int i=1;
MPI_BCAST(&i,1,MPI_INT,0,COMM);
}
Will the slave node broadcast “i(==0)” in the master node and set the “i” values in all slave nodes to be 0?
It’s a little unclear from what you’ve posted that you have the semantics right — all processes in the communicator have to call
MPI_BCAST, it’s one of MPI’s collective operations. Your program would then behave as if the process designated the root in the call toMPI_BCASTsends the message to all the other processes in the designated communicator which, in turn, receive the message.Your snippet suggests that you think that the call to
MPI_BCASTwould be successful if called only on what you call the ‘slave’ process(es), which would be incorrect.Your syntax for the call is, however, correct.
EDIT in response to comment
I believe that all processes have to execute the piece of code which calls
MPI_BCAST. If, as you suggest, the pseudo-code is like this:then the call will fail; it’s likely that your program will stall until the job management or operating system notices and chucks it out. There is no mechanism within MPI for ‘matching’ calls to
MPI_BCAST(or any of the other collective communications routines) across scopes.Your pseudo-code ought to be like this
or whatever variant your program requires