I am trying to send a part of the matrix from one process to another. This is the given matrix
It has 10 rows and 8 columns. I am trying to send half of the columns ( 4 to 7) [NOTE--> matrix is 0 indexed] from process 0 to process 1 with the help of MPI_Pack()
. For that I am using the following code
double snd_buf[4][r]; //r is the number of rows
double recv_buf[4][r];
double buf[4][r];
MPI_Request request[c]; //c is the number of columns
MPI_Request request1[c];
MPI_Status status[c];
//packing and sending the data
if(myrank==0)
{
//we will send the half of the matrix to process 2
for(int j=4;j<c;j++)
{
position=0; //reassigning position after each and every send
for(int i=0;i<r;i++)
{
MPI_Pack(&mat[i][j], 1 , MPI_DOUBLE,snd_buf[j-4],80,&position,MPI_COMM_WORLD);
}
}
//sending all the buffers
for(int j=4;j<c;j++)
{
MPI_Send (snd_buf[j-4], 10 , MPI_PACKED, 1 /*dest*/ , j /*tag*/ , MPI_COMM_WORLD);
}
}
And for receiving I am using the following code.
if(myrank==1)
{
for(j=4;j<c;j++)
{
MPI_Recv(recv_buf[j-4], 10, MPI_PACKED, 0 /*src*/ , j /*tag*/, MPI_COMM_WORLD,&status[j]);
}
for(int j=4; j<c;j++)
{
position=0;
for(int i=0;i<r;i++)
{
MPI_Unpack(recv_buf[j-4],80,&position,&buf[j-4][i], 1/*outcount*/, MPI_DOUBLE, MPI_COMM_WORLD);
}
}
}
But when I am printing the value of the recv_buf I am getting only the first element of each row in some cases followed by 0s and in some cases some garbage value as well. Given below is the content of the recv_buf.
Example-1:
Example-2:
I have checked my snd_buf[] as well, but it is packing all the values well and good.
I am not getting where I am going wrong and getting these 0's and sometimes garbage values in the recv_buf. Please help.