Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython Memoryviews -- From Array of Structs?

Tags:

python

c

cython

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.

like image 498
radikalus Avatar asked Jun 21 '13 15:06

radikalus


1 Answers

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

like image 182
radikalus Avatar answered Nov 05 '22 18:11

radikalus