Razzupaltuff
Razzupaltuff

Reputation: 2311

Compiling classes with numba?

I am trying to use numba with a DACS pathfinding algorithm I wrote in Python. How do I spec something like "LiFoQueue" or a list of CPathNode class instances?

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)

Upvotes: 0

Views: 64

Answers (1)

aerobiomat
aerobiomat

Reputation: 3437

You can't.

Regarding de declaration of Jitclass fields, the tuples contain the name of the field and the Numba type of the field.

All fields must be declared as Numba types because the data of a jitclass instance is allocated on the heap as a C-compatible structure so that any compiled functions can have direct access to the underlying data.

This is very restrictive, and it excludes:

  • Python types, like int, that must be replaced by nb.int16...
  • Numpy arrays, that must be replaced by Numba array declarations nb.int16[:]...
  • Python classes, like LiFoQueue
  • ...

For example, the error you get for a field defined as int is:

TypeError: spec values should be Numba type instances, got <class 'int'>

Upvotes: 1

Related Questions