c++ - Unexpected behavior with MPI_Ssend and MPI_Recv -


i have found unexpected behavior in mpi (using c++) in 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 compiled , run following commands:

mpic++ sim.cc -o sim mpirun -n 2 ./sim 

the code should executed in following order:

bar(); foo(); 

because process #0 starting receive after execution of bar(). in reality, foo() starting before bar() finished. can explain me , give solution problem?

  1. you haven't said how check function called first. cout'ing on screen doesn't guarantee proper order of displaying messages (at least while using mpi).

  2. you don't need put mpi_recv in loop since blocking function. not recommended while didn't assign starting value *rcvdone.

  3. its not safe use malloc mpi functions. read "thread , interrupt safety" section on http://www.mcs.anl.gov/research/projects/mpi/www/www3/mpi_ssend.html


Comments