forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_grouped.py
181 lines (126 loc) · 3.4 KB
/
test_grouped.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import taichi as ti
@ti.all_archs
def test_vector_index():
val = ti.field(ti.i32)
n = 4
m = 7
p = 11
ti.root.dense(ti.i, n).dense(ti.j, m).dense(ti.k, p).place(val)
@ti.kernel
def test():
for i in range(n):
for j in range(m):
for k in range(p):
I = ti.Vector([i, j, k])
val[I] = i + j * 2 + k * 3
test()
for i in range(n):
for j in range(m):
for k in range(p):
assert val[i, j, k] == i + j * 2 + k * 3
@ti.all_archs
def test_grouped():
val = ti.field(ti.i32)
n = 4
m = 8
p = 16
ti.root.dense(ti.i, n).dense(ti.j, m).dense(ti.k, p).place(val)
@ti.kernel
def test():
for I in ti.grouped(val):
val[I] = I[0] + I[1] * 2 + I[2] * 3
test()
for i in range(n):
for j in range(m):
for k in range(p):
assert val[i, j, k] == i + j * 2 + k * 3
@ti.all_archs
def test_grouped_ndrange():
val = ti.field(ti.i32)
n = 4
m = 8
ti.root.dense(ti.ij, (n, m)).place(val)
x0 = 2
y0 = 3
x1 = 1
y1 = 6
@ti.kernel
def test():
for I in ti.grouped(ti.ndrange((x0, y0), (x1, y1))):
val[I] = I[0] + I[1] * 2
test()
for i in range(n):
for j in range(m):
assert val[i, j] == (i +
j * 2 if x0 <= i < y0 and x1 <= j < y1 else 0)
@ti.all_archs
def test_static_grouped_ndrange():
val = ti.field(ti.i32)
n = 4
m = 8
ti.root.dense(ti.ij, (n, m)).place(val)
x0 = 2
y0 = 3
x1 = 1
y1 = 6
@ti.kernel
def test():
for I in ti.static(ti.grouped(ti.ndrange((x0, y0), (x1, y1)))):
val[I] = I[0] + I[1] * 2
test()
for i in range(n):
for j in range(m):
assert val[i, j] == (i +
j * 2 if x0 <= i < y0 and x1 <= j < y1 else 0)
@ti.all_archs
def test_grouped_ndrange_starred():
val = ti.field(ti.i32)
n = 4
m = 8
p = 16
dim = 3
ti.root.dense(ti.ijk, (n, m, p)).place(val)
@ti.kernel
def test():
for I in ti.grouped(ti.ndrange(*(((0, n), ) * dim))):
val[I] = I[0] + I[1] * 2 + I[2] * 3
test()
for i in range(n):
for j in range(m):
for k in range(p):
assert val[i, j,
k] == (i + j * 2 + k * 3 if j < n and k < n else 0)
@ti.all_archs
def test_grouped_ndrange_0d():
val = ti.field(ti.i32, shape=())
@ti.kernel
def test():
for I in ti.grouped(ti.ndrange()):
val[I] = 42
test()
assert val[None] == 42
@ti.all_archs
def test_static_grouped_ndrange_0d():
val = ti.field(ti.i32, shape=())
@ti.kernel
def test():
for I in ti.static(ti.grouped(ti.ndrange())):
val[I] = 42
test()
assert val[None] == 42
@ti.all_archs
def test_static_grouped_func():
K = 3
dim = 2
v = ti.Vector.field(K, dtype=ti.i32, shape=((K, ) * dim))
def stencil_range():
return ti.ndrange(*((K, ) * (dim + 1)))
@ti.kernel
def p2g():
for I in ti.static(ti.grouped(stencil_range())):
v[I[0], I[1]][I[2]] = I[0] + I[1] * 3 + I[2] * 10
p2g()
for i in range(K):
for j in range(K):
for k in range(K):
assert v[i, j][k] == i + j * 3 + k * 10