TuaimiAA
TuaimiAA

Reputation: 991

Sending C struct of any type safely in MPI

How can I safely send a C struct of any (unknown) type and content in MPI, considering heterogeneous processors. I know MPI_BYTE with MPI_Send as the data type could be used, but how can I ensure consistency and the correctness of the data representation at the receiver side in a heterogeneous system.

Upvotes: 0

Views: 440

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74495

You cannot. MPI is not automatically aware of how specific compilers represent in binary data structures, therefore it makes heavy use of its built-in extensive datatype system, which is used to explicitly tell the library where in memory to find the data and how to interpret it.

To send a C structure between instances of two different executables, which includes e.g. different executables for different platforms (heterogeneous computing) or different executables produced by different compilers or by the same compiler but with different alignment options (no idea why would anyone do such a thing, but it is anyway possible), you must construct an MPI datatype that describes the structure using MPI_Type_create_struct. Another option is to pack on the sending side the relevant structure fields using MPI_Pack and then unpack them on the receiving side using MPI_Unpack. In both cases, an MPI implementation that supports heterogeneous environments will take care of transforming the data to and from some intermediate format (with XDR often being the format of choice).

Using MPI_BYTE to send raw binary data between machines with different endianness or even with different type alignment is simply not an option.

Upvotes: 2

Related Questions