我无法让 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);
}
mpicc main.c
当我运行程序时,我可以编译得很好,mpirun -np 4 ./a.out
输出是:
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