Skip to content

Instantly share code, notes, and snippets.

@arjones6
Created May 7, 2013 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arjones6/5533938 to your computer and use it in GitHub Desktop.
Save arjones6/5533938 to your computer and use it in GitHub Desktop.
Passing multidimensional numpy arrays to C using cffi.
import numpy
from cffi import FFI
ffi = FFI()
ffi.cdef("""
void single_test(double *x, int n);
void multi_test(double **x, int n, int m);
""")
C = ffi.dlopen("./simple.so")
def single_test(x):
C.single_test(ffi.cast("double *", x.ctypes.data), len(x))
def multi_test_a(x):
ap = ffi.new("double* [%d]" % (len(x)))
for i in range(len(x)):
ap[i] = ffi.cast("double *", x[i].ctypes.data)
C.multi_test(ap, len(x), len(x[0]))
def multi_test_b(x):
dsize = ffi.sizeof("double")
ap = ffi.new("double* [%d]" % (x.shape[0]))
ptr = ffi.cast("double *", x.ctypes.data)
for i in range(x.shape[0]):
ap[i] = ptr + i*x.shape[1]
C.multi_test(ap, x.shape[0], x.shape[1])
x = numpy.linspace(1, 10, 10).astype('float64')
print "Before single", x
single_test(x)
print "After single", x
multi_array = [[1.1, 2.2, 1.3, 4.4, 5.5],
[5.0, 3.0, 2.0, 1.0, 3],
[6.0, 1.0, -3.2, -1, 2],
[0.0, 1.0, 1.0, 2.0, 1]]
x = [numpy.array(v, dtype='float64') for v in multi_array]
print "Before multi_a", x
multi_test_a(x)
print "After multi_a", x
x = numpy.array(multi_array, dtype='float64')
print "Before multi_b", x, x.shape
multi_test_b(x)
print "After multi_b", x
@the-ntq
Copy link

the-ntq commented Oct 24, 2023

This saved my day! Thanks!

@antonina2112
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment