Fatal error: MPI_Gatherv

Fatal error: MPI_Gatherv

我是 MPI 的新手,正在尝试使用 MPI_Gatherv。有两个问题,首先,该函数不会从所有处理器收集所有项目,有时它会给我致命错误。我不明白这是怎么回事!

每个处理器都有一个向量,包含最小数字的索引,向量的大小在每个处理器中可能不同。

哪一部分是错误的? 任何帮助,将不胜感激。

我的代码:

MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
vector<int> localMin;
for (int i=0; i<numPerProc; i++)
{
    if (receive_buffer[i]==min) {
        int adjIndex=numPerProc*my_rank+i;
        localMin.push_back(adjIndex);
    }
}
// I thought I might be better use array instead of vector:
int nelements=localMin.size();
int* localMinArray=new int[nelements];
for (int i=0; i<nelements; i++) {
    localMinArray[i]=localMin[i];
}

int *counts = new int[comm_size];

// Each process tells the root how many elements it holds
MPI_Gather(&nelements, 1, MPI_INT, counts, 1, MPI_INT, 0, MPI_COMM_WORLD);

// Displacements in the receive buffer for MPI_GATHERV
int *disps = new int[comm_size];
// Displacement for the first chunk of data - 0
for (int i = 0; i < comm_size; i++)
    disps[i] = (i > 0) ? (disps[i-1] + counts[i-1]) : 0;

int *allMin;
if (my_rank == 0)
    // disps[size-1]+counts[size-1] == total number of elements
    allMin = new int[disps[comm_size-1]+counts[comm_size-1]];


// Collect everything into the root
MPI_Gatherv(&localMinArray, nelements, MPI_INT, &allMin, counts, disps, MPI_INT, 0, MPI_COMM_WORLD);

结果是这样的:

Vector values in Rank  0: 4,11,
Vector values in Rank  1: 
Vector values in Rank  2: 24,31,
count values:
2,0,2,
disps values:
0,2,2,
allMin values:
4,11,0,-268435456,

这些是我有时会遇到的错误:

*** An error occurred in MPI_Gatherv
*** reported by process [3485859841,1]
*** on communicator MPI_COMM_WORLD
*** MPI_ERR_COUNT: invalid count argument
*** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
***    and potentially your MPI job)

我通过进行以下更改解决了这个问题:

 MPI_Gatherv(*&localMinArray, nelements, MPI_INT,*&allMin, counts, disps, MPI_INT, 0, MPI_COMM_WORLD);