我试图了解 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 数组(有效地在接收机器上制作副本),因此查看原始内存位置是没有意义的。