代码如下:
import taichi as ti
import scipy.io as sio
ti.init(arch = ti.gpu,default_fp=ti.f64)
NP=16384
x = ti.Vector.field(1,ti.f64,NP)
v = ti.Vector.field(3,ti.f64,NP)
nx=128
rho = ti.Vector.field(1,ti.f64,nx)
xx_mt=sio.loadmat('x_mt')#路径请手动设置
x_mt=xx_mt['x']
vvx_mt=sio.loadmat('vx_mt')
vx_mt=vvx_mt['vx']
for p in range(NP):
x[p].x = x_mt[p,0]
v[p].x = vx_mt[p,0]
v[p].y = 0.0
v[p].z = 0.0
@ti.kernel
def position():
for p in x:
x[p] += v[p].x
if x[p].x>=nx:
x[p] += -nx
if x[p].x<0:
x[p] += nx
@ti.kernel
def charge():
for i in rho:
rho[i].x = 0.000399992328#rho0
for p in x:#P2G
base = x[p].cast(int)
fx = (x[p].x - base.cast(ti.f64))
rho[base] += (1.0-fx)*(-3.1249400625e-06)#q
rho[base+1] += fx*(-3.1249400625e-06)
position()
charge()
mat文件放在了这里:
taichi-PIC-code/test_charge.zip at main · JiaoLuhuai/taichi-PIC-code (github.com)
使用IDLE运行,输出rho的前两个元素:
第1次测试结果:
[Taichi] version 1.0.2, llvm 10.0.0, commit 14f0acba, win, python 3.9.5
[Taichi] Starting on arch=cuda
rho[0].x
1.2526766219601858e-06
rho[1].x
-9.459752682234363e-07
第2次测试结果:
[Taichi] version 1.0.2, llvm 10.0.0, commit 14f0acba, win, python 3.9.5
[Taichi] Starting on arch=cuda
rho[0].x
1.2526766219601316e-06
rho[1].x
-9.459752682233065e-07
第3次测试结果(改为cpu上运行):
[Taichi] version 1.0.2, llvm 10.0.0, commit 14f0acba, win, python 3.9.5
[Taichi] Starting on arch=x64
rho[0].x
1.2526766219602847e-06
rho[1].x
-9.459752682232722e-07
第4次运行结果(cpu上运行):
[Taichi] version 1.0.2, llvm 10.0.0, commit 14f0acba, win, python 3.9.5
[Taichi] Starting on arch=x64
rho[0].x
1.2526766219602305e-06
rho[1].x
-9.459752682232722e-07
总结:
以rho的前两个元素为考察对象,
rho[0]每次都不一样(无论在GPU还是CPU上),差异在后四位;
rho[1]在GPU上得到的结果每次都不一样,差异在后四位,在CPU上得到的结果一样。
求助,怎么解决以上问题。