0
votes

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

2
MPI_Type_struct is your friend!simpel01
@simpel01, MPI_Type_struct was deprecated in MPI-2 and no longer exists in MPI-3. One should use MPI_Type_create_struct instead.Hristo Iliev
anyhelp on how to actually use them ?Hassan Jalil
Take a look at this answerPooja Nilangekar
thanks @PoojaNilangekar that link was able to help me find the solution :)Hassan Jalil

2 Answers

0
votes

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.

0
votes

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);