c++cmpi

MPI send and receive struct of structs


how can i send and receive this type of structs via MPI?

struct controlPoint{
    int hour,minute,second,x,y,z;
};

struct flight{
    int flightNum, controlNum;
    vector<controlPoint> point;
};

vector<flight> flights;

a sample code will be very helpful


Solution

  • You'll need to invoke the magic of user-defined datatypes. First, define an MPI_Contiguous datatype for controlPoint: let's call it cp_type. Next, you'll need an MPI_Struct to represent flight, which we'll call flight_type. This is a bit tricky, since you can't pass a vector through MPI (without outside help from e.g. Bost). Fortunately, you don't really need to: vector guarantees contiguous storage, just like an array. So your MPI_Struct would consist of 2 blocks: the first block has two integers with an offset of 0 (flightNum and controlNum), and the second block has point.size() number of cp_types, with an offset of point[0]. This way, you only send the data from the vector, and none of its implementation details. After this, you can send your struct as a single element of flight_type.

    Receiving is almost trivial: just define the same MPI datatypes as above, and receive your data as a single copy of this type. Note however, that the flight struct that you receive into will need to have enough space in its point vector to receive all the content. You can just resize() it before defining the MPI datatypes: that way, enough space will be allocated, and the values will be overwritten during the receive.