2017-06-20 36 views
-1

我有以下一個簡單的問題。Numba:在jitted函數中訪問類屬性

import numba 
import numpy as np 

class MyClass(object): 
    def __init__(self): 
     self.Value=0.0 
     self.Array=np.array([],dtype=np.float64) 

@numba.jit(nopython=True) 
def Fun(ClassInstance): 
    print ClassInstance.Array*1.0 

當我執行這些簡單的線條

A=MyClass() 
Fun(A) 

我得到以下錯誤:

numba/dataflow.py:346: RuntimeWarning: Python2 style print partially supported. Please use Python3 style print. 
    "Python3 style print.", RuntimeWarning) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "numba/dispatcher.py", line 310, in _compile_for_args 
    raise e 
numba.errors.UntypedAttributeError: Caused By: 
Traceback (most recent call last): 
    File "numba/compiler.py", line 230, in run 
    stage() 
    File "numba/compiler.py", line 444, in stage_nopython_frontend 
    self.locals) 
    File "numba/compiler.py", line 800, in type_inference_stage 
    infer.propagate() 
    File "numba/typeinfer.py", line 767, in propagate 
    raise errors[0] 
UntypedAttributeError: Unknown attribute 'Array' of type pyobject 
File "bumba.py", line 11 
[1] During: typing of get attribute at bumba.py (11) 

Failed at nopython (nopython frontend) 
Unknown attribute 'Array' of type pyobject 
File "bumba.py", line 11 
[1] During: typing of get attribute at bumba.py (11) 

This error may have been caused by the following argument(s): 
- argument 0: cannot determine Numba type of <class '__main__.MyClass'> 

它會把我爲什麼numba無法識別陣列。既然它是如此基本的功能,我必須假設我錯過了一些至關重要的東西,但我至今沒有找到任何解決方案。

+0

您禁止它使用'nopython = True'訪問任何Python API,然後通過'ClassInstance.Array'告訴它訪問Python API。 – user2357112

+1

如果你想訪問任意的Python對象屬性,你需要關閉nopython。 – user2357112

+0

所以請幫我找到我的想法中的錯誤。根據Numba的描述,'nopython'僅僅禁止Python的C API。我不知道訪問屬性構成訪問C API。 – Deathbreath

回答

0

正如@ user2537112表示的意見,Numba的nopython選項jit導致通過PyObject_GetAttr Python的C API的調用,因爲Numba目前不支持屬性訪問。解決方法包括將對象屬性直接傳遞給函數,而不是通過對象實例。