4

I have to use MPI API for send/receive matrices in my programs. To send a matrix I used the below syntax:

MPI_Send(matrix, ...)  <- USE THIS

MPI_Send(&matrix, ...)

MPI_Send(&matrix[0][0], ...)

Similar to the last one, but untested: MPI_Send(&matrix[0],....

I saw in different examples the use of the above and I wonder if there is a difference between them or is any one going to cause errors on big data? I'm asking because I've heard a person who knows a little bit of HPC talking about using only the first syntax (enumerated above) but I didn't have time to ask him why and I cannot find the reason on the Internet, or I don't know where to look. All of the above works just find on little examples that I compiled but I have only 2 cores on my processor so I may not be able to see the problem.

I my understanding those examples point to the same memory address so what will be the problem ? Is there a MPI API related issue?

Thanks in advance

bogdan.rusu
  • 901
  • 4
  • 21
  • 41

2 Answers2

4

All the matrix references in your question point to the same address, albeit with different types. MPI_Send takes a void*, so it looses the type anyway. So all calls are practically equivalent.

If the type of matrix is changed from a 2D array to a pointer, then the above are no longer equivalent. E.g., if matrix is an int*, only the first and last option both compile and produce the required address. Therefore, I too would recommend using the first option - it's safer should the type of matrix changes.

Community
  • 1
  • 1
Eran
  • 21,632
  • 6
  • 56
  • 89
0

With MPI_Send() you only specify the pointer of the data the matrix. It is recommended to store the matrix as a 1-dimentional array :

int *data = (int*)calloc(cols*rows,sizeof(int));
MPI_Send(data,cols*rows, ...);

When you use MPI_Recv() remember that you sent an array and you may for sure want to work with a matrix.

Ana-Maria
  • 63
  • 1
  • 8