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?

2
  • 1
    What are bar() and foo() doing to indicate they've been called? Are they writing to stderr? Commented Jul 11, 2013 at 14:14
  • Please make sure to initialize rcvDone before you test it! Commented Jul 12, 2013 at 11:26

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that. foo() and bar() are cout'ing Results and they apeared in a random order. The solution is also postet under stackoverflow.com/questions/15901226/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.