\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{olina} \pdfinfo{ /Title (taichi-basics.pdf) /Creator (Cheatography) /Author (olina) /Subject (Taichi Basics Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{3DBEFF} \definecolor{LightBackground}{HTML}{E6F6FF} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Taichi Basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{olina} via \textcolor{DarkBackground}{\uline{cheatography.com/165198/cs/34724/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}olina \\ \uline{cheatography.com/olina} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 18th October, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Taichi in a Nutshell}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{A domain specific language embedded in Python for high-performance parallel computing \{\{nl\}\}Just-in-time (JIT) compilation \{\{nl\}\}Automatically parallelizes outermost for loops in a kernel \{\{nl\}\}Supports multiple backends (CPUs, CUDA, OpenGL, Metal...) \{\{nl\}\}Supports ahead-of-time compilation} \tn % Row Count 6 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Hello, World!}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{1. Install Taichi:}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`pip install -U taichi`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{2. Verify installation - Taichi gallery:}}} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`ti gallery`} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{3. Write your first Taichi program:}}} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`import taichi as ti \{\{nl\}\}ti.init(arch=ti.cpu) \{\{nl\}\}\# A backend can be either ti.cpu or ti.gpu \{\{nl\}\}\# When ti.gpu is specified, Taichi moves down the backend list of ti.cuda, ti.vulkan, and ti.opengl/ti.metal`} \tn % Row Count 10 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Data types}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Primitive data types}}: & `i8`, `i16`, `i32`, `i64`, `u8`, `u16`, `u32`, `u64`, `f16`, `f32`, `f64` \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{i: integer; u: unsigned integer; f: floating-point number \{\{nl\}\} Number following i/u/f stands for precision bits} \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{Change default types:}}} \tn % Row Count 8 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`\# Default integer type: ti.i32; default floating-point type: ti.f32 \{\{nl\}\} \seqsplit{ti.init(default\_ip=ti.i64)} \# Sets the default integer type to ti.i64 \{\{nl\}\} \seqsplit{ti.init(default\_fp=ti.f64)} \# Sets the default floating-point type to ti.f64`} \tn % Row Count 13 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{Explicit type casting:}}} \tn % Row Count 14 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`\# Use ti.cast(): \{\{nl\}\} a = 3.14 \{\{nl\}\}b = ti.cast(a, ti.i32) \# 3 \{\{nl\}\}c = ti.cast(b, ti.f32) \# 3.0 \{\{nl\}\} \# Use primitive types to convert a scalar variable to a different scalar type: \{\{nl\}\} a = 3.14 \{\{nl\}\}x = int(a) \# 3 \{\{nl\}\}y = float(a) \# 3.14 \{\{nl\}\}z = ti.i32(a) \# 3 \{\{nl\}\}w = ti.f64(a) \# 3.14`} \tn % Row Count 21 (+ 7) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{Implicit type casting:}}} \tn % Row Count 22 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Integer + floating point -\textgreater{} floating point \{\{nl\}\} Low-precision bits + high-precision bits -\textgreater{} high-precision bits \{\{nl\}\} Signed integer + unsigned integer -\textgreater{} unsigned integer} \tn % Row Count 26 (+ 4) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Compound data types:}}} \tn % Row Count 27 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{Vectors and matrices:}}} \tn % Row Count 28 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`vec4d = ti.types.vector(4, ti.f64) \# A 64-bit floating-point 4D vector type \{\{nl\}\}mat4x3i = ti.types.matrix(4, 3, int) \# A 4x3 integer matrix type \{\{nl\}\}v = vec4d(1, 2, 3, 4) \# Creates a vector instance: v = {[}1.0 2.0 3.0 4.0{]}` \{\{nl\}\}} \tn % Row Count 33 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Data types (cont)}} \tn % Row 11 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{Structs:}}} \tn % Row Count 1 (+ 1) % Row 12 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`\# Defines a compound type vec3 to represent a sphere's center \{\{nl\}\}vec3 = ti.types.vector(3, float) \{\{nl\}\}\# Defines a compound type sphere\_type to represent a sphere \{\{nl\}\}sphere\_type = \seqsplit{ti.types.struct(center=vec3}, radius=float) \{\{nl\}\}sphere = \seqsplit{sphere\_type(center=vec3(0)}, radius=1.0)`} \tn % Row Count 7 (+ 6) % Row 13 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Quantized/low-precision data types:}}} \tn % Row Count 8 (+ 1) % Row 14 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`\# Defines a 5-bit unsigned integer \{\{nl\}\}u5 = \seqsplit{ti.types.quant.int(bits=5}, signed=False) \{\{nl\}\}\# Defines a 10-bit signed fixed point type within the range {[}-20.0, 20.0{]} \{\{nl\}\}fixed\_type\_a = \seqsplit{ti.types.quant.fixed(bits=10}, max\_value=20.0) \{\{nl\}\}\# Defines a 15-bit unsigned floating-point type with six exponent bits \{\{nl\}\}float\_type\_b = \seqsplit{ti.types.quant.float(exp=6}, frac=9, signed=False)`} \tn % Row Count 16 (+ 8) % Row 15 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Sparse matrix (pending)}}} \tn % Row Count 17 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data container}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Field (global data container):}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{Declare:}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`\# Declares a scalar field \{\{nl\}\}scalar\_field = ti.field(int, shape=(640, 480)) \{\{nl\}\}\# Declares a vector field \{\{nl\}\}vector\_field = ti.Vector.field(n=2, dtype=float, shape=(1,2,3)) \{\{nl\}\}\# Declares a matrix field \{\{nl\}\}matrix\_field = ti.Matrix.field(n=3, m=2, dtype=float, shape=(300, 400, 500))`} \tn % Row Count 8 (+ 6) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{Index:}}} \tn % Row Count 9 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`f\_0d = ti.field(float, shape=()) \{\{nl\}\}f\_0d{[}None{]} = 1.0 \# Accesses the element in a 0D field \{\{nl\}\}f\_1d = ti.field(int, shape=10) \{\{nl\}\}f\_1d{[}5{]} = 1 \{\{nl\}\}f\_2d = ti.field(int, shape=(10, 10)) \{\{nl\}\}f\_2d{[}1, 2{]} = 255 \{\{nl\}\}f\_3d = ti.Vector.field(3, float, shape=(10, 10, 10)) \{\{nl\}\}f\_3d{[}3, 3, 3{]} = 1, 2, 3 \{\{nl\}\}f\_3d{[}3, 3, 3{]}{[}0{]} = 1`} \tn % Row Count 16 (+ 7) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{Interact with external arrays:}}} \tn % Row Count 17 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`x = ti.field(ti.f32, 4) \{\{nl\}\}x\_np = x.to\_numpy() \# Exports data in Taichi fields to NumPy arrays \{\{nl\}\}x.from\_numpy(x\_np) \# Imports data from NumPy arrays to Taichi fields \{\{nl\}\}x\_torch = x.to\_torch() \# Exports data in Taichi fields to PyTorch tensors \{\{nl\}\}x.from\_torch(torch.tensor({[}1, 7, 3, 5{]})) \# Imports data from PyTorch tensors to Taichi fields \{\{nl\}\}@ti.kernel \{\{nl\}\}def numpy\_as\_ndarray(arr: ti.ndarray()): \# Passes a NumPy ndarray to a kernel\{\{nl\}\}~~~~ for i in ti.ndrange(arr.shape{[}0{]}): \{\{nl\}\}~~~~~~~~ ...`} \tn % Row Count 29 (+ 12) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Ndarray:}} A multidimensional container of elements of the same type and size} \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data container (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`pos = ti.Vector.ndarray(2, ti.f32, N) \{\{nl\}\}vel = ti.Vector.ndarray(2, ti.f32, N) \{\{nl\}\}force = ti.Vector.ndarray(2, ti.f32, N)`} \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.28 cm} x{2.584 cm} x{2.736 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Kernels and functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Kernel:}} An entry point where Taichi's runtime begins to take over computation tasks. The outermost for loops in a kernel are automatically parallelized.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{3}{x{8.4cm}}{{\bf{Taichi function:}} A building block of kernels. you can split your tasks into multiple Taichi functions to improve readability and reuse them in different kernels.} \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Taichi kernel vs. Taichi function}}} \tn % Row Count 9 (+ 1) % Row 3 \SetRowColor{white} & Taichi kernel & Taichi function \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} Decorated with & @ti.kernel & @ti.func \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} Called from & Python scope & Taichi scope \tn % Row Count 14 (+ 1) % Row 6 \SetRowColor{LightBackground} Type hint arguments & Required & Recommended \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} Type hint return values & Required & Recommended \tn % Row Count 18 (+ 2) % Row 8 \SetRowColor{LightBackground} Return type & Scalar/`ti.Vector`/\{\{nl\}\}`ti.Matrix` & Scalar/`ti.Vector`/\{\{nl\}\}`ti.Matrix`/`ti.Struct`/... \tn % Row Count 22 (+ 4) % Row 9 \SetRowColor{white} Max. No. of elements in arguments & 32 (for OpenGL) \{\{nl\}\} 64 (for others) & Unlimited \tn % Row Count 25 (+ 3) % Row 10 \SetRowColor{LightBackground} Max. No. of return values & 1 & Unlimited \tn % Row Count 28 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Visualization}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{GUI system:}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`gui = ti.GUI('Window Title', (640, 360)) \# Creates a window \{\{nl\}\} while not \seqsplit{gui.get\_event(ti.GUI.ESCAPE}, ti.GUI.EXIT): \{\{nl\}\}~~~~gui.show() \# Displays the window`} \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{GGUI system:}}} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`pixels = ti.Vector.field(3, float, (640, 480)) \{\{nl\}\} window = ti.ui.Window("Window Title", (640, 360)) \# Creates a window \{\{nl\}\} canvas = window.get\_canvas() \# Creates a canvas \{\{nl\}\} \{\{nl\}\} while window.running: \{\{nl\}\}~~~~canvas.set\_image(pixels) \{\{nl\}\}~~~~window.show()`} \tn % Row Count 13 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data-oriented programming}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Data-oriented class:}} \{\{nl\}\}A data-oriented class is used when your data is actively updated in the Python scope (such as current time and user input events) and tracked in Taichi kernels.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`@ti.data\_oriented \# Decorates a class with a @ti.data\_oriented decorator \{\{nl\}\}class TiArray:\{\{nl\}\}~~~~ def \_\_init\_\_(self, n):\{\{nl\}\}~~~~~~~~ self.x = ti.field(dtype=ti.i32, shape=n) \{\{nl\}\} \{\{nl\}\}~~~~ @ti.kernel \# Defines Taichi kernels in the data-oriented Python class \{\{nl\}\}~~~~ def inc(self): \{\{nl\}\}~~~~~~~~ for i in self.x: \{\{nl\}\}~~~~~~~~~~~~ self.x{[}i{]} += 1 \{\{nl\}\} \{\{nl\}\} a = TiArray(32) \{\{nl\}\} a.inc()`} \tn % Row Count 17 (+ 13) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Taichi dataclass:}} \{\{nl\}\} A dataclass is a wrapper of `ti.types.struct`. You can define Taichi functions as its methods and call these methods in the Taichi scope.} \tn % Row Count 21 (+ 4) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`@ti.dataclass \{\{nl\}\}class Sphere: \{\{nl\}\}~~~~center: vec3 \{\{nl\}\}~~~~radius: float \{\{nl\}\}~~~~@ti.func \{\{nl\}\}~~~~def area(self): \# Defines a Taichi function as method \{\{nl\}\}~~~~~~~~return 4 {\emph{ math.pi }} self.radius**2 \{\{nl\}\} \{\{nl\}\}@ti.kernel \{\{nl\}\} def test(): \{\{nl\}\}~~~~sphere = Sphere(vec3(0), radius=1.0) \{\{nl\}\}~~~~print(sphere.area())`} \tn % Row Count 31 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Math}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Import Taichi's math module:}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`import taichi.math as tm`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{The module supports the following:}}} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{Mathematical functions:}}} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`\# Call mathematical functions in the Taichi scope \{\{nl\}\}@ti.kernel \{\{nl\}\}def test(): \{\{nl\}\}~~~~a = tm.vec3(1, 2, 3) \# A function can take vectors and matrices \{\{nl\}\}~~~~x = tm.sin(a) \# {[}0.841471, 0.909297, 0.141120{]} \# Element-wise operations \{\{nl\}\}~~~~y = tm.floor(a) \# {[}1.000000, 2.000000, 3.000000{]} \{\{nl\}\}~~~~z = tm.degrees(a) \# {[}57.295780, 114.591560, 171.887344{]}`} \tn % Row Count 13 (+ 9) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{Small vector and matrix types:}}} \tn % Row Count 14 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{~~~~vec2/vec3/vec4: 2D/3D/4D floating-point vector types \{\{nl\}\}~~~~ivec2/ivec3/ivec4: 2D/3D/4D integer vector types \{\{nl\}\}~~~~uvec2/uvec3/uvec4: 2D/3D/4D unsigned integer vector types \{\{nl\}\}~~~~mat2/mat3/mat4: 2D/3D/4D floating-point square matrix types} \tn % Row Count 21 (+ 7) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{GLSL-standard functions:}}} \tn % Row Count 22 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`@ti.kernel \{\{nl\}\}def example(): \{\{nl\}\}~~~~\# Takes vectors and matrices as arguments and operates on them element-wise \{\{nl\}\}~~~~v = tm.vec3(0, 1, 2) \{\{nl\}\}~~~~w = tm.smoothstep(0, 1, v) \{\{nl\}\} ~~~~w = tm.clamp(w, 0.2, 0.8) \{\{nl\}\}~~~~w = tm.reflect(v, \seqsplit{tm.normalize(tm.vec3(1)))`}} \tn % Row Count 30 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Math (cont)}} \tn % Row 9 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{Complex number operations in the form of 2D vectors:}}} \tn % Row Count 2 (+ 2) % Row 10 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`@ti.kernel \{\{nl\}\}def test(): \{\{nl\}\}~~~~x = tm.vec2(1, 1) \# Complex number 1+1j \{\{nl\}\}~~~~y = tm.vec2(0, 1) \# Complex number 1j \{\{nl\}\}~~~~z = tm.cmul(x, y) \# vec2(-1, 1) = -1+1j \{\{nl\}\}~~~~w = tm.cdiv(x, y) \# vec2(2, 0) = 2+0j`} \tn % Row Count 9 (+ 7) % Row 11 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Commonly used functions:}}} \tn % Row Count 10 (+ 1) % Row 12 \SetRowColor{white} `tm.acos(x) \{\{nl\}\} tm.asin(x) \{\{nl\}\} tm.atan2(x) \{\{nl\}\} tm.ceil(x) \{\{nl\}\} tm.clamp(x, xmin, xmax) \{\{nl\}\} tm.cos(x) \{\{nl\}\} tm.cross(x, y) \{\{nl\}\} tm.dot(x,y) \{\{nl\}\} tm.exp(x) \{\{nl\}\} tm.floor(x) \{\{nl\}\}tm.fract(x) \{\{nl\}\} tm.inverse(mat) \{\{nl\}\} tm.length(x) \{\{nl\}\} tm.log(x) \{\{nl\}\} tm.max(x, y, ...) ` & `tm.min(x, y, ...) \{\{nl\}\} tm.mix(x, y, a) \{\{nl\}\} tm.mod(x,y) \{\{nl\}\} tm.normalize(x) \{\{nl\}\} tm.pow(x, a) \{\{nl\}\} tm.round(x) \{\{nl\}\} tm.sign(x) \{\{nl\}\} tm.sin(x) \{\{nl\}\} tm.smoothstep(e0, e1, x) \{\{nl\}\} tm.sqrt(x) \{\{nl\}\} tm.step(edge, x) \{\{nl\}\} tm.tan(x) \{\{nl\}\} tm.tanh(x) \{\{nl\}\} tm.degrees(x) \{\{nl\}\} tm.radians(x)` \tn % Row Count 27 (+ 17) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Performance}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Profiling:}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{`scoped\_profiler` (default):}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`\# Analyzes the performance of the JIT compiler \{\{nl\}\} \seqsplit{ti.profiler.print\_scoped\_profiler\_info()`}} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\emph{`kernel\_profiler`}}:} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`\# Analyzes the performance of Taichi kernels \{\{nl\}\} ti.init(ti.cpu, kernel\_profiler=True) \# Enables the profiler \{\{nl\}\} \seqsplit{ti.profiler.print\_kernel\_profiler\_info()} \# Displays the results`} \tn % Row Count 9 (+ 4) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Tuning:}}} \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{`loop\_config()`: Serializes the outermost for loop that immediately follows it}}} \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{`ti.loop\_config(serialize=True)} \{\{nl\}\} \seqsplit{ti.loop\_config(parallelize=8)} \# Uses 8 threads on the CPU backend \{\{nl\}\} \seqsplit{ti.loop\_config(block\_dim=16)} \# Uses 16 threads in each block of the GPU backend`} \tn % Row Count 16 (+ 4) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\emph{Offline cache (default): Saves compilation cache on disk for future runs}}} \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{`ti.init(offline\_cache=True)`}} \tn % Row Count 19 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Debugging}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Activate debug mode:}} & {\bf{Conciser tracebacks:}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{`ti.init(arch=ti.cpu}, debug=True)` & `import sys \{\{nl\}\}sys.tracebacklimit = 0` \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Runtime `print` in Taichi scope:}} & {\bf{Serial execution:}} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} `@ti.kernel \{\{nl\}\} def \seqsplit{inside\_taichi\_scope():} \{\{nl\}\}~~~~x = 256 \{\{nl\}\}~~~~print('hello', x) \{\{nl\}\}~~~~\#=\textgreater{} hello 256` & `\# Serializes the program \{\{nl\}\} ti.init(arch=ti.cpu, \seqsplit{cpu\_max\_num\_threads=1)} \{\{nl\}\} \# Serializes the for loop that immediately follows the line \{\{nl\}\} \seqsplit{ti.loop\_config(serialize=True)`} \tn % Row Count 17 (+ 10) % Row 4 \SetRowColor{LightBackground} {\bf{Compile-time `ti.static\_print`:}} & {\bf{Runtime assert in Taichi scope:}} \tn % Row Count 19 (+ 2) % Row 5 \SetRowColor{white} `x = ti.field(ti.f32, (2, 3)) \{\{nl\}\} y = 1 \{\{nl\}\} \{\{nl\}\} @ti.kernel \{\{nl\}\} def \seqsplit{inside\_taichi\_scope():} \{\{nl\}\}~~~~ti.static\_print(y) \{\{nl\}\}~~~~\# =\textgreater{} 1 \{\{nl\}\}~~~~ti.static\_print(x.shape) \{\{nl\}\}~~~~\# =\textgreater{} (2, 3) \{\{nl\}\}~~~~ti.static\_print(x.dtype) \{\{nl\}\}~~~~\# =\textgreater{} DataType.float32` & `\# Activate debug mode before using assert statements in the Taichi scope \{\{nl\}\} ti.init(arch=ti.cpu, debug=True) \{\{nl\}\} \{\{nl\}\}~~~~x = ti.field(ti.f32, 128) \{\{nl\}\}\{\{nl\}\} @ti.kernel \{\{nl\}\} def do\_sqrt\_all(): \{\{nl\}\}~~~~for i in x: \{\{nl\}\}~~~~~~~~assert x{[}i{]} \textgreater{}= 0 \{\{nl\}\}~~~~~~~~x{[}i{]} = ti.sqrt(x{[}i{]})` \tn % Row Count 40 (+ 21) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Debugging (cont)}} \tn % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Compile-time `ti.static\_assert`:}}} \tn % Row Count 1 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`@ti.func \{\{nl\}\} def copy(dst: ti.template(), src: ti.template()): \{\{nl\}\}~~~~ti.static\_assert(dst.shape == src.shape, "copy() needs src and dst fields to be same shape") \{\{nl\}\}~~~~for I in ti.grouped(src): \{\{nl\}\}~~~~~~~~dst{[}I{]} = src{[}I{]}`} \tn % Row Count 8 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}