2017-10-21 2 views
-1

numbaには、異なるターゲット(cpu、cuda、parallel)で互換性のある関数を記述しようとしています。私がいるprobelmは、新しい配列は例えば、CUDAデバイスコードの異なるの割り当てです:CPUの機能のために類似した何かをNUMBAのCPUとGPU関数の配列割り当て

cuda.local.array(shape, dtype) 

対、すなわち

np.empty(shape, dtype) 

あります巧妙な方法別の関数を記述することなくこれを処理する方法?

+0

あなたの関数の型をテストできませんでしたか? – 0TTT0

+0

問題は、numbaはコードとフリークアウトをコンパイルするので、私の関数内には何の記述もできません。それ以外の場合、私は単純なif/elseまたはそのようなことをします。 それを扱う自然な方法は、Cのプリプロセッサディレクティブですが、Pythonで利用可能なものはありません –

答えて

0

私はこの問題の回避策を見つけました。私はそれを働かせることができる唯一の方法です。 @jit@cuda.jitの代わりに@myjitデコレータを使用し、すべての配列をcuda.local.arrayとして割り当てます。

def myjit(f): 
''' 
f : function 
Decorator to assign the right jit for different targets 
In case of non-cuda targets, all instances of `cuda.local.array` 
are replaced by `np.empty`. This is a dirty fix, hopefully in the 
near future numba will support numpy array allocation and this will 
not be necessary anymore 
''' 
if target == 'cuda': 
    return cuda.jit(f, device=True) 
else: 
    source = inspect.getsource(f).splitlines() 
    assert '@myjit' in source[0] 
    source = '\n'.join(source[1:]) + '\n' 
    source = source.replace('cuda.local.array', 'np.empty') 
    exec(source) 
    fun = eval(f.__name__) 
    newfun = jit(fun, nopython=True) 
    # needs to be exported to globals 
    globals()[f.__name__] = newfun 
    return newfun