天天看點

Parallel Computing in Python using mpi4py

Parallel Computing in Python using mpi4py

Parallel Computing in Python using mpi4py
Parallel Computing in Python using mpi4py

感覺效果比較好的就是下面的對通信的集合操作的圖解,剛一看比較懵,細一看發現還比較形象。

Parallel Computing in Python using mpi4py
Parallel Computing in Python using mpi4py
Parallel Computing in Python using mpi4py
Parallel Computing in Python using mpi4py
Parallel Computing in Python using mpi4py
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)      

繼續閱讀