Migrating arrays from c to python

I am transferring a double-arrac from a c-function to a python-function. My code for this is:
C code:

double *compute(int size, const double a[])
{
    double* array;
    array = malloc(sizeof(double)*size);
    for (int i=0; i<size; i++)
    {
    array[i] = 3*a[i];
    }
    //printf("Array in compute-function is: \n[");
    //for(int i = 0; i < size; i++)
        //printf("%f, ", array[i]);
    //printf("]\n");
    return array;
}

gift-guard:

cdef class ArrayWrapper:
    cdef void* data_ptr
    cdef int size

    cdef set_data(self, int size, void* data_ptr):
        """ Set the data of the array
        This cannot be done in the constructor as it must recieve C-level
        arguments.
        Parameters:
        -----------
        size: int
            Length of the array.
        data_ptr: void*
            Pointer to the data            
        """
        self.data_ptr = data_ptr
        self.size = size

    def __array__(self):
        """ Here we use the __array__ method, that is called when numpy
            tries to get an array from the object."""
        cdef np.npy_intp shape[1]
        shape[0] = <np.npy_intp> self.size
        # Create a 1D array, of length 'size'
        ndarray = np.PyArray_SimpleNewFromData(1, shape,
                                               np.NPY_INT, self.data_ptr)
        return ndarray

    def __dealloc__(self):
        """ Frees the array. This is called by Python when all the
        references to the object are gone. """
        free(<void*>self.data_ptr)


def py_compute(int size, np.ndarray[np.double_t,ndim=1] a):
    """ Python binding of the 'compute' function in 'GNLSE_RHS.c' that does
        not copy the data allocated in C.
    """
    cdef double *array
    cdef np.ndarray ndarray
    # Call the C function
    array = compute(size, <double*> a.data)

    array_wrapper = ArrayWrapper()
    array_wrapper.set_data(size, <void*> array) 
    ndarray = np.array(array_wrapper, copy=False)
    # Assign our object to the 'base' of the ndarray object
    ndarray.base = <PyObject*> array_wrapper
    # Increment the reference count, as the above assignement was done in
    # C, and Python does not know that there is this additional reference
    Py_INCREF(array_wrapper)


    return ndarray

python code:

for i in xrange(10):
    x[i] = i;

a = cython_wrapper.py_compute(10, x)
print a

But my result

[         0          0          0 1074266112          0 1075314688          0 1075970048          0 1076363264]

instead of the expected

[  0.   3.   6.   9.  12.  15.  18.  21.  24.  27.]

Where is my mistake? I guess this has something to do with the problematic pointer wrapping, but I'm not sure.

+4
source share
1 answer

The error here is that in the line

ndarray = np.PyArray_SimpleNewFromData(1, shape,
                                      np.NPY_INT, self.data_ptr)

you point to numpy, which self.data_ptrpoints to an array of ints, not one of two elements.

You can fix your code by specifying the desired data type as follows:

ndarray = np.PyArray_SimpleNewFromData(1, shape,
                                      np.NPY_DOUBLE, self.data_ptr)

and it should work as expected.

, , np.ndarray, py_compute

def py_compute(np.ndarray[np.double_t,ndim=1] a):
    """ Python binding of the 'compute' function in 'GNLSE_RHS.c' that does
        not copy the data allocated in C.
    """
    cdef double *array
    cdef np.ndarray ndarray
    cdef size = a.shape[0]

    # Call the C function
    array = compute(size, &a[0])

    array_wrapper = ArrayWrapper()
    array_wrapper.set_data(size, <void*> array) 
    ndarray = np.array(array_wrapper, copy=False)
    # Assign our object to the 'base' of the ndarray object
    ndarray.base = <PyObject*> array_wrapper
    # Increment the reference count, as the above assignement was done in
    # C, and Python does not know that there is this additional reference
    Py_INCREF(array_wrapper)


    return ndarray
+1

Source: https://habr.com/ru/post/1615034/


All Articles