MPI Different methods to find displacements
Asked Answered
A

1

7

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.

  1. Using MPI_Type_extent

  2. Using offsetof() in stddef.h, it was explained in this answer: MPI Derived Type Send answer

  3. Using MPI_Get_address, also an example in the same answer.

  4. Using reinterpret_cast<const unsigned char*>, i didn't try but there is an example here: MPI Create Custom Data

Aleen answered 1/4, 2014 at 8:40 Comment(0)
L
3

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.

Leija answered 1/4, 2014 at 16:59 Comment(1)
While the point in the MPI standard is valid for pointers versus addresses, the difference between two pointers of the same type is well defined to be difference between the subscripts of the elements of an array of that type pointed to by the two pointers. Also the ISO C++ states that the value of a pointer is the address of the byte in memory, where the object storage starts (and that address is preserved on casts as far as I understand the standard), though the actual value representation might be implementation-specific. Therefore #4 should be fairly portable.Piero

© 2022 - 2024 — McMap. All rights reserved.