# Struct for和range for的性能比较

``````# 这个用了range for
@ti.kernel
def full_jacobian()->ti.f64:
...

# 这个用了struct for
@ti.kernel
def full_jacobian_sparse()->ti.f64:
...
``````

struct for on sparse matrix >> range for on dense matrix > range for on sparse matrix

2 Likes

struct for on sparse matrix >> range for on dense matrix > range for on sparse matrix

Taichi 的稀疏性能有时确实不如稠密，然而这个案例中主要是你使用方法的问题…

``````n = 50

A = ti.field(dtype=ti.f64)
ti.root.pointer(ti.ij, (n, n)).place(A)  # 错误的用法，pointer应当被嵌套

x = ti.field(dtype=ti.f64)
b = ti.field(dtype=ti.f64)
r = ti.field(dtype=ti.f64)
x_new = ti.field(dtype=ti.f64)
ti.root.pointer(ti.i, n).place(x, b, x_new,r)  # 错误的用法，x完全不必是稀疏的，只需要A稀疏即可
``````

``````n = 128  # n尽量为2的整数幂

A = ti.field(dtype=ti.f64)
ti.root.pointer(ti.ij, (n // 8, n // 8)).pointer(ti.ij, (8, 8)).place(A)  # 8x8个元素一组，两层稀疏结构。一层就和稠密没什么区别了

x = ti.field(dtype=ti.f64)
b = ti.field(dtype=ti.f64)
r = ti.field(dtype=ti.f64)
x_new = ti.field(dtype=ti.f64)
ti.root.dense(ti.i, n).place(x, b, x_new, r)  # x稠密即可，反正都要被写入
``````

``````Full Jacobian iteration took  3.334108829498291 sec and  20437 steps.
Sparse Jacobian iteration took  1.7451798915863037 sec and  20437 steps.
``````
1 Like

2. pointer嵌套多少层，每层多大，有什么指导性原则吗？