問題描述
在多處理調用的函數中,numpy ndarray 函數的范圍是否不同?這是一個例子:
Does the scope of a numpy ndarray function differently within a function called by multiprocessing? Here is an example:
使用 python 的多處理模塊我正在調用這樣的函數:
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)
調用f(core)
,x
變量是進程本地的,即.它按預期打印一個不同的隨機整數.這些從不超過 10,表明 x=0
在每個進程中.對嗎?
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?
調用 g(core)
并排列數組的副本會返回 4 個完全相同的混洗"數組.這似乎表明工作副本不是子進程的本地.那是對的嗎?如果是這樣,除了使用共享內存空間之外,當需要從共享內存空間填充時,是否可以將 ndarray 放在子進程的本地?
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)
以添加隨機整數似乎具有預期的效果.數組顯示不同的值.permutation
中一定發生了一些事情,隨機排列列(每個子進程的本地)相同...想法?
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
也表現出相同的行為.數組的內容正在洗牌,但在每個核心上都洗牌到相同的值.
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.
推薦答案
調用 g(core) 并排列數組的副本會返回 4 個完全相同的混洗"數組.這似乎表明工作副本不是子進程的本地.
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.
這可能表明隨機數生成器在每個子進程中的初始化相同,產生相同的序列.您需要為每個孩子的生成器播種(也許將孩子的進程 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).
這篇關于Python 多處理 Numpy 隨機的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!