NUMBA

时间:2017-10-21 00:27:54

标签: python numpy numba

我正在尝试在numba中编写一些函数,我可以互换使用它们用于不同的目标(cpu,cuda,parallel)。我遇到的问题是新数组的分配对于cuda设备代码是不同的,例如:

cuda.local.array(shape, dtype)

VS。为CPU功能做类似的事情,即

np.empty(shape, dtype)

有没有一种聪明的方法来处理这个问题而不必编写单独的函数?

1 个答案:

答案 0 :(得分:0)

我找到了一个问题的肮脏的解决方法。这是我能让它发挥作用的唯一途径。 使用@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