MPI:MPI_Probe应用的例子
说明
MPI_Probe()和MPI_Probe()函数探测接收消息的内容,但不影响实际接收到的消息。我们可以根据探测到的消息内容决定如何接收这些消息,比如根据消息大小分配缓冲区等等。需要说明的是,这两个函数第一个是阻塞方式,即只有探测到匹配的消息才返回;第二个是非阻塞方式,即无论探测到与否都立即返回。
函数原型:
int MPI_Probe (int source /* in */,
int tag /* in */,
MPI_Comm comm /* in */,
MPI_Status* status /*out*/)
以上是阻塞型探测,直到有一个符合条件的消息到达,返回MPI_ANY_SOURCE和 MPI_ANY_TAG
int MPI_Iprobe (int source /* in */,
int tag /* in */,
MPI_Comm comm /* in */,
int * flag /*out*/,
MPI_Status* status /*out*/)
以上是非阻塞型探测,无论是否有一个符合条件的消息到达,立即返回。有flag=true;否则flag=false
测试例:
int x; float y;
int send_x = 100;
float send_y = 3.14;
MPI_Comm_rank(comm, &rank);
if(rank ==0) /*0->2发送一int型数*/
MPI_Send(&send_x,1,MPI_INT,2,99,comm);
else if(rank == 1) /*1->2发送一float型数*/
MPI_Send(&send_y,1,MPI_FLOAT,2,99,comm);
else /* 根进程接收 */
for(int i=0;i<2;i++){
MPI_Probe(MPI_ANY_SOURCE,99,comm,&status);/*Blocking*/
if(status.MPI_SOURCE == 0)
MPI_Recv(&x,1,MPI_INT,0,99,&status);
else if(status.MPI_SOURCE == 1)
MPI_Recv(&y,1,MPI_FLOAT,1,99,&status);
}