【发布时间】:2018-11-15 10:46:24
【问题描述】:
首先是一些上下文:我正在尝试使用 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 循环和进一步的函数调用),但这有同样的问题。
我知道我分别处理/创建返回值 h 和 j 的方式既不高效也不优雅,但我无法为 odeint 获取正确形状的返回值,因为 numba 不能很好地处理 numpy.reshape。
我搜索了numba documentation 寻求帮助,但这并没有帮助我理解我的问题。 我已经搜索了错误消息,但只找到了这个link,这可能是相似的。但是将 numba 降级到 0.38.0 对我不起作用。
谢谢大家!
【问题讨论】:
标签: python python-3.x llvm numba pyc