# include "mpi. H"
# include & lt; Stdio. H>
Int main (int arg c, char * * argv)
{
Int rank, the size, I, buf [1].
MPI_Status status;
MPI_Init (& amp; Arg c, & amp; Argv);
MPI_Comm_rank (MPI_COMM_WORLD, & amp; Rank);
MPI_Comm_size (MPI_COMM_WORLD, & amp; The size);
Printf (" beginging... \n");
If (rank==0)
{
For (I=0; i<100 * (size - 1); I++) {
MPI_Recv (buf, 1, MPI_INT MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, & amp; Status).
Printf (" Msg=% d from % d with the tag % d \ n ", buf [0], the status, MPI_SOURCE, status, MPI_TAG);
}
}
The else
{
For (I=0; i<100; I++)
Buf [0]=rank + I;
MPI_Send (MPI_INT buf, 1, 0, I, MPI_COMM_WORLD);
}
MPI_Finalize ();
return 0;
}
CodePudding user response:
Because you use MPI_Recv collected 100 * (size - 1) time, but only MPI_Send size 1 time, so that excess MPI_Recv waiting, you can refer to the following code,
# include "mpi. H"
# include & lt; Stdio. H>
Int main (int arg c, char * * argv)
{
Int rank, the size, I, buf [1].
MPI_Status status;
MPI_Init (& amp; Arg c, & amp; Argv);
MPI_Comm_rank (MPI_COMM_WORLD, & amp; Rank);
MPI_Comm_size (MPI_COMM_WORLD, & amp; The size);
Printf (" beginging... \n");
If (rank==0)
{
For (I=0; i<(the size - 1); I++) {
MPI_Recv (buf, 1, MPI_INT MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, & amp; Status).
Printf (" Msg=% d from % d with the tag % d \ n ", buf [0], the status, MPI_SOURCE, status, MPI_TAG);
}
}
The else
{
Buf [0]=rank;
MPI_Send (MPI_INT buf, 1, 0, rank, MPI_COMM_WORLD);
}
MPI_Finalize ();
return 0;
}