1

I am trying to use MPI_Datatype to send the below structure but the MPI_Send crashes while sending the structure. I am wondering how to handle this situation. Here is the code I have written to define new MPI data-type:

   typedef struct
    {
       double x;
       double y;
    } vertex;

   typedef struct
   {
        int num_vertices;
        vertex vertex[2];
   } vertex_list;


       MPI_Datatype vertexType;
        MPI_Type_contiguous(2,MPI_DOUBLE,&vertexType);
        MPI_Type_commit(&vertexType);

      MPI_Datatype vertexListType;
        MPI_Datatype typev[3] = {MPI_INT, vertexType, MPI_UB};
        int blocklenv[3] = {1, 2, 1};
        MPI_Aint dispv[3];
        /* compute displacements of structure components */
        MPI_Address( vertexl, dispv);
        MPI_Address( vertexl[0].vertex, dispv+1);
        MPI_Address(  vertexl+1, dispv+2);
        base = dispv[0];

        for (i=0; i <3; i++)
         dispv[i] -= base;

        /* build datatype describing structure */
        MPI_Type_struct( 3, blocklenv, dispv, typev, &vertexListType);
        MPI_Type_commit(&vertexListType);

https://docs.google.com/document/d/1OQFtx0ClkKQx7X91BlVgiizs5D9jShhtgsKafrgC7hk/edit?hl=en

4
  • You can edit your post rather than add comments. Code in the post proper can be nicely formatted and the question will be better all around. Commented May 11, 2011 at 20:14
  • I've copied in the code for you (just waiting for the peer review). In the future, as pmg says, you can edit your own post and using the code formatting helps to get a quicker response. Commented May 11, 2011 at 20:26
  • You should show the send as well. Commented May 18, 2011 at 8:17
  • thanks for your reply @Captain @Head. how to MPI_Send in case of non-contiguous data in the nested structure for example when vertices is a pointer field : typedef struct{ double x; double y; } vertex; typedef struct { int num_vertices; vertex *vertices; } vertex_list; Commented Jun 24, 2011 at 4:14

1 Answer 1

3

I'm not sure your structs are going to work the way you're intending here, but I can share my experience with sending structs with MPI_Send.

Rather than creating an explicit MPI datatype it's possible to simply send the struct itself since all of its contents are in a contiguous piece of memory. The trick is providing the correct size and datatype for the MPI_Send operation.

Using your structs, here's what I've done in the past (assuming variable vertex_list list has already been defined):

MPI_Send(&list, sizeof(vertex_list), MPI_BYTE, <rankToSendTo>, <tagInteger>, <comm>);

So the data buffer to be sent is a pointer to the list struct, the size of the buffer is the size of a vertex_list in bytes and the MPI datatype is simply bytes.

On the receiving end, you just need to supply a vertex_list reference as the receiving buffer:

vertex_list receiveList;
MPI_Recv(&receiveList, sizeof(vertex_list), MPI_BYTE, <rankOfSender>, <tagInteger>, <comm>, <status>);

Hope that helps!

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

2 Comments

@Head thanks for your reply. how to MPI_Send in case of non-contiguous data in the nested structure for example when vertices is a pointer field ie "vertex *vertex" instead of "vertex vertex[2]"
That certainly gets trickier because (I assume in this case) there is no simple way for the receiving process to know how many vertices are in the *vertex list. Edit: Perhaps some more info on what you're attempting to do with those vertices on the receiving end might spark some ideas.

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.