Creating a python c-module: passing double arrays to c functions. segmentation fault. swig

kim kmgrds at gmail.com
Mon Mar 31 14:52:11 EDT 2008


thanks a lot, sturlamolden, for the quick reply!

so i tried to use numpy and ctypes as you advised and i got it
working: with some little changes for my linux machine - i hope they
aren't the cause of the results:

to load it, i only got it going with LoadLibrary:
lib = numpy.ctypeslib.load_library("_timewarpsimple.so",".")
_timewarp = lib.timewarp

i replaced double by c_double:
array_pointer_t = ndpointer(dtype=c_double)

and i needed to define a restype, too:
_timewarp.restype = c_double

so, the strange errors with lots of stack traces are gone, but for
large lists i got the same problem: Segmentation fault. for example
for the following two lines:

list1,list2 = numpy.array([0.1 for i in  range(999)]),
numpy.array([0.7 for i in  range(1041)])
print timewarp(list1,list2)

So it is maybe more of a c problem, but still, if i copy the big
arrays into my c code, everything works out just fine
(x[]={0.1,0.1,... }...) so it may still have something to do with the
python-c interface and memory allocation or something like that.

below i put my new shorter python script, the c and the rest hasn't
changed.
what can i try now?

thanks again
kim


import numpy
import ctypes
from numpy.ctypeslib import ndpointer
from ctypes import c_int,c_double

lib = numpy.ctypeslib.load_library("_timewarpsimple.so",".")
_timewarp = lib.timewarp
#_timewarp = ctypes.cdll.timewarp.timewarp  # timewarp.dll
array_pointer_t = ndpointer(dtype=c_double)
_timewarp.argtypes = [array_pointer_t, c_int, array_pointer_t, c_int]
_timewarp.restype = c_double

def timewarp(x, y):
   lenx, leny = x.shape[0], y.shape[0]
   print lenx,leny
   return _timewarp(x, lenx, y, leny)

# testing:
list1,list2 = numpy.array([0.1 for i in  range(999)]),
numpy.array([0.7 for i in  range(1041)])
print timewarp(list1,list2)

for x in range(999,1111):
	for y in range(999,1111):
		list1,list2 = [0.1 for i in  range(x)], [0.9 for i in  range(y)]
		print len(list1),len(list2),len(list1)+len(list2)
		list1,list2 = numpy.array(list1), numpy.array(list2)
		print timewarp(list1,list2)



On Mar 30, 10:56 pm, sturlamolden <sturlamol... at yahoo.no> wrote:
> On 30 Mar, 22:21, kmg... at gmail.com wrote:
>
> > Hello everybody,
>
> > I'm building a python module to do some heavy computation in C (for
> > dynamic time warp distance computation).
>
> Why don't you just ctypes and NumPy arrays instead?
>
> # double timewarp(double x[], int lenx, double y[], int leny);
>
> import numpy
> import ctypes
> from numpy.ctypeslib import ndpointer
> from ctypes import c_int
>
> _timewarp = ctypes.cdll.timewarp.timewarp  # timewarp.dll
> array_pointer_t = ndpointer(dtype=double)
> _timewarp.argtypes = [array_pointer_t, c_int, array_pointer_t, c_int]
>
> def timewarp(x, y):
>    lenx, leny = x.shape[0], y.shape[0]
>    return _timewarp(x, lenx, y, leny)




More information about the Python-list mailing list