0

I would like to use in a njit function a numpy array, which dtype is some jitclass. Here is a minimal working example:

from numba import njit, float32
from numba.experimental import jitclass
import numpy as np

spec = [
    ("bar", float32),
]
@jitclass(spec)
class Foo:
    def __init__(self, bar):
        self.bar = bar

@njit
def f(array):
    return array  

array = np.empty(100, dtype=Foo)
f(array)

Here is the error I get:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
Input In [91], in <cell line: 20>()
     16     return array
     19 array = np.empty(100 + 1, dtype=Foo)
---> 20 f(array)

File /usr/local/lib/python3.9/site-packages/numba/core/dispatcher.py:420, in _DispatcherBase._compile_for_args(self, *args, **kws)
    414         msg = str(e).rstrip() + (
    415             "\n\nThis error may have been caused by the following argument(s):\n%s\n"
    416             % "\n".join("- argument %d: %s" % (i, err)
    417                         for i, err in failed_args))
    418         e.patch_message(msg)
--> 420     error_rewrite(e, 'typing')
    421 except errors.UnsupportedError as e:
    422     # Something unsupported is present in the user code, add help info
    423     error_rewrite(e, 'unsupported_error')

File /usr/local/lib/python3.9/site-packages/numba/core/dispatcher.py:361, in _DispatcherBase._compile_for_args.<locals>.error_rewrite(e, issue_type)
    359     raise e
    360 else:
--> 361     raise e.with_traceback(None)

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
non-precise type array(pyobject, 1d, C)
During: typing of argument at /var/folders/v_/4dwsfgm50bl87pbgq646ry700000gn/T/ipykernel_70414/2508373139.py (16)

File "../../../../var/folders/v_/4dwsfgm50bl87pbgq646ry700000gn/T/ipykernel_70414/2508373139.py", line 16:
<source missing, REPL/exec in use?>

Is there a way of making this work? Ideally, I would like to use as most as possible a numpy array with a structured dtype.

Alexis Rosuel
  • 563
  • 1
  • 5
  • 12
  • In pure numpy use that would be a object dtype array. It's not a 'structured dtype'. – hpaulj Aug 21 '22 at 14:59
  • This is not possible to use a jitted-class objects as a Numpy array types in Numba codes yet. In fact, Numpy has a similar problem: it only speed up native types and not pure-Python objects like class-based objects (note that `object` but be used instead of `Foo`). Besides, note that jit-classes are experimental. Also note that even if it would be supported, array of structure are generally considered as quite inefficient as they prevent low-level optimizations and results in more cache misses (see [this](https://stackoverflow.com/questions/71101579) for more information). – Jérôme Richard Aug 21 '22 at 15:14

0 Answers0