我正在研究一个示例编码,但无法发送和接收相同的值。
我创建的数据类型是
typedef struct customData{
double iv;
double dv[5];
char cv[10];
} customData;
我在这里
创建数据类型
MPI_Datatype type;
MPI_Datatype ctype[3] = {MPI_DOUBLE, MPI_DOUBLE, MPI_CHAR};
int blocklen[3] = {1, 5, 10};
MPI_Aint disp[3] = { 0, sizeof(double), sizeof(double)*6 };
MPI_Type_create_struct(1, blocklen, disp, ctype, &type);
MPI_Type_commit(&type);
发送和接收
if(rank == 0){
customData data = {5,
{1,2,3,4,5},
{'h','d','h','a','q','w','e','s','l','z'}};
printf("%f %.2f %cn", data.iv, data.dv[0], data.cv[0]);
MPI_Send(&data, 1, type, 1, 0, MPI_COMM_WORLD);
} else {
customData recv;
MPI_Status status;
MPI_Recv(&recv, 1, type, 0, 0, MPI_COMM_WORLD, &status);
printf("%f %.2f %cn", recv.iv, recv.dv[0], recv.cv[0]);
}
输出为
5.000000 1.00 h
5.000000 0.00
你应该把结构中的元素数量放在你的MPI_Type_create_struct中,3而不是1。
MPI_Type_create_struct(3, blocklen, disp, ctype, &type);
我测试了它,我得到了:
5.000000 1.00 h
5.000000 1.00 h
一些信息在这里:
难以理解MPI_Type_create_struct
C 语言中的结构序列化和通过 MPI 传输