我要传递numba作为我的功能的签名
@numba.jit(numba.types.UniTuple(numba.float64[:, :], 2)(
numba.float64[:, :], numba.float64[:, :], numba.float64[:, :],
earth_model_type))其中earth_model_type被定义为
earth_model_type = numba.deferred_type()
earth_model_type.define(em.EarthModel.class_type.instance_type)它编译得很好,但是当我试图调用这个函数时,
* TypeError:不匹配参数类型数组(float64,2d,F),数组(float64,2d,C),数组(float64,2d,F),instance.jitclass.EarthModel#7fd9c48dd668
在我看来,具有不匹配定义的参数类型与上面的类型大致相同。另一方面,如果我不通过仅仅使用@numba.jit(nopython=True)来指定签名,它就可以正常工作,由numba编译的函数的签名是
ipdb> numbed_cowell_propagator_propagate.signatures (数组(float64,2d,F),数组(float64,2d,C),数组(float64,2d,F),instance.jitclass.EarthModel#7f81bbc0e780)
编辑
如果我使用常见问题中的方式强制执行C级数组,我仍然会得到一个错误
TypeError:不匹配参数类型数组(float64,2d,C),数组(float64,2d,C),数组(float64,2d,C),instance.jitclass.EarthModel#7f6edd8d57b8
我非常肯定这个问题与延迟类型有关,因为如果不是传递jit类,而是传递该类(4个numba.float64s)所需的所有属性,那么它可以正常工作。
当我指定签名时,我做错了什么?
干杯。
发布于 2019-08-28 18:43:33
在不完全理解完整代码的工作原理的情况下,我不知道为什么需要使用延迟类型。通常,它用于包含相同类型的实例变量的jitclasses,如链接列表或其他节点树,因此需要推迟到编译器处理类本身之后(请参阅来源),下面的最小示例工作(如果使用延迟类型,我可以再现您的错误):
import numpy as np
import numba as nb
spec = [('x', nb.float64)]
@nb.jitclass(spec)
class EarthModel:
def __init__(self, x):
self.x = x
earth_model_type = EarthModel.class_type.instance_type
@nb.jit(nb.float64(nb.float64[:, :], nb.float64[:, :], nb.float64[:, :], earth_model_type))
def test(x, y, z, em):
return em.x然后运行它:
em = EarthModel(9.9)
x = np.random.normal(size=(3,3))
y = np.random.normal(size=(3,3))
z = np.random.normal(size=(3,3))
res = test(x, y, z, em)
print(res) # 9.9https://stackoverflow.com/questions/57640039
复制相似问题