3

I have a structure :

    struct vertex 
    {  
     double a; double b;
    }      

    struct polygon
    {
     int numofVertex;
     vertex *v;
    }

How to send this nested structure in MPI using MPI_Send? The problem is that the structure contains pointer field "v" due to which MPI_Send crashes. I have tried MPI_Datatype to define new data type, it doesnot work. I read that serialization is the only solution but C doesnot provide such implementation. Any suggestions how to get around with this problem?

3
  • You're right that C doesn't provide an implementation to serialize arbitrary structures. What's stopping you from writing one for these structures? Commented May 12, 2011 at 0:21
  • @Carl I think it is difficult to do so, and in case there is some existing implementation, I dont want to reinvent the wheel. @CarlNorum Commented May 12, 2011 at 0:23
  • I think you're going to have to write it... it shouldn't be that hard. Please come back and ask a specific question if you have a problem with your implementation. Commented May 12, 2011 at 0:25

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.