如何正确使用MPI_Scaterv()

how to use MPI_Scatterv() properly

本文关键字:Scaterv MPI 何正确      更新时间:2023-10-16

我在并行程序中使用MPI_Scatterv时遇到问题。以下是它的定义:

int MPI_Scatterv(const void *sendbuf, const int *sendcounts,
    const int *displs, MPI_Datatype sendtype, void *recvbuf,
    int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)

按照我的理解,MPI_ScattervMPI_Scatter之间的区别在于,在MPI_Scatterv中,段不必具有相同的长度,也不必是连续的(允许内存中存在间隙)。我不明白的是,如果recvbuf可以是每个进程的不同大小的数组,那么recvcount应该使用什么。假设我想向进程0发送sendbuf的5个元素,向进程1发送15个元素。recvcount的值应该是多少?

每个进程都必须用正确的recvcount调用MPI_Scatterv。您可以传递一个变量,该变量的值取决于每个进程的级别。

例如:

int recvcount = (rank == 0) ? 5 : 15;
MPI_Scatterv( sendbuf, sendcounts, displs, sendtype,
              recvbuf, recvcount, recvtype, 0, MPI_COMM_WORLD );

具有秩0的进程调用具有5recvcountMPI_Scatterv,而进程1通过15的计数。