【问题标题】:Working and manipulating numpy arrays with numba使用 numba 处理和操作 numpy 数组
【发布时间】:2015-05-06 01:08:44
【问题描述】:

为什么 Numba 的 jit 不能编译一个简单的 Numpy 数组操作?

这是一个重现 Numba 无法编译的最小非工作示例

import numpy as np
from numba import jit

rows = 10
columns = 999999
A = np.empty((rows, columns))
b = np.linspace(0, 1, num=rows)

@jit(nopython=True)
def replicate(A, b):
    for i in range(A.shape[1]):
        A[:, i] = b
    return A #optional

replicate(a, b)

出现以下错误:

TypingError: Failed at nopython (nopython frontend)
Cannot resolve setitem: array(float64, 1d, C, nonconst)[(slice3_type, int64)] = array(float64, 1d, C, nonconst)
File "<ipython-input-32-db24fbe2922f>", line 12

我做错了吗?

顺便说一句,我需要 nopython 模式,因为在我的实际情况中,我需要经常执行数组加法、标量乘法以及数组填充其他数组。我的理解是,在对象模式下我将无法进行循环抖动,因此我不会看到任何真正的执行性能提升。

【问题讨论】:

    标签: python numpy jit numba


    【解决方案1】:

    Numba 不支持 nopython 模式下的 numpy 切片。尝试显式展开循环:

    rows = 10
    columns = 999999
    a = np.empty((rows, columns))
    b = np.linspace(0, 1, num=rows)
    
    @jit(nopython=True)
    def replicate(A, b):
        for i in xrange(A.shape[0]):
            for j in xrange(A.shape[1]):
                A[i, j] = b[i]
        return A #optional
    
    replicate(a, b)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      相关资源
      最近更新 更多