When I trying to run this code Message "DEADLOCK: attempting to send a message to the local process without a prior matching receive"
#include "pch.h"
#include <iostream>
#include <mpi.h>
using namespace std;
void main(int argc, char* argv[])
{ int ierr, procid, numprocs;
ierr = MPI_Init(&argc, &argv);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &procid);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
// All procids send the value - procid to procid 0
double val = -1.0 * procid;
MPI_Send(&val, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
cout << "ProciD " << procid << " send value " << val << " to procid 0.\n";
if (procid == 0)
{
// procid 0 must recieve numprocs values
int i; double val, sum = 0; MPI_Status status;
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)
{
cout << "Procid " << procid << " recieve value " << val;
sum = sum + val;
}
else
MPI_Abort(MPI_COMM_WORLD, 1);
}
cout << " The Total is " << sum << "\n";
}
ierr = MPI_Finalize();
}
I don't understand why this error happend