1

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);
    }   
 }
}
user2893825
  • 95
  • 2
  • 9
  • Standard warning: Do **not** cast `void *` as returned by `malloc` & friends! (and format your code properly). – too honest for this site Jul 25 '15 at 17:35
  • 1
    possible duplicate of [MPI Broadcasting dynamic 2D array to other processors](http://stackoverflow.com/questions/14292669/mpi-broadcasting-dynamic-2d-array-to-other-processors) – Jonathan Dursi Jul 25 '15 at 18:34
  • 1
    The issue here is that, e.g., `MPI_Send(source,4,MPI_DOUBLE..` is trying to send 4 doubles starting at location `source`, but that's not how the data is laid out - source is an array of pointers to doubles. This comes up frequently; I've pointed out one Q&A where it's answered, but I know there are better examples - @HristoIliev , you and I have both answered these sorts of questions but a quick search doesn't turn anything up - is there a better canonical dup for this? – Jonathan Dursi Jul 25 '15 at 18:47
  • 2
    @JonathanDursi, [this one](http://stackoverflow.com/questions/5104847/mpi-bcast-a-dynamic-2d-array) maybe? By the way, it seems that one is not getting notifications for mentions in comment feeds where one doesn't participate. I just came here while clicking randomly through the [mpi] questions. – Hristo Iliev Jul 25 '15 at 23:52
  • Allocate matrices continuously. This could will perform terribly. The solution is C 101. – Jeff Hammond Jul 26 '15 at 04:19
  • @HristoIliev - ah, that's a shame, thanks for letting me know. – Jonathan Dursi Jul 26 '15 at 17:31

0 Answers0