0

I am new to distributed parallel programming. In the following code the process 0 gets stuck on MPI.Recv method execution..

    if (me != 0) {
        if (finalTour != null) {
            Node[] nodes = new Node[5];
            nodes[0] = finalTour;

            MPI.COMM_WORLD.Send(nodes, 0, 5, MPI.OBJECT, 0, 0);
        }
    }
    if (me == 0) {
        for (int i = 1; i < processes; i++) {
            Node[] nodes = new Node[5];
            MPI.COMM_WORLD.Recv(nodes, 0, 5, MPI.OBJECT, MPI.ANY_SOURCE, MPI.ANY_TAG);
            if (nodes[0] != null) {
                if (finalTour != null) {
                    if (finalTour.cost > nodes[0].cost) {
                        finalTour = nodes[0];
                    }
                    minCostPath = finalTour;
                } else {
                    finalTour = nodes[0];
                    minCostPath = finalTour;
                }
            }
        }
    }
    MPI.Finalize();
    if (minCostPath != null) {
        print(size, minCostPath);
    }

When I debugged this snippet, I observed the following:
1. Sometimes the Recv method is executed before the corresponding send method does. Could this pose a problem?
2. All the processes except 0 send a message to process 0 and execute their MPI.Finalize() method, while process 0 is waiting on a Recv. Could the problem be because of this?

Varun
  • 49
  • 1
  • 11
  • The answer to both questions is negative. `MPI_Recv` (or the corresponding MPJ Express method) blocks until a matching message has arrived. It will hang only if no such message arrives at all. As the send operation is inside a conditional, make sure that `finalTour` is non-null in all processes with ranks > 0. A bit of `println` debugging could help in that case. – Hristo Iliev Nov 02 '15 at 06:50
  • Yeah, now that you mentioned it, maybe finaltour is null for some process, I'll try that. Thanks Hristo. – Varun Nov 03 '15 at 06:59
  • I tried it after removing the condition of finalTour!= null for the send part, didn't work process 0 still gets stuck on Recv. – Varun Nov 03 '15 at 18:07
  • How many receives succeed? – Hristo Iliev Nov 03 '15 at 20:57
  • It gets blocked on the first receive for process 1, however all the processes seem to execute send just fine and execute Mpi.finalize. – Varun Nov 04 '15 at 22:48
  • What happens if you replace `MPI.ANY_TAG` with `0` and `MPI.ANY_SOURCE` with `i` in the receive call? – Hristo Iliev Nov 05 '15 at 06:30

0 Answers0