运行环境
[Taichi] version 0.8.4, llvm 10.0.0, commit 895881b5, win, python 3.8.9
问题描述
在写N-body问题时,发现多余的分支会报错,尽管在逻辑上该分支并不执行,如下:
@ti.kernel
def initial(self,center:ti.template(),expand_size:ti.f32,init_vel:ti.f32):
for i in range(self.n):
if self.n == 1:
self.pos[i] = center
for j in ti.static(range(self.dim)):
self.vel[i][j] = 0
elif self.dim == 3:
offset = ti.Vector([ti.random(),ti.random(),ti.random()])-ti.Vector([expand_size,expand_size,expand_size])*0.5
originvel = ti.Vector([-offset[1],offset[0],0])*init_vel
self.pos[i] = center+offset
self.vel[i] = originvel
"""
elif self.dim == 2:
offset = ti.Vector([ti.random(),ti.random()])-ti.Vector([expand_size,expand_size])*0.5
originvel = ti.Vector([-offset[1],offset[0]])*init_vel
self.pos[i] = center+offset
self.vel[i] = originvel
"""
其中注释部分的内容如果去掉注释,在dim为3时并不执行,但仍会报错
Traceback (most recent call last):
File "G:\taichi\nbodyggui.py", line 29, in <module>
bodies.initial(centerpos,expandsize,initvel)
File "F:\python\lib\site-packages\taichi\lang\kernel_impl.py", line 859, in __call__
return self._primal(self._kernel_owner, *args, **kwargs)
File "F:\python\lib\site-packages\taichi\lang\kernel_impl.py", line 723, in __call__
key = self.ensure_compiled(*args)
File "F:\python\lib\site-packages\taichi\lang\kernel_impl.py", line 714, in ensure_compiled
self.materialize(key=key, args=args, arg_features=arg_features)
File "F:\python\lib\site-packages\taichi\lang\kernel_impl.py", line 518, in materialize
taichi_kernel = _ti_core.create_kernel(taichi_ast_generator,
File "F:\python\lib\site-packages\taichi\lang\kernel_impl.py", line 513, in taichi_ast_generator
compiled()
File "G:\taichi\adjustnbody.py", line 53, in initial
self.pos[i] = center+offset
File "F:\python\lib\site-packages\taichi\lang\common_ops.py", line 16, in __add__
return ti.add(self, other)
File "F:\python\lib\site-packages\taichi\lang\ops.py", line 74, in wrapped
return a.element_wise_binary(imp_foo, b)
File "F:\python\lib\site-packages\taichi\lang\matrix.py", line 161, in element_wise_binary
assert self.m == other.m and self.n == other.n, f"Dimension mismatch between shapes ({self.n}, {self.m}), ({other.n}, {other.m})"
AssertionError: Dimension mismatch between shapes (3, 1), (2, 1)