Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

python - 使用numba jitclass作为numba jit函数中的参数(Use numba jitclass as a parameter in numba jit function)

I'm using numba 0.46.0 and I want to pass an object of my class to my function as an argument and run this function on my GPU with CUDA.

(我正在使用numba 0.46.0,并且要将类的对象作为参数传递给我的函数,然后使用CUDA在我的GPU上运行此函数。)

If I want to use a simple Python object (like int ), I use something like this:

(如果我想使用一个简单的Python对象(如int ),则使用类似以下的内容:)

from numba import jit, cuda
from numba.types import void, int32

@jit(void(int32), target='cuda')
def f(int_object):
    pass

f(123)

And this works fine.

(这很好。)

Now I try to do the same thing with a class:

(现在,我尝试对一个类执行相同的操作:)

from numba import jit, cuda
from numba,types import void

@jitclass([])
class MyClass:
    def __init__(self):
        pass

@jit(void(MyClass), target='cuda')
def f(MyClass_object):
    pass

And this fails with NotImplementedError without any comments.

(并且此操作失败,没有任何注释的NotImplementedError 。)

I also tried to compile this in a lazy way:

(我还尝试以一种惰性的方式来编译它:)

@jit(target='cuda')
def f(MyClass_object):
    pass

f(MyClass())

This failes with

(这失败了)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/numba/cuda/dispatcher.py", line 42, in __call__
    return self.compiled(*args, **kws)
  File "/usr/local/lib/python3.6/dist-packages/numba/cuda/compiler.py", line 801, in __call__
    cfg(*args)
  File "/usr/local/lib/python3.6/dist-packages/numba/cuda/compiler.py", line 537, in __call__
    sharedmem=self.sharedmem)
  File "/usr/local/lib/python3.6/dist-packages/numba/cuda/compiler.py", line 604, in _kernel_call
    self._prepare_args(t, v, stream, retr, kernelargs)
  File "/usr/local/lib/python3.6/dist-packages/numba/cuda/compiler.py", line 715, in _prepare_args
    raise NotImplementedError(ty, val)
NotImplementedError: (instance.jitclass.MyClass#7f983418fc88<>, <numba.jitclass.boxing.MyClass object at 0x7f983416ca10>)

Can I use a jitclass object as a jit function argument?

(我可以使用jitclass对象作为jit函数参数吗?)

If yes, what is wrong in the example above?

(如果是,那么上面的示例出了什么问题?)

UPD: By the way, I have checked this with numpy arrays:

(UPD:顺便说一句,我已经用numpy数组进行了检查:)

import numpy as np
from numba import jit, cuda
from numba.types import void

@jit(void(np.ndarray), target='cuda')
def f1(ndarray_object):
    pass
# Fails with NotImplementedError with no comments

@jit(target='cuda')
def f2(ndarray_object):
    pass

a = np.asarray([])
f2(a) # Executes with no errors, only a warning about autojit

Why this works with numpy, but doesn't work with my class?

(为什么这适用于numpy,但不适用于我的班级?)

Why this works with numpy in lazy mode (f2), but does not work with given signatures (f1)?

(为什么这在延迟模式(f2)中适用于numpy,但不适用于给定签名(f1)?)

  ask by Kolay.Ne translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...