使用 python 多重处理,每个进程具有不同的随机种子
我希望并行运行多个模拟实例,但每个模拟都有自己独立的数据集。
目前我的实现如下:
P = mp.Pool(ncpus) # Generate pool of workers
for j in range(nrun): # Generate processes
sim = MDF.Simulation(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat,savetemp)
lattice = MDF.Lattice(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat, kb, ks, kbs, a, p, q, massL, randinit, initvel, parangle,scaletemp,savetemp)
adatom1 = MDF.Adatom(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat, ra, massa, amorse, bmorse, r0, z0, name, lattice, samplerate,savetemp)
P.apply_async(run,(j,sim,lattice,adatom1),callback=After) # run simulation and ISF analysis in each process
P.close()
P.join() # start processes
其中 sim
、adatom1
和 lattice
是传递给启动函数 run
的对象模拟。
然而,我最近发现,我同时运行的每个批次(即,每个 ncpus
都超出了总 nrun
模拟运行)给出了完全相同的结果。
这里有人可以告诉我如何解决这个问题吗?
I wish to run several instances of a simulation in parallel, but with each simulation having its own independent data set.
Currently I implement this as follows:
P = mp.Pool(ncpus) # Generate pool of workers
for j in range(nrun): # Generate processes
sim = MDF.Simulation(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat,savetemp)
lattice = MDF.Lattice(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat, kb, ks, kbs, a, p, q, massL, randinit, initvel, parangle,scaletemp,savetemp)
adatom1 = MDF.Adatom(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat, ra, massa, amorse, bmorse, r0, z0, name, lattice, samplerate,savetemp)
P.apply_async(run,(j,sim,lattice,adatom1),callback=After) # run simulation and ISF analysis in each process
P.close()
P.join() # start processes
where sim
, adatom1
and lattice
are objects passed to the function run
which initiates the simulation.
However, I recently found out that each batch I run simultaneously (that is, each ncpus
runs out of the total nrun
of simulations runs) gives the exact same results.
Can someone here enlighten how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是想我会添加一个实际的答案以使其他人清楚。
引用aix 在此问题中的答案:
使用 random.seed() 方法(或 scipy/numpy 等效方法)设置正确播种。另请参阅此 numpy 线程。
Just thought I would add an actual answer to make it clear for others.
Quoting the answer from aix in this question:
Use the random.seed() method (or the scipy/numpy equivalent) to set the seed properly. See also this numpy thread.
这是一个未解决的问题。尝试为每个进程生成唯一的种子。您可以将以下代码添加到函数的开头来解决该问题。
This is an unsolved problem. Try to generate a unique seed for each process. You can add below code to beginning of your function to overcome the issue.
该问题的解决方案是在函数
run
中使用scipy.random.seed()
,它为调用的随机函数分配一个新的种子运行
。类似的问题(我从中获得了解决方案)可以在 multiprocessing.Pool 似乎可以在 Windows 中运行,但不能在 ubuntu 中运行?
A solution for the problem was to use
scipy.random.seed()
in the functionrun
which assign a new seed for random functions called fromrun
.A similar problem (from which i obtained the solution) can be found in multiprocessing.Pool seems to work in Windows but not in ubuntu?