在上一篇我們介紹了 mpi4py 中非重復(fù)非阻塞標(biāo)準(zhǔn)通信模式档插,下面我們將介紹非重復(fù)非阻塞緩沖通信傍菇。
非重復(fù)非阻塞的緩沖通信模式是與阻塞的緩沖通信模式相對應(yīng)的,其通信方法(MPI.Comm 類的方法)接口有一個前綴修飾 I/i仰挣,如下:
ibsend(self, obj, int dest, int tag=0)
irecv(self, buf=None, int source=ANY_SOURCE, int tag=ANY_TAG)
Ibsend(self, buf, int dest, int tag=0)
Irecv(self, buf, int source=ANY_SOURCE, int tag=ANY_TAG)
這些方法調(diào)用中的參數(shù)是與阻塞標(biāo)準(zhǔn)通信模式的方法調(diào)用參數(shù)一樣的饭于。
需要注意的是,上面雖然給出的是非阻塞的發(fā)送和非阻塞的接收方法撬码,但非阻塞發(fā)送可與阻塞接收相匹配儿倒,反之,阻塞發(fā)送也可與非阻塞接收相匹配呜笑。
下面給出非重復(fù)非阻塞緩沖通信的使用例程:
# ibsend_irecv.py
from mpi4py import MPI
comm = MPI.COMM_WORLD
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)
send_obj = {'a': [1, 2.4, 'abc', -2.3+3.4J],
'b': {2, 3, 4}}
if rank == 0:
send_req = comm.ibsend(send_obj, dest=1, tag=11)
send_req.wait()
print 'process %d sends %s' % (rank, send_obj)
elif rank == 1:
recv_req = comm.irecv(source=0, tag=11)
recv_obj = recv_req.wait()
print 'process %d receives %s' % (rank, recv_obj)
# Remove an existing attached buffer
MPI.Detach_buffer()
運行結(jié)果如下:
$ mpiexec -n 2 python ibsend-irecv.py
process 0 sends {'a': [1, 2.4, 'abc', (-2.3+3.4j)], 'b': set([2, 3, 4])}
process 1 receives {'a': [1, 2.4, 'abc', (-2.3+3.4j)], 'b': set([2, 3, 4])}
# Ibsend_Irecv.py
import numpy as np
from mpi4py import MPI
comm = MPI.COMM_WORLD
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.Ibsend(send_buf, dest=1, tag=11)
send_req.Wait()
print 'process %d sends %s' % (rank, send_buf)
elif rank == 1:
recv_req = comm.Irecv(recv_buf, source=0, tag=11)
recv_req.Wait()
print 'process %d receives %s' % (rank, recv_buf)
# Remove an existing attached buffer
MPI.Detach_buffer()
運行結(jié)果如下:
$ mpiexec -n 2 python Ibsend_Irecv.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]
上面我們介紹了 mpi4py 中非重復(fù)非阻塞緩沖通信模式夫否,在下一篇中我們將介紹非重復(fù)非阻塞就緒通信。