0

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?

tomfroehle
  • 602
  • 5
  • 16

1 Answers1

2
  1. You haven't said how you do the check which function is called first. Cout'ing something on screen doesn't guarantee proper order of displaying messages (at least while using MPI).

  2. You don't need to put MPI_Recv in loop since it is a blocking function. Its not even recommended while you didn't assign starting value to *rcvDone.

  3. Its not safe to use malloc together with some MPI functions. Read "thread and interrupt safety" section on http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Ssend.html

kukis
  • 4,489
  • 6
  • 27
  • 50
  • Thanks for that. foo() and bar() are cout'ing Results and they apeared in a random order. The solution is also postet under http://stackoverflow.com/questions/15901226/an-atomic-call-to-cout-in-mpi – tomfroehle Jul 11 '13 at 19:22