Hari Sundararajan
Hari Sundararajan

Reputation: 678

MPI_Recv - How to determine count?

So let's say I have an MPI program with 2 processes, rank 0 and rank 1.

int i[20], j[20], temp, size; 

In process with rank 0, I have

for(temp=0; temp<20; temp++)
    i[temp] =  temp; 
MPI_Send(i, 15, MPI_INT, 1, 1, MPI_COMM_WORLD);

and let's say process with rank 1 then does

// At this point, size is declared, but not assigned any value. 
MPI_Recv(j,size, MPI_INT, 0, 1, MPI_COMM_WORLD): 
cout << "I have received " << size << " elements" ; 

My question is, in the above statement, does "size" need to be declared? Or does MPI_Recv somehow "know" that it is receiving 15 elements, and automatically sets size = 15? If size is not defined, what happens to the code?

Basically, my question is, I am sending a different number of elements to processors with different ranks, all messages originating from rank 0. I want to know if I should first send the size across, and then prepare the processors to receive that many elements, or if I can just send the array and the processes automatically pick the size up from there.

Upvotes: 9

Views: 5252

Answers (1)

casablanca
casablanca

Reputation: 70691

Look at the documentation for MPI_Recv:

Input Parameters

count maximum number of elements in receive buffer (integer)

So yes, you do need to pass in a value. Note that it need not be the actual number of elements you're receiving, but only the maximum number of elements that your buffer can hold. In your case, you just need to pass 20. The Notes section also mentions how to determine the actual number of elements that were received:

The count argument indicates the maximum length of a message; the actual number can be determined with MPI_Get_count.

Upvotes: 15

Related Questions