Skip to content

Instantly share code, notes, and snippets.

@sseemayer
Created April 23, 2014 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sseemayer/11218162 to your computer and use it in GitHub Desktop.
Save sseemayer/11218162 to your computer and use it in GitHub Desktop.
CC = gcc
CFLAGS = -g -Wall -fPIC -lm -std=gnu99 -O3 -march=native -DCONJUGRAD_FLOAT=64
all: libmyfunc.so
m.PHONY : clean
libmyfunc.so: myfunc.o
gcc -shared -Wl,-soname,$@ -o $@ $^
%.o: %.c
$(CC) -c $(CFLAGS) $<
clean:
rm -vf libmyfunc.so *.o
#include "myfunc.h"
double myfunc(mystruct_t* data) {
double result = 0.;
for(int i = 0; i < data->n; i++) {
result += data->x[i];
}
return result;
}
typedef struct mystruct_t {
int n;
double *x;
} mystruct_t;
double myfunc(mystruct_t* data);
#!/usr/bin/env python
import numpy as np
import numpy.ctypeslib as npct
import ctypes
import os
array_1d_double = npct.ndpointer(dtype=np.double, ndim=1, flags='C_CONTIGUOUS')
class MyStruct(ctypes.Structure):
_fields_ = [
('n', ctypes.c_int16),
('x', array_1d_double)
]
libmyfunc = npct.load_library('libmyfunc', os.path.dirname(__file__))
libmyfunc.myfunc.restype = ctypes.c_double
libmyfunc.myfunc.argtypes = [
ctypes.POINTER(MyStruct)
]
x = np.array([1.0, 2.0, 3.0, 4.0])
mystruct = MyStruct()
mystruct.n = len(x)
mystruct.x = x
res = libmyfunc.myfunc(mystruct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment