【问题标题】:MPI datatype alignmentMPI 数据类型对齐
【发布时间】:2018-11-30 01:31:17
【问题描述】:

我正在编写示例编码,但无法发送和接收相同的值。

我创建的数据类型是

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 %c\n", 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 %c\n", recv.iv, recv.dv[0], recv.cv[0]);
}

输出是
5.000000 1.00 小时
5.000000 0.00

【问题讨论】:

  • 一般来说,你应该使用offsetof()来填充位移数组(因为编译器可能会在这里和那里添加一些填充)

标签: c mpi


【解决方案1】:

您应该将结构中的元素数放入 MPI_Type_create_struct 中,而不是 1。

MPI_Type_create_struct(3, blocklen, disp, ctype, &type);

我测试了它,我得到了:

5.000000 1.00 h
5.000000 1.00 h

这里有一些信息:

Trouble Understanding MPI_Type_create_struct

struct serialization in C and transfer over MPI

【讨论】:

  • 所以“计数”不是ctype的数量而是条目的数量?
  • 您在 ctype 中有 3 个变量,对应于您的结构的 3 个变量。如果您的结构由 n 个变量组成,您将使用 count=n。希望对您有所帮助!
猜你喜欢
  • 2015-11-22
  • 2012-02-11
  • 2012-02-03
  • 2011-02-08
  • 1970-01-01
  • 2013-08-21
  • 2014-12-03
相关资源
最近更新 更多