# debug模式下与正常模式运算结果不一样

import taichi as ti
from matplotlib import pyplot as plt
import numpy as np
num=1000
xmax=10000
ti.init(arch=ti.gpu)
temp=ti.Vector.field(n=2, dtype=int, shape=(xmax+1, num))
back2zero=ti.field(shape=(xmax+1),dtype=float)
first_back2zero=ti.field(shape=(xmax+1,num),dtype=float)
test=ti.Vector([0,1])
length=ti.field(shape=(xmax+1),dtype=float)
@ti.kernel
def compute():
temp.fill(0)
back2zero.fill(0)
first_back2zero.fill(0)

``````for x in range(10,xmax+1):
for i in range(num):
for j in range(x):
r1,r2= abs(ti.random(ti.i32)%2), abs(ti.random(ti.i32)%2)
temp[x,i][r1]+=2*r2-1
if (temp[x,i][0]==0) and (temp[x,i][1]==0):
back2zero[x]+=1
if first_back2zero[x,i]==0:
first_back2zero[x,i]=j+1
for x in range(10,xmax+1):
back2zero[x]/=num
for x in range(10,xmax+1):
for i in range(num):
first_back2zero[x,0]+=first_back2zero[x,i]
first_back2zero[x,0]/=num
for x in range(10,xmax+1):
for i in range(num):
length[x]+=(temp[x,i][0]**2+temp[x,i][1]**2)**0.5
length[x]/=num
``````

compute()
plt.plot(range(10,xmax+1),length.to_numpy()[10:])
plt.plot(range(10,xmax+1),back2zero.to_numpy()[10:])
plt.plot(range(10,xmax+1),first_back2zero.to_numpy()[10:,0])
plt.show()

``````import taichi as ti
from matplotlib import pyplot as plt
import numpy as np
num=20
xmax=10000
ti.init(arch=ti.gpu)
temp=ti.Vector.field(n=2, dtype=int, shape=(xmax+1, num))
back2zero=ti.field(shape=(xmax+1),dtype=float)
first_back2zero=ti.field(shape=(xmax+1,num),dtype=float)
test=ti.Vector([0,1])
length=ti.field(shape=(xmax+1),dtype=float)
@ti.kernel
def compute():
temp.fill(0)
back2zero.fill(0)
first_back2zero.fill(0)

for x in range(10,xmax+1):
for i in range(num):
for j in range(x):
r1,r2= abs(ti.random(ti.i32)%2), abs(ti.random(ti.i32)%2)
temp[x,i][r1]+=2*r2-1
if (temp[x,i][0]==0) and (temp[x,i][1]==0):
back2zero[x]+=1
if first_back2zero[x,i]==0:
first_back2zero[x,i]=j+1
for x in range(10,xmax+1):
back2zero[x]/=num
for x in range(10,xmax+1):
for i in range(num):
first_back2zero[x,0]+=first_back2zero[x,i]
first_back2zero[x,0]/=num
for x in range(10,xmax+1):
for i in range(num):
length[x]+=(temp[x,i][0]**2+temp[x,i][1]**2)**0.5
length[x]/=num

compute()
plt.plot(range(10,xmax+1),length.to_numpy()[10:])
plt.plot(range(10,xmax+1),back2zero.to_numpy()[10:])
plt.plot(range(10,xmax+1),first_back2zero.to_numpy()[10:,0])
plt.show()
``````