python中mpi4py的所有基础使用案例详解

Thadea ·
更新时间:2024-09-21
· 1181 次阅读

python中mpi4py的基础使用

大多数 MPI 程序都可以使用命令 mpiexec 运行。在实践中,运行 Python 程序如下所示:

$ mpiexec -n 4 python script.py

案例1:测试comm.send 和comm.recv函数,代码如下

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代表进程编号,其总数是mpiexec -n中的n的个数,最大的n受到电脑cpu内核数的限制
dest代表发送的目标,tag是一个标志位可以忽略,source为数据来源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: 快速发送实例

这里的Send和Recv都是大写,用于numpy数据的传输

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端口下的所有信息发送到任何一个进程

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下的数据一次分发到各个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下的数据收集到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使用内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



mpi Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号