MPI收集不同大小

3

我有一个由10台计算机和2个变量组成的C++集群: - result: 大小为int 100; - result_final:(仅在主机上)大小为int 1000; 我的问题是如何将“result”的数据片段汇聚起来并创建“result_final”,因为它们的大小不同。 谢谢!

int *rcounts = (int *) malloc(commSize * sizeof(int));
int *displs = (int *) malloc(commSize * sizeof(int));
for (i = 0; i < commSize; ++i) {
    displs[i] = commRank * result_size * size;
    rcounts[i] = result_size * size;
}
MPI_Gatherv(h_result, result_size * size, MPI_INT, h_result_final, rcounts,
        displs, MPI_INT, 0, MPI_COMM_WORLD);

如果您展示您编写的代码,这里的人会提供帮助。如果您不展示,这里的人会添加无用的评论(比如这个)。 - High Performance Mark
1个回答

5
如果每个块的大小不同,您需要使用MPI_Gatherv()而不是MPI_Gather()。同样的情况也适用于MPI_Scatter()MPI_Scatterv()

我可以这样做吗? MPI_Gatherv(h_result,result_size * size,MPI_INT,h_result_final,displs,rcounts,MPI_INT,0,MPI_COMM_WORLD); - Yannis Assael
你看过MPI_Gatherv的描述了吗?如果你要收集相同大小的数据块,那么请使用MPI_Gather。请查看文档 - 它非常有用! - Hristo Iliev
为什么?那是我的代码... int *rcounts = (int *) malloc(commSize * sizeof(int)); int *displs = (int *) malloc(commSize * sizeof(int)); for (i = 0; i < commSize; ++i) { displs[i] = commRank * result_size * size; rcounts[i] = result_size * size; } MPI_Gatherv(h_result, result_size * size, MPI_INT, h_result_final, rcounts, displs, MPI_INT, 0, MPI_COMM_WORLD); - Yannis Assael
由于您的计数都相同,最好使用 MPI_Gather。这一点在您最初的问题中并不明显。 - Hristo Iliev
应该是:MPI_Gatherv(h_result, result_size * size, MPI_INT, h_result_final, result_size * size, MPI_INT, 0, MPI_COMM_WORLD);。你从问题中删除了之前的代码示例,所以我不确定你是否应该将两个size参数都乘以size。请注意,MPI_GatherMPI_Gatherv中的大小都是以元素而不是字节为单位指定的。 - Hristo Iliev

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