0

I am trying to do some MPI parallel work. I am able to run this on any number of processors. The issue is that each processor will take one job, execute it and send it back then the program is done. I want to be able to send a processor another job when it has finished. Im not sure how to implement this. Basically I am trying to send each core 10 jobs.

if (myRank == 0) {
    int numCores = MPI::COMM_WORLD.Get_size();

    for (int rank = 1; rank < numCores; rank++) {
        MPI::COMM_WORLD.Send(&yarray[0], imax, MPI::DOUBLE, rank, 0);
        MPI::COMM_WORLD.Send(&tarray[0], imax, MPI::DOUBLE, rank, 0);

        MPI::COMM_WORLD.Recv(&ans, imax, MPI::DOUBLE, MPI::ANY_SOURCE, MPI_ANY_TAG, mystatus);
        answers[counter] = ans;
        counter++;
    }
}
else
{
    MPI::COMM_WORLD.Recv(&yarray1, imax, MPI::DOUBLE, MPI::ANY_SOURCE, MPI_ANY_TAG, mystatus);
    MPI::COMM_WORLD.Recv(&tarray1, imax, MPI::DOUBLE, MPI::ANY_SOURCE, MPI_ANY_TAG, mystatus);

    double floor = 0.5, ceiling = 3.5, range = (ceiling - floor);
    double rnd = floor + double((range * rand()) / (RAND_MAX + 1.0));

    yarray [0] = rnd;
    yarray1 [0] = rnd;

    double temp = 0;
    for (int k = 0; k < imax; k++) {
        tarray1[k+1] = tarray1[k] + h;
        yarray1[k+1] = yarray1[k] + h * (2 * yarray1[k] - 2 * tarray1[k] * tarray1[k] - 3);
    }
    temp = yarray1[int(imax)];

    //cout << "Rank = " << myRank << " Solution = " << temp << endl;

    MPI::COMM_WORLD.Send(&temp, 1, MPI::DOUBLE, 0, 0);

}

Update: within in myrank == 0

while(counter != jobs){
    MPI::COMM_WORLD.Recv(&ans, imax, MPI::DOUBLE, MPI::ANY_SOURCE, MPI_ANY_TAG, mystatus);
        answers[counter] = ans;
        counter++;
}
diggers3
  • 229
  • 3
  • 17

1 Answers1

3

You need to have some sort of feedback from rank 0 to the other ranks. After the other ranks return their work to rank 0, they should receive a new message back that tells them either their next job or that there is no more work to be completed. The ranks should continue looping until there is no more work to be done.

Wesley Bland
  • 8,816
  • 3
  • 44
  • 59
  • So would I want to do a while(counter <= numJobs) that contains the receive in myrank==0 and sends the new message increment the counter? – diggers3 Dec 04 '13 at 18:29
  • Close. It would be more like `while(!more_work)` and an `MPI_RECV` from rank 0 that finds out if there is more work or not. – Wesley Bland Dec 04 '13 at 22:05
  • Within if(myrank == 0) I have the send and receive. I want to put the receive in the while(!more_work) ? Do I not need to loop the send part too? – diggers3 Dec 07 '13 at 23:06