为什么Numba pycc编译会中止?

时间:2018-11-15 10:46:25

标签: python python-3.x llvm numba pyc

首先,我要尝试使用scipy.integrate.odeint和不同的初始条件x_以及参数r_d_来集成耦合的ODE。 我试图通过提前编译ODE的右侧并减少我调用odeint函数的时间来加快集成速度。

我正在尝试使用numba.pycc.CC提前编译python函数。这适用于简单的功能,例如:

import numpy
from numba.pycc import CC

cc = CC('x_test')
cc.verbose = True

@cc.export('x_test', 'f8(f8[:])')
def x_test(y):
    return numpy.sum(numpy.log(y) * .5) # just a random combination of numpy functions I used to test the compilation

cc.compile() 

我要编译的实际函数如下:

# code_generation3.py
import numpy
from numba.pycc import CC

"""
N = 94
input for x_dot_all could look like:
    x_ = numpy.ones(N * 5)
    x[4::5] = 5e13
    t_ := some float from a numpy linspace. it is passed by odeint.
    r_ = numpy.random.random(N * 4)
    d_ = numpy.random.random(N * 4) * .8

    In practice the size of x_ is 470 and of r_ and d_ is 376.
"""

cc = CC('x_temp_dot1')
cc.verbose = True

@cc.export('x_temp_dot1', 'f8[:](f8[:], f8, f8[:], f8[:], f8[:])')
def x_dot_all(x_,t_,r_,d_, h):
    """
    rhs of the lotka volterra equation for all "patients"
    :param x: initial conditions, always in groupings of 5: the first 4 is the bacteria count, the 5th entry is the carrying capacity
    :param t: placeholder required by odeint
    :param r: growth rates of the types of bacteria
    :param d: death rates of the types of bacteria

    returns the right hand side of the competitive lotka-volterra equation with finite and shared carrying capacity in the same ordering as the initial conditions 
    """
        def x_dot(x, t, r, d, j):
        """
        rhs of the differential equation describing the intrahost evolution of the bacteria
        :param x: initial conditions i.e. bacteria sizes and environmental carrying capacity
        :param t: placeholder required by odeint
        :param r: growth rates of the types of bacteria
        :param d: death rates of the bacteria
        :param j: placeholder for the return value

        returns the right hand side of the competitive lotka-volterra equation with finite and shared carrying capacity
        """

        j[:-1] = x[:-1] * r * (1 - numpy.sum(x[:-1]) / x[-1]) - d * x[:-1]
        j[-1]   = -numpy.sum(x[:-1])
        return j 

    N = r_.shape[0]
    j = numpy.zeros(5)
    g = [x_dot(x_[5 * i : 5 * (i+1)], t_, r_[4 * i : 4* (i+1)], d_[4 * i: 4 * (i+1)], j) for i in numpy.arange(int(N / 4) )]

    for index, value in enumerate(g):
        h[5 * index : 5 * (index + 1)] = value

    return h

cc.compile()

在这里,我收到以下错误消息:

[xxxxxx@xxxxxx ~]$ python code_generation3.py 
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
generating LLVM code for 'x_temp_dot1' into /tmp/pycc-build-x_temp_dot1-wyamkfsy/x_temp_dot1.cpython-36m-x86_64-linux-gnu.o
python: /root/miniconda3/conda-bld/llvmdev_1531160641630/work/include/llvm/IR/GlobalValue.h:233: void llvm::GlobalValue::setVisibility(llvm::GlobalValue::VisibilityTypes): Assertion `(!hasLocalLinkage() || V == DefaultVisibility) && "local linkage requires default visibility"' failed.
Aborted

我想知道我做错了什么?

这两个函数都使用@jit(nopython = True)装饰器。 令我感到羞耻的是,我还尝试对列表理解进行硬编码(以尝试避免任何for循环和进一步的函数调用),但这存在相同的问题。

我知道,我处理/创建返回值hj的方式既不高效也不优雅,但是我很难以正确的形状获取返回值odeint,因为numba不能很好地处理numpy.reshape。

我搜索了https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#orm-jpa-hibernate来寻求帮助,但这并没有帮助我理解我的问题。 我已经搜索了错误消息,但只发现了这个numba documentation,这可能很相似。但是将numba降级为0.38.0对我来说不起作用。

谢谢大家!

1 个答案:

答案 0 :(得分:0)

我想如果您先编译x_dot然后再编译x_dot_all的话,它会起作用。 无论如何,我建议将这两个功能结合起来。

Numba中,循环通常不是问题,但是列表理解肯定是问题。还应尝试避免大量的小循环。 (矢量化命令,例如numpy.sum(x[:-1])都是单独的循环)。有时Numba能够组合此循环以获取有效的代码,但并非每次都能。

示例

# code_generation3.py
import numpy
import numba as nb
from numba.pycc import CC

cc = CC('x_dot_all')
cc.verbose = True


@cc.export('x_dot_all_mod', 'f8[:](f8[:], f8, f8[:], f8[:], f8[:])')
def x_dot_all(x_,t_,r_,d_, h):
  N = r_.shape[0]

  for i in range(int(N / 4)):
    sum_x=x_[5*i+0]+x_[5*i+1]+x_[5*i+2]+x_[5*i+3]
    TMP=1.-(sum_x)/x_[5*i+4]

    h[i*5+0]=x_[i*5+0]*r_[4*i+0]*TMP-d_[4*i+0]*x_[i*5+0]
    h[i*5+1]=x_[i*5+1]*r_[4*i+1]*TMP-d_[4*i+1]*x_[i*5+1]
    h[i*5+2]=x_[i*5+2]*r_[4*i+2]*TMP-d_[4*i+2]*x_[i*5+2]
    h[i*5+3]=x_[i*5+3]*r_[4*i+3]*TMP-d_[4*i+3]*x_[i*5+3]
    h[i*5+4]=-sum_x

  return h

if __name__ == "__main__":
    cc.compile()

性能

N=94
x_ = np.ones(N * 5)
x_[4::5] = 5e13
t_ = 15
r_ = np.random.random(N * 4)
d_ = np.random.random(N * 4) * .8
h = np.zeros(N * 5)

#your version: 38 µs
#new version:  1.8µs