所以我试图使用cffi在pypy中快速访问c库。
我正在使用我的mac pro与命令行工具9.1
具体来说,我正在比较纯python优先级队列,heapq,cffi和项目的ctypes。
我从roman10的网站获得了代码,用于优先级队列的C实现。
我正在关注cffi的文档但是在尝试调用库内的函数时出现错误。
我有5个文件:.c / .h cheap.c
cheap.h
for优先级队列,带有cffi heap_inter.py
的接口python文件,程序访问队列heap.py
的包装器,以及测试脚本test_heap.py
这是错误:我尝试了export CC=gcc
和export CC=clang
以及我的mpich
pypy test_heap.py
AttributeError:cffi库'_heap_i'没有名为'initQueue'的函数,常量或全局变量
但是我确实在c库中有一个名为initQueue的函数。
当我在heap_inter.py上调用pypy时,执行的命令是building '_heap_i' extension
clang -DNDEBUG -O2 -fPIC -I/opt/local/include -I/opt/local/lib/pypy/include -c _heap_i.c -o ./_heap_i.o
cc -pthread -shared -undefined dynamic_lookup ./_heap_i.o -L/opt/local/lib -o ./_heap_i.pypy-41.so
clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
这是我的消息来源,
cheap.h
#include <stdio.h>
#include <stdlib.h>
/* priority Queue implimentation via roman10.net */
struct heapData {
//everything from event
//tx string
char tx[64];
//txID int
int txID;
//rx string
char rx[64];
//rxID int
int rxID;
//name string
char name[64];
//data Object
//time float
float time;
};
struct heapNode {
int value;
struct heapData data; //dummy
};
struct PQ {
struct heapNode* heap;
int size;
};
void insert(struct heapNode aNode, struct heapNode* heap, int size);
void shiftdown(struct heapNode* heap, int size, int idx);
struct heapNode removeMin(struct heapNode* heap, int size);
void enqueue(struct heapNode node, struct PQ *q);
struct heapNode dequeue(struct PQ *q);
struct heapNode peak(struct PQ *q);
void initQueue(struct PQ *q, int n);
int nn = 1000000;
struct PQ q;
int main(int argc, char **argv);
然后heap.c
void insert(struct heapNode aNode, struct heapNode* heap, int size) {
int idx;
struct heapNode tmp;
idx = size + 1;
heap[idx] = aNode;
while (heap[idx].value < heap[idx/2].value && idx > 1) {
tmp = heap[idx];
heap[idx] = heap[idx/2];
heap[idx/2] = tmp;
idx /= 2;
}
}
void shiftdown(struct heapNode* heap, int size, int idx) {
int cidx; //index for child
struct heapNode tmp;
for (;;) {
cidx = idx*2;
if (cidx > size) {
break; //it has no child
}
if (cidx < size) {
if (heap[cidx].value > heap[cidx+1].value) {
++cidx;
}
}
//swap if necessary
if (heap[cidx].value < heap[idx].value) {
tmp = heap[cidx];
heap[cidx] = heap[idx];
heap[idx] = tmp;
idx = cidx;
} else {
break;
}
}
}
struct heapNode removeMin(struct heapNode* heap, int size) {
int cidx;
struct heapNode rv = heap[1];
//printf("%d:%d:%dn", size, heap[1].value, heap[size].value);
heap[1] = heap[size];
--size;
shiftdown(heap, size, 1);
return rv;
}
void enqueue(struct heapNode node, struct PQ *q) {
insert(node, q->heap, q->size);
++q->size;
}
struct heapNode dequeue(struct PQ *q) {
struct heapNode rv = removeMin(q->heap, q->size);
--q->size;
return rv;
}
struct heapNode peak(struct PQ *q) {
return q->heap[1];
}
void initQueue(struct PQ *q, int n) {
q->size = 0;
q->heap = (struct heapNode*)malloc(sizeof(struct heapNode)*(n+1));
}
int nn = 1000000;
struct PQ q;
int main(int argc, char **argv) {
int n;
int i;
struct PQ q;
struct heapNode hn;
n = atoi(argv[1]);
initQueue(&q, n);
srand(time(NULL));
for (i = 0; i < n; ++i) {
hn.value = rand()%10000;
printf("enqueue node with value: %dn", hn.value);
enqueue(hn, &q);
}
printf("ndequeue all values:n");
for (i = 0; i < n; ++i) {
hn = dequeue(&q);
printf("dequeued node with value: %d, queue size after removal: %dn", hn.value, q.size);
}
}
heap_inter.py
:
from cffi import FFI
ffibuilder = FFI()
ffibuilder.set_source("_heap_i",
r"""//passed to C compiler
#include <stdio.h>
#include <stdlib.h>
#include "cheap.h"
""",
libraries=[])
ffibuilder.cdef("""
struct heapData {
char tx[64];
int txID;
char rx[64];
int rxID;
char name[64];
float time;
} ;
struct heapNode {
int value;
struct heapData data; //dummy
} ;
struct PQ {
struct heapNode* heap;
int size;
} ;
""")
#
# Rank (Simian Engine) has a Bin heap
if __name__ == "__main__":
ffibuilder.compile(verbose=True)
然后heap.py
from _heap_i import ffi, lib
infTime = 0
# /* create heap */
def init():
#global infTime
#infTime = int(engine.infTime) + 1
cheap = ffi.new("struct PQ *")
lib.initQueue(cheap,nn)
return cheap
def push(arr, element):
hn = ffi.new("struct heapNode")
value = element["time"]
hn.value = value
rx = ffi.new("char[]", element["rx"])
hn.data.rx = rx
tx = ffi.new("char[]", element["tx"])
hn.data.tx = tx
txID = ffi.new("int", element["txID"])
hn.data.txID = txID
rxID = ffi.new("int", element["rxID"])
hn.data.rxID = rxID
name = ffi.new("int", element["name"])
hn.data.name = name
hn.data.time = value
result = lib.enqueue(hn, arr)
def pop(arr):
hn = lib.dequeue(arr)
element = {"time": hn.value,
"rx" : hn.data.rx,
"tx" : hn.data.tx,
"rxID" : hn.data.rxID,
"txID" : hn.data.txID,
"name" : hn.data.name,
"data" : hn.data.data,
}
return element
def annihilate(arr, event):
pass
def peak(arr):
hn = lib.peak(arr)
element = {"time": hn.value,
"rx" : hn.data.rx,
"tx" : hn.data.tx,
"rxID" : hn.data.rxID,
"txID" : hn.data.txID,
"name" : hn.data.name,
"data" : hn.data.data,
}
return element
def isEvent(arr):
if arr.size:
return 1
else:
return None
def size(arr):
return arr.size
最后是test_heap.py
import heap
pq = heap.init()
for x in range(1000) :
heap.push({"time" : x,
"rx" : "a",
"tx" : "b",
"txID" : 1,
"rxID" : 1,
"name" : "bob",
"data": "none",
},pq)
for x in range(100) :
heap.peak(pq)
for x in range(1000):
y = heap.dequeue(pq)
print y
感谢任何人的关注,如果你以前经历过这个,请告诉我。
你必须告诉ffi.set_source()关于heap.c
。最简单的方法是
with open('heap.c', 'r') as fid:
ffi.set_source(fid.read())
就像在this example。它的工作方式是:
distutils
关键字)您的ffi.cdef()没有任何函数声明,因此生成的_heap_i库不会导出任何函数。尝试从.h文件中复制粘贴函数声明。