0
votes

I know that dead lock occurs in general when:
one thing is waiting for another thing to complete it's work, while the other thing is waiting the first thing to complete. and this is my code:

    MPI_Comm_rank(MPI_COMM_WORLD, &myrank); /* Get rank */
    if( myrank == 0 ) {
    MPI_Recv( b, 100, MPI_DOUBLE, 1, 19, MPI_COMM_WORLD, &status );
    MPI_Send( a, 100, MPI_DOUBLE, 1, 17, MPI_COMM_WORLD );
    }
    else if( myrank == 1 ) {
    MPI_Recv( b, 100, MPI_DOUBLE, 0, 17, MPI_COMM_WORLD, &status );
    MPI_Send( a, 100, MPI_DOUBLE, 0, 19, MPI_COMM_WORLD );
    }

In the article Deadlock with MPI , He was asking if the code makes a deadlock situation.
This is his code:

    MPI_Comm_rank (comm, &my_rank);
    if (my_rank == 0) {
       MPI_Send (sendbuf, count, MPI_INT, 1, tag, comm);
       MPI_Recv (recvbuf, count, MPI_INT, 1, tag, comm, &status);
    } else if (my_rank == 1) {
       MPI_Send (sendbuf, count, MPI_INT, 0, tag, comm);
       MPI_Recv (recvbuf, count, MPI_INT, 0, tag, comm, &status);
    }

Well, the program might freeze, but its situation isn't considered a deadlock situation. If we consider it a deadlock situation, what about the following case:

    MPI_Recv (recvbuf, count, MPI_INT, 1, tag, comm, &status);

The program will also freeze. I'm writing an article about deadlock situations, and I'm confused.

1
Write a test program, and test what happens. Deadlocks are generally quite easy to understand once you have coded them up.Chiel
I;m saying that, if the program has only MPI_Recv, it will freeze because nothing is sending, and I think it's a freeze, not a deadlock, the same thing about the article I've pointed touser3379482
I would call that a bug, rather than a deadlock :)Chiel
The same thing about the article?user3379482
Personally, if you have communication started on one process but not on the other, I would also call that a bug. However, in your original case, that is the definition of a deadlock. There's a "matching" send/recv pair, but they are ordered incorrectly so we end up having both processes waiting for the other to enter the matching communication call. I don't understand fully what your third situation is.NoseKnowsAll

1 Answers

0
votes

Your first block of code will deadlock no matter what.

The behaviour of your second block of code is dependent on many factors, such as MPI implementations, size of your message, and MPI runtime configurations. The basic idea here is that the blocking MPI_Send routine will only return when it is safe to overwrite the send buffer. Data leaving the send buffer may go directly to the destination process, or it may be copied onto some intermediate buffer space. If the former is true your code will deadlock. Otherwise your code may appear to work well but you may run into problem when you port your code to a different system.

The 'Deadlock with MPI' article you referred to explains this quite well. It also gives your two solutions to avoid deadlock: (1) to change the order of send and receive properly; (2) to use non-block send and receive. There is actually a third way to avoid deadlock: you can use MPI_Sendrecv, which is effectively a combined send and receive call that guarantees not to deadlock.