adam05
#1
学习了一下例子mgpcg_adavanced的代码。
有一个关于ti.root.pointer 的问题。
ti.root.pointer一共被调用了两次:
self.grid = ti.root.pointer(indices, [self.N_tot // 4]).dense(indices, 4).place(self.x, self.p, self.Ap)
for l in range(self.n_mg_levels):
self.grid = ti.root.pointer(indices,[self.N_tot // (4 * 2**l)]).dense(indices,4).place(self.r[l], self.z[l])
这里grid被赋值了两次指针,之后没有被调用过,那具体作用是什么呢?
这部分代码和下面的具体由什么区别?
ti.root.dense(indices, self.N_tot // 4).dense(indices, 4).place(self.x, self.p, self.Ap)
for l in range(self.n_mg_levels):
ti.root.dense(indices,self.N_tot // (4 * 2**l)).dense(indices,4).place(self.r[l], self.z[l])
个人感觉最后x, p, Ap, r, z的allocate应该是一致的。
但是程序给出的结果却不一样。
ti.root.pointer
declares a sparse tensor.
ti.root.dense
declares a dense tensor.
If x
is a dense tensor, then:
for i in x:
print(i)
will be same as:
for i in range(N):
print(i)
Otherwise, if x
is a sparse tensor, then:
for i in x:
print(i)
will be same as:
for i in range(N):
if ti.is_active(x, i):
print(i)
adam05
#3
感谢解答。
但是我还有一个问题:
active是怎么声明的?
比如:
self.grid = ti.root.pointer(indices, [4]).dense(indices, 4).place(x)
ti.root.dense(indices,4).dense(indices, 4).place(x)
最后x都应该是一个16*16的矩阵吧?
怎么看出哪部分x是active的呢?
怎么看出哪部分x是active的呢?
All indices that has been written is active:
x[2] = 233
x[4] = 666
x[5] = 888
for i in x:
print(i) # 2 4 5
adam05
#5
Ok, got it.
Thanks a lot.