How to set 2d array on jit decorator without numpy?

Viewed 1042

I'm using Numba library in Python3.

A function's parameter is a 2d array.

I set Numba jit decorators to list[list[int]], but show TypeError: 'type' object is not subscriptable after run code.

I using print(numba.typeof(matrix)) to detect parameter type, it return list(reflected list(int32)) type.

But even if I change decorators to list[list[numba.int32]] , not working too.

Code:

from numba import jit

size = 3
matrix = [[0, 1, 2], [4, 5, 6], [7, 8, 9]]


@jit(list[list[int]])
def test(jitmatrix):
    _total = 0
    for i in range(size):
        for j in range(size):
            _total += jitmatrix[j][i]


test(matrix)

Have any idea to set 2d array on jit decorator without numpy library ?

Or it must to use numpy library?

1 Answers

Numba as of 0.44 does not support list of lists as inputs to functions in nopython mode. See:

http://numba.pydata.org/numba-doc/latest/reference/pysupported.html#list-reflection

In the arguments to @jit, numba has no knowledge of list and cannot convert it automatically to any numba type. The TypeError ... subscriptable error is coming from python itself, because you are trying to access an element of a built-in type (list in this case), which is not allowed.

The following would work though:

from numba import jit
import numba as nb
import numpy as np

size = 3
matrix = np.array([[0, 1, 2], [4, 5, 6], [7, 8, 9]])


@jit(nopython=True)
# or @jit(nb.int64(nb.int64[:,:]))
def test(jitmatrix):
    _total = 0
    for i in range(size):
        for j in range(size):
            _total += jitmatrix[j,i]  # note the change in indexing, which is faster

    return _total


test(matrix)
Related