0

The code below fails, and I've exhausted all of my guesses as to why.

import numba

import numpy as np
from numba import int32, float64, types, deferred_type
from numba.experimental import jitclass

import constants


p_spec = [
    ('x', float64),
    ('y', float64),
]


@jitclass(p_spec)
class Particle(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y


particle_type = deferred_type()
particle_type.define(Particle.class_type.instance_type)

ps_spec = [
    ('size', int32),
    ('particles', types.ListType(particle_type)),
]


@jitclass(ps_spec)
class Particles(object):
    def __init__(self, size):
        self.size = size
        self.particles = numba.typed.List([Particle(v[0], v[1]) for v in zip(np.random.uniform(0, constants.WIDTH, size),
                                                                             np.random.uniform(0, constants.HEIGHT, size))])


p = Particles(20)

The error is :

Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\Emergence\Particles2.py", line 40, in <module>
    p = Particles(20)
  File "C:\Users\User\PycharmProjects\Emergence\venv\lib\site-packages\numba\experimental\jitclass\base.py", line 124, in __call__
    return cls._ctor(*bind.args[1:], **bind.kwargs)
  File "C:\Users\User\PycharmProjects\Emergence\venv\lib\site-packages\numba\core\dispatcher.py", line 468, in _compile_for_args
    error_rewrite(e, 'typing')
  File "C:\Users\User\PycharmProjects\Emergence\venv\lib\site-packages\numba\core\dispatcher.py", line 409, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: native lowering)
Cannot cast ListType[instance.jitclass.Particle#1f2c6ecb850<x:float64,y:float64>] to DeferredType#2142231399584: %"inserted.data.4" = insertvalue {i8*, i8*} %"inserted.meminfo.4", i8* %".714", 1
During: lowering "(self).particles = $60call_method.28" at C:\Users\User\PycharmProjects\Emergence\Particles2.py (36)
During: resolving callee type: jitclass.Particles#1f2c6f5efa0<size:int32,particles:DeferredType#2142231399584>
During: typing of call at <string> (3)

During: resolving callee type: jitclass.Particles#1f2c6f5efa0<size:int32,particles:DeferredType#2142231399584>
During: typing of call at <string> (3)


File "<string>", line 3:
<source missing, REPL/exec in use?>
    
    
    File "<string>", line 3:
    <source missing, REPL/exec in use?>

 
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124

1 Answers1

0

This seems to work but I'm not sure why or if this is the 'best' answer.

ps_spec = [
    ('size', int32),
    ('particles', types.ListType(Particle.class_type.instance_type)),
]
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124