問題描述
在多處理調(diào)用的函數(shù)中,numpy ndarray 函數(shù)的范圍是否不同?這是一個(gè)例子:
Does the scope of a numpy ndarray function differently within a function called by multiprocessing? Here is an example:
使用 python 的多處理模塊我正在調(diào)用這樣的函數(shù):
Using python's multiprocessing module I am calling a function like so:
for core in range(cores):
#target could be f() or g()
proc = mp.Process(target=f, args=(core))
jobs.append(proc)
for job in jobs:
job.start()
for job in jobs:
job.join()
def f(core):
x = 0
x += random.randint(0,10)
print x
def g(core):
#Assume an array with 4 columns and n rows
local = np.copy(globalshared_array[:,core])
shuffled = np.random.permutation(local)
調(diào)用f(core)
,x
變量是進(jìn)程本地的,即.它按預(yù)期打印一個(gè)不同的隨機(jī)整數(shù).這些從不超過 10,表明 x=0
在每個(gè)進(jìn)程中.對(duì)嗎?
Calling f(core)
, the x
variable is local to the process, ie. it prints a different, random integer as expected. These never exceed 10, indicating that x=0
in each process. Is that correct?
調(diào)用 g(core)
并排列數(shù)組的副本會(huì)返回 4 個(gè)完全相同的混洗"數(shù)組.這似乎表明工作副本不是子進(jìn)程的本地.那是對(duì)的嗎?如果是這樣,除了使用共享內(nèi)存空間之外,當(dāng)需要從共享內(nèi)存空間填充時(shí),是否可以將 ndarray 放在子進(jìn)程的本地?
Calling g(core)
and permuting a copy of the array returns 4 identically 'shuffled' arrays. This seems to indicate that the working copy is not local the child process. Is that correct? If so, other than using sharedmemory space, is it possible to have an ndarray be local to the child process when it needs to be filled from shared memory space?
更改 g(core)
以添加隨機(jī)整數(shù)似乎具有預(yù)期的效果.數(shù)組顯示不同的值.permutation
中一定發(fā)生了一些事情,隨機(jī)排列列(每個(gè)子進(jìn)程的本地)相同...想法?
Altering g(core)
to add a random integer appears to have the desired effect. The array's show a different value. Something must be occurring in permutation
that is randomly ordering the columns (local to each child process) the same...ideas?
def g(core):
#Assume an array with 4 columns and n rows
local = np.copy(globalshared_array[:,core])
local += random.randint(0,10)
編輯二:np.random.shuffle
也表現(xiàn)出相同的行為.數(shù)組的內(nèi)容正在洗牌,但在每個(gè)核心上都洗牌到相同的值.
EDIT II:
np.random.shuffle
also exhibits the same behavior. The contents of the array are shuffling, but are shuffling to the same value on each core.
推薦答案
調(diào)用 g(core) 并排列數(shù)組的副本會(huì)返回 4 個(gè)完全相同的混洗"數(shù)組.這似乎表明工作副本不是子進(jìn)程的本地.
Calling g(core) and permuting a copy of the array returns 4 identically 'shuffled' arrays. This seems to indicate that the working copy is not local the child process.
這可能表明隨機(jī)數(shù)生成器在每個(gè)子進(jìn)程中的初始化相同,產(chǎn)生相同的序列.您需要為每個(gè)孩子的生成器播種(也許將孩子的進(jìn)程 id 混入其中).
What it likely indicates is that the random number generator is initialized identically in each child process, producing the same sequence. You need to seed each child's generator (perhaps throwing the child's process id into the mix).
這篇關(guān)于Python 多處理 Numpy 隨機(jī)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!