0

I am working on MPI on C. I have this custom struct that I want to serialize and send to other nodes using MPI Collective communication (Gather, Scatter, Broadcast)

The struct is as following

typedef struct {
double x[2];        /* Old and new X-axis coordinates */
double y[2];        /* Old and new Y-axis coordinates */
double xf;          /* force along X-axis */
double yf;          /* force along Y-axis */
double xv;          /* velocity along X-axis */
double yv;          /* velocity along Y-axis */
double mass;        /* Mass of the body */
double radius;      /* width (derived from mass) */
} bodyType;

I have tried to understand serialization of custom structs on MPI but could not really understand the process. If some one can help me out here it would be great

Thank You

Hassan Jalil
  • 1,114
  • 4
  • 14
  • 34

2 Answers2

0

Your struct is just ten contiguous doubles. You don't need to inform MPI about your type, therefore, as it can be treated as an array. If you have an array of seven of your structs, just tell MPI you have an array of 70 doubles. You should tell your compiler to "pack" your struct (e.g. __attribute__((__packed__)) in GCC or Clang) so that it has no padding.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 2
    You don't have to do this, and packing structs may have negative performance effects elsewhere. It may not matter in the case of all doubles, but it's bad advice in general. – Jeff Hammond Nov 29 '15 at 01:22
  • 2
    MPI is about portability of the programs using it. I second Jeff. – Hristo Iliev Nov 29 '15 at 10:07
  • @Jeff: Packing structs containing only doubles will have no negative performance effects. This should be obvious, because a struct full of doubles is, layout-wise, the same as an array of doubles. The packing thing is belt-and-suspenders, and actually won't be needed on most systems, if you still feel it hurts somehow. You can leave packing off and add a compile-time assertion that the size of an array of two structs is the same as the size of 20 doubles. – John Zwinck Nov 29 '15 at 14:10
0

Ok so I was able to go through documents and wrote this

const int nitems=8;
    int   blocklengths[8] = {2,2,1,1,1,1,1,1};
    MPI_Datatype types[8] = {MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE};
    MPI_Datatype mpi_body_type;
    MPI_Aint     offsets[8];
    offsets[0] = offsetof(bodyType, x);
    offsets[1] = offsetof(bodyType, y);
    offsets[2] = offsetof(bodyType, xf);
    offsets[3] = offsetof(bodyType, yf);
    offsets[4] = offsetof(bodyType, xv);
    offsets[5] = offsetof(bodyType, yv);
    offsets[6] = offsetof(bodyType, mass);
    offsets[7] = offsetof(bodyType, radius);
    MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_body_type);
    MPI_Type_commit(&mpi_body_type);
Hassan Jalil
  • 1,114
  • 4
  • 14
  • 34