python numba class_Numba jitclass不能使用python Lis
我正在使用python 3.6和{}。在
这个问题有一个sister,在这里我从另一个视角来处理这个问题。但它们不是同一个问题。
我实现了一个r-tree结构,并尝试使用Numba来提高性能,但是当我在搜索项目时不得不对必须访问的节点进行排队时,我遇到了一些问题。在
下面我提供了一个基本元素(节点)和get_all函数的示例,在这里我模拟了树中的搜索(这里我只获取其中的所有元素)。在
这个例子有点长,但希望足够清楚。在import numba as nb
import numpy as np
INF = np.iinfo(np.int64).max
node_type = nb.deferred_type()
stack_type = nb.deferred_type()
node_spec = [
('bbox', nb.int64[:]),
('data', nb.optional(nb.int32)),
('leaf', nb.optional(nb.boolean)),
('height', nb.optional(nb.int16)),
('children', nb.optional(stack_type))
]
@nb.jitclass(node_spec)
class Node(object):
def __init__(self, bbox, data, leaf, height, children):
self.bbox = bbox
self.data = data
self.leaf = leaf
self.height = height
self.children = children
node_type.define(Node.class_type.instance_type)
stack_spec = [
('node', node_type),
('next', nb.optional(stack_type)),
('valid', nb.boolean)
]
@nb.jitclass(stack_spec)
class Stack(object):
def __init__(self, node, next):
self.node = node
self.next = next
self.valid = True
stack_type.define(Stack.class_type.instance_type)
@nb.jit
def push(node, stack=None):
return Stack(node, stack)
@nb.jit
def get(stack, i):
ind = 0
while stack is not None:
if ind == i:
return stack
stack = stack.next
ind+=1
return stack
@nb.jit
def length(stack):
cnt = 0
while stack is not None and stack.valid == True:
cnt+=1
stack = stack.next
return cnt
def create_node(bbox, data=None, leaf=None, height=None, children=None):
node = Node(bbox, data, leaf, height, children)
return node
def create_root(children=None, height=1, leaf=True):
bbox = np.array([INF, INF, -INF, -INF], dtype=int)
return create_node(bbox, leaf=leaf, height=height, children=children)
def create_tree():
data = np.asarray([[1,2,1,2],
[2,3,2,3],
[3,5,3,5],
[5,7,6,8],
[5,6,5,6],
[6,8,6,8]], dtype=int)
bbox1 = np.array([1,3,1,3], dtype=int)
node1 = create_node(bbox1, leaf=True)
item0 = create_node(data[0], data=0)
node1.children = push(item0, node1.children)
item1 = create_node(data[1], data=1)
node1.children = push(item1, node1.children)
bbox2 = np.array([3,5,3,5], dtype=int)
node2 = create_node(bbox2, leaf=True)
item2 = create_node(data[2], data=2)
node2.children = push(item2, node2.children)
bbox3 = np.array([5,9,5,9], dtype=int)
node3 = create_node(bbox3, leaf=True)
item3 = create_node(data[3], data=3)
node3.children = push(item3, node3.children)
item4 = create_node(data[4], data=4)
node3.children = push(item4, node3.children)
item5 = create_node(data[5], data=5)
node3.children = push(item5, node3.children)
root = create_root(leaf=False)
root.children = push(node1, root.children)
root.children = push(node2, root.children)
root.children = push(node3, root.children)
return root
@nb.njit
def get_all(root_node):
nodes_to_search = [root_node]
items = []
while len(nodes_to_search) > 0:
print(len(nodes_to_search))
node = nodes_to_search.pop()
if not node.leaf:
for i in range(length(node.children)):
child = get(node.children, i)
nodes_to_search.append(child.node)
else:
for i in range(length(node.children)):
item = get(node.children, i)
items.append(item.node.data)
return items
def runme():
root = create_tree()
data = get_all(root)
print(data)
if __name__ == '__main__':
runme()
当我们使用njit修饰符运行此代码时,numba会引发以下错误:numba.errors.LoweringError: Failed at nopython (nopython mode backend)
list(instance.jitclass.Node#7fe171d3a5a8):
unsupported nested memory-managed object
如果我使用nopython=False(即jit)修饰符,代码运行良好,尽管numba warnings会抱怨lowering问题以及{}的问题。在
我的问题是:我做错了什么?如何避免上述列表问题?在
谢谢。在
python numba class_Numba jitclass不能使用python Lis相关推荐
- python numba class_Numba 开发手册 (二)
四.灵活专业的@generated_jit @jit修饰符在很多情况下是极其有效的,但是有时需要根据输入变量的类型来决定函数的实现功能.@generated_jit修饰符编译时能够进行一些特例化选择. ...
- python numba教程_numba从入门到精通(1)—为什么numba能够加速
真是哭了...为什么我之前写的numba教程全没了啊啊啊啊啊啊!!!!????? 只能从头整理了无语.How Numba and Cython speed up Python coderushter ...
- python: numba 加速python的 for loop
一.背景 写了个算法 需要多次计算两个字符串的编辑距离.这部分耗时比较多,想要进行优化.编辑距离代码见最后的附录.编辑距离本质上是双层for loop,而且是有依赖关系的for loop,因此无法并行 ...
- python numba cuda_Numba:基于CUDA加速的高性能Python
[注意:这篇文章最初于2013年9月19日发布,后于2017年9月19日更新.] Python是一种高效率的动态编程语言,广泛应用于科学,工程和数据分析等领域.导致python如此流行的原因有很多,主 ...
- python 预编译加速_让Python代码运行更快的最佳方式
Python因其强大.灵活且易于使用等特性,而赢得了声誉.这些优点使其在各种各样的应用程序.工作流程和领域中得到了广泛应用.但是就语言的设计,也就是它天然的解释能力还有它的运行时的动态性而言,Pyth ...
- Python reduce / map / filter 函数区别 - Python零基础入门教程
目录 一.map 函数 二.reduce 函数 三.filter 函数 四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 Pyth ...
- anaconda和python都有_Anaconda和python有什么区别-Anaconda和python区别介绍
软件功能:Anaconda正式版是款针对程序员们使用的编程开发工具.Anaconda最新版应用领域范围非常广,遍及了金融.人工智能.系统运维.Web开发.科学计算.云计算.大数据.游戏开发等多个方面. ...
- python国内外研究现状-对当今Python 快速发展的研究与展望
对当今Python 快速发展的研究与展望 摘要:本文通过分析Python语言的发展历史.设计哲学.应用领域和优缺点,分析了Pytho近年来快速发展的原因. 一.Python 简史 Python 是由G ...
- 真的不建议学Python,煞笔才学习Python,学Python难?两个小时足够搞定
对于Python这么语言,可以当作一门兴趣或爱好来学习,但是若是想找到份好的工作还是谨慎为主,这也是为什么不建议你搞Python的原因. 如果说,有些读者的学历非常牛逼,然后学习能力也非常强,那么选择 ...
最新文章
- 火狐浏览器中添加httprequest的方法
- 按下回车键指向下一个位置的一个函数
- python文本替换 数据库_Python pptx(Power Point)查找和替换文本(ctrl+H)
- C语言长精度除法,高精度除法小数点位数
- 光流 | 基于光流的运动物体语义分割(源代码)
- Unable to execute dex: Multiple dex files define Lcom/kenai/jbosh/AbstractAttr
- mysql复制架构迁移到pxc_2020-03-18 Mysql常见的HA中间层架构mmm,pxc,mha
- Equals和==的差别
- 去年写的测试GDAL用法的一些函数
- centos dns服务器_用 OpenStack Designate 构建一个 DNS 即服务(DNSaaS) | Linux 中国
- 2010年ImagineCup,我们共同走过
- SpringBoot系列(2):SpringBoot集成单机版Redis
- 射线法判断点在多边形内适用范围_电力行业NDT无损检测技术应用大盘点!
- cadz轴归零命令_cadz轴归零(cad全部z轴归零)
- 易经六十四卦详解白话文解释——易经64卦全解(下)
- EDI文件类型和传输协议
- 国外漂亮html5网页设,高端大气上档次!10个精美的国外HTML5网站欣赏_html/css_WEB-ITnose...
- 产品经理——产品原型设计规范
- java中x--与--x_GitHub - xsonorg/xco: XSON Common Object
- matlab中如何对一组复数频域信号进行快速傅里叶逆变换
热门文章
- revit打开服务器文件,Revit官方教程丨链接及打开IFC文件
- 数字水印处理的小小心得!!!
- 百度专业问答内容质量标准说明
- 8583报文举例解读及推荐阅读
- Gartner 2020 战略技术趋势- Hyper 自动化
- 什么汤晚上千万不能喝
- (一)maya2018快速装备创建骨骼和HumanIK创建骨骼、如何增加蒙皮,选择单个骨骼进行控制
- 420集的python教程视频_阿里达摩院推的420集的python教程高清版,据说懂中文就能入门...
- 4个led数码管从左至右显示123451单片机c语言,51单片机:4位数码管动态显示,从1234开始,每次按S1键加1...
- docker push异常error parsing HTTP 404 response body: invalid character ‘p‘ after top-level value: “404