您的当前位置:首页正文

mpi4py 点到点通信之可重复的非阻塞缓冲通信

来源:华佗小知识
Bsend_init(self, buf, int dest, int tag=0)
Recv_init(self, buf, int source=ANY_SOURCE, int tag=ANY_TAG)

需要注意的是,上面虽然给出的是可重复非阻塞的发送和可重复非阻塞的接收方法,但实际上可重复非阻塞发送可与任何接收动作(阻塞接收,非重复非阻塞接收)匹配,反之,可重复非阻塞接收也可与任何发送动作(阻塞发送,非重复非阻塞发送)匹配。

下面给出可重复非阻塞缓冲通信的使用例程:

# Bsend_init_Recv_init.py

import numpy as np
from mpi4py import MPI


comm = 
rank = comm.Get_rank()

# MPI.BSEND_OVERHEAD gives the extra overhead in buffered mode
BUFSISE = 2000 + MPI.BSEND_OVERHEAD
buf = bytearray(BUFSISE)

# Attach a user-provided buffer for sending in buffered mode
MPI.Attach_buffer(buf)

count = 10
send_buf = np.arange(count, dtype='i')
recv_buf = np.empty(count, dtype='i')

if rank == 0:
    send_req = comm.Bsend_init(send_buf, dest=1, tag=11)
    send_req.Start()
    send_req.Wait()
    print 'process %d sends %s' % (rank, send_buf)
elif rank == 1:
    recv_req = comm.Recv_init(recv_buf, source=0, tag=11)
    recv_req.Start()
    recv_req.Wait()
    print 'process %d receives %s' % (rank, recv_buf)

# Remove an existing attached buffer
MPI.Detach_buffer()

运行结果如下:

$ mpiexec -n 2 python Bsend_init_Recv_init.py
process 0 sends [0 1 2 3 4 5 6 7 8 9]
process 1 receives [0 1 2 3 4 5 6 7 8 9]