How to send(MPI_Send) nested structure having pointer fields in MPI using C

后端 未结 1 1619
走了就别回头了
走了就别回头了 2021-01-14 10:50

I have a structure :

    struct vertex 
    {  
     double a; double b;
    }      

    struct polygon
    {
     int numofVertex;
     vertex *v;
    }


        
相关标签:
1条回答
  • 2021-01-14 11:25

    You'll have to send it in two messages:

    // 'p' is a pointer to your polygon
    vertex *tmp = p->v;
    p->v = NULL;
    MPI_Send(p, sizeof(struct polygon), MPI_BYTES, dest, 1, ...);
    MPI_Send(tmp, sizeof(struct vertex), MPI_BYTES, dest, 2, ...);
    p->v = tmp;
    

    On the receiving end, you simply receive the struct in two steps:

    polygon p;
    MPI_Recv(&p, sizeof(struct polygon), MPI_BYTES, src, 1, ...);
    p.vertex = malloc(sizeof(struct vertex));
    MPI_Recv(p.vertex, sizeof(struct vertex), MPI_BYTES, src, 2, ...);
    

    Obviously this is not very nice, so it'll be easier if you keep your struct pointer-less so you can send it at once.

    0 讨论(0)
提交回复
热议问题