問題描述
我希望并行運行多個模擬實例,但每個模擬都有自己獨立的數據集.
I wish to run several instances of a simulation in parallel, but with each simulation having its own independent data set.
目前我實現如下:
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 的答案in this問題:
Quoting the answer from aix in this question:
發生的情況是,在 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 多處理的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!