0
votes
//#define SIZE 3
void master(int n,int size)
{
for(int j=1;j<size;j++){
    MPI_Send(&n,1,MPI_INT,j,1,MPI_COMM_WORLD);
    printf("\n Sent %d to process %d",n,j);
    fflush(stdout);
    }
}

void slave(int size)
{
for(int j=1;j<size;j++){
    int k=0;
    MPI_Status status;
    MPI_Recv(&k,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
    printf("\n Process %d has received %d",j,k);
    fflush(stdout);
    }
}

int main(int argc,char** argv)
{

MPI_Init(&argc,&argv);
int la_size;
int rank;
MPI_Comm_size(MPI_COMM_WORLD,&la_size);

MPI_Comm_rank(MPI_COMM_WORLD,&rank);

for(int i=0;i<3;i++){
    if(rank==0)
        master(i,la_size);
    else
        slave(la_size);

}
MPI_Finalize();
printf("\nprogram finished...");
fflush(stdout);
return 0;

}  

The program above seems simple enough but its stalling. Is it a deadlock ? The output is :

     Sent 0 to process 1  
 Sent 0 to process 2  
 Sent 1 to process 1  
 Sent 1 to process 2  
 Process 1 has received 0  
 Process 2 has received 1  
 Process 1 has received 2  
 Sent 2 to process 1  
 Sent 2 to process 2  
 Process 1 has received 0  
 Process 2 has received 1  
 Process 1 has received 2
2
Why is everybody posting their code and expecting people to debug?banarun
Because we don't understand what's going on... how things are working. Is there a better way of asking these kinds of questions ?Arjun J Rao

2 Answers

2
votes

In each iteration of the loop in main, rank 0 is doing a single send to each of the many slave ranks, but each of the slaves is posting as many receives as there are slave ranks in total. Since there are no posted sends to match the later receives, the receivers block indefinitely, and the program hangs.

0
votes

Thanks to Novelocrat's answer, the correct code is actually this :

#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
//#define SIZE 3

void master(int n,int size)
{
    for(int j=1;j<size;j++){
    MPI_Send(&n,1,MPI_INT,j,1,MPI_COMM_WORLD);
    printf("\n Sent %d to process %d",n,j);
    fflush(stdout);
    }
}

void slave(int size)
{
    int k=0,rank=0;
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Status status;
    MPI_Recv(&k,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
    printf("\n Process %d has received %d",rank,k);
    fflush(stdout);

}

int main(int argc,char** argv)
{

    MPI_Init(&argc,&argv);
    int la_size;
    int rank;
    MPI_Comm_size(MPI_COMM_WORLD,&la_size);

    MPI_Comm_rank(MPI_COMM_WORLD,&rank);

    for(int i=0;i<3;i++){
    if(rank==0)
        master(i,la_size);
    else
        slave(la_size);

    }
    MPI_Finalize();
    printf("\nprogram finished...");
    fflush(stdout);
    return 0;

}