使用numba guvectorize列出索引错误

时间:2014-06-18 12:58:15

标签: python anaconda numba numba-pro

我是numba / numbapro的新手。我试图运行其中一个示例,这个示例关于使用guvectorize的广义Ufunc:

(此处为示例链接):http://docs.continuum.io/numbapro/quickstart.html#numbapro-guvectorize

import numbapro as numbapro

@numbapro.guvectorize(['void(int32[:], int32[:])'], '(n)->()')
def sum_row(inp, out):
    """
    Sum every row

    function type: two arrays
                   (note: scalar is represented as an array of length 1)
    signature: n elements to scalar
    """
    tmp = 0.
    for i in range(inp.shape[0]):
        tmp += inp[i]
    out[0] = tmp

我收到此错误:

IndexError                                Traceback (most recent call last)
<ipython-input-98-79514a184595> in <module>()
----> 1 @numbapro.guvectorize(['void(int32[:], int32[:])'], '(n)->()')
      2 def sum_row(inp, out):
      3     """
      4     Sum every row
      5 

/users/adelacalle/anaconda_linux/lib/python2.7/site-packages/numba/npyufunc/decorators.pyc in wrap(func)
    117         for fty in ftylist:
    118             guvec.add(fty)
--> 119         return guvec.build_ufunc()
    120 
    121     return wrap

/users/adelacalle/anaconda_linux/lib/python2.7/site-packages/numba/npyufunc/ufuncbuilder.pyc in build_ufunc(self)
    149 
    150         for sig, cres in self.nb_func.overloads.items():
--> 151             dtypenums, ptr = self.build(cres)
    152             dtypelist.append(dtypenums)
    153             ptrlist.append(utils.longint(ptr))

/users/adelacalle/anaconda_linux/lib/python2.7/site-packages/numba/npyufunc/ufuncbuilder.pyc in build(self, cres)
    167         signature = cres.signature
    168         wrapper = build_gufunc_wrapper(ctx, cres.llvm_func, signature,
--> 169                                        self.sin, self.sout)
    170         ctx.engine.add_module(wrapper.module)
    171         ptr = ctx.engine.get_pointer_to_function(wrapper)

/users/adelacalle/anaconda_linux/lib/python2.7/site-packages/numba/npyufunc/wrappers.pyc in build_gufunc_wrapper(context, func, signature, sin, sout)
    143     for i, (typ, sym) in enumerate(zip(signature.args, sin + sout)):
    144         ary = GUArrayArg(context, builder, arg_args, arg_dims, arg_steps, i,
--> 145                          step_offset, typ, sym, sym_dim)
    146         step_offset += ary.ndim
    147         arrays.append(ary)

/users/adelacalle/anaconda_linux/lib/python2.7/site-packages/numba/npyufunc/wrappers.pyc in __init__(self, context, builder, args, dims, steps, i, step_offset, typ, syms, sym_dim)
    207         self.array = arycls(context, builder)
    208         self.array.data = builder.bitcast(self.data, self.array.data.type)
--> 209         self.array.shape = cgutils.pack_array(builder, self.shape)
    210         self.array.strides = cgutils.pack_array(builder, self.strides)
    211         self.array_value = self.array._getpointer()

/users/adelacalle/anaconda_linux/lib/python2.7/site-packages/numba/cgutils.pyc in pack_array(builder, values)
    257 def pack_array(builder, values):
    258     n = len(values)
--> 259     ty = values[0].type
    260     ary = Constant.undef(Type.array(ty, n))
    261     for i, v in enumerate(values):

IndexError: list index out of range

我找不到比此链接更多的文档。难道我做错了什么?我发现当签名中有一个空括号时会发生这种情况。 我在linux机器上运行它,我的numbapro版本是0.14.1

提前致谢,

亚历

1 个答案:

答案 0 :(得分:1)

最后,我自己得出了结论。您可以分配n个元素的实际数组,因此您可以写为签名(n)->(n)而不是(n)->()。文档中必定存在错误,或者文档已过时。

然而,这是无效的,因为我们必须分配整个数组,这将浪费内存(尽管它有效!)。更重要的是,使用guvectorize对数组中的元素求和它可能是最好,最有效的方法,所以我使用@jit。