A Simple Tutorial
TaichiZoo supports a subset of the Taichi APIs. Here we list the supported Taichi features:
32-bits scalar data types
ti.i32
: A 32-bit signed integerti.f32
: A 32-bit single precision floating point
Dense field:
Function decorators
@ti.kernel
: Note that a TaichiZoo kernel only supports the aforementioned scalars as its paramter types. That is, a TaichiZoo kernel doesn't support types liketi.template()
orti.ext_arr()
.@ti.func
Taichi internal functions
- Basic mathematical functions:
ti.sin()
,ti.cos()
,ti.exp()
, etc. You can find these functions here. ti.random()
- Note that
print
is not supported yet
- Basic mathematical functions:
GUI draw functions:
GUI events
GUI key alias
ti.GUI.LEFT
,ti.GUI.RIGHT
,ti.GUI.UP
,ti.GUI.DOWN
,ti.GUI.LMB
,ti.GUI.RMB
,ti.GUI.PRESS
,ti.GUI.ESCAPE
,ti.GUI.EXIT
More details can be found on and .
Here, we will walkthrough how code is organized in TaichiZoo using the Fractal
example.
Initialize the Taichi runtime. Note that for TaichiZoo, the kernel will always run on the CPU backend (via WebAssembly), regardless of the specified
arch
.Allocate a field,
pixels
, to record the results.dtype
andshape
need to be specified during this process. In taichi,field
is a core notion representing a multi-dimensional array.Define a Taichi function,
complex_sqr()
, to do the real computation. The types of the parameters are inferred from the calling kernel scope.complex_sqr()
accepts a single parameterz
, which represents a complex number:z[0] + z[1] * i
. The function returnsz * z
.Define a Taichi kernel,
paint()
. This kernel simplify fills in each entry inpixels
.Initialize the GUI system. The size of the canvas in the browser is determined by the
res
parameter.Finally, we have some control flow code to invoke the Taichi kernels. TaichiZoo allows for control flows like
if
,for
andwhile
. From python scope, we can call into Taichi kernels and GUI-related methods.gui.set_image()
directly set each pixel color. We only support grayscale for this.gui.show()
will update the canvas in the browser.
Please make sure that all kernels are defined before calling any of them.