Skip to content

Commit

Permalink
[Doc] Update doc regarding dynamic index (taichi-dev#7148)
Browse files Browse the repository at this point in the history
  • Loading branch information
strongoier authored and quadpixels committed May 13, 2023
1 parent 7e62aa3 commit 3ddad05
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
29 changes: 15 additions & 14 deletions docs/lang/articles/advanced/meta.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,27 @@ def func():
print(3)
```

## When to use `ti.static` with for loops

There are two reasons to use `ti.static` with for loops:

- Loop unrolling for improving runtime performance (see [Compile-time evaluations](#compile-time-evaluations)).
- 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**.
:::note
Before v1.4.0, indices for accessing Taichi matrices/vectors must be compile-time constants.
Therefore, if the indices come from a loop, the loop must be unrolled:

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:
```python {7}
# Here we declare a field containing 3 vectors. Each vector contains 8 elements.
x = ti.Vector.field(8, ti.f32, shape=3)

```python {6}
# Here we declare a field contains 3 vector. Each vector contains 8 elements.
x = ti.Vector.field(8, ti.f32, shape=(3))
@ti.kernel
def reset():
for i in x:
for j in ti.static(range(x.n)):
# The inner loop must be unrolled since j is an index for accessing a vector
x[i][j] = 0
for i in x:
for j in ti.static(range(x.n)):
# The inner loop must be unrolled since j is an index for accessing a vector.
x[i][j] = 0
```

Starting from v1.4.0, indices for accessing Taichi matrices/vectors can be runtime variables.
Therefore, the loop above is no longer required to be unrolled.
That said, unrolling it will still help you reduce runtime overhead.
:::

## Compile-time recursion of `ti.func`

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.
Expand Down
1 change: 0 additions & 1 deletion docs/lang/articles/contribution/write_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,3 @@ Now, Taichi supports the following extensions:
| bls | Block-local storage |
| assertion | Run-time asserts in Taichi kernels |
| extfunc | Support inserting external function calls or backend source |
| dynamic_index | Dynamic index support for tensors |
3 changes: 0 additions & 3 deletions docs/lang/articles/reference/global_settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ Following are some frequently-used configurations in `ti.init()`:
default_ip: [ti.i32, ti.i64]
Set the default precision of integers in the Taichi scope.
dynamic_index: bool
Enable/disable the use of variables as indices when accessing vector/matrix elements in the Taichi scope.
kernel_profiler: bool
Turn on/off kernel performance profiling.
Expand Down
6 changes: 0 additions & 6 deletions docs/lang/articles/reference/language_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,6 @@ Otherwise, `primary` has a Taichi type. All Taichi types excluding primitive
types support subscriptions. You can refer to documentation of these types
for subscription usage.

:::note
When `primary` has a Taichi matrix type, all expressions in `expression_list`
are required to be evaluated to Python values. This restriction can be got rid
of by setting `ti.init(dynamic_index=True)`.
:::

#### Slicings

```
Expand Down

0 comments on commit 3ddad05

Please sign in to comment.