I try to send and receive a large array to self-rank using MPI. The code works well for small arrays (array size <10000) when I further increase the array size to 100000, it will deadlock. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Status stat;
int N = 100000;
// Allocate memory for A on CPU
double *A = (double*)malloc(N*sizeof(double));
double *B = (double*)malloc(N*sizeof(double));
// Initialize all elements of A to 0.0
for(int i=0; i<N; i ){
A[i] = 0.0;
}
int tag1 = 10;
int tag2 = 20;
//printf("rank=%d",rank);
MPI_Send(A, N, MPI_DOUBLE, 0, tag1, MPI_COMM_WORLD);
MPI_Recv(B, N, MPI_DOUBLE, 0, tag1, MPI_COMM_WORLD, &stat);
MPI_Finalize();
return 0;
}
I compile the code by: mpicc
command for the run: mpirun -np 1 ./
CodePudding user response:
That's the definition of send and receive: they are blocking. The statement after the send will not execute until the send has been successfully completed.
- The safe way to solve this is using
MPI_Isend
andMPI_Irecv
. - The case for a small messages which don't block is an optimization that is system dependent and you can not count on it.