[英]calling njit function in python numba jitclass fails
[英]Creating dictionary with @jitclass in @njit decorated function
我們有一些 Python 代碼,我想通過使用 numba 來加快速度。 如果定義了類型,numba 似乎開始支持字典,所以我嘗試在 @njit 裝飾的 class 中定義字典。在下面的代碼中,class 計數器在 @njit 裝飾的 function 中似乎有所不同,因為一些代碼引發了例外。 我是否需要將有關 class 計數器的信息傳遞給@njit function? 如果是這樣,我該怎么做?
from numba import njit
from numba.core import types
from numba.typed import Dict
from numba.experimental import jitclass
@jitclass([
("value", types.int64),
])
class Counter():
def __init__(self):
self.value = 0
@njit
def main():
a = Counter() # works, so Counter seems to be know
dic = Dict.empty(
key_type=types.unicode_type,
value_type=Counter.class_type.instance_type, # raises Unknown attribute 'class_type' of type jitclass.Counter#190a6b970f0<value:int64>
)
dic = Dict.empty( # this works
key_type=types.unicode_type,
value_type=Counter.class_type.instance_type,
)
main()
弄清楚了。 請參閱https://numba.pydata.org/numba-doc/dev/reference/pysupported.html :
@njit
def main():
dic = dict() # numba.typed.Dict()
for i in range(1000):
dic[str(i)] = Counter()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.