c++ - MPI 查找位移的不同方法

标签 c++ mpi

假设我有一个结构类型如下:

typedef struct {
 float x, y, z;
 float velocity;
 int  n, type;
} Particle;

我要发送。我必须创建一个 MPI_Type。我知道 4 种方法。我在下面列出了它们。我想知道它们的区别、限制和好处是什么。

  1. 使用 MPI_Type_extent

  2. 使用 offsetof()stddef.h ,在这个答案中有解释:MPI Derived Type Send answer

  3. 使用 MPI_Get_address ,也是同一个答案中的一个例子。

  4. 使用 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/

相关文章:

c - MPI 获取具有最小值的处理器

c - MPI 集体通讯

C++ 结构和数组概念 (-Wmissing-braces])

c - mpi c mpirun 注意到进程等级 0 在信号 11 上退出

c++ - 为什么 Readline 在 autoconf 中不能作为子目录工作?

c++ - 为什么这个类是抽象的?

mpi - 当我尝试发送 2D int 数组时,为什么 MPI_Send 会阻塞?

c++ - 开放 MPI 广播延迟测量

c++ - 分配类 C++ 的数组

c++ - Box2d 错误 C :\filepath\box2D. lib 不是有效的 Win32 应用程序