【问题标题】:Arrays allocated at same address Cython + Numpy在同一地址分配的数组 Cython + Numpy
【发布时间】:2018-09-05 13:29:53
【问题描述】:

我在使用 numpy + cython 时遇到了一些有趣的内存行为,同时尝试从 numpy 数组作为 C 数组获取数据,以便在无 GIL 函数中使用。我已经查看了 cython 和 numpy 的数组 API,但我没有找到任何解释。所以考虑以下代码行:

cdef np.float32_t *a1 = <np.float32_t *>np.PyArray_DATA(np.empty(2, dtype="float32"))
print "{0:x}".format(<unsigned int>a1)
cdef np.float32_t *a2 = <np.float32_t *>np.PyArray_DATA(np.empty(2, dtype="float32"))
print "{0:x}".format(<unsigned int>a2)[]

我使用 numpy 的空函数分配了两个 numpy 数组,并希望为每个数组检索指向数据缓冲区的指针。您会期望这两个指针指向堆上的两个不同内存地址,可能间隔 2*4 字节。但是不,我得到指向相同内存地址的指针,例如

>>>96a7aec0
>>>96a7aec0

怎么会?我设法通过在 PyArray_DATA 调用之外声明我的 numpy 数组来解决这个问题,在这种情况下,我得到了我所期望的。

我能想到的唯一解释是,我没有在 PyArray_DATA 函数的范围之外创建任何 Python 对象,并且调用此函数不会增加 Python 的引用计数。因此 GC 会立即回收这个内存空间,并且下一个数组被分配到现在空闲的前一个内存地址。比我更精通 cython 的人能否证实这一点或给出其他解释?

【问题讨论】:

  • 你创建了两个临时的numpy数组,它们恰好在同一个地址。
  • 为什么称它们为临时 numpy 数组?它是否与我的解释有关,没有为它们保留 python 参考,因此它们是“临时的”。
  • 是的。它们会立即被垃圾收集。 a1a2 也是悬空指针
  • 好的,谢谢你的回答。

标签: python arrays numpy memory cython


【解决方案1】:

您创建了两个临时 numpy 数组,它们恰好位于同一地址。由于没有为它们保留 python 引用,它们会立即被垃圾回收,a1a2 也成为悬空指针。

如果为他们保留引用,他们的地址不能相同,例如:

cdef int[:] a = np.arange(10)  # A memoryview will keep the numpy array from GC.
cdef int[:] b = np.arange(10)
cdef int* a_ptr = &a[0]
cdef int* b_ptr = &b[0]
print(<size_t>a_ptr)
print(<size_t>b_ptr)

在使用对象的基础数据时必须格外小心。如果使用不当,经常会遇到悬空指针。 例如:

void cfunc(const char*)
# Fortunately, this won't compile in cython. 
# Error: Storing unsafe C derivative of temporary Python reference
cdef const char* = ("won't" + " compile").encode()
cfunc(char)

正确的方法:

# make sure keep_me is alive before cfunc have finished with it.
cdef bytes keep_me = ("right" + "way").encode() 
cfunc(temp)
# Or for single use.
cfunc(("right" + "way").encode())

c++中的另一个例子std::string的成员c_str()

// The result of `+` will immediately destructed. cfunc got a  dangling pointer.
const char * s = (string("not") + string("good")).c_str();
cfunc(s); 

正确的方法:

// keep `keep_me` for later use.
string keep_me = string("right") + string("way"); 
cfunc(keep_me.c_str());
// Or, for single use.
cfunc((string("right") + string("way")).c_str())

参考std::string::c_str() and temporaries

【讨论】:

  • 您的 c++ 示例是错误的。只要 cfunc 不将指针保留在某处,您的第一次调用就不会产生悬空指针。你可能想写的是:const char* not_good = (string("not") + string("good")).c_str() // Produces a dangling pointer
猜你喜欢
  • 1970-01-01
  • 2014-07-12
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 2011-11-18
  • 2017-04-06
相关资源
最近更新 更多