I need to work hard using numpy 2D arrays of various sizes, and I would like to offload these calculations in cython. The idea is that my numpy 2D arrays will be passed from python to cython, where it will be converted to a c-array or memory representation and used in a cascade of other level c functions to perform the calculations.
After some profiling, I ruled out the use of numpy arrays in cython due to some serious python overhead. Using memory representations was much faster and fairly easy to use, but I suspect that I can compress even more acceleration from using c-arrays.
Here is my question: how can I declare a 2D-c array in a tseton without predetermining its sizes with the set values? For example, I can create a c-array from numpy as follows:
narr = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]], dtype=np.dtype("i"))
cdef int c_arr[3][4]:
for i in range(3):
for j in range(4):
c_arr[i][j] = narr[i][j]
and then pass it to the function:
cdef void somefunction(int c_Arr[3][4]):
...
But this means that I have a fixed array value, which in my case will be useless. So I tried something like this:
narr = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]], dtype=np.dtype("i"))
cdef int a = np.shape(narr)[0]
cdef int b = np.shape(narr)[1]
cdef int c_arr[a][b]: # INCORRECT - EXAMPLE ONLY
for i in range(a):
for j in range(b):
c_arr[i][j] = narr[i][j]
with the intention of passing it to such a function:
cdef void somefunction(int a, int b, int c_Arr[a][b]):
...
But this will not work, and the compilation failed with the error "Not allowed in constant expression." I suspect I need to do this with malloc / free somehow? I examined this problem ( How to declare a 2D list in Cython ), but it does not give an answer to my problem.
UPDATE:
, , c-, , cython , cython:
@Veedrac !