2
votes

I noticed a strange thing with MPI_Send and MPI_Recv buffer size which I can't understand. The documentation says that the count argument of these functions describes a number of elements of datatype type:

int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest, 
             int tag, MPI_Comm comm);

count [in] number of elements in send buffer (nonnegative integer)

int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, 
             int tag, MPI_Comm comm,  MPI_Status *status);

count [in] maximum number of elements in receive buffer (integer)

Suppose we have 2 processes, the first (root) process allocates an array of n integers and fills it with some data

int* temp = (int*) malloc(n * sizeof(int));
for (int i = 0; i < n; i++) temp[i] = ...;

then sends it to the second process with rank = 1.

MPI_Send(temp, n, MPI_INT, 1, 0, MPI_COMM_WORLD);

The second process receives this array of n integers.

MPI_Recv(temp, n, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_INGNORE);

But after MPI_Recv the temp array appeared truncated. When I changed count argument from n to n * sizeof(int), I received a correct array. But n * sizeof(int) describes buffer size in bytes, not in elements as documentation says. Is it a common behavior or a simple misunderstanding? P.S. I use MPICH2 x86 as MPI implementation and 32-bit VS08 on Windows 7.

1
You are reading the documentation correctly, and there is no problem with the Send/Recv lines as posted, which suggests issues elsewhere. Are temp and n correctly defined on both tasks?Jonathan Dursi
Thanks for the comment. Yes, n values are equal on both tasks and temp array are correctly defined.Symbolist
Well, you'll have to supply more code, because the problem is not apparent from what you've provided. As always, a simple, self-contained, reproducable example is best.Jonathan Dursi

1 Answers

2
votes

According to
https://www.open-mpi.org/doc/v1.8/man3/MPI_Recv.3.php ,
count argument holds the number of elements in the buffer.

count
Maximum number of elements to receive (integer).

Number of bytes must be calculated by multiplying number of elements (count) and size of each element (given by the constant of type MPI_Datatype).