0

This MPI program is written in C. When I enter 2 as the number of processors, the output is as follows:

P:0 Got data from processor 1 P:0 Got 100 elements P:0 value[5]=5.000000 

But when I enter 3 or more processors, the program outputs

P:0 Got data from processor 1 P:0 Got 100 elements P:0 value[5]=5.000000 

And then it stalls, and nothing else gets printed. I have to exit the program with [ctrl]+[c]. I do not know why the program stalls. I'd appreciate some hints or good direction.

Here is the code:

#include <stdio.h> #include <stdlib.h> #include <mpi.h> /* Run with two processes */ int main(int argc, char *argv[]) { int rank, i, count; float data[100],value[200]; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); if(rank==1) { for(i=0;i<100;++i) data[i]=i; MPI_Send(data,100,MPI_FLOAT,0,55,MPI_COMM_WORLD); } else { MPI_Recv(value,200,MPI_FLOAT,MPI_ANY_SOURCE,55,MPI_COMM_WORLD,&status); printf("P:%d Got data from processor %d \n",rank, status.MPI_SOURCE); MPI_Get_count(&status,MPI_FLOAT,&count); printf("P:%d Got %d elements \n",rank,count); printf("P:%d value[5]=%f \n",rank,value[5]); } MPI_Finalize(); } 

2 Answers 2

5

Because you are sending the data only to the process with rank == 0 with the process with rank == 1. That is why it works fine with 2 processes (0 and 1). With more processes, the processes with rank 2,3,4,5.... will enter the else block and wait for non-sent data. That is why they halt the execution (waiting for data that will be never sent). You should make the process with rank 1 send the data to all other processes, with a single for loop encapsulating the send operation and give the for loop iterator as the destination rank in the MPI_Send call.

Sign up to request clarification or add additional context in comments.

Comments

2

Your processor of rank 2 is waiting to receive data that is never sent to it. Let's step through your code: Processor 1 goes into the if block and sends information to processor 0. Processor 0 receives this information in the else block and continues on. Processor 2 also goes into the else block and is waiting to receive information from some other processor, but nothing ever sends it anything, so it simply hangs. In order to get around this, add in a MPI_Send(data,100,MPI_FLOAT,1,55,MPI_COMM_WORLD); command to run on three processors. Alternatively, use a loop to send to all processors that are not rank 1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.