美文网首页Python 并行计算
mpi4py 中的 Status 对象

mpi4py 中的 Status 对象

作者: 自可乐 | 来源:发表于2018-07-22 21:10 被阅读68次

    上一篇中我们介绍了 mpi4py 中的 info 和 assertion,下面我们将介绍 mpi4py 中的 Status 对象。

    在 MPI-1 中,Status 对象仅用于点到点通信,而 MPI-2 则在通用化请求以及并行 I/O 中使用 Status 对象。因此新的 MPI.Status 对象在不同上下文环境下,可能存在有效域或无效域,特别是在通用化请求操作中,应用程序需要在回调函数中设置某些状态信息。为管理泛化后的 MPI.Status 对象,MPI-2 新增加了下列函数:MPI.Status.Set_elements 和 MPI.Status.Set_cancelled。

    需要注意的是,Status 对象最好对应每个操作单独使用,而不推荐复用的方式。此外,如果在通用化请求中需要传递额外信息,则不应修改 Status 对象中各域的语义,而推荐借助 extra_status 参数传递信息。通用化请求将在下一篇中介绍。

    下面给出 MPI.Status 对象的方法接口。

    MPI.Status.Get_source(self)
    

    获得并返回接收消息的源。也可以通过属性 source 获取。

    MPI.Status.Set_source(self, int source)
    

    设置接收消息的源。也可以通过属性 source 设置。

    MPI.Status.Get_tag(self)
    

    获得并返回接收消息的 tag。也可以通过属性 tag 获取。

    MPI.Status.Set_tag(self, int tag)
    

    设置接收消息的tag。也可以通过属性 tag 设置。

    MPI.Status.Get_error(self)
    

    获得接收消息的错误。也可以通过属性 error 获取。

    MPI.Status.Set_error(self, int error)
    

    设置接收消息的错误。也可以通过属性 error 设置。

    MPI.Status.Get_count(self, Datatype datatype=BYTE)
    

    返回接收到的元素个数(以 datatype 为单位计算)。如果 datatype 的 size 为 0,该方法也会返回 0;如果接收到的数据不是 datatype 的整数倍,则会返回 MPI.UNDEFINED。也可以通过属性 count 获取以 MPI.BYTE 为单位的计数。

    MPI.Status.Get_elements(self, Datatype datatype)
    

    返回 datatype 中的基本数据类型的个数。如果支持 MPI-3,mpi4py 实际调用的是 MPI_Get_elements_x 以支持大的计数。

    MPI.Status.Set_elements(self, Datatype datatype, Count count)
    

    设置 datatype 中的基本数据类型的个数。如果支持 MPI-3,mpi4py 实际调用的是 MPI_Status_set_elements_x 以支持大的计数。

    MPI.Status.Is_cancelled(self)
    

    检测某个通信请求是否被取消。也可用通过属性 cancelled 来查询。

    MPI.Status.Set_cancelled(self, bool flag)
    

    将取消状态设置为 flag注意:该方法只能用于通用化请求的回调函数中。

    例程

    下面给出使用例程。

    # status.py
    
    """
    Demonstrates the usage of MPI.Status.
    
    Run this with 2 processes like:
    $ mpiexec -n 2 python status.py
    """
    
    import numpy as np
    from mpi4py import MPI
    
    
    comm = MPI.COMM_WORLD
    rank = comm.rank
    
    status = MPI.Status()
    print 'status.source == MPI.ANY_SOURCE:', status.source == MPI.ANY_SOURCE
    print 'status.tag == MPI.ANY_TAG:', status.tag == MPI.ANY_TAG
    print 'status.error == MPI.SUCCESS:', status.error == MPI.SUCCESS
    print 'MPI.UNDEFINED = %d' % MPI.UNDEFINED
    
    comm.Barrier()
    
    if rank == 0:
        send_buf = np.arange(10, dtype='i')
        # send 10 MPI.INTs, 40 MPI.BYTEs
        comm.Send(send_buf, dest=1, tag=0)
    
        # create a datatype and send it (multiple of sizeof(int))
        send_type = MPI.Datatype.Create_struct([1, 16], [0, 4], [MPI.INT, MPI.CHAR])
        send_type.Commit()
        # send 1 MPI.INT, 16 MPI.CHARs (= 4 MPI.INTs), 1 + 16 basic elements
        comm.Send([send_buf, 1, send_type], dest=1, tag=1)
        send_type.Free()
    
        # create a datatype and send it (not a multiple of sizeof(int))
        send_type = MPI.Datatype.Create_struct([1, 17], [0, 4], [MPI.INT, MPI.CHAR])
        send_type.Commit()
        # send 1 MPI.INT, 17 MPI.CHARs (not a multiple of MPI.INT), 1 + 17 basic elements
        comm.Send([send_buf, 1, send_type], dest=1, tag=2)
        send_type.Free()
    elif rank == 1:
        recv_buf = np.full(10, -1, dtype='i')
        # receive 10 ints
        status = MPI.Status()
        comm.Recv(recv_buf, source=0, tag=0, status=status)
        print
        print 'Get count with MPI.INT:', status.Get_count(MPI.INT)
        print 'Get elements with MPI.INT:', status.Get_elements(MPI.INT)
        print 'Get count with MPI.BYTE:', status.Get_count(MPI.BYTE)
        print 'Get elements with MPI.BYTE:', status.Get_elements(MPI.BYTE)
        print
    
        # create a datatype and receive it
        recv_type = MPI.Datatype.Create_struct([1, 36], [0, 4], [MPI.INT, MPI.CHAR])
        recv_type.Commit()
        status = MPI.Status()
        comm.Recv([recv_buf, 1, recv_type], source=0, tag=1, status=status)
        print 'Get count with MPI.INT:', status.Get_count(MPI.INT)
        print 'Get elements with MPI.INT:', status.Get_elements(MPI.INT)
        print 'Get count with recv_type:', status.Get_count(recv_type)
        print 'Get elements with recv_type:', status.Get_elements(recv_type)
        print
    
        status = MPI.Status()
        comm.Recv([recv_buf, 1, recv_type], source=0, tag=2, status=status)
        print 'Get count with MPI.INT:', status.Get_count(MPI.INT)
        print 'Get elements with MPI.INT:', status.Get_elements(MPI.INT)
        print 'Get count with recv_type:', status.Get_count(recv_type)
        print 'Get elements with recv_type:', status.Get_elements(recv_type)
    
        recv_type.Free()
    

    运行结果如下:

    $ mpiexec -n 2 python status.py
    status.source == MPI.ANY_SOURCE: True
    status.tag == MPI.ANY_TAG: True
    status.error == MPI.SUCCESS: True
    MPI.UNDEFINED = -32766
    status.source == MPI.ANY_SOURCE: True
    status.tag == MPI.ANY_TAG: True
    status.error == MPI.SUCCESS: True
    MPI.UNDEFINED = -32766
    
    Get count with MPI.INT: 10
    Get elements with MPI.INT: 10
    Get count with MPI.BYTE: 40
    Get elements with MPI.BYTE: 40
    
    Get count with MPI.INT: 5
    Get elements with MPI.INT: 5
    Get count with recv_type: -32766
    Get elements with recv_type: 17
    
    Get count with MPI.INT: -32766
    Get elements with MPI.INT: -32766
    Get count with recv_type: -32766
    Get elements with recv_type: 18
    

    以上介绍了 mpi4py 中的 Status 对象,在下一篇中我们将介绍 mpi4py 中的通用化请求。

    相关文章

      网友评论

        本文标题:mpi4py 中的 Status 对象

        本文链接:https://www.haomeiwen.com/subject/veyrmftx.html