【发布时间】:2015-02-19 21:28:51
【问题描述】:
我试图了解 IPython 并行的 numpy 数组的非复制发送/接收会发生什么。我知道消息的非复制接收是只读的,但这让我期望我收到的 numpy 数组就像一个视图,指向原始数组(至少在共享内存机器上)。然后我希望如果修改 numpy 数组 其中一个计算节点,我在笔记本中对该数组的视图将被更新, 但是,情况似乎并非如此。为了演示让我感到困惑的地方,代码
from IPython.parallel import Client
dv = Client()[:]
with dv.sync_imports():
import numpy
dv.execute('a = numpy.zeros(2)')
print dv['a']
mya = dv['a'][1] # my copy of the array in the notebook
# mya[0] = -1 # can't assign to mya because it is read-only
dv.execute('a[1] = 1') # update the arrays on the compute nodes
print mya # value of mya is not updated
print dv['a'] # even though the arrays are updated on the compute nodes
有输出
importing numpy on engine(s)
[array([ 0., 0.]), array([ 0., 0.]), array([ 0., 0.]), array([ 0., 0.])]
[ 0. 0.]
[array([ 0., 1.]), array([ 0., 1.]), array([ 0., 1.]), array([ 0., 1.])]
我认为非复制发送的目的是通过让数组的每个视图都指向同一块内存来节省内存,但事实并非如此 似乎是这里的情况。幕后发生了什么,我对非复制发送的使用有何误解?
我上面的主要问题的上下文是共享内存环境(我的笔记本电脑), 但我有一个可能相关的问题是 numpy 数组的非复制发送如何工作以及它们在分布式内存情况下的用途是什么,其中计算节点 是不同的物理机器,您必须(我假设)通过网络发送 numpy 数组(有效地在接收机器上制作副本),因此查看原始内存位置是没有意义的。
【问题讨论】:
标签: python numpy ipython ipython-parallel