Taichi Programming Language

1.7.4 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This example initializes Taichi, declares two 2D fields, and then defines a Taichi kernel to perform a simple computation over all elements. The `@ti.kernel` decorator compiles the Python function into a high-performance kernel that runs on the chosen backend (CPU or GPU).

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])

view raw JSON →