我确信它会MPI_Gather
从包括根进程本身在内的所有进程中收集数据。
如何MPI_Gather
从不包括根进程本身的所有进程中收集数据?或者有什么替代功能吗?
复制MPI_Gather
使用的功能,MPI_Gatherv
但指定0
为根等级的块大小。像这样的东西:
int rank, size, disp = 0;
int *cnts, *displs;
MPI_Comm_size(MPI_COMM_WORLD, &size);
cnts = malloc(size * sizeof(int));
displs = malloc(size * sizeof(int));
for (rank = 0; rank < size; rank++)
{
cnts[i] = (rank != root) ? count : 0;
displs[i] = disp;
disp += cnts[i];
}
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Gatherv(data, cnts[rank], data_type,
bigdata, cnts, displs, data_type,
root, MPI_COMM_WORLD);
free(displs); free(cnts);
请注意,这MPI_Gatherv
可能比MPI_Gather
因为 MPI 实现很可能无法优化通信路径并且会退回到收集操作的一些愚蠢的线性实现而慢得多。MPI_Gather
因此,在根进程中仍然使用并提供一些虚拟数据可能是有意义的。
您还可以提供MPI_IN_PLACE
根进程发送缓冲区的值,它不会向自身发送数据,但是您必须再次在接收缓冲区中为根数据保留位置(就地操作期望根将将其数据直接放在接收缓冲区内的正确位置):
if (rank != root)
MPI_Gather(data, count, data_type,
NULL, count, data_type, root, MPI_COMM_WORLD);
else
MPI_Gather(MPI_IN_PLACE, count, data_type,
big_data, count, data_type, root, MPI_COMM_WORLD);