C语言 MPI_Gatherv不包括来自根进程的数据



在尝试使用MPI_Gatherv时,我遇到了奇怪的行为,我一定是误解了这个函数的一些基本内容。为了演示,我将代码简化为一个玩具问题,希望在没有太多上下文的情况下易于理解。

int * gatherv_counts = (int *)malloc(2*sizeof(int)); 
int * gatherv_displacements = (int *)malloc(2*sizeof(int)); 
double * receive_buffer = (double *)malloc(10*sizeof(double)); 
for(int i = 0; i < 2; i++)
{
gatherv_counts[i] = 5;
gatherv_displacements[i] = 0;
}
double * data = (double *)malloc(5*sizeof(double));
for(int i = 0; i < 5; i++)
{
data[i] = (double)(mpirank + 2);
}
int mpiret = MPI_Gatherv( data, 5, MPI_DOUBLE, receive_buffer, gatherv_counts, gatherv_displacements, MPI_DOUBLE, 0, mpicomm); 
if (mpirank == 0) {
FILE *file = fopen("output.txt", "a");

for (int i = 0; i < 10; i++) {
fprintf (file, "%16.15e n", receive_buffer[i]);
}        

fflush(file);
fclose(file);
}

我用2个MPI进程运行这个。来自每个进程的数据只有5个double(总组合大小为10),值设置为rank+2。计数只是硬编码为5,位移都是0。这是我所能想象的最简单的MPI_Gatherv用法,我希望在完成后receive_buffer为[2,2,2,2,2,2,2,3,3,3,3,3,3]。

相反,receive_buffer是[3,3,3,3,3,0,0,4.940656458412465e -324, 1.182342568274937e-316, 1.146400746224656e+248]。它似乎完全跳过了秩0 (MPI_Gatherv的根)中的数据,从秩1中获取数据,并将剩余的空间填充为垃圾。有人能解释一下这里出了什么问题吗?

另外,为了记录,我看到了一些类似的标题问题,但这些似乎不是同一个问题:MPI_Gatherv没有正确收集数据

MPI_Gatherv: Garbage values received in root's array

MPI_GATHERV的用法没有发送缓冲区

所有的位移都是0。做的事:

displacements[1] = displacements[0] + counts[0]

和类似的更高的秩计数。

最新更新