0
votes

I am trying to implement master/slave model with MPI, but I have a slight problem.

What I want to do is, slaves should wait for orders from master, they shouldn't work until master sends an order. Master should send an order at the same time to all slaves, wait for all slaves to finish the order, and then send orders to all slaves at the same time again.

For example, I have 3 processors (1 master, 2 slaves), I send orders to slaves twice and I want to print:

Master initialization done.
Master sends order to slave 1
Master sends order to slave 2
Slave 1 got the order from master
Slave 2 got the order from master
Master got response from Slave 1
Master got response from Slave 2
_________________________________
Master sends order to slave 1
Master sends order to slave 2
Slave 1 got the order from master
Slave 2 got the order from master
Master got response from Slave 1
Master got response from Slave 2
All done.

Here's what I have done so far.

int count = 0;
int number;
if (procnum == 0) {
    // initialize master, slaves shouldn't be working until this ends
    std::cout << "Master initialization done." << endl;
    while (count < 2) {
        for (int i = 1; i < numprocesses; i++) {
            number = i * 2;
            std::cout << "Master sends order to slave " << i << endl;
            MPI_Send(&number, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
            MPI_Recv(&number, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            std::cout << "Master got response from Slave " << i << endl;
        }
        count++;
    }
    std::cout << "All done" << endl;
} else {
    int received;
    MPI_Recv(&received, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    std::cout << "Slave " << procnum << " got the order from master" << endl;
    MPI_Send(&received, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}

But I am getting this:

Master initialization done.
Master sends order to slave 1
Slave 1 got the order from master
Master got response from Slave 1
Master sends order to slave 2
Slave 2 got the order from master
Master got response from Slave 2
Master sends order to slave 1

Then it gets stuck. What am I doing wrong?

1
why are you writing "std::cout << "All done" << endl;" inbetween if & else??Nishant
@Nishant I had problem with formatting when copy pasting, it's actually written correctly in the program. I'm editing now.user3616495

1 Answers

0
votes
for (int i = 1; i < size; i++) {

Should be

for (int i = 1; i <= size; i++) {

EDIT: it's OK because size is 3 (includes the server)

About the sequence: MPI_Send and MPI_Recv are blocking calls, so the output is as expected (?).

If the master is blocked on the 2nd round, it's because the slave is not responding. The while (count < 2) loop should wrap both master and slave.