[英]Is strided numpy array shared across multiple processes?
Let`s assume that we created a numpy array with views on another array using stride tricks:假设我们使用跨步技巧创建了一个 numpy 数组,其中包含另一个数组的视图:
import numpy as np
from numpy.lib import stride_tricks
x = np.arange(20).reshape([4, 5])
arr = stride_tricks.as_strided(x, shape=(3, 2, 5),strides=(20, 20, 4))
We can confirm that this new array is indeed a view:我们可以确认这个新数组确实是一个视图:
assert not arr.flags['OWNDATA']
# True
Question:问题:
If I pass arr
as an argument into multiprocessing.Process()
will arr
be copied into each process?如果我将arr
作为参数传递给multiprocessing.Process()
将arr
复制到每个进程中吗? Will x
be copied? x
会被复制吗? Please explain why.请解释原因。
If the sharing is via pickle
serialization, then clearly the view
(how ever generated) will produce a copy:如果共享是通过pickle
序列化进行的,那么显然view
(如何生成)将生成一个副本:
In [298]: x = np.arange(10)
In [299]: y = x.reshape(2,5)
In [300]: import pickle
In [301]: B = pickle.dumps(y)
In [302]: Y = pickle.loads(B)
In [303]: Y
Out[303]:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
In [304]: y.__array_interface__['data']
Out[304]: (43176224, False)
In [305]: x.__array_interface__['data']
Out[305]: (43176224, False)
In [306]: Y.__array_interface__['data']
Out[306]: (59035584, False)
For what it's worth the pickle
of a numpy array is actually performed by np.save
.值得一提的是pickle
数组实际上是由np.save
执行的。
Passing x
and making the view in each process might be better.传递x
并在每个进程中创建视图可能会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.