none
DEADLOCK: attempting to send a message to the local process without a prior matching receive RRS feed

  • Question

  • Hi,

    I am new to MPI programming and still trying out very basic codes.

    I wanted to make core 0 to receive simple values sent by other workers.  But it gives me a MPI error at run time. when executing MPI_Send command.

    Here is my code,

    #include <stdio.h>
    #include <mpi.h>

    #include<iostream>

    using namespace std;

    int main(int argc, char *argv[]) {
    int numprocs, rank, namelen, ierr;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    MPI_Init(&argc, &argv); // What ever I write in between will be parallelize.,....................
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Current Worker index
    MPI_Get_processor_name(processor_name, &namelen);

        // Send rank to process 0

            double val = -1.0*rank;
            MPI_Send(&val, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
            printf("ProcID %d sent value %f to ProcID %d",rank,val,0);
      


        if(rank == 0){

           int i;
           double val, sum = 0;
            MPI_Status status;

            int cp = 0;

            for (i = 0 ; i!=numprocs ; ++i){
                ierr = MPI_Recv(&val, 1, MPI_DOUBLE, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);

                if(ierr == MPI_SUCCESS){
                    printf("ProcID %d Receives value %f from ProcID %d\n",rank,val);
                    sum += 1;
                    cp++;
                 }
                 else MPI_Abort(MPI_COMM_WORLD,1);

            }
            printf("the Total is %f\n",sum);
        }

    MPI_Finalize();//.......................................................................
    }

    When I change MPI_Send line to,

    if(rank != 0) {
            double val = -1.0*rank;
            MPI_Send(&val, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
            printf("ProcID %d sent value %f to ProcID %d",rank,val,0);
        }

    I don't get any thing at all. It doesn't do anything.

    I am running this on my laptop with a core i7 processor. 

    Any help would be appreciated?

    Thursday, August 4, 2016 5:38 PM

All replies

  • Hi there,

    When you make the change to MPI_Send line, you also need to reduce the number of receives as well. This means the for loop should be for( i = 0; i < numprocs - 1; ++i ). Otherwise you have more recv's than matching send and the program deadlocks

    I also notice that the printf after MPI_Recv is missing the destination rank value, you could get it my using status.MPI_SOURCE (i.e., printf("ProcID %d Receives value %f from ProcID %d\n",rank,val, status.MPI_SOURCE); )

    Anh


    Tuesday, August 9, 2016 11:43 PM