-1
我想寫一些函數在numba,我可以交換使用不同的目標(cpu,cuda,並行)。我遇到的萬阿英,蔣達清是一個新的數組的分配是CUDA設備代碼,例如,不同:陣列分配的CPU和GPU功能在NUMBA
cuda.local.array(shape, dtype)
對比做了CPU的功能類似,即
np.empty(shape, dtype)
是否有聰明的方式如何處理這個,而不必編寫單獨的功能?
我想寫一些函數在numba,我可以交換使用不同的目標(cpu,cuda,並行)。我遇到的萬阿英,蔣達清是一個新的數組的分配是CUDA設備代碼,例如,不同:陣列分配的CPU和GPU功能在NUMBA
cuda.local.array(shape, dtype)
對比做了CPU的功能類似,即
np.empty(shape, dtype)
是否有聰明的方式如何處理這個,而不必編寫單獨的功能?
我發現問題的一個骯髒的解決方法。這是我能做到的唯一方法。 使用@myjit
裝飾器代替@jit
和@cuda.jit
,並將所有陣列分配爲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
難道你不能在你的函數中測試類型嗎? – 0TTT0
問題是我不能在我的函數中有任何聲明不起作用,因爲numba編譯代碼並且嚇壞了。否則,我會做一個簡單的if/else或這樣的。 處理這種情況的自然方式是C中的預處理器指令,但是沒有這樣的東西可用於python –