将MPI数据类型返回给MPI Gather

3
我有以下的MPI_DataType代码。
typedef struct resultset {
    int rank, totalProcessed, bracket1, bracket2, bracket3, bracket4;
    float bracket1percent, bracket2percent, bracket3percent, bracket4percent;
} resultset;

MPI_Datatype createResultType()
{
    // Set-up the arguments for the type constructor
    MPI_Datatype new_type;

    int count = 2;

    int blocklens[] = { 6, 4 };

    MPI_Aint indices[2];
    indices[0] = 0;
    MPI_Type_extent( MPI_FLOAT, &indices[1] );
    indices[1] *= 4;    // There are 4 float

    MPI_Datatype old_types[] = { MPI_INT, MPI_FLOAT };

    // Call the data type constructor
    MPI_Type_struct(count, blocklens, indices, old_types, &new_type);
    MPI_Type_commit(&new_type);

    return new_type;
}

我试图使用以下代码从其他进程中接收数据。rank是一个整数,定义了该进程的排名。

MPI_Datatype resType = createResultType();
if(rank != 0){
    MPI_Gather(&results, sizeof(resultset), resType, NULL, 1, resType, 0, MPI_COMM_WORLD);
}
else {
    resultset* all_results = new resultset[numProcs];
    MPI_Gather(&results, sizeof(resultset), resType, all_results, sizeof(resultset), resType, 0, MPI_COMM_WORLD);
}

问题在于我无法使用这种方法循环遍历所有结果(all_results)。
for(int i = 0; i < numProcs; ++i){
    std::cout << all_results[i].rank << " processed " << all_results[i].totalProcessed <<std::endl;
}

我认为问题出在我的数据类型上,但我不确定。感谢您的任何建议。
编辑:我解决了循环问题,现在我得到的是不完整的数据。我在gather函数中将sizeof(resultset)更改为1。现在我收到正确的数据,除了最后两个浮点数为-431602080而不是应该的0.0022234327654。
MPI_Datatype resType = createResultType();
if(rank != 0){
    MPI_Gather(&results, 1, resType, NULL, 1, resType, 0, MPI_COMM_WORLD);
}
else {
    resultset* all_results = new resultset[numProcs];
    MPI_Gather(&results, 1, resType, all_results, 1, resType, 0, MPI_COMM_WORLD);
}
1个回答

1

浮点数需要是“6”

int blocklens[] = { 6, 4 };

int blocklens[] = { 6, 6 };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接