next up previous
Next: Summary: the end Up: first.c in detail Previous: Collective communicationbroadcast

Paying homage to the master

The last part of the code does not introduce any new elements. It is basically a reversal of the first send-receive operation. This time the slave processes send some information about themselves to the master, and the master having received that information prints it on standard output.

if (! i_am_the_master) {
   sprintf (char_buffer, "Greetings to the master (%s, %d) from (%s, %d)",
            host_name, host_rank, my_node_name, my_rank);
   MPI_Send (char_buffer, strlen(char_buffer) + 1, MPI_CHAR, host_rank, 
             2002, MPI_COMM_WORLD);
}
else {
   for (source = 0; source < pool_size; source++) {
      if (source != my_rank) {
         MPI_Recv(char_buffer, BUFSIZ, MPI_CHAR, source, 2002,
                  MPI_COMM_WORLD, &status);
         printf ("%s\n", char_buffer);
      }
   }
}
Observe that in this case the host process receives the messages in a specific order. The order corresponds to the rank of the sender. If the messages arrive out of order, they will be spooled until the reading process is ready to accept them.



Zdzislaw Meglicki
Tue Feb 28 15:07:51 EST 1995