使用 MPI 集体通信发送结构

Sending a Struct using MPI collective communications

我正在 C 上使用 MPI。我有这个自定义结构,我想使用 MPI 集体通信(收集、分散、广播)将其序列化并发送到其他节点

结构如下

typedef struct {
double x[2];        /* Old and new X-axis coordinates */
double y[2];        /* Old and new Y-axis coordinates */
double xf;          /* force along X-axis */
double yf;          /* force along Y-axis */
double xv;          /* velocity along X-axis */
double yv;          /* velocity along Y-axis */
double mass;        /* Mass of the body */
double radius;      /* width (derived from mass) */
} bodyType;

我试图了解 MPI 上自定义结构的序列化,但无法真正理解该过程。如果有人可以帮助我,那就太好了

谢谢

你的结构只是十个连续的双打。因此,您不需要将您的类型告知 MPI,因为它可以被视为一个数组。如果您有一个包含七个结构的数组,只需告诉 MPI 您有一个包含 70 个双精度数的数组。您应该告诉编译器 "pack" 您的结构(例如 GCC 或 Clang 中的 __attribute__((__packed__))),以便它没有填充。

好的,所以我能够浏览文档并写下这篇文章

const int nitems=8;
    int   blocklengths[8] = {2,2,1,1,1,1,1,1};
    MPI_Datatype types[8] = {MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE};
    MPI_Datatype mpi_body_type;
    MPI_Aint     offsets[8];
    offsets[0] = offsetof(bodyType, x);
    offsets[1] = offsetof(bodyType, y);
    offsets[2] = offsetof(bodyType, xf);
    offsets[3] = offsetof(bodyType, yf);
    offsets[4] = offsetof(bodyType, xv);
    offsets[5] = offsetof(bodyType, yv);
    offsets[6] = offsetof(bodyType, mass);
    offsets[7] = offsetof(bodyType, radius);
    MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_body_type);
    MPI_Type_commit(&mpi_body_type);