Dag Sverre Seljebotn wrote:
> No, only the first line. You assign b[i] to point to memory in your
> contiguous array; which is already allocated.
>
> Anyway, you do the same in Cython:
>
> from stdlib cimport free, malloc
>
Thanks! It now works. I had it working before but just using a C
interface function to do what now is done in Cython:
def matmul1(np.ndarray[DTYPE_t, ndim=2] a):
cdef int N = a.shape[0]
cdef float **b
cdef float res
b = <float **> malloc(N*sizeof(float*))
for i in range(N):
b[i] = &(<float *>a.data)[i*N]
res = sum1(b,N)
free(b)
return res
Now, just another question... How do I do the reverse? Ie., convert from
**float to numpy? I looked in previous emails and I saw something using
np.memcpy. Adapting for 2d it would be something like this (assuming a
float **res):
cdef np.ndarray[DTYPE_t,ndim=2]result = np.zeros((N,N),dtype=DTYPE)
if data != NULL: np.memcpy(result.data,res,N*N*sizeof(float))
The problem is res is an array of pointers (so I guess this does not
work), but cython also complains that it can't convert float **res to
Python type. So it seems memcpy is expecting a Python type, which means
this example would never work...
I can always use brute force and do a couple of loops for setting
result[i,j]=res[i][j]. But I wonder if there is a more elegant way of
doing this.
Thanks again,
Tiago
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev