在每个进程中使用具有不同随机种子的python多重处理 [英] Using python multiprocessing with different random seed for each process
问题描述
我希望并行运行几个模拟实例,但是每个模拟都有其自己的独立数据集.
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
其中sim
,adatom1
和lattice
是传递给函数run
的对象,该函数启动模拟.
where sim
, adatom1
and lattice
are objects passed to the function run
which initiates the simulation.
但是,我最近发现我同时运行的每个批次(即,每个ncpus
都在模拟运行的总nrun
中运行)给出了完全相同的结果.
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?
推荐答案
我以为我会添加一个实际答案以使他人清楚.
Just thought I would add an actual answer to make it clear for others.
在aix 中引用答案问题:
发生的事情是,在Unix上,每个工作进程都继承了相同的进程 来自父进程的随机数生成器的状态.这是 为什么它们会生成相同的伪随机序列.
What happens is that on Unix every worker process inherits the same state of the random number generator from the parent process. This is why they generate identical pseudo-random sequences.
使用 random.seed()方法(或等效的scipy/numpy)正确设置种子.另请参见此numpy线程.
Use the random.seed() method (or the scipy/numpy equivalent) to set the seed properly. See also this numpy thread.
这篇关于在每个进程中使用具有不同随机种子的python多重处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!