<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dunfan Lu on Medium]]></title>
        <description><![CDATA[Stories by Dunfan Lu on Medium]]></description>
        <link>https://medium.com/@dunfan?source=rss-3989b4cc81ef------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wGb4FK3AMpOolMrcweE-1Q.jpeg</url>
            <title>Stories by Dunfan Lu on Medium</title>
            <link>https://medium.com/@dunfan?source=rss-3989b4cc81ef------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 16:53:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dunfan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Painless WebGPU Programming With taichi.js]]></title>
            <link>https://medium.com/better-programming/painless-webgpu-programming-with-taichi-js-afa43c7adb2e?source=rss-3989b4cc81ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/afa43c7adb2e</guid>
            <category><![CDATA[webgpu]]></category>
            <category><![CDATA[computer-graphics]]></category>
            <category><![CDATA[webgl]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dunfan Lu]]></dc:creator>
            <pubDate>Thu, 25 May 2023 01:59:22 GMT</pubDate>
            <atom:updated>2023-06-09T05:24:49.220Z</atom:updated>
            <content:encoded><![CDATA[<h4>Modern GPU compute and rendering in JavaScript</h4><p>As a computer graphics and programming languages geek, I am delighted to have found myself working on several GPU compilers in the past two years.</p><p>This began in 2021 when I started to contribute to <a href="https://github.com/taichi-dev/taichi">taichi</a>, a Python library that compiles Python functions into GPU kernels in CUDA, Metal, or Vulkan. Later on, I joined Meta and started working on <a href="https://sparkar.facebook.com/ar-studio/learn/sparksl/sparksl-overview/">SparkSL</a>, which is the shader language that powers cross-platform GPU programming for AR effects on Instagram and Facebook.</p><p>Aside from personal pleasure, I have always believed, or at least hoped, that these frameworks were quite useful. They make GPU programming more accessible to non-experts, empowering people to create fascinating graphics content without having to master complex GPU concepts.</p><p>In my latest installment of compilers, I turned my eyes to WebGPU — the next-generation graphics API for the web. WebGPU promises to bring high-performance graphics via low CPU overhead and explicit GPU control, aligning with the trend started by Vulkan and D3D12 some seven years ago.</p><p>Just like Vulkan, the performance benefits of WebGPU come at the cost of a steep learning curve. Although I’m confident this won’t stop talented programmers worldwide from building amazing content with WebGPU, I wanted to provide people with a way to play with WebGPU without having to confront its complexity. This is how <a href="https://github.com/AmesingFlank/taichi.js">taichi.js</a> came to be.</p><p>Under the taichi.js programming model, programmers don&#39;t have to reason about WebGPU concepts such as devices, command queues, bind groups, etc. Instead, they write plain JavaScript functions, and the compiler translates those functions into WebGPU compute or render pipelines. This means anyone can write WebGPU code via taichi.js, as long as they are familiar with basic JavaScript syntax.</p><p>The remainder of this article will demonstrate the programming model of taichi.js via a &quot;Game of Life&quot; program. As you will see, with less than 100 lines of code, we will create a fully parallel WebGPU program containing three GPU compute pipelines plus a render pipeline. The full source code of the demo can be found <a href="https://github.com/AmesingFlank/taichi.js/blob/master/examples/game-of-life/index.js">here</a>, and if you want to play with the code without having to set up any local environments, go to <a href="https://taichi-js.com/playground/game-of-life">this page</a>.</p><figure><img alt="https://taichi-js.com/playground/game-of-life" src="https://cdn-images-1.medium.com/max/512/1*BVb4Q01xLSswe6ulD-SU0A.gif" /></figure><h3>The Game</h3><p>The Game of Life is a classic example of a cellular automaton, a cell system that evolves over time according to simple rules. It was invented by the mathematician John Conway in 1970 and has since become a favorite of computer scientists and mathematicians alike. The game is played on a two-dimensional grid, where each cell can be alive or dead. The rules for the game are simple:</p><ul><li>If a living cell has fewer than two or more than three living neighbors, it dies</li><li>If a dead cell has exactly three living neighbors, it becomes alive.</li></ul><p>Despite its simplicity, the Game of Life can exhibit surprising behavior. Starting from any random initial state, the game often converges to a state where a few patterns are dominant as if these are “species” which survived through evolution.</p><h3>Simulation</h3><p>Let’s dive into the Game of Life implementation using taichi.js. To begin with, we import the taichi.js library under the shorthand ti, and define an async main() function that will contain all of our logic. Within main(), we begin by calling ti.init(), which initializes the library and its WebGPU contexts.</p><pre>import * as ti from &quot;path/to/taichi.js&quot;<br><br>let main = async () =&gt; {<br>    await ti.init();<br>    ...<br>};<br><br>main()</pre><p>Following ti.init(), let&#39;s define the data structures needed by the &quot;Game of Life&quot; simulation:</p><pre>    let N = 128;<br><br>    let liveness = ti.field(ti.i32, [N, N])<br>    let numNeighbors = ti.field(ti.i32, [N, N])<br><br>    ti.addToKernelScope({ N, liveness, numNeighbors });</pre><p>Here, we defined two variables, liveness and numNeighbors, both of which are ti.fields. In taichi.js, a &quot;field&quot; is essentially an n-dimensional array, whose dimensionality is provided in the 2nd argument to ti.field(). The element type of the array is defined in the first argument. In this case, we have ti.i32, indicating 32-bit integers. However, field elements may also be more complex types, including vectors, matrices, and structures.</p><p>The next line of code, ti.addToKernelScope({...}), ensures that the variables N, liveness, and numNeighbors are visible in taichi.js &quot;kernel&quot;s, which are GPU compute and/or render pipelines defined in the form of JavaScript functions. As an example, the following init kernel is used to populate our grid cells with initial liveness vales, where each cell has a 20% chance of being alive initially:</p><pre>    let init = ti.kernel(() =&gt; {<br>        for (let I of ti.ndrange(N, N)) {<br>            liveness[I] = 0<br>            let f = ti.random()<br>            if (f &lt; 0.2) {<br>                liveness[I] = 1<br>            }<br>        }<br>    })<br>    init()</pre><p>The init() kernel is created by calling ti.kernel() with a JavaScript lambda as the argument. Under the hood, taichi.js will look at the JavaScript string representation of this lambda and compile its logic into WebGPU code. Here, the lambda contains a for-loop, whose loop index I iterates through ti.ndrange(N, N). This means that I will take NxN different values, ranging from [0, 0] to [N-1, N-1].</p><p>Here comes the magical part — in taichi.js, all the top-level for-loops in the kernel will be parallelized. More specifically, for each possible value of the loop index, taichi.js will allocate one WebGPU compute shader thread to execute it. In this case, we dedicate one GPU thread to each cell in our &quot;Game of Life&quot; simulation, initializing it to a random liveness state. The randomness comes from a ti.random() function, one of the many functions provided in the taichi.js library for kernel use. A full list of these built-in utilities is available <a href="https://taichi-js.com/docs/docs/basics/kernel-scope-builtin-functions">here</a> in the taichi.js documentation.</p><p>Having created the game&#39;s initial state, let’s move on to define how the game evolves. These are the two taichi.js kernels defining this evolution:</p><pre>    let countNeighbors = ti.kernel(() =&gt; {<br>        for (let I of ti.ndrange(N, N)) {<br>            let neighbors = 0<br>            for (let delta of ti.ndrange(3, 3)) {<br>                let J = (I + delta - 1) % N<br>                if ((J.x != I.x || J.y != I.y) &amp;&amp; liveness[J] == 1) {<br>                    neighbors = neighbors + 1;<br>                }<br>            }<br>            numNeighbors[I] = neighbors<br>        }<br>    });<br>    let updateLiveness = ti.kernel(() =&gt; {<br>        for (let I of ti.ndrange(N, N)) {<br>            let neighbors = numNeighbors[I]<br>            if (liveness[I] == 1) {<br>                if (neighbors &lt; 2 || neighbors &gt; 3) {<br>                    liveness[I] = 0;<br>                }<br>            }<br>            else {<br>                if (neighbors == 3) {<br>                    liveness[I] = 1;<br>                }<br>            }<br>        }<br>    })</pre><p>Same as the init() kernel we saw before, these two kernels also have top-level for loops iterating over every grid cell, which are parallelized by the compiler. In countNeighbors(), for each cell, we look at the eight neighboring cells and count how many of these neighbors are &quot;alive.&quot;</p><p>The number of live neighbors is stored in the numNeighbors field. Notice that when iterating through neighbors, the loop for (let delta of ti.ndrange(3, 3)) {...} is not parallelized, because it is not a top-level loop. The loop index delta ranges from [0, 0] to [2, 2], and is used to offset the original cell index I. We avoid out-of-bounds accesses by taking a modulo on N. (For the topologically-inclined reader, this essentially means the game has toroidal boundary conditions).</p><p>Having counted the number of neighbors for each cell, we update their liveness states in the updateLiveness() kernel. This is a simple matter of reading the liveness state of each cell and its current amount of live neighbors and writing back a new liveness value according to the game&#39;s rules. As usual, this process applies to all cells in parallel.</p><p>This essentially concludes the implementation of the game’s simulation logic. Next up, we will see how to define a WebGPU render pipeline to draw the game’s evolution onto a webpage.</p><h3>Rendering</h3><p>Writing rendering code in taichi.js is slightly more involved than writing general-purpose compute kernels, and it does require some understanding of vertex shaders, fragment shaders, and rasterization pipelines in general. However, you will find that the simple programming model of taichi.js makes these concepts extremely easy to work with and reason about.</p><p>Before drawing anything, we need access to a piece of canvas that we are drawing onto. Assuming that a canvas named result_canvas exists in the HTML, the following lines of code creates a ti.CanvasTexture object, which represents a piece of texture that can be rendered onto by a taichi.js render pipeline.</p><pre>    let htmlCanvas = document.getElementById(&#39;result_canvas&#39;);<br>    htmlCanvas.width = 512;<br>    htmlCanvas.height = 512;<br>    let renderTarget = ti.canvasTexture(htmlCanvas);</pre><p>On our canvas, we will render a square and draw the Game’s 2D grid onto this square. In GPUs, geometries to be rendered are represented as triangles. In this case, the square we are trying to render will be represented as two triangles. These two triangles are defined in a ti.field, which store the coordinates of each of the six vertices of the two triangles:</p><pre>    let vertices = ti.field(ti.types.vector(ti.f32, 2), [6]);<br>    await vertices.fromArray([<br>        [-1, -1],<br>        [1, -1],<br>        [-1, 1],<br>        [1, -1],<br>        [1, 1],<br>        [-1, 1],<br>    ]);</pre><p>As we did with the liveness and numNeighbors fields, we need to explicitly declare the renderTarget and vertices variables to be visible in GPU kernels in taichi.js:</p><pre>    ti.addToKernelScope({ vertices, renderTarget });</pre><p>Now we have all the data we need to implement our render pipeline. Here’s the implementation of the pipeline itself:</p><pre>    let render = ti.kernel(() =&gt; {<br>        ti.clearColor(renderTarget, [0.0, 0.0, 0.0, 1.0]);<br>        for (let v of ti.inputVertices(vertices)) {<br>            ti.outputPosition([v.x, v.y, 0.0, 1.0]);<br>            ti.outputVertex(v);<br>        }<br>        for (let f of ti.inputFragments()) {<br>            let coord = (f + 1) / 2.0;<br>            let texelIndex = ti.i32(coord * (liveness.dimensions - 1));<br>            let live = ti.f32(liveness[texelIndex]);<br>            ti.outputColor(renderTarget, [live, live, live, 1.0]);<br>        }<br>    });</pre><p>Next, we define two top-level for-loops, which, as you already know, are loops that are parallelized in WebGPU. However, unlike the previous loops where we iterate over ti.ndrange objects, these loops iterate over ti.inputVertices(vertices) and ti.inputFragments(), respectively. This indicates that these loops will be compiled into WebGPU &quot;vertex shaders&quot; and &quot;fragment shaders,&quot; which work together as a render pipeline.</p><p>The vertex shader has two responsibilities:</p><ul><li>For each triangle vertex, compute its final location on the screen (or, more accurately, its “Clip Space” coordinates). In a 3D rendering pipeline, this will normally involve a bunch of matrix multiplications that transform the vertex’s model coordinates into world space, then into camera space, and then finally into “Clip Space.” However, for our simple 2D square, the input coordinates of the vertices are already at their correct values in clip space, so we can avoid all of that. All we have to do is append a fixed z value of 0.0, and a fixed w value of 1.0 (don&#39;t worry if you don&#39;t know what those are — it’s not important here!).</li></ul><pre>          ti.outputPosition([v.x, v.y, 0.0, 1.0]);</pre><ul><li>For each vertex, generate data to be interpolated and then passed into the fragment shader. In a render pipeline, a built-in process known as “Rasterization” is executed on all the triangles after the vertex shader is executed. This is a hardware-accelerated process that computes, for each triangle, which pixels are covered by this triangle. These pixels are also known as “fragments.” <br>For each triangle, the programmer can generate additional data at each of the three vertices, which will be interpolated during the rasterization stage. For each fragment in the pixel, its corresponding fragment shader thread will receive the interpolated values, according to its location within the triangle. In our case, the fragment shader only needs to know the fragment&#39;s location within the 2D square, so it can fetch the corresponding liveness values of the game. <br>For this purpose, it suffices to pass the 2D vertex coordinate into the rasterizer, which means the fragment shader will receive the interpolated 2D location of the pixel itself:</li></ul><pre>          ti.outputVertex(v);</pre><p>Here’s what the fragment shader’s code looks like:</p><pre>        for (let f of ti.inputFragments()) {<br>            let coord = (f + 1) / 2.0;<br>            let cellIndex = ti.i32(coord * (liveness.dimensions - 1));<br>            let live = ti.f32(liveness[cellIndex]);<br>            ti.outputColor(renderTarget, [live, live, live, 1.0]);<br>        }</pre><p>The value f is the interpolated pixel location passed-on from the vertex shader. Using this value, the fragment shader will look up the cell&#39;s liveness state in the game covering this pixel. This is done by first converting the pixel coordinates f in to the [0, 0] ~ [1, 1] range, and storing this coordinate into the coord variable. This is then multiplied by the dimensions of the liveness field, which produces the index of the covering cell.</p><p>Finally, we fetch the live value of this cell, which is 0 if it is dead, and 1 if it is alive. It outputs the RGBA value of this pixel onto the renderTarget, where the R, G, B components are all equal to live, and the A component is equal to 1, for full opacity.</p><p>With the render pipeline defined, all that’s left is to put everything together by calling the simulation kernels and the render pipeline every frame:</p><pre>    async function frame() {<br>        countNeighbors()<br>        updateLiveness()<br>        await render();<br>        requestAnimationFrame(frame);<br>    }<br>    await frame();</pre><p>And that’s it! We have completed a WebGPU-based “Game of Life” implementation in taichi.js.</p><p>If you run the program, you should see the following animation where 128x128 cells evolve for around 1,400 generations before converging to a few species of stabilized organisms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*BVb4Q01xLSswe6ulD-SU0A.gif" /></figure><h3>Exercises</h3><p>I hope you found this demo interesting! If you did, I have a few extra exercises and questions I invite you to experiment with and think about. (Btw, for quickly experimenting with the code, go to <a href="https://taichi-js.com/playground/game-of-life">this page</a>)</p><ol><li>[Easy] Add an FPS counter to the demo! What FPS value can you obtain with the current setting where N = 128? Try increasing the value of N and see how the framerate changes. Would you be able to write a vanilla JavaScript program that obtains this framerate without taichi.js or WebGPU?</li><li>[Medium] What would happen if we merge countNeighbors() and updateLiveness() into a single kernel and keep the neighbors counter as a local variable? Would the program still work correctly always?</li><li>[Hard] In taichi.js, a ti.kernel(..) always produces an async function, regardless of whether it contains compute pipelines or render pipelines. If you have to guess, what is the meaning of this async-ness? And what is the meaning of calling await on these async calls? Finally, in the frame function defined above, why did we put await only for the render() function but not the other two?</li></ol><p>The last two questions are especially interesting, as they touch on the inner workings of the compiler and runtime of the taichi.js framework, as well as principles of GPU programming. Let me know your answer!</p><h3>Resources</h3><p>Of course, this Game of Life example only scratches the surface of what you can do with taichi.js. From real-time fluid simulations to physically based renderers, there are many other taichi.js programs for you to play with and even more for you to write yourself. For additional examples and learning resources, check out:</p><ul><li><a href="https://github.com/AmesingFlank/taichi.js">GitHub page</a></li><li><a href="https://taichi-js.com/docs/docs/basics/getting-started">Docs</a></li><li><a href="https://taichi-js.com/playground">Playground</a></li></ul><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afa43c7adb2e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/painless-webgpu-programming-with-taichi-js-afa43c7adb2e">Painless WebGPU Programming With taichi.js</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Head First Taichi: A Beginner’s Guide to High Performance Computing in Python]]></title>
            <link>https://medium.com/parallel-programming-in-python/head-first-taichi-a-beginners-guide-to-high-performance-computing-in-python-be6afc5db93e?source=rss-3989b4cc81ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/be6afc5db93e</guid>
            <category><![CDATA[parallel-computing]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[high-performace-computing]]></category>
            <category><![CDATA[computer-graphics]]></category>
            <category><![CDATA[rendering-3d-animation]]></category>
            <dc:creator><![CDATA[Dunfan Lu]]></dc:creator>
            <pubDate>Mon, 22 Nov 2021 09:47:12 GMT</pubDate>
            <atom:updated>2022-07-21T07:49:08.766Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever since the Python programming language was born, its core philosophy has always been to maximize the readability and simplicity of code. In fact, the reach for readability and simplicity is so deep within Python’s root that, if you type import this in a Python console, it will recite a little poem:</p><pre>Beautiful is better than ugly.<br>Explicit is better than implicit.<br>Simple is better than complex.<br>Complex is better than complicated.<br>Flat is better than nested.<br>Sparse is better than dense.<br>Readability counts.<br>...</pre><p>Simple is better than complex. Readability counts. No doubt, Python has indeed been quite successful at achieving these goals: it is by far the most friendly language to learn, and an average Python program is often <a href="https://www.python.org/doc/essays/comparisons/">5–10 times shorter</a> than equivalent C++ code. Unfortunately, there is a catch: Python’s simplicity comes at the cost of reduced performance. In fact, it is almost never surprising for a Python program to be <a href="https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/gpp-python3.html">10–100 times slower</a> than its C++ counterpart. It thus appears that there is a perpetual trade-off between speed and simplicity, and no programming language shall ever possess both.</p><p>But don’t you worry, all hope is not lost.</p><h3>Taichi: best of both worlds</h3><p>The <a href="https://github.com/taichi-dev/taichi">Taichi Programming Language (Taichi Lang)</a> is an attempt to extend the Python programming language with constructs that enable general-purpose, high-performance computing. It is seamlessly embedded in Python, yet can summon every ounce of computing power in a machine — the multi-core CPU, and more importantly, the GPU.</p><p>The following gif shows an example program written using Taichi. The program uses the GPU to run a real-time physical simulation of a piece of cloth falling onto a sphere, and simultaneously renders the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*Vr0FmpSZF2nieoksP6D3-g.gif" /></figure><p>Writing a real-time GPU physics simulator is rarely an easy task, but the Taichi source code behind this program is surprisingly simple. The remainder of this article will walk you through the entire implementation, so you can get a taste of the functionalities that Taichi provides, and just how powerful and friendly they are.</p><p>Before we begin, take a guess of how many lines of code this program consists of. You will find the answer at the end of the article.</p><h3>Algorithmic Overview</h3><p>Our program will model the piece of cloth as a mass-spring system. More specifically, we will represent the piece of cloth as an N by N grid of point-masses, where adjacent points are linked by springs. The following image, <a href="https://graphics.stanford.edu/~mdfisher/contact.html">provided by Matthew Fisher</a>, illustrates this structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*CAwkZGxdXUcwHs3u" /></figure><p>The motion of this mass-spring system is affected by four factors:</p><ul><li>Gravity</li><li>Internal forces of the springs</li><li>Damping</li><li>Collision with the red ball in the middle</li></ul><p>For the simplicity of this blog, we ignore the self-collisions of the cloth. Our program begins at time t = 0. Then, at each step of the simulation, it advances time by a small constant dt. The program estimates what happens to the system in this small period of time by evaluating the effect of each of the 4 factors above, and updates the position and velocity of each mass point at the end of the timestep. The updated positions of mass points are then used to update the image rendered on the screen.</p><h3>Getting Started</h3><p>Although Taichi is a programming language in its own right, it exists in the form of a Python package, and can be installed by simply running</p><pre>pip install taichi</pre><p>To start using Taichi in a python program, import it under the alias ti:</p><pre>import taichi as ti</pre><p>The performance of a Taichi program is maximized if your machine has a CUDA-enabled Nvidia GPU. If this is the case, add the following line of code after the import:</p><pre>ti.init(arch=ti.cuda)</pre><p>If you don’t have a CUDA GPU, Taichi can still interact with your GPU via other graphics APIs, such as ti.metal, ti.vulkan, and ti.opengl. However, Taichi&#39;s support for these APIs are not as complete as its CUDA support, so, for now, use the CPU backend:</p><pre>ti.init(arch=ti.cpu)</pre><p>And don’t worry, Taichi is blazing fast even if it only runs on the CPU.</p><p>Having initialized Taichi, we can start declaring the data structures used to describe the mass-spring cloth. We add the following lines of code:</p><pre>N = 128<br>x = ti.Vector.field(3, float, (N, N))<br>v = ti.Vector.field(3, float, (N, N))</pre><p>These three lines declare x and v to be 2D arrays of size N by N, where each element of the array is a 3-dimensional vector of floating-point numbers. In Taichi, arrays are referred to as &quot;field&quot;s, and these two fields respectively record the position and velocity of the point masses. Notice that, if you initialized Taichi to run on a CUDA GPU, these fields/arrays will be automatically stored in GPU memory.</p><p>Apart from the cloth, we also need to define the ball in the middle:</p><pre>ball_radius = 0.2<br>ball_center = ti.Vector.field(3, float, (1,))</pre><p>Here, ball center is a 1D field of size 1, with its single component being a 3-dimensional floating point vector.</p><p>Having declared the fields needed, let’s initialize these fields with the corresponding data at t = 0. We wish to ensure that, for any pair of adjacent points on the same row or column, the distance between them is equal to cell_size = 1.0 / N. This is ensured by the following initialization routine:</p><pre>def init_scene():<br>    for i, j in ti.ndrange(N, N):<br>        x[i, j] = ti.Vector([i * cell_size,<br>                             j * cell_size / ti.sqrt(2),<br>                             (N - j) * cell_size / ti.sqrt(2)])<br>    ball_center[0] = ti.Vector([0.5, -0.5, 0.0])</pre><p>No need to worry about the meaning behind the value of each x[i,j] -- it is only chosen so that the cloth falls down at the 45 degrees angle as shown in the gif.</p><h3>Simulation</h3><p>At each timestep, our program simulates 4 things that affect the motion of the cloth: gravity, internal forces of springs, damping, and collision with the red ball.</p><p>Gravity is the most straightforward to handle. Here’s the code that does it:</p><pre><a href="http://twitter.com/ti">@ti</a>.kernel<br>def step():<br>    for i in ti.grouped(v):<br>        v[i].y -= gravity * dt</pre><p>There’re two things to be noted here: firstly, for i in ti.grouped(x) means that the loop will iterate over all elements of x, regardless of how many dimensions there are in x. Secondly and most importantly, the annotation ti.kernel means that taichi will automatically parallelize any top-level for-loops inside the function. In this case, taichi will update the y component of each of the N*N vectors in v in parallel.</p><p>Next up, we will handle the internal forces of the strings. Firstly, notice from the previous illustration that each mass point is connected to at most 8 neighbors. These links are represented in our program as follows:</p><pre>links = [[-1, 0], [1, 0], [0, -1], [0, 1], <br>         [-1, -1], [1, -1], [-1, 1], [1, 1]]<br>links = [ti.Vector(v) for v in links]</pre><p>From a physical perspective, each spring s in the system is initialized with a rest length, l(s,0). At any time t, if the current length l(s,t) of s exceeds l(s,0), then the spring will exert a force on its endpoints that pulls them together. Conversely, if l(s,t) is smaller than l(s,0), then the spring will push the endpoints away from each other. The magnitude of these forces are always proportional to the absolute value of l(s,0)-l(s,0). This interaction is captured by the following code snippet:</p><pre>    for i in ti.grouped(x):<br>        force = ti.Vector([0.0,0.0,0.0])<br>        for d in ti.static(links):<br>            j = min(max(i + d, 0), [N-1,N-1])<br>            relative_pos = x[j] - x[i]<br>            current_length = relative_pos.norm()<br>            original_length = cell_size * float(i-j).norm()<br>            if original_length != 0:<br>                force +=  stiffness * relative_pos.normalized() *<br>                    (current_length - original_length) /<br>                    original_length<br>        v[i] +=  force * dt</pre><p>Notice that this for loop should still be placed as a top-level for loop in the substep function, which was annotated with @ti.kernel. This ensures that the spring forces applied to each mass point are computed in parallel. The variable stiffness is a constant which controls the extent to which the springs resist change in their length. In our program, we will use stiffness = 1600.</p><p>In the real world, when springs oscillate, the energy stored in the springs dissipates into the surrounding environment, and its oscillations eventually stop. To capture this effect, at each timestep, we slightly reduce the magnitude of the velocity of each point:</p><pre>    for i in ti.grouped(x):<br>        v[i] *= ti.exp(-damping * dt)</pre><p>where damping takes the fixed value of 2.</p><p>We also need to handle the collision between the cloth and the red ball. To do this, we simply decrease the velocity of a mass point to 0 as soon as it comes into contact with the ball. This ensures that the cloth “hang”s on the ball instead of penetrating it or sliding down:</p><pre>    if (x[i]-ball_center[0]).norm() &lt;= ball_radius:<br>            v[i] = ti.Vector([0.0, 0.0, 0.0])</pre><p>And finally, we update the position of each mass point using its velocity:</p><pre>    x[i] += dt * v[i]</pre><p>And that’s it! This is all the code that we need to perform a parallel simulation of a mass-spring-cloth.</p><h3>Rendering</h3><p>We will use taichi’s built-in GPU-based GUI system (nicknamed “GGUI”) to render the cloth. GGUI uses the Vulkan graphics API for rendering, so make sure you <a href="https://docs.taichi-lang.org/docs/ggui">have Vulkan installed on your machine</a>. GGUI supports rendering two types of 3D objects: triangle meshes, and particles. We will render the cloth as a triangle mesh, and render the red ball as a single particle.</p><p>GGUI represents a triangle mesh with two taichi fields: a field of vertices, and a field of indices. The vertices fields is a 1-dimensional field where each element extract is a 3D vector that represents the position of a vertex, possibly shared by multiple triangles. In our application, every point mass is a triangle vertex, so we can simply copy data from x into vertices:</p><pre>vertices = ti.Vector.field(3, float, N * N)</pre><pre><a href="http://twitter.com/ti">@ti</a>.kernel<br>def set_vertices():<br>    for i, j in ti.ndrange(N, N):<br>        vertices[i * N + j] = x[i, j]</pre><p>Notice that set_vertices needs to be called every frame, because the vertex positions are constantly being updated by the simulation.</p><p>Our cloth is represented by an N by N grid of mass points, which can also be seen as an N-1 by N-1 grid of small squares. Each of these squares will be rendered as two triangles. Thus, there are a total of (N - 1) * (N - 1) * 2 triangles. Each of these triangles will be represented as 3 integers in the vertices field, which records the indices of the vertices of the triangle in the vertices field. The following code snippet captures this structure:</p><pre>num_triangles = (N - 1) * (N - 1) * 2<br>indices = ti.field(int, num_triangles * 3)</pre><pre><a href="http://twitter.com/ti">@ti</a>.kernel<br>def set_indices():<br>    for i, j in ti.ndrange(N, N):<br>        if i &lt; N - 1 and j &lt; N - 1:<br>            square_id = (i * (N - 1)) + j<br>            # 1st triangle of the square<br>            indices[square_id * 6 + 0] = i * N + j<br>            indices[square_id * 6 + 1] = (i + 1) * N + j<br>            indices[square_id * 6 + 2] = i * N + (j + 1)<br>            # 2nd triangle of the square<br>            indices[square_id * 6 + 3] = (i + 1) * N + j + 1<br>            indices[square_id * 6 + 4] = i * N + (j + 1)<br>            indices[square_id * 6 + 5] = (i + 1) * N + j</pre><p>Notice that, unlike set_vertices, the function set_indices only needs to be called once. This is because the indices of the triangle vertices don&#39;t actually change -- it&#39;s only the positions that are changing.</p><p>For rendering the red ball as a particle, we don’t actually need to prepare any data, the ball_center and ball_radius variable that we previously defined are all that&#39;s needed by GGUI.</p><h3>Putting everything together</h3><p>At this point, we have covered all the core functions of the program! Here’s how we will call these functions</p><pre>init()<br>set_indices()</pre><pre>window = ti.ui.Window(&quot;Cloth&quot;, (800, 800), vsync=True)<br>canvas = window.get_canvas()<br>scene = ti.ui.Scene()<br>camera = ti.ui.make_camera()</pre><pre>while window.running:<br>    for i in range(30):<br>        step()<br>    set_vertices()</pre><pre>    camera.position(0.5, -0.5, 2)<br>    camera.lookat(0.5, -0.5, 0)<br>    scene.set_camera(camera)</pre><pre>    scene.point_light(pos=(0.5, 1, 2), color=(1, 1, 1))<br>    scene.mesh(vertices, indices=indices, <br>               color=(0.5, 0.5, 0.5),two_sided = True)<br>    scene.particles(ball_center, radius=ball_radius, <br>                    color=(0.5, 0, 0))<br>    canvas.scene(scene)<br>    window.show()</pre><p>One small thing to note is that instead of calling step() once, we will call it 30 times for each frame in the main program loops. The purpose of this is just so the animation doesn&#39;t run too slowly.</p><p>Putting everything together, the entire program should look something like this:</p><pre>import taichi as ti<br>ti.init(arch=ti.cuda) # Alternatively, ti.init(arch=ti.cpu)</pre><pre>N = 128<br>cell_size = 1.0 / N<br>gravity = 0.5<br>stiffness = 1600<br>damping = 2<br>dt = 5e-4</pre><pre>ball_radius = 0.2<br>ball_center = ti.Vector.field(3, float, (1,))</pre><pre>x = ti.Vector.field(3, float, (N, N))<br>v = ti.Vector.field(3, float, (N, N))</pre><pre>num_triangles = (N - 1) * (N - 1) * 2<br>indices = ti.field(int, num_triangles * 3)<br>vertices = ti.Vector.field(3, float, N * N)</pre><pre>def init_scene():<br>    for i, j in ti.ndrange(N, N):<br>        x[i, j] = ti.Vector([i * cell_size ,<br>                             j * cell_size / ti.sqrt(2),<br>                             (N - j) * cell_size / ti.sqrt(2)])<br>    ball_center[0] = ti.Vector([0.5, -0.5, -0.0])</pre><pre><a href="http://twitter.com/ti">@ti</a>.kernel<br>def set_indices():<br>    for i, j in ti.ndrange(N, N):<br>        if i &lt; N - 1 and j &lt; N - 1:<br>            square_id = (i * (N - 1)) + j<br>            # 1st triangle of the square<br>            indices[square_id * 6 + 0] = i * N + j<br>            indices[square_id * 6 + 1] = (i + 1) * N + j<br>            indices[square_id * 6 + 2] = i * N + (j + 1)<br>            # 2nd triangle of the square<br>            indices[square_id * 6 + 3] = (i + 1) * N + j + 1<br>            indices[square_id * 6 + 4] = i * N + (j + 1)<br>            indices[square_id * 6 + 5] = (i + 1) * N + j</pre><pre>links = [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [1, -1], [-1, 1], [1, 1]]<br>links = [ti.Vector(v) for v in links]</pre><pre><a href="http://twitter.com/ti">@ti</a>.kernel<br>def step():<br>    for i in ti.grouped(x):<br>        v[i].y -= gravity * dt<br>    for i in ti.grouped(x):<br>        force = ti.Vector([0.0,0.0,0.0])<br>        for d in ti.static(links):<br>            j = min(max(i + d, 0), [N-1,N-1])<br>            relative_pos = x[j] - x[i]<br>            current_length = relative_pos.norm()<br>            original_length = cell_size * float(i-j).norm()<br>            if original_length != 0:<br>                force += stiffness * relative_pos.normalized() *<br>                         (current_length - original_length) <br>                         / original_length<br>        v[i] +=  force * dt<br>    for i in ti.grouped(x):<br>        v[i] *= ti.exp(-damping * dt)<br>        if (x[i]-ball_center[0]).norm() &lt;= ball_radius:<br>            v[i] = ti.Vector([0.0, 0.0, 0.0])<br>        x[i] += dt * v[i]</pre><pre><a href="http://twitter.com/ti">@ti</a>.kernel<br>def set_vertices():<br>    for i, j in ti.ndrange(N, N):<br>        vertices[i * N + j] = x[i, j]</pre><pre>init_scene()<br>set_indices()</pre><pre>window = ti.ui.Window(&quot;Cloth&quot;, (800, 800), vsync=True)<br>canvas = window.get_canvas()<br>scene = ti.ui.Scene()<br>camera = ti.ui.make_camera()</pre><pre>while window.running:<br>    for i in range(30):<br>        step()<br>    set_vertices()</pre><pre>    camera.position(0.5, -0.5, 2)<br>    camera.lookat(0.5, -0.5, 0)<br>    scene.set_camera(camera)</pre><pre>    scene.point_light(pos=(0.5, 1, 2), color=(1, 1, 1))<br>    scene.mesh(vertices, indices=indices, <br>               color=(0.5, 0.5, 0.5), two_sided = True)<br>    scene.particles(ball_center, radius=ball_radius, <br>                    color=(0.5, 0, 0))<br>    canvas.scene(scene)<br>    window.show()</pre><p>Total number of lines: 91.</p><h3>Fun things to do</h3><p>I hope you enjoyed this program! If you did, I have a few challenges for you:</p><ul><li>[Easy] Mess around with the parameters: see how varying the stiffness, damping, and dt changes the behavior of this program.</li><li>[Easy] Search the program text for vsync=True, and change it to vsync=False. This will remove the 60 FPS limit on the program. Observe how fast the program can run on your machine.</li><li>[Medium] Implement a slightly more complicated interaction between the cloth and the ball: make it slide down the ball without penetrating it.</li><li>[Medium] Add more balls: make the cloth interact with more than one ball.</li></ul><p>As an example of what the last two challenges may look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*zHvLdduVRcnKqd2-fHbDyA.gif" /></figure><p>And finally,</p><ul><li>[Hard] Having completed the second challenge, try to implement the same program in another programming language, or in Python but without Taichi. See what’s the maximum FPS that you can obtain, and what’s the amount of code that you need to write in order to obtain similar performance.</li></ul><h3>Parting Words</h3><p>Let’s review what Taichi enabled us to accomplish in 91 lines of Python:</p><ul><li>Simulate a mass-spring system with over ten thousand mass points and around a hundred thousand springs.</li><li>Using the @ti.kernel annotation, automatically parallelize the simulation via a CUDA GPU or multi-threading on CPU</li><li>Render the result in real-time via a GPU renderer.</li></ul><p>Not only did Taichi allow us to implement all these complex functionalities with a small amount of code, it spares us the trouble of having to learn CUDA, multi-thread programming, or GPU rendering. With Taichi, anyone can write high-performance programs, and they can focus on the algorithmic aspects of their code, while leaving the performance aspects to the programming language itself. This brings us to the motto upon which Taichi is built: Parallel Programming for Everyone.</p><p>To learn more about Taichi, please do visit its <a href="https://github.com/taichi-dev/taichi">Github Page</a>, where you can find detailed documentation, as well as quite a few example Taichi programs, all of which are quite amusing. And finally, if you believe in the mission of making a friendly yet powerful language for parallel computation, you are more than welcome to join Taichi as an open-source contributor.</p><p>In my next article, I will discuss the inner workings of Taichi, and how it interacts with GPUs on different platforms for computation and rendering. Until then, happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be6afc5db93e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/head-first-taichi-a-beginners-guide-to-high-performance-computing-in-python-be6afc5db93e">Head First Taichi: A Beginner’s Guide to High Performance Computing in Python</a> was originally published in <a href="https://medium.com/parallel-programming-in-python">Parallel-Programming-in-Python</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>