From 3ddad05bc7a1f230feb6c6ea825debae6da61478 Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Thu, 12 Jan 2023 20:09:22 +0800 Subject: [PATCH] [Doc] Update doc regarding dynamic index (#7148) Issue: #2590 --- docs/lang/articles/advanced/meta.md | 29 ++++++++++--------- docs/lang/articles/contribution/write_test.md | 1 - .../articles/reference/global_settings.md | 3 -- .../articles/reference/language_reference.md | 6 ---- 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/docs/lang/articles/advanced/meta.md b/docs/lang/articles/advanced/meta.md index 23439380a1690..5f6a6456e3e15 100644 --- a/docs/lang/articles/advanced/meta.md +++ b/docs/lang/articles/advanced/meta.md @@ -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. diff --git a/docs/lang/articles/contribution/write_test.md b/docs/lang/articles/contribution/write_test.md index b79129a60370d..3f65b8a38e6f2 100644 --- a/docs/lang/articles/contribution/write_test.md +++ b/docs/lang/articles/contribution/write_test.md @@ -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 | diff --git a/docs/lang/articles/reference/global_settings.md b/docs/lang/articles/reference/global_settings.md index 44c54666fed3b..b5d3e4b00e501 100644 --- a/docs/lang/articles/reference/global_settings.md +++ b/docs/lang/articles/reference/global_settings.md @@ -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. diff --git a/docs/lang/articles/reference/language_reference.md b/docs/lang/articles/reference/language_reference.md index 667e213770176..3f9f6e19fce00 100644 --- a/docs/lang/articles/reference/language_reference.md +++ b/docs/lang/articles/reference/language_reference.md @@ -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 ```