2

単純なアルゴリズムをいくつか試して、numbapro の jit/vectorize の背後にあるセマンティクスを理解しようとしています。これは、後で複数回実行したいミラーラビン素数性テストです。

これはうまくいきます

from numbapro import jit , guvectorize , vectorize, uint64, int32, int8, bool_
import random
@jit(bool_(uint64,int32),target='cpu')
def is_prime(n,k):
    if n % 2 == 0:
        return False
    # n-1 as 2^s * d
    dn = n - 1
    s1 = 0
    while dn % 2 == 0:
        dn = dn >> 1
        s1 += 1
    for j in range(k):
        a1 = random.randint(2,n-2)
        x = pow(a1,dn) % n
        if x == 1 or x == n - 1:
            continue
        for i in range(s1):
            x = (x * x) % n
            if x == 1:
                return False
            if x == n - 1:
                break
    return True

しかし、デコレータを

@vectorize(bool_(uint64,int32),target='cpu')

エラーを与える

Traceback (most recent call last):
  File "h:\users\mushfaque.cradle\documents\visual studio 2013\Projects\EulerPraxis\EulerPraxis\EulerPraxis.py", line 12, in <module>
    @vectorize(int8(uint64,int32),target='cpu')
  File "H:\Apps\Anaconda3\lib\site-packages\numba\npyufunc\decorators.py", line 67, in wrap
    for fty in ftylist:
TypeError: 'NotImplementedType' object is not callable

ufunc で vectorize を使用する必要があることは理解していますが、これを ufunc にするために何が欠けていますか?

4

1 に答える 1

0

私はこれを解決しました。

  • @vectorize([int32(uint64,int32)],target='cpu') は最初の問題を修正します。型注釈の周りの「[]」に注意してください
  • 戻り値の型としての bool_ はサポートされていません (バグ) と報告されています。その間、int32を使用してください
  • より微妙な問題は、 n = 3 を使用すると randint が例外をスローすることです。Numba はまだコード内で例外を処理していないため、その可能性をキャッチするために追加の「if」が必要です
于 2014-11-23T15:35:29.923 に答える