-
-
Save MilesCranmer/771849e66623d0e6ea61a03dba4c16ad to your computer and use it in GitHub Desktop.
Passing multidimensional numpy arrays to C using cffi.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment