next up previous
Next: Summary Up: fourth.c in detail Previous: The master part

The worker part

The worker begins by receiving the broadcast from the master:

   MPI_Bcast(b, COLS, MPI_INT, host_rank, MPI_COMM_WORLD);
Then all workers receive their first batch to work on:
   MPI_Recv(int_buffer, COLS, MPI_INT, host_rank, MPI_ANY_TAG,
            MPI_COMM_WORLD, &status);
At this stage every worker enters a loop at the top of which it checks if the tag of the received message is equal to ROWS. If it is, the worker exits.

If the tag of the message is different from ROWS, the worker performs the computation:

   sum = 0;
   for (i = 0; i < COLS; i++) sum = sum + int_buffer[i] * b[i];
and sends the answer back to the master using the row number as the tag:
   int_buffer[0] = sum;
   MPI_Send (int_buffer, 1, MPI_INT, host_rank, row, MPI_COMM_WORLD);
At this stage every worker knows that it should receive either the next job or the termination tag from the master:
   MPI_Recv (int_buffer, COLS, MPI_INT, host_rank, MPI_ANY_TAG,
             MPI_COMM_WORLD, &status);
When this message is received, the worker goes back to the top of the loop, where the tag is inspected.



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