On 01/01/15 19:00, Yuxiang Wang wrote:

> Could anyone please give any thoughts to help?

Say you want to call "void foobar(int m, int n, double **x)"
from dummy.so or dummpy.dll with ctypes. Here is a fully worked
out example (no tested, but is will work unless I made a typo):


import numpy as np
from numpy.ctypeslib import ndpointer
import ctypes

__all__ = ['foobar']

_doublepp = ndpointer(dtype=np.uintp, ndim=1, order='c')

_dll = ctypes.CDLL('dummpy.so') # or dummpy.dll

_foobar = _dll.foobar
_foobar.argtypes = [ctypes.c_int, ctypes.c_int, _doublepp]
_foobar.restype = None

def foobar(x):
    assert(x.flags['C_CONTIGUOUS'])
    assert(x.ndim == 2)
    xpp = (x.__array_interface__['data'][0]
      + np.arange(x.shape[0])*x.strides[0]).astype(np.uintp)
    m = ctype.c_int(x.shape[0])
    n = ctype.c_int(x.shape[1])
    _foobar(m,n,xpp)



Sturla


_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to