2016-07-31 26 views
6

Jitclass dekoratörünün yuvalanmış sınıflarla nasıl çalıştığını anlamaya çalışıyorum. İki kukla sınıf yazdım: fifi ve toto fifi toto niteliğine sahiptir. Her iki sınıf da Jitclass dekoratörüne sahip ancak derleme başarısız oluyor. İşte kod:numba jitclass nasıl oluşturulur

fifi.py

from numba import jitclass, float64 
from toto import toto 

spec = [('a',float64),('b',float64),('c',toto)] 

@jitclass(spec) 
class fifi(object): 
    def __init__(self, combis): 
    self.a = combis 
    self.b = 2 
    self.c = toto(combis) 

    def mySqrt(self,x): 
    s = x 
    for i in xrange(self.a): 
     s = (s + x/s)/2.0 
    return s 

toto.py:

from numba import jitclass,int32 

spec = [('n',int32)] 

@jitclass(spec) 
class toto(object): 
    def __init__(self,n): 
    self.n = 42 + n 

    def work(self,y): 
    return y + self.n 

kodu başlattı komut dosyası:

from datetime import datetime 
from fifi import fifi 
from numba import jit 

@jit(nopython = True) 
def run(n,results): 
    for i in xrange(n): 
    q = fifi(200) 
    results[i+1] = q.mySqrt(i + 1) 

if __name__ == '__main__': 
    n = int(1e6) 
    results = [0.0] * (n+1) 
    starttime = datetime.now() 
    run(n,results) 
    endtime = datetime.now() 

    print("Script running time: %s"%str(endtime-starttime)) 
    print("Sqrt of 144 is %f"%results[145]) 

Senaryoyu çalıştırmak

, ben elde [...]

TypingError: Untyped global name 'toto' File "fifi.py", line 11

'fif' içinde 'toto' ile ilgili herhangi bir başvuruyu kaldırırsam, kodun düzgün çalıştığını ve numba sayesinde x16 hızına ulaştığımı unutmayın.

cevap

5

Bunu yapmanın yolu belgelenmemiş olsa da jitclass'ı başka bir jitclass üyesi olarak kullanmak mümkündür. Bir deferred_type örneğini kullanmanız gerekir. Bu, Numba 0.27 ve muhtemelen daha önce çalışır. Sonra çıktı olarak almak

from numba import jitclass, float64, deferred_type 
from toto import toto 

toto_type = deferred_type() 
toto_type.define(toto.class_type.instance_type) 

spec = [('a',float64),('b',float64),('c',toto_type)] 

@jitclass(spec) 
class fifi(object): 
    def __init__(self, combis): 
    self.a = combis 
    self.b = 2 
    self.c = toto(combis) 

    def mySqrt(self,x): 
    s = x 
    for i in xrange(self.a): 
     s = (s + x/s)/2.0 
    return s 

ve: fifi.py değiştirme

  • : Örneğin

    $ python test.py 
    Script running time: 0:00:01.991600 
    Sqrt of 144 is 12.041595 
    

    Bu işlevsellik veri yapılarının daha gelişmiş jitclass örneklerden bazıları görülebilir, stack.py

  • linkedlist.py