I have found some unexpected behavior in MPI (using C++) in this small code example:
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if(rank == 1) {
int *sendDone = (int*)malloc(sizeof(int));
*sendDone = 1;
MPI_Ssend(sendDone,1,MPI_INT, 0, 1,MPI_COMM_WORLD);
foo();
} else {
int *rcvDone = (int*)malloc(sizeof(int));
bar();
while(*rcvDone != 1) {
MPI_Recv(rcvDone,1,MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
cout << *rcvDone << endl;
}
MPI_Finalize();
It is compiled and run with the following commands:
mpic++ sim.cc -o sim
mpirun -n 2 ./sim
The Code should be executed in the following order:
bar();
foo();
Because the Process #0 is starting to receive after the execution of bar(). But in reality, foo() is sometimes starting before bar() is finished. Can somebody explain that to me and give a solution to the problem?