//#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;
}
上面的程序似乎很簡單,但它的拖延。這是一個僵局嗎? 的輸出是:爲什麼這個MPI程序失速
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
爲什麼大家發佈他們的代碼,並希望人們調試? – banarun
因爲我們不明白髮生了什麼事情......工作如何。有沒有更好的方式來提出這些問題? –