我正在尝试使用numbapro来运行对象交互的基本模拟。但我收到此错误,我不知道问题是什么。尝试GPU加速时我很新:( 这是我的代码:
from turtle import *
import numpy as np
from numbapro import vectorize
setup( width = 1920, height = 1080, startx = None, starty = None)
colormode(255)
G = 1 #6.67428*10**(-11)
AU = 1 #149597871 * 1000 # 1 AU in meters
dt = 0.1
dt = np.float64(dt)
SCALE = 1 #150 / Au
bodiesVx = np.random.randint(1,11,10)
bodiesVy = np.random.randint(1,11,10)
bodiesM = np.random.randint(1,11,10)
bodiesPx = np.random.randint(760,1060,10)
bodiesPy = np.random.randint(440,640,10)
bodiesM = bodiesM.astype('float64')
bodiesPx = bodiesPx.astype('float64')
bodiesPy = bodiesPy.astype('float64')
bodiesVx = bodiesVx.astype('float64')
bodiesVy = bodiesVy.astype('float64')
@vectorize(['float64(float64)'],target="gpu")
def UpdateX(i):
print (i)
global bodiesM, bodiesPx, bodiesVx, dt, G
PX = bodiesPx[bodiesPx-i == 0][0]
MArr = bodiesM[bodiesPx-bodiesPx[i] != 0]
PxArr = bodiesPx[bodiesPx != PX]
AccArrX = MArr*G/(PxArr-PX)**2
Vx = dt * np.sum(AccArrX) + bodiesVx[bodiesPx == PX][0]
return Vx * dt
@vectorize(['float64(float64)'],target="gpu")
def UpdateY(i):
global bodiesM, bodiesPy, bodiesVy, dt, G
PY = bodiesPy[bodiesPy-i == 0][0]
MArr = bodiesM[bodiesPy-bodiesPy[i] != 0]
PyArr = bodiesPy[bodiesPy != PY]
AccArrY = MArr*G/(PyArr-PY)**2
Vy = dt * np.sum(AccArrY) + bodiesVy[bodiesPy == PY][0]
return Vy * dt
def move(i):
Turtle.goto(bodiesPx[i], bodiesPy[i])
def main():
global bodiesPx, bodiesPy
while True:
bodiesPx += UpdateX
bodiesPy += UpdateY
for i in range(len(bodiesPx)):
move(i)
main()
这是错误:
---------------------------------------------------------------------------
TypingError Traceback (most recent call last)
<ipython-input-29-03f769017233> in <module>()
27 bodiesVy = bodiesVy.astype('float64')
28
---> 29 @vectorize(['float64(float64)'],target="gpu")
30 def UpdateX(i):
31 print (i)
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\npyufunc\decorators.py in wrap(func)
108 vec = Vectorize(func, **kws)
109 for sig in ftylist:
--> 110 vec.add(sig)
111 if len(ftylist) > 0:
112 vec.disable_compile()
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\npyufunc\deviceufunc.py in add(***failed resolving arguments***)
388 kernelsource = self._get_kernel_source(self._kernel_template,
389 devfnsig, funcname)
--> 390 corefn, return_type = self._compile_core(devfnsig)
391 glbl = self._get_globals(corefn)
392 sig = signature(types.void, *([a[:] for a in args] + [return_type[:]]))
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\cuda\vectorizers.py in _compile_core(self, sig)
15 class CUDAVectorize(deviceufunc.DeviceVectorize):
16 def _compile_core(self, sig):
---> 17 cudevfn = cuda.jit(sig, device=True, inline=True)(self.pyfunc)
18 return cudevfn, cudevfn.cres.signature.return_type
19
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\cuda\decorators.py in device_jit(func)
96 def device_jit(func):
97 return compile_device(func, restype, argtypes, inline=inline,
---> 98 debug=debug)
99
100 if device:
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\cuda\compiler.py in compile_device(pyfunc, return_type, args, inline, debug)
120
121 def compile_device(pyfunc, return_type, args, inline=True, debug=False):
--> 122 cres = compile_cuda(pyfunc, return_type, args, debug=debug, inline=inline)
123 devfn = DeviceFunction(cres)
124
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\cuda\compiler.py in compile_cuda(pyfunc, return_type, args, debug, inline)
38 return_type=return_type,
39 flags=flags,
---> 40 locals={})
41
42 library = cres.library
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\compiler.py in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library)
663 pipeline = Pipeline(typingctx, targetctx, library,
664 args, return_type, flags, locals)
--> 665 return pipeline.compile_extra(func)
666
667
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\compiler.py in compile_extra(self, func)
364 raise e
365
--> 366 return self.compile_bytecode(bc, func_attr=self.func_attr)
367
368 def compile_bytecode(self, bc, lifted=(), lifted_from=None,
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\compiler.py in compile_bytecode(self, bc, lifted, lifted_from, func_attr)
373 self.lifted_from = lifted_from
374 self.func_attr = func_attr
--> 375 return self._compile_bytecode()
376
377 def compile_internal(self, bc, func_attr=DEFAULT_FUNCTION_ATTRIBUTES):
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\compiler.py in _compile_bytecode(self)
650
651 pm.finalize()
--> 652 return pm.run(self.status)
653
654
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\compiler.py in run(self, status)
249 # No more fallback pipelines?
250 if is_final_pipeline:
--> 251 raise patched_exception
252 # Go to next fallback pipeline
253 else:
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\compiler.py in run(self, status)
241 for stage, stage_name in self.pipeline_stages[pipeline_name]:
242 try:
--> 243 res = stage()
244 except _EarlyPipelineCompletion as e:
245 return e.result
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\compiler.py in stage_nopython_frontend(self)
461 self.args,
462 self.return_type,
--> 463 self.locals)
464
465 with self.fallback_context('Function "%s" has invalid return type'
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\compiler.py in type_inference_stage(typingctx, interp, args, return_type, locals)
778
779 infer.build_constraint()
--> 780 infer.propagate()
781 typemap, restype, calltypes = infer.unify()
782
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\typeinfer.py in propagate(self)
563 self.debug.propagate_finished()
564 if errors:
--> 565 raise errors[0]
566
567 def add_type(self, var, tp, unless_locked=False):
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\typeinfer.py in propagate(self, typeinfer)
109 for constraint in self.constraints:
110 try:
--> 111 constraint(typeinfer)
112 except TypingError as e:
113 errors.append(e)
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\typeinfer.py in __call__(self, typeinfer)
335 class IntrinsicCallConstraint(CallConstraint):
336 def __call__(self, typeinfer):
--> 337 self.resolve(typeinfer, typeinfer.typevars, fnty=self.func)
338
339
C:\Users\Gavin\Anaconda3\lib\site-packages\numba\typeinfer.py in resolve(self, typeinfer, typevars, fnty)
315 head = headtemp.format(fnty, ', '.join(map(str, args)))
316 msg = '\n'.join([head, desc])
--> 317 raise TypingError(msg, loc=self.loc)
318 typeinfer.add_type(self.target, sig.return_type)
319 # If the function is a bound function and its receiver type
TypingError: Failed at nopython (nopython frontend)
Invalid usage of - with parameters (readonly array(float64, 1d, C), float64)
Known signatures:
* (int64, int64) -> int64
* (int64, uint64) -> int64
* (uint64, int64) -> int64
* (uint64, uint64) -> uint64
* (float32, float32) -> float32
* (float64, float64) -> float64
* (complex64, complex64) -> complex64
* (complex128, complex128) -> complex128
* (uint32,) -> uint64
* (uint16,) -> uint64
* (uint8,) -> uint64
* (uint64,) -> uint64
* (int16,) -> int64
* (int32,) -> int64
* (int64,) -> int64
* (int8,) -> int64
* (float32,) -> float32
* (float64,) -> float64
* (complex64,) -> complex64
* (complex128,) -> complex128
* parameterized
File "<ipython-input-29-03f769017233>", line 33
任何想法都将不胜感激! 谢谢!
答案 0 :(得分:0)
我在检查您的代码时看到了许多问题:
您不能像在标准python中那样使用Numba中的global
关键字。在numba函数中调用的所有全局数组在编译时都变为静态(http://numba.pydata.org/numba-doc/0.26.0/user/faq.html#numba-doesn-t-seem-to-care-when-i-modify-a-global-variable)。通常,您应该将需要修改的所有数组作为参数传递给函数。
当你在UpdateX
中调用UpdateY
和main()
时,你没有传递一个参数,尽管你在函数定义中用一个参数定义它。