我试图在进程的数组中保存值,特别是在数字 0 处。因此,我设置了一个条件,如果其等级为 0,则保存这些值:
int main(int argc, char *argv[])
{
int rank,numprocs;
int count[numprocs];
int disp[numprocs];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//... whatever
if (rank==0) {
//whatever
for(i=0;i<numprocs-1;i++) {
count[i]= ...
disp[i] = ...
}
//whatever
}
//... whatever
MPI_Gatherv(sendbuff,size, MPI_Type, //Send from all
recbuff, count, disp, MPI_Type,
0,MPI_Communicator); //Receive on root
}
事实上,进程 0 处的 count
和 disp
是所有值均为 0 的数组。我不理解这种行为。第一次循环有什么问题吗?
编辑:
错误不在这部分代码上。此代码按预期工作
最佳答案
也许这行:
for(i=0;i<numprocs-1;i++)
应该是:
for(i=0;i<numprocs;i++)
在第一种情况(上图)中,如果 numprocs 为 2,则仅初始化 count[] 和 disp[] 的索引 0;这些数组的索引 1 未初始化。
关于c - MPI 库 - 在数组上保存值时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368740/