# 同一个程序用ti.cpu与ti.cuda计算结果不同

``````import taichi as ti
from numpy import *
from os import listdir

dr=0.5
maxr=100;

@ti.data_oriented
class Staticgr:
def __init__(self, N ,M,CX,CY) -> None:
self.n = N
self.m = M
self.cx = CX
self.cy = CY
self.pos = ti.Vector.field(2, ti.f64, shape=self.n)
self.grRe = ti.Vector.field(1, ti.f64, shape=self.m)

def initialize(self):
for i in range(self.n):
self.pos[i] = ti.Vector([self.cx[i], self.cy[i]])
for j in range(self.m):
self.grRe[j]=ti.Vector([0])

@ti.kernel
def computegr(self):
for i in range(self.n):
p = self.pos[i]
for j in range(self.n):
if i != j:
diff = self.pos[j] - p
r=ti.sqrt(diff[0]*diff[0]+diff[1]*diff[1])
if r<maxr:
ids=ti.ceil(r/dr)-1
self.grRe[ids] +=1.0

def file2matrix(filename):
fr=open(filename)
returnMat=zeros((numberOfLines,2))
fr=open(filename)
index=0
line=line.strip()
listFromLine=line.split('\t')
returnMat[index,:]=listFromLine[0:2]
index+=1
return returnMat

ti.init(kernel_profiler=True, arch=ti.cuda)
data=file2matrix("7.txt")
gg = Staticgr(N=data.shape[0], M=ti.ceil(maxr/dr),CX=data[:,0],CY=data[:,1])
gg.initialize()
gg.computegr()
ti.profiler.print_kernel_profiler_info()
print(gg.grRe[98])
``````

cpu下输出的结果是 270710，gpu下输出的结果则是270712

``````ti.init(arch=ti.cpu, cpu_max_num_threads=1)
``````
1 Like