question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

parallel=True does not work for functions with a jitclass instance as an argument

See original GitHub issue

I am using version 0.53.1.

Here is a MWE:

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

spec = [('value', int32)]
@jitclass(spec)
class Bag(object):
    def __init__(self, value):
        self.value = value
        
Bag_type = deferred_type()
Bag_type.define(Bag.class_type.instance_type)
spec = [('value', int32),('bag',Bag_type)]
@jitclass(spec)
class Sack(object):
    def __init__(self, value):
        self.value = value
        self.bag = Bag(value+5)
        
    def call_foo(self,n):
        print(foo(self.bag,n))
        
    def call_foo_parallel(self,n):
        print(foo_parallel(self.bag,n))

@njit
def foo(bag,n):
    out = 0
    for i in prange(n):
        out += bag.value
    return out

@njit(parallel=True)
def foo_parallel(bag,n):
    out = 0
    for i in prange(n):
        out += bag.value
    return out

sack = Sack(10)
print(sack.value)
print(sack.bag.value)
sack.call_foo(3)
sack.call_foo_parallel(3)

Which outputs the following:

10 15 45 Traceback (most recent call last):

File “C:\Users\MTH31973\Desktop\commz\commz\tests\benchmark\parallel_processing_benchmark.py”, line 51, in <module> sack.call_foo_parallel(3)

File “C:\Aeroapps\AnacondaP3x64-2020.07\lib\site-packages\numba\experimental\jitclass\boxing.py”, line 60, in wrapper return method(*args, **kwargs)

File “C:\Aeroapps\AnacondaP3x64-2020.07\lib\site-packages\numba\core\dispatcher.py”, line 420, in _compile_for_args error_rewrite(e, ‘typing’)

File “C:\Aeroapps\AnacondaP3x64-2020.07\lib\site-packages\numba\core\dispatcher.py”, line 361, in error_rewrite raise e.with_traceback(None)

TypingError: - Resolution failure for literal arguments: Failed in nopython mode pipeline (step: nopython frontend) Internal error at <numba.core.typeinfer.CallConstraint object at 0x000002588836FD60>. Failed in nopython mode pipeline (step: nopython mode backend) cannot store %“deferred.2578128644272.value” to %“deferred.2578128644272.data”*: mismatching types

File “parallel_processing_benchmark.py”, line 43: def foo_parallel(bag,n): <source elided> out = 0 for i in prange(n): ^

During: lowering “id=11[LoopNest(index_variable = parfor_index.1771, range = (0, n, 1))]{16: <ir.Block at C:\Users\MTH31973\Desktop\commz\commz\tests\benchmark\parallel_processing_benchmark.py (43)>}Var(parfor_index.1771, parallel_processing_benchmark.py:43)” at C:\Users\MTH31973\Desktop\commz\commz\tests\benchmark\parallel_processing_benchmark.py (43) During: resolving callee type: type(CPUDispatcher(<function foo_parallel at 0x0000025887FD3550>)) During: typing of call at C:\Users\MTH31973\Desktop\commz\commz\tests\benchmark\parallel_processing_benchmark.py (31)

Enable logging at debug level for details.

File “parallel_processing_benchmark.py”, line 31: def call_foo_parallel(self,n): print(foo_parallel(self.bag,n)) ^

  • Resolution failure for non-literal arguments: None

During: resolving callee type: BoundFunction((<class ‘numba.core.types.misc.ClassInstanceType’>, ‘call_foo_parallel’) for instance.jitclass.Sack#2584933e0d0value:int32,bag:DeferredType#2578128644272) During: typing of call at <string> (3)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
stuartarchibaldcommented, Dec 15, 2021

@hunterdsp no problem. As to future support, @DrTodd13 might have a view.

As a work around, if you dereference the members of the jitclass you want to use in the prange body into temporary variables outside of the loop, it’ll probably compile (just a guess from what the error is).

Example:

from numba.experimental import jitclass
from numba import njit, prange, int32, deferred_type

spec = [('value', int32)]
@jitclass(spec)
class Bag(object):
    def __init__(self, value):
        self.value = value

Bag_type = deferred_type()
Bag_type.define(Bag.class_type.instance_type)
spec = [('value', int32),('bag',Bag_type)]
@jitclass(spec)
class Sack(object):
    def __init__(self, value):
        self.value = value
        self.bag = Bag(value+5)

    def call_foo(self,n):
        print(foo(self.bag,n))

    def call_foo_parallel(self,n):
        print(foo_parallel(self.bag,n))

@njit
def foo(bag,n):
    out = 0
    for i in prange(n):
        out += bag.value
    return out

@njit(parallel=True)
def foo_parallel(bag,n):
    out = 0
    WORKAROUND = bag.value
    for i in prange(n):
        out += WORKAROUND
    return out

sack = Sack(10)
print(sack.value)
print(sack.bag.value)
sack.call_foo(3)
sack.call_foo_parallel(3)

0reactions
stuartarchibaldcommented, May 23, 2022

It would be great if this can be implemented.

Thanks @stuartarchibald for the workaround for the time being.

@songololo no problem!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compiling Python classes with @jitclass - Numba
A jitclass class object is treated as a function (the constructor) inside a Numba compiled function. isinstance() only works in the interpreter. Manipulating ......
Read more >
Compiling Python classes with @jitclass
A jitclass class object is treated as a function (the constructor) inside a Numba compiled function. isinstance() only works in the interpreter. Manipulating ......
Read more >
Why this python class is not working with numba jitclass?
I am trying to speed up this code as I would be working with large data sets in the future. import numpy as...
Read more >
Nogil with jitclass? - Community Support - Numba Discussion
Dear all, I am trying to use joblib parallel (with threading backend) to parallelize a set of calls to a jitclass. That doesn't...
Read more >
Speed Up your Algorithms Part 2— Numba | by Puneet Grover
First, Python function is taken, optimized and is converted into ... True argument with your jit wrapper, using which it won't use the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found