Created
April 23, 2014 14:45
-
-
Save sseemayer/11218162 to your computer and use it in GitHub Desktop.
This file contains 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
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 |
This file contains 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
#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; | |
} |
This file contains 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
typedef struct mystruct_t { | |
int n; | |
double *x; | |
} mystruct_t; | |
double myfunc(mystruct_t* data); |
This file contains 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
#!/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