简体   繁体   中英

Convert pointer to pointer in C++ to Numpy Array in Cython

I am working on an algorithm that leverages Cython and C++ code to speed up a computation. Part of the computation involves keeping track of a 2D matrix, vecs that is D x D' dimensions (eg 1000 x 100). The algorithm parallelizes within Cython to set values per column. I am then interested in then obtaining the vecs values as a NumPy array in Python.

Modifying vecs in Cython

The pseudocode for setting each column of vecs is something like:

# this occurs in a Cython/C++ function
for icolumn in range(D'):
   for irow in range(D):
      vecs[irow, icolumn] = val

Data structure for vecs

To represent such a matrix, I am using a pointer of pointers of type npy_float32 (which I think is just numpy's float 32 type). I have a pointer to pointer array now that looks like this:

ctypedef np.npy_float32 DTYPE_t    
cdef DTYPE_t** vecs             # (D, D') array of vectors

Goal to obtain the vecs in NumPy Array at the Python Level

I am interested in converting this vecs variable into a numpy array. This is my attempt, but it doesn't work. I'm pretty novice at C++ and Cython.

numpy_vec_arr = np.asarray(<DTYPE_t[:,:]> proj_vecs_arr)

I'm not an expert in NumPy nor Cython, but I do know some about C++ and about interoperating C++ with other languages.

What problem are you trying to solve?

Answering that will prevent the XY problem, and might allow folks here to better help you.

Now, answering your original question. I see two ways to do this.

  1. Use an available constructor of the NumPy array to construct a NumPy array. This will clearly give you a NumPy array, problem solved.
  2. Create an object with the same in-memory structure as the NumPy array that Python expects, then convert that object to a NumPy array. This is tricky to do, bug prone, and depends on many implementation details. For those reasons, I would not suggest taking this approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM