Taichi Programming Language
Taichi is an open-source parallel programming language designed for high-performance visual computing. It allows users to write highly parallelized computational kernels using Python syntax, which are then compiled to run efficiently on various backends like GPUs (CUDA, Metal, Vulkan, OpenGL) or CPUs. It's currently at version 1.7.4 and maintains an active release schedule with frequent minor updates.
Common errors
-
AttributeError: module 'taichi' has no attribute 'experimental'
cause Attempting to use `@ti.experimental.real_func` after Taichi v1.7.0.fixReplace `@ti.experimental.real_func` with `@ti.real_func`. -
TypeError: ndarray() got an unexpected keyword argument 'field_dim'
cause Using the `field_dim` argument with `ti.ndarray` after Taichi v1.5.0.fixChange `field_dim` to `ndim` when defining your `ti.ndarray`, e.g., `ti.ndarray(ti.f32, ndim=2)`. -
AttributeError: 'Matrix' object has no attribute 'rotation2d'
cause Calling `rotation2d()` directly on a `ti.Matrix` object after Taichi v1.4.0.fixUse `ti.math.rotation2d()` for 2D rotation matrix generation. -
TypeError: ti.init() got an unexpected keyword argument 'packed'
cause Using the `packed` argument in `ti.init()` after Taichi v1.4.0.fixRemove `packed=True/False` from your `ti.init()` call. The SNode system manages memory packing automatically.
Warnings
- breaking The `@ti.experimental.real_func` decorator was deprecated in Taichi v1.7.0 and replaced by `@ti.real_func`.
- breaking The `field_dim` argument for `ti.ndarray` has been removed. Use `ndim` instead.
- breaking The `ti.Matrix.rotation2d()` method was removed. Its functionality is now provided by `ti.math.rotation2d()`.
- breaking The `packed` and `dynamic_index` arguments in `ti.init()` have been removed. `packed` was removed in v1.4.0, `dynamic_index` in v1.5.0.
- gotcha Slicing a single row or column of a `ti.Matrix` now returns a vector, not a 1xN or Nx1 matrix, potentially leading to dimension mismatches in subsequent matrix operations.
- deprecated The `ti.cc` backend is deprecated in favor of the Taichi Runtime (TiRT) and its C API, especially for Ahead-of-Time (AOT) deployment.
Install
-
pip install taichi
Imports
- taichi
import taichi as ti
- ti.math
import taichi as ti # ... later in code ... vec = ti.math.vec3(1, 2, 3)
- ti.real_func
@ti.experimental.real_func
@ti.real_func
Quickstart
import taichi as ti
ti.init(arch=ti.cpu) # Try ti.gpu for GPU acceleration if available
# Declare a 2D field (similar to a NumPy array)
N = 32
x = ti.field(ti.f32, shape=(N, N))
y = ti.field(ti.f32, shape=(N, N))
@ti.kernel
def compute():
for i, j in x: # Iterate over all elements in the field x
x[i, j] = float(i + j) / N
y[i, j] = x[i, j] * 2.0
if __name__ == '__main__':
compute()
print(f"x[0,0]: {x[0,0]}")
print(f"y[0,0]: {y[0,0]}")
# Fields can be converted to NumPy arrays for further processing:
# x_np = x.to_numpy()
# print(x_np[0,0])