Once all processes within the MPI_COMM_WORLD
communicator
know the rank of the master process we can attempt a collective
communication. This is accomplished in the following three lines of the
code:
if (i_am_the_master) strcpy (char_buffer, my_node_name); MPI_Bcast(char_buffer, BUFSIZ, MPI_CHAR, host_rank, MPI_COMM_WORLD); strcpy (host_name, char_buffer);The call to function
MPI_Bcast
is issued by all processes.
But in this operation only one process defined by the fourth argument,
i.e., the process whose rank is host_rank
, sends the
information originally contained in char_buffer
of that
process. All other processes receive that information into their
own char_buffer
arrays. The length of that buffer is defined by
the second argument, and the type of the data to be received is defined
by the third argument.
The broadcast is restricted to the MPI_COMM_WORLD
communicator.
Note that it is dangerous to use here something like
strlen(char_buffer) + 1in place of BUFSIZ, because only on the master process will
strlen(char_buffer)
return anything meaningful.
Note also that we could not use broadcast until all processes knew
the rank of the host process. For this reason we had to communicate that
rank using MPI_Send
.
Collective communications are described in
Chapter 4
of ``MPI: A
Message-Passing Interface Standard''. And MPI_Bcast
is described
on page 93, section
4.4.
In ``Using MPI...'' broadcast is the first communication operation covered by the first example in Chapter 3, page 23. This can be done under MPICH because the host process has a rank number 0 by default and all other processes know that from the beginning.