大一/python/taichi/数据

初始化

1
2
import taichi as ti
ti.init(arch=ti.gpu)

修饰

1
2
@ti.kernel#某个函数
#最外层才会并行

数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cast#i32/i64/f32/f64
b=ti.cast(a,ti.i32)#转换

#大写
a=ti.Vector([0.0,0.0,0.0])
B=ti.Matrix([1.5,1.4],[1.3,1.2])
B[1,0]
r=ti.Struct(v1=a,v2=0)#结构体

#N-d array(global)
c=ti.field(dtype=ti.f32,shape=(256,256))
#shape=4 -vector
#shape=() [None]
vf=ti.Vector.field(n=3,shape=4)#m每个元素为(3*1)的小向量
#m=2(3*2)

计算

1
2
+=/ti.atomic_add(a,b)
#防止数据征用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import taichi as ti
ti.init(ti.gpu)

N=2
vol=ti.Vector.field(2,ti.f32,N)#二维向量
acc=ti.Vector.field(2,ti.f32,N)
pos=ti.Vector.field(2,ti.f32,N)
substepping=10

@ti.kernel
def initialize():
for i in range(N):
pos[i]=[ti.random(),ti.random()]
vol[i]=[ti.random(),ti.random()]

gui=ti.GUI("n body problem",(512,512))

while(gui.running):
# for i in range(substepping):
gui.circle(pos.to_numpy(),color=0xFFFFFF,radius=2)
gui.show()
Shiwei Pan wechat