import taichi as ti
ti.init(arch=ti.cuda) # 换成vulkan就会出问题
quality = 2
n_rigid_p = 80*quality # 每条边 /4个点
rigid_line = ti.Vector.field(2,ti.f32,shape=n_rigid_p)
rigid_p = ti.Vector.field(2,dtype=ti.f32,shape=n_rigid_p) # 每个点对应的位置
center = ti.Vector.field(2,dtype=ti.f32,shape=1)
my_test = ti.Vector.field(2,dtype=ti.f32,shape=1) # 用于输出结果
@ti.func
def init_rigid(x:ti.f32,y:ti.f32,w:ti.f32):
k = (n_rigid_p / 4)
dw = w/k
for i in range(k):
rigid_line[i] = ti.Vector([x+i*dw,y])
for i in range(k,2*k):
rigid_line[i] = ti.Vector([x+w,y+(i-k)*dw])
for i in range(2*k,3*k):
rigid_line[i] = ti.Vector([x+w-(i-2*k)*dw,y+w])
for i in range(3*k,4*k):
rigid_line[i] = ti.Vector([x,y+w-(i-3*k)*dw])
for i in range(n_rigid_p):
rigid_p[i] = (rigid_line[i]+rigid_line[(i+1)%n_rigid_p])/2
center[0]+=rigid_p[i]
center[0] /= n_rigid_p
@ti.func
def compute_rigid():
center[0] = ti.Vector([0.0,0.0])
for i in rigid_p:
center[0]+=rigid_p[i]
center[0] = center[0] / n_rigid_p
my_test[0] = ti.Vector([0,0])
for i in range(n_rigid_p):
x_rel = ti.Vector([0.0,0.0])
x_rel += (rigid_p[i] - center[0])
my_test[0] += x_rel
@ti.kernel
def init():
init_rigid(0.25,0.65,0.0625)
@ti.kernel
def sub_step():
compute_rigid()
def show(canvas):
canvas.circles(rigid_p, radius=0.003, color=(0, 0.98, 0.97))
def main():
init()
window = ti.ui.Window("my mpm", res=(512, 512), vsync=True)
canvas = window.get_canvas()
canvas.set_background_color((0.067, 0.184, 0.255))
step_size = 2
while window.running:
for i in range(step_size):
sub_step()
print(my_test[0])
show(canvas)
window.show()
if __name__ == '__main__':
main()
这段代码用cuda运行起来是正常的,但是用vulkan就很奇怪,我不知道这段代码是哪里出问题了,还是说我环境有问题,但我运行其他example里的程序好像都是正常的。
cuda的结果
vulkan的结果