# 同一段代码用cuda和vulkan运行结果不一样

``````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):

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的结果

``````    for i in range(n_rigid_p):
my_test[0] += (rigid_p[i] - center[0])
``````

``````import taichi as ti

ti.init(arch=ti.cuda) # 换成vulkan就会出问题

n_rigid_p = 160 # 每条边 /4个点
rigid_p = ti.Vector.field(2,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.kernel
def init_rigid(x:ti.f32,y:ti.f32,w:ti.f32):
# x,y就是正方形的左下角，w是正方形的边长
k = (n_rigid_p / 4) #每一条边上的点的个数
dw = w/k
# 初始化每一个点的位置
for i in range(k):
rigid_p[i] = ti.Vector([x+i*dw,y])
for i in range(k,2*k):
rigid_p[i] = ti.Vector([x+w,y+(i-k)*dw])
for i in range(2*k,3*k):
rigid_p[i] = ti.Vector([x+w-(i-2*k)*dw,y+w])
for i in range(3*k,4*k):
rigid_p[i] = ti.Vector([x,y+w-(i-3*k)*dw])

@ti.kernel
def test():
#先求出中心
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

# 这里就是出问题的代码，它应该是一个很接近[0,0]的向量
# 但是使用ti.init(arch=ti.vulkan) 结果却很奇怪
# 使用ti.init(arch=ti.cuda)感觉更符合预期结果
my_test[0] = ti.Vector([0,0])
for i in rigid_p:
my_test[0] += (rigid_p[i] - center[0])

def main():
init_rigid(0.25,0.65,0.0625)
test()
print(my_test[0])

if __name__ == '__main__':
main()
``````

``````center = ti.Vector.field(2,dtype=ti.f32,shape=())
my_test = ti.Vector.field(2,dtype=ti.f32,shape=())
``````

Report 里的复现代码进行了进一步精简，目前问题似乎的确是 Vulkan backend 有bug的样子