LJ Archive

Listing 5. Slaves

void slave(int num_threads){
  long result;
  long bounds[2];
  MPI_Status status;

  while(true){
    MPI_Recv(bounds, 2, MPI_LONG, 0, MPI_ANY_TAG,
             MPI_COMM_WORLD, &status);
    if (status.MPI_TAG == KILL)
      return;

    try {
      long start(bounds[0]);
      long stop(bounds[1]);
... insert code from threaded version's main() ...
      MPI_Send(&count,1,MPI_LONG, 0,
                   0, MPI_COMM_WORLD);
    }
    catch (range_error e){
      cout << "Exception: " << e.what() << endl;
   }
  }
}
LJ Archive