亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python中mpi4py的所有基礎使用案例詳解

 更新時間:2022年08月18日 15:34:48   作者:看那片云  
這篇文章主要介紹了python中mpi4py的所有基礎使用,本文通過10個案例給大家詳細講解,結合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

python中mpi4py的基礎使用

大多數(shù) MPI 程序都可以使用命令 mpiexec 運行。在實踐中,運行 Python 程序如下所示:

$ mpiexec -n 4 python script.py

案例1:測試comm.send 和comm.recv函數(shù),代碼如下

from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if rank == 0:
? ? data = {'a': 7, 'b': 3.14}
? ? comm.send(data, dest=1, tag=11)
elif rank == 1:
? ? data = comm.recv(source=0, tag=11)

rank代表進程編號,其總數(shù)是mpiexec -n中的n的個數(shù),最大的n受到電腦cpu內核數(shù)的限制
dest代表發(fā)送的目標,tag是一個標志位可以忽略,source為數(shù)據(jù)來源rank標志

案例2:具有非阻塞通訊的python對象

from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if rank == 0:
? ? data = {'a': 7, 'b': 3.14}
? ? req = comm.isend(data, dest=1, tag=11)
? ? req.wait()
elif rank == 1:
? ? req = comm.irecv(source=0, tag=11)
? ? data = req.wait()

案例3: 快速發(fā)送實例

這里的Send和Recv都是大寫,用于numpy數(shù)據(jù)的傳輸

from mpi4py import MPI
import numpy

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

# passing MPI datatypes explicitly
if rank == 0:
? ? data = numpy.arange(1000, dtype='i')
? ? comm.Send([data, MPI.INT], dest=1, tag=77)
elif rank == 1:
? ? data = numpy.empty(1000, dtype='i')
? ? comm.Recv([data, MPI.INT], source=0, tag=77)

# automatic MPI datatype discovery
if rank == 0:
? ? data = numpy.arange(100, dtype=numpy.float64)
? ? comm.Send(data, dest=1, tag=13)
elif rank == 1:
? ? data = numpy.empty(100, dtype=numpy.float64)
? ? comm.Recv(data, source=0, tag=13)

案例4:集體通訊,廣播機制

廣播機制就是將當前root=0端口下的所有信息發(fā)送到任何一個進程

from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if rank == 0:
? ? data = {'key1' : [7, 2.72, 2+3j],
? ? ? ? ? ? 'key2' : ( 'abc', 'xyz')}
else:
? ? data = None
data = comm.bcast(data, root=0)

案例5:scatter,將root=0下的數(shù)據(jù)一次分發(fā)到各個rank下

from mpi4py import MPI

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

if rank == 0:
? ? data = [(i+1)**2 for i in range(size)]
else:
? ? data = None
data = comm.scatter(data, root=0)
assert data == (rank+1)**2

案例6:gather,將所有rank下的數(shù)據(jù)收集到root下

from mpi4py import MPI

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

data = (rank+1)**2
data = comm.gather(data, root=0)
if rank == 0:
? ? for i in range(size):
? ? ? ? assert data[i] == (i+1)**2
else:
? ? assert data is None

案例7,numpy的廣播機制

與之前一樣都是大寫

from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if rank == 0:
? ? data = np.arange(100, dtype='i')
else:
? ? data = np.empty(100, dtype='i')
comm.Bcast(data, root=0)
for i in range(100):
? ? assert data[i] == i

案例8:numpy的Scatter機制

from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

sendbuf = None
if rank == 0:
? ? sendbuf = np.empty([size, 100], dtype='i')
? ? sendbuf.T[:,:] = range(size)
recvbuf = np.empty(100, dtype='i')
comm.Scatter(sendbuf, recvbuf, root=0)
assert np.allclose(recvbuf, rank)

案例9:numpy的Gather機制

from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

sendbuf = np.zeros(100, dtype='i') + rank
recvbuf = None
if rank == 0:
? ? recvbuf = np.empty([size, 100], dtype='i')
comm.Gather(sendbuf, recvbuf, root=0)
if rank == 0:
? ? for i in range(size):
? ? ? ? assert np.allclose(recvbuf[i,:], i)

案例10 :allgather機制

allgather就是 scatter 加上廣播機制。
rank0 = a
rank1 = b
rank2 = c
allgather后結果為
rank0 = a,b,c
rank1 = a,b,c
rank2 = a,b,c

from mpi4py import MPI
import numpy

def matvec(comm, A, x):
? ? m = A.shape[0] # local rows
? ? p = comm.Get_size()
? ? xg = numpy.zeros(m*p, dtype='d')
? ? comm.Allgather([x, ?MPI.DOUBLE],
? ? ? ? ? ? ? ? ? ?[xg, MPI.DOUBLE])
? ? y = numpy.dot(A, xg)
? ? return y

到此這篇關于一文讀懂python中mpi4py的所有基礎使用的文章就介紹到這了,更多相關python mpi4py使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論