MPI
MPI
百度百科
https://baike.baidu.com/item/MPI/15277241?fr=aladdin
6個MPI調用接口
mpi init()
初始化MPI執行環境,建立多個MPI進程之間的聯繫,爲後續通信做準備 [1] ;mpi finalize
結束MPI執行環境 [1] ;mpi comm rank
用來標識各個MPI進程的,給出調用該函數的進程的進程號,返回整型的錯誤值。兩個參數:MPI_Comm類型的通信域,標識參與計算的MPI進程組; &rank返回調用進程中的標識號 [1] ;mpi comm size
用來標識相應進程組中有多少個進程 [1] ;mpi send(buf,counter,datatype,dest,tag,comm)
: buf:發送緩衝區的起始地址,可以是數組或結構指針;count:非負整數,發送的數據個數;datatype:發送數據的數據類型;dest:整型,目的的進程號;tag:整型,消息標誌;comm:MPI進程組所在的通信域 [1]
含義:向通信域中的dest進程發送數據,數據存放在buf中,類型是datatype,個數是count,這個消息的標誌是tag,用以和本進程向同一目的進程發送的其它消息區別開來 [1] 。mpi recv(buf,count,datatype,source,tag,comm,status)
: source:整型,接收數據的來源,即發送數據進程的進程號; status:MPI_Status結構指針,返回狀態信息 [1] 。
點對點和集羣通信兩種方式
官網文檔
https://mpi4py.readthedocs.io/en/stable/mpi4py.run.html?highlight=get_size
from mpi4py import MPI
MPI.COMM_WORLD.Get_rank()
MPI.COMM_WORLD.Get_size()
rank
用來標識各個MPI進程的,給出調用該函數的進程的進程號,返回整型的錯誤值。兩個參數:MPI_Comm類型的通信域,標識參與計算的MPI進程組; &rank返回調用進程中的標識號
size
用來標識相應進程組中有多少個進程
test.py
from mpi4py import MPI
assert MPI.COMM_WORLD.Get_size() > 1
rank = MPI.COMM_WORLD.Get_rank()
size = MPI.COMM_WORLD.Get_size()
# import ipdb; ipdb.set_trace()
print("{0}, {1}".format(rank, size))
if rank == 0:
# 1/0
MPI.COMM_WORLD.send(None, dest=1, tag=42)
elif rank == 1:
MPI.COMM_WORLD.recv(source=0, tag=42)
mpiexec -n 5 python test.py
並行的進程數,這裏設爲5個
3, 5
1, 5
0, 5
2, 5
4, 5
mpirun vs mpiexec
# 並行的進程數,這裏設爲8個
mpirun -np 8 python -m name.py
mpiexec -n 8 python name.py
解釋如下:
mpiexec is defined in the MPI standard (well, the recent versions at least) and I refer you to those (your favourite search engine will find them for you) for details.
mpirun is a command implemented by many MPI implementations. It has never, however, been standardised and there have always been, often subtle, differences between implementations. For details see the documentation of the implementation(s) of your choice.
And yes, they are both used to launch MPI programs, these days mpiexec is generally preferable because it is standardised.
Reference:
[1]: https://mpi4py.readthedocs.io/en/stable/
[2]: https://www.cnblogs.com/zhbzz2007/p/5827059.html