gpt4 book ai didi

python - 与 numba 的 prange 并行并传递元组

转载 作者:行者123 更新时间:2023-12-01 09:12:31 25 4
gpt4 key购买 nike

我尝试按如下方式并行化,紧随documented example :

@numba.jit(nopython=True)
def foo(uIdx, grids):
return uIdx

@numba.jit(nopython=True, parallel=True)
def bar(grid, grids):

LIdxGrid = np.zeros(len(grid))

for uIdx in numba.prange(len(grid)):
LIdxGrid[uIdx] = foo(uIdx, grids)
return LIdxGrid



if __name__ == '__main__':
import numpy as np
grid = np.arange(12)
grids = (grid, grid)
bar(grid, grids)

但它似乎不起作用。 问题似乎源于传递网格(甚至没有在最终的foo函数中使用它)。如果我删除 foobar 中的引用,它就会起作用:

bar(grid, 0)
Out[47]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.])

我该如何解决/解决这个问题?

完整的回溯是

Traceback (most recent call last):
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/errors.py", line 491, in new_error_context
yield
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/lowering.py", line 216, in lower_block
self.lower_inst(inst)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/lowering.py", line 365, in lower_inst
func(self, inst)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/npyufunc/parfor.py", line 119, in _lower_parfor_parallel
index_var_typ)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/npyufunc/parfor.py", line 691, in call_parallel_gufunc
sout, {})
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/npyufunc/parallel.py", line 251, in build_gufunc_wrapper
cache=cache)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/npyufunc/wrappers.py", line 460, in build_gufunc_wrapper
return wrapcls(py_func, cres, sin, sout, cache).build()
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/npyufunc/wrappers.py", line 411, in build
self._build_wrapper(wrapperlib, wrapper_name)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/npyufunc/wrappers.py", line 372, in _build_wrapper
arg_steps, i, step_offset, typ, sym, sym_dim)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/npyufunc/wrappers.py", line 614, in __init__
"argument #{1}".format(typ, i + 1))
TypeError: scalar type tuple(array(int64, 1d, C) x 2) given for non scalar argument #2
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-46-b6a12a1ce616>", line 3, in <module>
bar(grid, grids)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/dispatcher.py", line 360, in _compile_for_args
raise e
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/dispatcher.py", line 311, in _compile_for_args
return self.compile(tuple(argtypes))
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/dispatcher.py", line 618, in compile
cres = self._compiler.compile(args, return_type)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/dispatcher.py", line 83, in compile
pipeline_class=self.pipeline_class)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 871, in compile_extra
return pipeline.compile_extra(func)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 365, in compile_extra
return self._compile_bytecode()
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 802, in _compile_bytecode
return self._compile_core()
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 789, in _compile_core
res = pm.run(self.status)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 251, in run
raise patched_exception
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 243, in run
stage()
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 676, in stage_nopython_backend
self._backend(lowerfn, objectmode=False)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 626, in _backend
lowered = lowerfn()
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 613, in backend_nopython_mode
self.flags)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/compiler.py", line 990, in native_lowering_stage
lower.lower()
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/lowering.py", line 135, in lower
self.lower_normal_function(self.fndesc)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/lowering.py", line 176, in lower_normal_function
entry_block_tail = self.lower_function_body()
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/lowering.py", line 201, in lower_function_body
self.lower_block(block)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/lowering.py", line 216, in lower_block
self.lower_inst(inst)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/errors.py", line 499, in new_error_context
six.reraise(type(newerr), newerr, tb)
File "/home/foo/anaconda3/envs/myenv3/lib/python3.6/site-packages/numba/six.py", line 659, in reraise
raise value
numba.errors.LoweringError: Failed at nopython (nopython mode backend)
scalar type tuple(array(int64, 1d, C) x 2) given for non scalar argument #2
File "<ipython-input-44-ec97cbf0b87b>", line 9:
def bar(grid, grids):
<source elided>
LIdxGrid = np.zeros(len(grid))
^
[1] During: lowering "id=7[LoopNest(index_variable = parfor_index.317, range = (0, grid_size0.315, 1))]{51: <ir.Block at <ipython-input-44-ec97cbf0b87b> (9)>}Var(parfor_index.317, <ipython-input-44-ec97cbf0b87b> (9))" at <ipython-input-44-ec97cbf0b87b> (9)
-------------------------------------------------------------------------------
This should not have happened, a problem has occurred in Numba's internals.

最佳答案

对引用计数项(例如np.ndarrays)的支持是相当新的(从numba 0.39开始),我不确定是否可以使用ref的tuples 。计数的项目已经起作用。 Afaik 引用文献的元组。尚不支持计数项目。因此,为了确保您的代码有效,您必须将 tuple 替换为 list:

if __name__ == '__main__':
import numpy as np
grid = np.arange(12)
grids = [grid, grid]
bar(grid, grids)

并确保您安装了 numba 版本 0.39!否则这将无法正常工作。
当然,列表不是元组,因此这只是一种解决方法。但是没有其他方法可以解决这个问题,只要 ref 的元组就可以了。不完全支持计数项目。

关于python - 与 numba 的 prange 并行并传递元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51537589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com