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?