What is wrong with my code? MPI_Recv is blocking the code, should I use MPI_Gather to bring all values to master process? Not sure if I'm allowed to use MPI_Scatter with MPI_Recv.
int rank;
int size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0)
{
int matrix[size];
// Filling matrix here
...
int value;
MPI_Scatter(matrix, 1, MPI_INT, &value, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("%d\n", value);
}
else
{
int value;
MPI_Recv(&value, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("%d\n", value);
}
MPI_Finalize();
CodePudding user response:
The MPI_Scatter
routine is a so-called "collective", meaning that all processes should call it. While you are right that after a scatter from process zero every other process "receives" data in a metaphorical sense, technically they receive it in the receive buffer of the scatter call.
So you should delete the whole conditional, and replace it by only the MPI_Scatter
line. Life is much simpler than you thought.