You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/lang/articles/advanced/meta.md
+15-14
Original file line number
Diff line number
Diff line change
@@ -166,26 +166,27 @@ def func():
166
166
print(3)
167
167
```
168
168
169
-
## When to use `ti.static` with for loops
170
-
171
-
There are two reasons to use `ti.static` with for loops:
172
-
173
-
- Loop unrolling for improving runtime performance (see [Compile-time evaluations](#compile-time-evaluations)).
174
-
- Accessing elements of Taichi matrices/vectors. Indices for accessing Taichi fields can be runtime variables, while indices for Taichi matrices/vectors **must be a compile-time constant**.
169
+
:::note
170
+
Before v1.4.0, indices for accessing Taichi matrices/vectors must be compile-time constants.
171
+
Therefore, if the indices come from a loop, the loop must be unrolled:
175
172
176
-
For example, when accessing a vector field `x` with `x[field_index][vector_component_index]`, the `field_index` can be a runtime variable, while the `vector_component_index` must be a compile-time constant:
173
+
```python {7}
174
+
# Here we declare a field containing 3 vectors. Each vector contains 8 elements.
175
+
x = ti.Vector.field(8, ti.f32, shape=3)
177
176
178
-
```python {6}
179
-
# Here we declare a field contains 3 vector. Each vector contains 8 elements.
180
-
x = ti.Vector.field(8, ti.f32, shape=(3))
181
177
@ti.kernel
182
178
defreset():
183
-
for i in x:
184
-
for j in ti.static(range(x.n)):
185
-
# The inner loop must be unrolled since j is an index for accessing a vector
186
-
x[i][j] =0
179
+
for i in x:
180
+
for j in ti.static(range(x.n)):
181
+
# The inner loop must be unrolled since j is an index for accessing a vector.
182
+
x[i][j] =0
187
183
```
188
184
185
+
Starting from v1.4.0, indices for accessing Taichi matrices/vectors can be runtime variables.
186
+
Therefore, the loop above is no longer required to be unrolled.
187
+
That said, unrolling it will still help you reduce runtime overhead.
188
+
:::
189
+
189
190
## Compile-time recursion of `ti.func`
190
191
191
192
A compile-time recursive function is a function with recursion that can be recursively inlined at compile time. The condition which determines whether to recurse is evaluated at compile time.
0 commit comments