MPI_Isend与MPI_Irecv小例子

访问本站观看效果更佳
与MPI_Send与MPI_Recv不同,MPI_Isend与MPI_Irecv均为非阻塞式通信。
函数原型:
MPI_Isend

int MPI_Isend(const void *buf, int count, MPI_Datatype datatype, int dest, int tag,MPI_Comm comm, MPI_Request *request)

输入参数:
buf:发送缓冲区的首地址
count:需要发送的字节数
datatype:每个发送元素的数据类型
dest:目标的rank(id)
tag:消息标识(integer)
comm:通信域
输出参数:
request:communication request (handle)

MPI_Irecv

int MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int source,
int tag, MPI_Comm comm, MPI_Request *request)

输出参数:
buf:接收缓冲区的首地址
count:接收缓冲区存放字节数(integer)
datatype:每个接收元素的数据类型
source:发送者的rank (integer)
tag:消息标识(integer)
comm:通信域
输出参数:
request:communication request (handle)

request 返回的非阻塞通信对象(用于TEST/WAIT使用)。
由于非阻塞通信在调用后不用等待通信完全结束就可以返回,所以非阻塞通信的返回并不意味着通信的完成。在返回后,用户还需要检测甚至等待通信的完成。MPI提供了下面的函数来完成这些目的。

#include "mpi.h"
int main( int argc, char* argv[] ){
    int rank, nproc;
    int isbuf, irbuf, count;
    MPI_Request request;
    MPI_Status status;
    int TAG = 100;
 
    MPI_Init( &argc, &argv );
    MPI_Comm_size( MPI_COMM_WORLD, &nproc );
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );

    if(rank == 0) {
         isbuf = 9;
         MPI_Isend( &isbuf, 1, MPI_INT, 1, TAG, MPI_COMM_WORLD, &request );
    } else if(rank == 1) {
        MPI_Irecv( &irbuf, 1, MPI_INT, 0, TAG, MPI_COMM_WORLD, &request);
        MPI_Wait(&request, &status);
        MPI_Get_count(&status, MPI_INT, &count);
        printf( "irbuf = %d source = %d tag = %d count = %d\n", 
                   irbuf, status.MPI_SOURCE, status.MPI_TAG, count);
        }
    MPI_Finalize();
}

猜你喜欢

转载自blog.csdn.net/zcgyq/article/details/83088197
mpi