0
//#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
4

2 回答 2

2

在循环的每次迭代中main,rank 0 对许多从属 rank 中的每一个进行一次发送,但每个从属发布与从属 rank 总数一样多的接收。由于没有发布的发送来匹配后来的接收,接收器无限期地阻塞,程序挂起。

于 2013-07-08T06:27:22.203 回答
0

感谢 Novelocrat 的回答,正确的代码实际上是这样的:

#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
//#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)
{
    int k=0,rank=0;
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Status status;
    MPI_Recv(&k,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
    printf("\n Process %d has received %d",rank,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;

}
于 2013-07-08T06:48:33.333 回答