我無法將MPI_Isend發送到隨機目標。如果我硬編碼的目的地,它工作正常,但如果我嘗試生成一個隨機的一個它不。下面是一些相關的代碼:MPI_Isend到隨機目標
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
srand48(myid);
request=MPI_REQUEST_NULL;
if(myid == 0){
buffer=drand48();
do {
destination=lrand48() % numprocs;
} while (destination == 0); //Prevent sending to self
MPI_Isend(&buffer,1,MPI_DOUBLE,destination,1234,MPI_COMM_WORLD,&request);
}
else if (myid == destination) {
MPI_Irecv(&buffer,1,MPI_DOUBLE,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&request);
}
if(myid == 0){
printf("processor %d sent %lf to %d\n",myid,buffer,destination);
}
else {
printf("processor %d got %lf\n",myid,buffer);
}
當我mpirun -np 4 ./a.out
運行的程序,我可以編譯只是mpicc main.c
罰款輸出爲:
processor 0 sent 0.170828 to 2
processor 1 got 0.000000
processor 2 got 0.000000
processor 3 got 0.000000
如果我硬編碼目的地爲2,例如,然後我得到預期輸出:
processor 0 sent 0.170828
processor 1 got 0.000000
processor 2 got 0.170828
processor 3 got 0.000000
'destination'未設置除了0以外的任何等級。有一個很好的機會,沒有處理器會執行Irecv;即使他們這樣做,您在執行Wait之前也會打印結果,因此不能保證在輸出時接收完成。 – 2013-02-19 18:42:11