c - 为什么这个 MPI 程序停滞

标签 c mpi

//#define SIZE 3
void master(int n,int size)
{
for(int j=1;j<size;j++){
    MPI_Send(&n,1,MPI_INT,j,1,MPI_COMM_WORLD);
    printf("\n Sent %d to process %d",n,j);
    fflush(stdout);
    }
}

void slave(int size)
{
for(int j=1;j<size;j++){
    int k=0;
    MPI_Status status;
    MPI_Recv(&k,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
    printf("\n Process %d has received %d",j,k);
    fflush(stdout);
    }
}

int main(int argc,char** argv)
{

MPI_Init(&argc,&argv);
int la_size;
int rank;
MPI_Comm_size(MPI_COMM_WORLD,&la_size);

MPI_Comm_rank(MPI_COMM_WORLD,&rank);

for(int i=0;i<3;i++){
    if(rank==0)
        master(i,la_size);
    else
        slave(la_size);

}
MPI_Finalize();
printf("\nprogram finished...");
fflush(stdout);
return 0;

}  

上面的程序看起来很简单但是它停滞了。这是一个僵局吗? 输出是:

     Sent 0 to process 1  
 Sent 0 to process 2  
 Sent 1 to process 1  
 Sent 1 to process 2  
 Process 1 has received 0  
 Process 2 has received 1  
 Process 1 has received 2  
 Sent 2 to process 1  
 Sent 2 to process 2  
 Process 1 has received 0  
 Process 2 has received 1  
 Process 1 has received 2

最佳答案

main 循环的每次迭代中,等级 0 对许多从等级中的每一个进行一次发送,但是每个从等级都发送与从等级中的从等级一样多的接收全部的。由于没有发布的发送来匹配后来的接收,接收者无限期地阻塞,程序挂起。

关于c - 为什么这个 MPI 程序停滞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17520088/

相关文章:

c - 函数的返回值在 c 中各不相同

c - 简单的C scanf 不起作用?

c - 如何使用GCC将C代码编译成8088汇编?

c++ - MPI 错误 : Out of Memory - What are some solution options

c - MPI 发送和接收问题

c - 在 C 中实现 getchar 问题

c - 从 FAT12 查找可用软盘空间

c++ - 使用 Boost::Test 并行代码

c - 使用 MPI_BYTE 接收任何数据类型

c - 我如何循环这些 MPI 调用,或者为了清洁起见,将它们转换为函数并循环函数?