MPI 收集 returns 结果顺序错误

MPI gather returns results in wrong order

我正在尝试使用 MPI 将矩阵乘以矩阵 (A*B)。我将矩阵 B 拆分为列 B = [b1, ... bn] 并进行一系列乘法运算 ci = A*bi。问题是,然后我收集他们订购的结果列有时似乎是错误的。所以,而不是 [c1, ... cn] 例如,我得到 [c2,c1,c4, ..].

MPI_Scatter(matrix,MM,MPI_INT,part_of_matrix,MM,MPI_INT,0,MPI_COMM_WORLD);

for (i=0; i<n; i++)  {
    get_block_of_matrix(block,part_of_matrix,M,n,i);
    matvect(tmp,val,I,J,M,nnz,block);
    for (j=0; j<M; j++)
        result[M*i+j]=tmp[j];
}


MPI_Gather(result, MM, MPI_INT, res, MM, MPI_INT, 0, MPI_COMM_WORLD);

您的索引已关闭。这一行:

 result[M*i+j]=tmp[j];

应该阅读

 result[n*i+j]=tmp[j];

从上面的代码片段来看,问题并不明显。查看完整的源代码将我带到函数 takevect。里面的索引是错误的,应该是这样的:

void takevect(int *temp,int *matr, int size1, int size2, int i) {
   int j;
   for (j=0; j<size1; j++) temp[j]=matr[size1*i+j];
}

使用 1 个进程时你很幸运,因为 size1 等于 size2(并且 matr 是对称的)。 可以看出,不再需要size2

此外,您可以完全删除此功能并缩短如下内容:

MPI_Scatter(S,MM,MPI_INT,buf_S,MM,MPI_INT,0,MPI_COMM_WORLD);

for (i=0; i<local_n; i++)
   matvect(buf_res+i*M,val,I,J,M,nnz,buf_S+i*M);

MPI_Gather(buf_res, MM, MPI_INT, res, MM, MPI_INT, 0, MPI_COMM_WORLD);