用 numba 编译 类?
Compiling classes with numba?
我正在尝试将 numba 与我在 Python 中编写的 DACS 寻路算法结合使用。如何指定“LiFoQueue”或 CPathNode class 实例列表?
class CPathNode:
def __init__ (self, node = -1, edge = -1):
self.node = node
self.edge = edge
dialHeapSpec = [
('route', CPathNode),
('nodeLists', np.int16[:]),
('pathCost', np.uin16[:]),
('finalCost', np.uin16[:]),
('nodeListLinks', np.in16[:]),
('predecessors', np.in16[:]),
('edges', np.in16[:]),
('dirtyIndex', np.uin16[:]), # LiFoQueue?
('dirtyCost', np.uin16[:]), # LiFoQueue?
('dirtyFinalCost', np.uin16[:]), # LiFoQueue?
('maxNodes', int),
('costIndex', int),
('noCost', int),
('maxCost', int)
]
@jitclass(dialHeapSpec)
class CDialHeap:
def __init__ (self):
self.nodeLists = None
self.dirtyIndex = None # list of used nodeLists table entries
self.nodeListLinks = None # list of nodes with same position in costIndex
self.predecessors = None
self.edges = None
self.pathCost = None
self.finalCost = None
self.dirtyCost = None # list of used pathCost table entries
self.dirtyFinalCost = None
self.route = []
self.maxNodes = int (0)
self.costIndex = int (0)
self.noCost = 65535
self.maxCost = self.noCost - 1
def Create (self, maxNodes):
self.maxNodes = int (maxNodes)
self.nodeLists = np.full (65536, int (-1), np.int16)
self.pathCost = np.full (self.maxNodes, int (65535), np.uint16)
self.finalCost = np.full (self.maxNodes, int (65535), np.uint16)
self.nodeListLinks = np.full (self.maxNodes, int (-1), np.int16)
self.predecessors = np.full (self.maxNodes, int (-1), np.int16)
self.edges = np.zeros (self.maxNodes, np.int16)
self.dirtyIndex = LifoQueue (maxsize = 65536)
self.dirtyCost = LifoQueue (maxsize = 65536)
self.dirtyFinalCost = LifoQueue (maxsize = maxNodes)
你不能。
关于 Jitclass 字段的声明,the tuples contain the name of the field and the Numba type of the field。
所有字段都必须声明为 Numba 类型,因为 jitclass 实例的数据作为 C 兼容结构分配在堆上,以便任何编译函数都可以直接访问底层数据.
这是非常严格的,它不包括:
- Python 类型,如
int
,必须替换为 nb.int16
...
- Numpy 数组,必须用 Numba 数组声明替换
nb.int16[:]
...
- Python 类,喜欢LiFoQueue
- ...
例如,对于定义为 int
的字段,您得到的错误是:
TypeError: spec values should be Numba type instances, got <class 'int'>
我正在尝试将 numba 与我在 Python 中编写的 DACS 寻路算法结合使用。如何指定“LiFoQueue”或 CPathNode class 实例列表?
class CPathNode:
def __init__ (self, node = -1, edge = -1):
self.node = node
self.edge = edge
dialHeapSpec = [
('route', CPathNode),
('nodeLists', np.int16[:]),
('pathCost', np.uin16[:]),
('finalCost', np.uin16[:]),
('nodeListLinks', np.in16[:]),
('predecessors', np.in16[:]),
('edges', np.in16[:]),
('dirtyIndex', np.uin16[:]), # LiFoQueue?
('dirtyCost', np.uin16[:]), # LiFoQueue?
('dirtyFinalCost', np.uin16[:]), # LiFoQueue?
('maxNodes', int),
('costIndex', int),
('noCost', int),
('maxCost', int)
]
@jitclass(dialHeapSpec)
class CDialHeap:
def __init__ (self):
self.nodeLists = None
self.dirtyIndex = None # list of used nodeLists table entries
self.nodeListLinks = None # list of nodes with same position in costIndex
self.predecessors = None
self.edges = None
self.pathCost = None
self.finalCost = None
self.dirtyCost = None # list of used pathCost table entries
self.dirtyFinalCost = None
self.route = []
self.maxNodes = int (0)
self.costIndex = int (0)
self.noCost = 65535
self.maxCost = self.noCost - 1
def Create (self, maxNodes):
self.maxNodes = int (maxNodes)
self.nodeLists = np.full (65536, int (-1), np.int16)
self.pathCost = np.full (self.maxNodes, int (65535), np.uint16)
self.finalCost = np.full (self.maxNodes, int (65535), np.uint16)
self.nodeListLinks = np.full (self.maxNodes, int (-1), np.int16)
self.predecessors = np.full (self.maxNodes, int (-1), np.int16)
self.edges = np.zeros (self.maxNodes, np.int16)
self.dirtyIndex = LifoQueue (maxsize = 65536)
self.dirtyCost = LifoQueue (maxsize = 65536)
self.dirtyFinalCost = LifoQueue (maxsize = maxNodes)
你不能。
关于 Jitclass 字段的声明,the tuples contain the name of the field and the Numba type of the field。
所有字段都必须声明为 Numba 类型,因为 jitclass 实例的数据作为 C 兼容结构分配在堆上,以便任何编译函数都可以直接访问底层数据.
这是非常严格的,它不包括:
- Python 类型,如
int
,必须替换为nb.int16
... - Numpy 数组,必须用 Numba 数组声明替换
nb.int16[:]
... - Python 类,喜欢LiFoQueue
- ...
例如,对于定义为 int
的字段,您得到的错误是:
TypeError: spec values should be Numba type instances, got <class 'int'>