網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
python中mpi4py的基礎(chǔ)使用
大多數(shù) MPI 程序都可以使用命令 mpiexec 運(yùn)行。在實(shí)踐中,運(yùn)行 Python 程序如下所示:
$ mpiexec -n 4 python script.py
案例1:測(cè)試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代表進(jìn)程編號(hào),其總數(shù)是mpiexec -n中的n的個(gè)數(shù),最大的n受到電腦cpu內(nèi)核數(shù)的限制
dest代表發(fā)送的目標(biāo),tag是一個(gè)標(biāo)志位可以忽略,source為數(shù)據(jù)來(lái)源rank標(biāo)志
案例2:具有非阻塞通訊的python對(duì)象
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ā)送實(shí)例
這里的Send和Recv都是大寫(xiě),用于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:集體通訊,廣播機(jī)制
廣播機(jī)制就是將當(dāng)前root=0端口下的所有信息發(fā)送到任何一個(gè)進(jìn)程
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ā)到各個(gè)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的廣播機(jī)制
與之前一樣都是大寫(xiě)
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機(jī)制
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機(jī)制
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機(jī)制
allgather就是 scatter 加上廣播機(jī)制。
rank0 = a
rank1 = b
rank2 = c
allgather后結(jié)果為
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
原文鏈接:https://blog.csdn.net/alsj123456/article/details/126382097
相關(guān)推薦
- 2022-07-25 Python?APScheduler?定時(shí)任務(wù)詳解_python
- 2022-03-16 C++中的Lambda函數(shù)詳解_C 語(yǔ)言
- 2022-02-24 golang:日期與時(shí)間戳相互轉(zhuǎn)換
- 2024-01-11 spring 事務(wù)控制 設(shè)置手動(dòng)回滾 TransactionAspectSupport.curren
- 2022-09-03 docker鏡像管理命令詳解_docker
- 2023-05-19 Python基于TensorFlow接口實(shí)現(xiàn)深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)回歸_python
- 2022-04-28 Python中自定義函方法與參數(shù)具有默認(rèn)值的函數(shù)_python
- 2022-04-25 ASP.NET?Core?Web資源打包與壓縮技術(shù)介紹_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支