假设我有一个结构类型如下:
typedef struct {
float x, y, z;
float velocity;
int n, type;
} Particle;
我要发送。我必须创建一个 MPI_Type。我知道 4 种方法。我在下面列出了它们。我想知道它们的区别、限制和好处是什么。
使用
MPI_Type_extent
使用
offsetof()
在stddef.h
,在这个答案中有解释:MPI Derived Type Send answer使用
MPI_Get_address
,也是同一个答案中的一个例子。使用
reinterpret_cast<const unsigned char*>
,我没试过,但这里有一个例子:MPI Create Custom Data
最佳答案
根据您链接的答案,选项 1 是错误的。
选项 2 是最直接的,并且具有作为常量表达式而不是函数调用的优点。
选项 3 和 4 在功能上可能相同,但 3 更安全。考虑:
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.)
来源:http://www.mpi-forum.org/docs/mpi-2.2/mpi22-report/node74.htm
就我个人而言,我会选择选项 3,只是为了绝对确保获得的值与其他 MPI 调用兼容。您可能想要创建一个类似于 offsetof()
的函数或宏,它在内部使用 MPI_Get_address()
。
关于c++ - MPI 查找位移的不同方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22780766/