I am simply trying to create a matrix, send and receive it, but it does not work.
The creation is fine it seems but the sending/receiving crushes. originally I had a struct, 1 of its field is a matrix and 2 other are the col and row sizes.
So if it's easier to pass around structures I may prefer it. I have seem similar questions but there were different implementations there different than mine, and how to deal with structures so I decided to post.
void test()
{
int numprocs, myid;
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Status status;
if(myid==0) //master
{
int i,j;
double **source,**dest;
source=(double**)calloc(2,sizeof(double*));
dest=(double**)calloc(2,sizeof(double*));
for(i=0;i<2;i++)
{
dest[i]=(double*)calloc(2,sizeof(double));
source[i]=(double*)calloc(2,sizeof(double));
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
source[i][j]=i+j;
}
// MPI_Send(source,4, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
MPI_Recv(&dest,4,MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &status);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("dest=%f ",dest[i][j]);
Newline();
}
for(i=0;i<2;i++)
{
free(dest[i]);
free(source[i]);
}
free(dest);
free(source);
}
else //slave (myid!=0)
{
int i,j;
if(myid==1)
{
double** dd;
dd=(double**)calloc(2,sizeof(double*));
for(i=0;i<2;i++)
dd[i]=(double*)calloc(2,sizeof(double));
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
dd[i][j]=i;
printf("dd=%f\n",dd[i][j]);
}
// MPI_Recv(dd,4,MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
MPI_Send(dd,4, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
for(i=0;i<2;i++)
free(dd[i]);
free(dd);
}
}
}