I have this code, where master sends initial data to his slaves. It uses MPI_Bcast (broadcast) function but when the Broadcast is called, the master continues but the slaves are stuck in deadlock.
Code snippets are following:
Master {
run() {
...
MPI_Pack(&offset, 1, MPI_INT, buffer, BUFFERSIZE, &position, MPI_COMM_WORLD);
MPI_Pack(&dataSize, 1, MPI_INT, buffer, BUFFERSIZE, &position, MPI_COMM_WORLD);
MPI_Pack(dataLoader.getData(), dataSize*dataSize, MPI_CHAR, buffer, BUFFERSIZE, &position, MPI_COMM_WORLD);
MPI_Bcast(buffer, position, MPI_PACKED, 0, MPI_COMM_WORLD);
printf("master ok");
}
}
Slave {
run() {
MPI_Bcast(buffer, BUFFERSIZE, MPI_PACKED, 0, MPI_COMM_WORLD);
printf("Slave OK");
//unpack
MPI_Unpack(buffer, BUFFERSIZE, &position, &offset, 1, MPI_INT, MPI_COMM_WORLD);
MPI_Unpack(buffer, BUFFERSIZE, &position, &dataSize, 1, MPI_INT, MPI_COMM_WORLD);
data = new char[dataSize * dataSize];
MPI_Unpack(buffer, BUFFERSIZE, &position, data, dataSize*dataSize, MPI_CHAR, MPI_COMM_WORLD);
}
}
The output would be: Master ok
and the slave would stop responding. Thanks for help
mpi_bcast
should be called by all processes in the communicator and they should all encounter the call at the same point in the code. As you have written your (pseudo-)code the root process makes a call to broadcast which the worker processes do not, and the worker processes make a call which the root does not.
mpi_bcast
is one of MPI's collective routines, the library takes care of determining the correct pattern of message sending based on the arguments you pass to it. In the case of mpi_bcast
the 4th argument is the id of the source of the broadcast (it doesn't need to be the root process of the communicator though it often is); all the other processes in the communicator will be receivers.
来源:https://stackoverflow.com/questions/20030922/mpi-bcast-deadlock