2015-11-28 43 views
0

我工作的MPI对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自定义结构体的序列化,但不能真正理解这个过程。如果有人能帮助我在这里将是巨大的

谢谢

+0

'MPI_Type_struct'是你的朋友! – simpel01

+2

@ simpel01,'MPI_Type_struct'在MPI-2中被弃用,并且在MPI-3中不再存在。应该使用'MPI_Type_create_struct'来代替。 –

+0

anyhelp如何实际使用它们? –

回答

0

好了,所以我能够去通过文件和写这

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); 
0

你的结构,才十连续双打。你不需要告诉MPI你的类型,因此,它可以被当作一个数组来处理。如果你有七个结构的数组,那么告诉MPI你有70个双打数组。你应该让你的编译器“打包”你的结构(例如GCC或Clang中的__attribute__((__packed__))),以便它没有填充。

+2

您不必这样做,打包结构可能会有负面的性能影响其他地方在所有双打的情况下可能无关紧要,但这是一般糟糕的建议。 – Jeff

+2

MPI是关于使用它的程序的可移植性。我第二个杰夫。 –

+0

@Jeff:只包含双打的包装结构将不会产生负面的表现效果。这应该是显而易见的,因为双精度结构在布局方面与双精度数组相同。包装的东西是皮带和吊带,实际上在大多数系统中都不需要,如果你仍然觉得它有点痛。您可以将包装关闭并添加编译时断言,即两个结构数组的大小与20个双倍大小相同。 –