【问题标题】:Numba JIT giving LoweringError although code works fine otherwiseNumba JIT 给出 LoweringError 虽然代码工作正常,否则
【发布时间】:2019-03-23 04:34:42
【问题描述】:

我在 python 中有以下代码来计算 Mandelbrot 集。它工作正常,但是当我尝试通过在函数 def 之前添加 @jit 装饰器来使用 JIT 编译它时,它不再工作了。谁能告诉我为什么?如果您不批评我的 Mandelbrot 计算(我猜它可以被优化),我将不胜感激,只是让我知道为什么 JIT 不适用于此功能。顺便说一句,代码在def之后缩进。当我将它插入此处时,它并没有这样显示。

def mandelbrot(xmin,xmax,ymin,ymax,width,height,maxiter):
points=[]
x=np.linspace(xmin,xmax,width)
y=np.linspace(ymin,ymax,height)
for ix,re in enumerate(x):
    points.append([])
    for iy,im in enumerate(y):
        cx=re
        cy=im
        zx=0
        zy=0
        for n in range(maxiter):
            if zx*zx+zy*zy>4.0:
                iters=n
                break
            else:
                oldzx=zx
                oldzy=zy
                zy = 2*oldzx*oldzy+cy
                zx = oldzx*oldzx-oldzy*oldzy+cx  
                iters=n
        points[ix].append(int(iters))
return points

我收到以下错误报告,最后一行是 LoweringError

runfile('D:/python 程序/mandelbrot/mandelbrot.py', wdir='D:/python 程序/mandelbrot') Traceback(最近一次调用最后一次):

文件“”,第 1 行,在 runfile('D:/python 程序/mandelbrot/mandelbrot.py', wdir='D:/python 程序/mandelbrot')

文件 "C:\Users\Matthew\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", 第 786 行,在运行文件中 execfile(文件名,命名空间)

文件 "C:\Users\Matthew\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", 第 110 行,在 execfile 中 exec(编译(f.read(),文件名,'exec'),命名空间)

文件“D:/python 程序/mandelbrot/mandelbrot.py”,第 41 行,在 mandelbrot_set=mandelbrot(-2.0,1.0,-1.5,1.5,500,500,50)

文件 "C:\Users\Matthew\Anaconda3\lib\site-packages\numba\dispatcher.py", 第 368 行,在 _compile_for_args 提高e

文件 "C:\Users\Matthew\Anaconda3\lib\site-packages\numba\dispatcher.py", 第 325 行,在 _compile_for_args 返回 self.compile(tuple(argtypes))

文件 "C:\Users\Matthew\Anaconda3\lib\site-packages\numba\dispatcher.py", 第 653 行,在编译中 cres = self._compiler.compile(args, return_type)

文件 "C:\Users\Matthew\Anaconda3\lib\site-packages\numba\dispatcher.py", 第 83 行,在编译中 pipeline_class=self.pipeline_class)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 第873章 返回 pipeline.compile_extra(func)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 367,在 compile_extra 中 return self._compile_bytecode()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 804,在_compile_bytecode中 返回 self._compile_core()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 791,在_compile_core res = pm.run(self.status)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 253,运行中 引发 patched_exception

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 245,运行中 阶段()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 第438章 cres = self.frontend_looplift()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 428,在frontend_looplift中 提升=元组(循环),提升_从=无)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 887,在 compile_ir 中 lifted_from=lifted_from)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 375,在 compile_ir 中 return self._compile_ir()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 811,在_compile_ir 返回 self._compile_core()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 791,在_compile_core res = pm.run(self.status)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 253,运行中 引发 patched_exception

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 245,运行中 阶段()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 第652章 self._backend(lowerfn, objectmode=True)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 628,在_后端 降低 = lowerfn()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 601,在 backend_object_mode self.flags)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\compiler.py”,行 1018,在 py_lowering_stage lower.lower()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\lowering.py”,行 173,在较低 self.lower_normal_function(self.fndesc)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\lowering.py”,行 214,在 lower_normal_function 中 entry_block_tail = self.lower_function_body()

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\lowering.py”,行 239,在lower_function_body中 self.lower_block(块)

文件 “C:\Users\Matthew\Anaconda3\lib\site-packages\numba\lowering.py”,行 254,在lower_block中 self.lower_inst(inst)

文件“C:\Users\Matthew\Anaconda3\lib\contextlib.py”,第 130 行,在 退出 self.gen.throw(类型、值、回溯)

文件 "C:\Users\Matthew\Anaconda3\lib\site-packages\numba\errors.py", 第 585 行,在 new_error_context 六.reraise(type(newerr), newerr, tb)

文件 "C:\Users\Matthew\Anaconda3\lib\site-packages\numba\six.py", 第 659 行,在再加注中 提升价值

LoweringError: iters

文件“mandelbrot.py”,第 17 行:def mandelbrot(xmin,xmax,ymin,ymax,宽度,高度,maxiter): 对于 ix,re 在 enumerate(x) 中: points.append([]) ^

【问题讨论】:

    标签: python-3.x math jit mandelbrot


    【解决方案1】:

    我解决了这个问题。看来我需要在循环开始时将变量“iters”初始化为 0。因此,当我将 @jit 放在它前面时,它就起作用了。有人知道为什么吗?

    def mandelbrot(xmin,xmax,ymin,ymax,width,height,maxiter):
    points=[]
    x=np.linspace(xmin,xmax,width)
    y=np.linspace(ymin,ymax,height)
    for ix,re in enumerate(x):
        points.append([])
        for iy,im in enumerate(y):
            iters=0
            cx=re
            cy=im
            zx=0
            zy=0
            for n in range(maxiter):
                if zx*zx+zy*zy>4.0:
                    iters=n
                    break
                else:
                    oldzx=zx
                    oldzy=zy
                    zy = 2*oldzx*oldzy+cy
                    zx = oldzx*oldzx-oldzy*oldzy+cx  
                    iters=n
            points[ix].append(iters)
    return points
    

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      相关资源
      最近更新 更多