If you view your float** as a pointer to rows, there's no need to copy
the rows themselves, just point to the original data in the numpy
array. E.g. (untested)
cdef float** npy2c_float2d(np.ndarray[float, ndim=2, mode=c] a):
cdef float** a_c = malloc(a.shape[0] * sizeof(float*))
for k in range(a.shape[0]):
a_c[k] = &a[k, 0]
return a_c
will do the conversion without copying any data. (The caller is
responsible for freeing the C array.) To return the result, do
something like
def mul(a, b):
# any checks you need to do
try:
res = np.ndarray((N, N), dtype=float)
cdef float** a_c = npy2c_float2d(a)
cdef float** b_c = npy2c_float2d(b)
cdef float** res_c = npy2c_float2d(res)
matmul(a_c, b_c, res_c, N)
return res
finally:
free(a_c)
free(b_c)
free(res_c)
- Robert
On Thu, Sep 8, 2011 at 5:56 AM, Wonjun, Choi <wonjun...@gmail.com> wrote:If you view your float** as a pointer to rows, there's no need to copy
> Hi
>
> I have checked example which is wrapping c by using numpy.
> there is two converting function( function for converting numpy to c,
> function for converting c to numpy)
> and they both use malloc function.
> but if I do not use memory copy, how can I make the code?
the rows themselves, just point to the original data in the numpy
array. E.g. (untested)
cdef float** a_c = malloc(a.shape[0] * sizeof(float*))
for k in range(a.shape[0]):
a_c[k] = &a[k, 0]
return a_c
will do the conversion without copying any data. (The caller is
responsible for freeing the C array.) To return the result, do
something like
def mul(a, b):
# any checks you need to do
try:
res = np.ndarray((N, N), dtype=float)
cdef float** a_c = npy2c_float2d(a)
cdef float** b_c = npy2c_float2d(b)
cdef float** res_c = npy2c_float2d(res)
matmul(a_c, b_c, res_c, N)
return res
finally:
free(a_c)free(res_c)
free(b_c)
- Robert
> ----------------------------------------------------------------------------------
Maybe. Maybe your NumPy arrays are already in fortran contiguous
order. Or maybe your fortran libraries can handle C-style arrays. Or
maybe it doesn't matter because the product of the transpose is the
transpose of the reverse product.
You didn't cimport or declare "free."
- Robert