I need to create an MPI derived type to represent a class in my program. The class is fairly straight forward, but large (about 75 data members**). All the data members are single values, 1D arrays, or 2D arrays. Here is an example:
class RestartData {
int dsr;
double firea2sorgn;
int ifwoody[NUM_PFT];
double rootfrac[MAX_ROT_LAY][NUM_PFT];
....
....
}
I think that using the MPI_Type_struct
is appropriate.
(e.g. http://www.open-mpi.org/doc/v1.5/man3/MPI_Type_struct.3.php)
And I more or less follow the example in this question: struct serialization in C and transfer over MPI, but I am not sure how to handle the 2D arrays. Can I make an MPI_Type_struct
that contains several MPI_Type_vector
s? I have been unable to find an example of creating an MPI_Type_struct
containing 2D arrays. Am I on the right approach?
Thanks in advance.
** I think I understand the possible problems with passing a single large message, but in this case, the message is passed infrequently, and at a natural synchronization point (slaves sending data back to the master when they are done crunching numbers)
Derived types in MPI can be freely constructed from other derived types and then used to further create other derived types.
2D arrays, as long as they are contiguous in memory as in your case, are not that different from 1D arrays. When it comes to the
rootfrac
member, you could either create a contiguous datatype withMAX_ROOT_LAY * NUM_PFT
elements of typeMPI_DOUBLE
or you could create a contiguous datatype (let's call itt_dbl_pft
) withNUM_PFT
elements of typeMPI_DOUBLE
and then use it to create another contiguous datatype withMAX_ROOT_LAY
elements of typet_dbl_pft
. Another option is to not create a datatype at all since the MPI structured type constructor takes a separate block length (i.e. number of elements) for each element of the structure.For example, the following type describes the data members that you've shown:
Note that you have to either create the MPI datatype inside a member function of
RestartData
or declare the routine where the type is created asfriend
to the class so that it could access the private and protected data members. Also note thatoffsetof
only works with POD (plain old data) classes, e.g. no fancy constructors and members of types likestd::string
.The easiest way to go is to just treat the whole object as a big buffer:
I don't see the benefit of telling MPI about the internal structure of your class.
Alternatively a 2D array is an array of 1D arrays. So (I imagine) you could use one call to
MPI_Type_contiguous
per array dimension to build up the array datatype.