【转载】 Parallel Computing in Python using mpi4py

原地址:

https://research.computing.yale.edu/sites/default/files/files/mpi4py.pdf

 

 

 

 

 

 

 

 

 

 

=============================================

 

 

 

说明:

本文只摘录了部分因为比较重点的部分内容,如要查阅全部请关注原地址:

https://research.computing.yale.edu/sites/default/files/files/mpi4py.pdf

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

感觉效果比较好的就是下面的对通信的集合操作的图解,刚一看比较懵,细一看发现还比较形象。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

=====================================================

 

 

 

示例代码:

import numpy as np
from mpi4py import MPI


def rbind(comm, x):
    return np.vstack(comm.allgather(x))


comm = MPI.COMM_WORLD

x = np.arange(4, dtype=np.int) * comm.Get_rank()
a = rbind(comm, x)

print(a)

 

 

import numpy as np
from mpi4py import MPI


def rbind2(comm, x):
    size = comm.Get_size()
    m = np.zeros((size, len(x)), dtype=np.int)
    comm.Allgather([x, MPI.INT], [m, MPI.INT])
    return m


comm = MPI.COMM_WORLD

x = np.arange(4, dtype=np.int) * comm.Get_rank()
a = rbind2(comm, x)

print(a)

 

 

 

 

 

import numpy as np
import math
from mpi4py import MPI


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

x = range(20)
m = int(math.ceil(float(len(x)) / size))
x_chunk = x[rank*m:(rank+1)*m]
r_chunk = map(math.sqrt, x_chunk)
r = comm.allreduce(list(r_chunk))

if rank == 0:
    print(len(r), r)

 

posted on 2021-08-16 22:16  Angry_Panda  阅读(60)  评论(0编辑  收藏  举报

导航