I have to admit, I was quite shocked to see how many lines of code are required to transfer one C struct with MPI.
Under what circumstances will it work to simply transmit a struct using the predefined datatype MPI_CHAR
? Consider the following example:
struct particle {
double x;
double y;
long i;
};
struct particle p;
MPI_Isend(&p, sizeof(particle), MPI_CHAR, tag, MPI_COMM_WORLD, &sendr);
In my case, all processes run on the same architecture. Is padding the only issue?
A datatype can be defined easily by specifying a sequence of couples. Each couple represent a block : (type, displacement) . The type is one of the usual types used in MPI, while the displacement indicates the offset in bytes where this data block starts in memory.
Definition. In FORTRAN-2008, an MPI datatype is of type TYPE(MPI_Datatype). When sending a message in MPI, the message length is expressed as a number of elements and not a number of bytes. Example: sending an array that contains 4 integers is expressed as a buffer containing 4 MPI_INTEGER, not 8 or 16 bytes.
MPI_AINT is an MPI_Datatype used to inform MPI about the type of a variable passed to a routine. Similar to MPI_INT being the MPI_Datatype corresponding to an int, MPI_AINT is the MPI_Datatype corresponding to an MPI_Aint.
MPI_BYTE
is the datatype to use when sending untyped data, not MPI_CHAR
. If the two machines have the same architecture but use different character encoding, using MPI_CHAR
may corrupt your data. Sending your data as MPI_BYTE
will leave the binary representation as it is and perform no representation conversion whatsoever.
That said, yes, technically it is correct to send a struct that way, if (and only if) you can guarantee that the data representation will be the same on the sending and receiving ends. However, it is poor programming practice, as it obfuscates the purpose of your code, and introduces platform dependency.
Keep in mind that you only have to define and commit a datatype once, while you will generally need to write code to send it several times. Reducing the clarity of all your sends just to save a couple lines on the single definition is not a trade up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With