【问题标题】:dynamically/condionally choosing from pure python, numba.jit and numba.cuda.jit动态/有条件地从纯 python、numba.jit 和 numba.cuda.jit 中选择
【发布时间】:2017-11-05 23:14:44
【问题描述】:

对装饰器进行更好的动态控制的最佳方法是什么 - 从numba.cuda.jitnumba.jit 和无(纯 python)中选择。 [请注意一个项目可以有10s或100s的功能,所以这应该很容易应用于所有功能] 这是来自 numba 网站的示例。

import numba as nb
import  numpy as np

# global control of this --> @nb.jit or @nb.cuda.jit  or none 
# some functions with @nb.jit or cuda.jit with kwargs like (nopython=True, **other_kwargs)
def sum2d(arr):
    M, N = arr.shape
    result = 0.0
    for i in range(M):
        for j in range(N):
            result += arr[i,j]
    return result


a = np.arange(81).reshape(9,9)

sum2d(a)

【问题讨论】:

    标签: python performance numba


    【解决方案1】:

    您可能想要更复杂的东西,但一个相对简单的解决方案是根据设置重新定义jit。例如

    def _noop_jit(f=None, *args, **kwargs):
        """ returns function unmodified, discarding decorator args"""
        if f is None:
            return lambda x: x
        return f
    
    # some config flag
    if settings.PURE_PYTHON_MODE:
        jit = _noop_jit
    else: # etc
        from numba import jit
    
    @jit(nopython=True)
    def f(a):
        return a + 1
    

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多