Assume that i have a struct type as follows:
typedef struct {
float x, y, z;
float velocity;
int n, type;
} Particle;
I want to send it. I have to create an MPI_Type. I know 4 ways to do it. I listed them below. I want to know what are the differences, limits and benefits of them.
Using MPI_Type_extent
Using offsetof()
in stddef.h
, it was explained in this answer: MPI Derived Type Send answer
Using MPI_Get_address
, also an example in the same answer.
Using reinterpret_cast<const unsigned char*>
, i didn't try but there is an example here: MPI Create Custom Data
Option 1 is wrong as per the answer you linked.
Option 2 is the most straightforward, and has the advantage of being a constant expression rather than a function call.
Options 3 and 4 are probably functionally identical, but 3 is safer. Consider:
Advice to users.
C users may be tempted to avoid the usage of MPI_GET_ADDRESS and rely on the availability of the address operator &. Note, however, that & cast-expression is a pointer, not an address. ISO C does not require that the value of a pointer (or the pointer cast to int) be the absolute address of the object pointed at --- although this is commonly the case. Furthermore, referencing may not have a unique definition on machines with a segmented address space. The use of MPI_GET_ADDRESS to "reference" C variables guarantees portability to such machines as well. ( End of advice to users.)
Source: http://www.mpi-forum.org/docs/mpi-2.2/mpi22-report/node74.htm
Personally, I'd go with option 3, just to make absolutely sure that the values obtained will be compatible with the other MPI calls. You may want to whip up a function or macro similar to offsetof()
that uses MPI_Get_address()
internally.
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