当类的属性包含另一个类实例时,如何指定numba jitclass? [英] How to specify numba jitclass when the class's attribute contains another class instance?

查看:137
本文介绍了当类的属性包含另一个类实例时,如何指定numba jitclass?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用numba来提高scipy.integrate.odeint的python性能. 为此,我必须对定义ODE系统的函数使用@ nb.jit(nopython = True).但是,此函数必须在我的程序中使用另一个python类实例作为参数.我还必须使用具有适当规格的@ nb.jitclass(s​​pec)来初始化该类.这很好用,直到当我发现一个严重的问题时,类的规范包括另一种类型的类实例作为其方法.我的代码如下.

I'm trying to use numba to boost the python performance of scipy.integrate.odeint. To this end, I have to use @nb.jit(nopython=True) for the function defining the ODE system. However, this function has to take another python-class instance as an argument in my program. I had to jit the class also with @nb.jitclass(spec) with appropriate specs. This worked fine, until I found a serious issue when the specs of the class includes another type of class instance as its method. My code is following.

import numba as nb
from scipy.integrate import odeint


spec1=[("hi", nb.i4)]
@nb.jitclass(spec1)
class Hi(object):
    def __init__(self):
        self.hi = 0

spec2=[("dummy", nb.i4), ("dummy1", nb.i4)]
@nb.jitclass(spec2)
class Dummy(object):
    def __init__(self, anotherClassInstance):
        self.dummy = 0
        self.dummy1 = anotherClassInstance

class A:
    def __init__(self, someClassInstance):
        self.a1 = someClassInstance

    def odeSystem(self, x, t):
        return _odeSystem(x, t, self.a1)

    def odeSolve(self, iValues, ts):
        sol = odeint(self.odeSystem, iValues, ts)
        return sol

@nb.jit(nopython=True)
def _odeSystem(x, t, someClassInstance):
    return 1-x



if __name__ == "__main__":
    c = Hi()
    b = Dummy(c)
    a = A(b)
    print a.odeSolve(0.5, range(0, 10))

总结: 因此,"A类"是我的ode求解器.

In summary: So here "class A" is my ode solver.

  1. 要使用numba编译方法"odeSystem",它不能为类方法.因此,我在类"_odeSystem"之外创建了另一个函数.

  1. To compile the method "odeSystem" with numba, it must not a class method. So I made another function outside of the class "_odeSystem".

但是,不幸的是,我的odeSystem必须具有一个类实例作为参数.因此,我使用@jitclass正确编译了类实例参数.

Unfortunately however, my odeSystem has to have a class instance as an argument. Hence I used @jitclass to compile the class instance argument properly.

我再次遇到另一个问题,该类"Dummy"也将另一种类型的类实例作为其属性之一.我不知道如何为此类设置规格".我用"nb.typeof(Hi)"尝试了"dummy1"的类型,但是没有用.

I again encountered another problem, where this class "Dummy" also takes another type of class instance as one of its attribute. I have no idea how to set "spec" for this class. I tried the type for "dummy1" with "nb.typeof(Hi)" but it didn't work.

请帮助我.预先感谢.

推荐答案

您可以在规范定义中使用.class_type.instance_type来保存其他类型的实例.请参见numba源代码树中的示例此处

You can use .class_type.instance_type in your spec definition for holding an instance of another type. See example in the numba source tree here

spec2=[("dummy", nb.i4), ("dummy1", Hi.class_type.instance_type)]
@nb.jitclass(spec2)
class Dummy(object):
    def __init__(self, anotherClassInstance):
        self.dummy = 0
        self.dummy1 = anotherClassInstance

这篇关于当类的属性包含另一个类实例时,如何指定numba jitclass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆