Cython Memoryviews — From Array of Structs?

China☆狼群 提交于 2020-01-01 09:45:10

问题


I'd like to quickly fill with as few copies as possible a long array of structs that I'm receiving incrementally from C.

If my struct is only primary data types, like the following:

cdef packed struct oh_hi:
    int lucky
    char unlucky

Then the following works fine:

  DEF MAXPOWER = 1000000
  cdef oh_hi * hi2u = <oh_hi *>malloc(sizeof(oh_hi)*MAXPOWER)
  cdef oh_hi [:] hi2me = <oh_hi[:MAXPOWER]> hi2u

But once I change my struct to hold a character array:

cdef packed struct oh_hi:
    int lucky
    char unlucky[10]

The previous memoryview casting compiles but when run gives a:

  ValueError: Expected 1 dimension(s), got 1

Is there an easy way to do this in Cython? I'm aware that I could create a structured array, but afaik, that wouldn't let me assign the C structs straight into it.


回答1:


Actually, just building a structured numpy array and then a memoryview works just fine.

cdef np.ndarray hi2u = np.ndarray((MAXPOWER,),dtype=[('lucky','i4'),('unlucky','a10')])
cdef oh_hi [:] hi2me = hi2u

The performance of this seems quite good and this saves a later copy if you need the data back in python. As per usual, the numpy version is pretty good. =p



来源:https://stackoverflow.com/questions/17239091/cython-memoryviews-from-array-of-structs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!