2.2. Conductor-Worker Program Structure

Navigate to: ../01.conductorWorker

Make and run the code on 4 processes:

make

mpirun -hostfile ~/hostfile -np 4 ./conductorWorker

Exercises:

2.2.1. Explore the code

What is different between this example and the previous one?

#include <stdio.h>
#include <mpi.h>

int main(int argc, char** argv) {
  int id = -1, numWorkers = -1, length = -1;
  char hostName[MPI_MAX_PROCESSOR_NAME];

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &id);
  MPI_Comm_size(MPI_COMM_WORLD, &numWorkers);
  MPI_Get_processor_name (hostName, &length);

  if ( id == 0 ) {  // process 0 is the conductor 
    printf("Greetings from the conductor, #%d (%s) of %d processes\n",
             id, hostName, numWorkers);
  } else {          // processes with ids > 0 are workers 
    printf("Greetings from a worker, #%d (%s) of %d processes\n",
             id, hostName, numWorkers);
  }

  MPI_Finalize();
  return 0;
}

The answer to the above question illustrates what we can do with this pattern: based on the process id, we can have one process carry out something different than the others. This concept is used a lot as a means to coordinate activities, where one process, what we are calling the conductor, has the responsibility of handing out work and keeping track of results. We will see this in later examples.

Note

By convention, the conductor coordinating process is usually the process number 0.

You have attempted of activities on this page