我做了一些修改以简化您的示例。串行版本在我的Mac上运行大约18秒,而带有4个引擎的并行版本运行大约一半的时间。鉴于任务持续时间不均,这似乎是合理的。
按照之前的设置方式,引擎中出现了错误,因此快速返回。看来通过字典传递类是不够的。相反,代码现在导入定义每个引擎上的类的模块。
请注意,我只是黑了系统。此示例的路径
,但在生产环境中,您可能会适当地处理此问题。
我认为你不想在循环中“等待”。此外,async_map()方法似乎比async_apply()更方便。
要运行此操作,请创建一个目录,将以下代码复制到该目录中名为“photon.py”的文件中,并创建一个空“
初始化
.py”。修改代码中插入sys.path的行,以引用新目录。更改目录并运行“python photon.py”:
# photon.py
import ipyparallel
import numpy as np
from numpy.random import random as rand
import time
NPHOTONS = 100000 # Nb photons
PI = np.pi
EPS = 1.e-6
L = 100. # scattering layer thickness
class Photon():
mut = 0.02
k = [0,0,1]
def __init__(self,ko,pos):
Photon.k = ko
self.x = pos[0]
self.y = pos[1]
self.z = pos[2]
def move(self):
ksi = rand(1)
s = -np.log(1-ksi)/Photon.mut
self.x = self.x + s*Photon.k[0]
self.y = self.y + s*Photon.k[1]
self.z = self.z + s*Photon.k[2]
zPos = self.z
return zPos
def exittop(self):
newZpos = 0
def exitbase(self):
newZpos = 0
def HG(self,g):
rand_teta = rand(1)
costeta = 0.5*(1+g**2-((1-g**2)/(1-g + 2.*g*rand_teta))**2)/g
return costeta
def scatter(self):
# calculate new angle of scattering
phi = 2*PI*rand(1)
costeta = self.HG(0.85)
sinteta = (1-costeta**2)**0.5
sinphi = np.sin(phi)
cosphi = np.cos(phi)
temp = (1-Photon.k[2]**2)**0.5
if np.abs(temp) > EPS:
mux = sinteta*(Photon.k[0]*Photon.k[2]*cosphi-Photon.k[1]*sinphi)/temp + Photon.k[0]*costeta
muy = sinteta*(Photon.k[1]*Photon.k[2]*cosphi+Photon.k[0]*sinphi)/temp + Photon.k[1]*costeta
muz = -sinteta*cosphi*temp + Photon.k[2]*costeta
else:
mux = sinteta*cosphi
muy = sinteta*sinphi
if Photon.k[2]>=0:
muz = costeta
else:
muz = -costeta
# update the new direction of the photon
Photon.k[0] = mux
Photon.k[1] = muy
Photon.k[2] = muz
class RunPhotonPackage():
def __init__(self,L,NPHOTONS):
self.L = L
self.NPHOTONS = NPHOTONS
def RunPhoton(self):
Dist_Pos = np.zeros((3,self.NPHOTONS))
# loop over number of photon
for i in range(self.NPHOTONS):
# inititate initial photon direction
k_init = [0,0,1]
k_init_norm = k_init/np.linalg.norm(k_init) # initial photon direction.
# initiate new photon with initial direction
pos_init = [0,0,0]
newPhoton = Photon(k_init_norm,pos_init)
newZpos = 0.
# while the photon is still in the layer, move it and scatter it
while ((newZpos >= 0.) and (newZpos <= self.L)):
newZpos = newPhoton.move()
newscatter = newPhoton.scatter()
Dist_Pos[0,i] = newPhoton.x
Dist_Pos[1,i] = newPhoton.y
Dist_Pos[2,i] = newPhoton.z
return Dist_Pos
def RunPhoton(L):
print('L={0}'.format(L))
return RunPhotonPackage(L, 10000).RunPhoton()
def serialTest(values):
print "Running serially..."
tic = time.time()
results = map(RunPhoton, values)
print results
toc = time.time()
print('sec Elapsed: {0}s'.format(toc-tic))
def parallelTest(values):
print "Running in parallel..."
client = ipyparallel.Client()
view = client[:]
view.execute('import sys')
# CHANGE THIS PATH TO REFER TO WHEREVER YOU PUT THIS CODE
view.execute('sys.path.insert(0, "/Users/rjp/ipp")')
view.execute('from photon import *')
tic = time.time()
asyncResults = view.map_async(RunPhoton, values)
print asyncResults.get()
toc = time.time()
print('sec Elapsed: {0}s'.format(toc-tic))
if __name__ == "__main__":
values = np.arange(10, 100, 10)
serialTest(values)
parallelTest(values)