<?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[Parallel-Programming-in-Python - Medium]]></title>
        <description><![CDATA[Taichi Lang is an open-source, imperative, parallel programming language for high-performance numerical computation. It is embedded in Python. The language has broad applications spanning real-time physical simulation, numberical computation, augmented reality and much more. - Medium]]></description>
        <link>https://medium.com/parallel-programming-in-python?source=rss----efb825834ab2---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Parallel-Programming-in-Python - Medium</title>
            <link>https://medium.com/parallel-programming-in-python?source=rss----efb825834ab2---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 16:50:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/parallel-programming-in-python" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Taichi’s Quantized Data Types: Same Computational Code, Optimized GPU Memory Usage]]></title>
            <link>https://medium.com/parallel-programming-in-python/taichis-quantized-data-types-same-computational-code-optimized-gpu-memory-usage-c6ac9f38515e?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/c6ac9f38515e</guid>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[parallel-computing]]></category>
            <category><![CDATA[simulation]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Taichi Lang]]></dc:creator>
            <pubDate>Fri, 23 Dec 2022 04:16:30 GMT</pubDate>
            <atom:updated>2022-11-22T09:29:57.749Z</atom:updated>
            <content:encoded><![CDATA[<p>Taichi’s Quantized Data Types: Same Computational Code, Optimized GPU Memory Usage</p><p>Author: <a href="https://github.com/strongoier">Yi Xu</a></p><p>Starting from v1.1.0, Taichi provides quantized data types. But why is quantization important, especially in scenarios where Taichi stands out, such as physical simulation? This blog demonstrates how this new feature reduces your GPU memory usage significantly and requires <em>zero</em> change to your computational code.</p><h3>What is quantization?</h3><p>Quantization, a concept introduced for digital signal processing, refers to the process of mapping a large set of input values to a smaller set of output values. For example, rounding real signals into integer signals can reduce the numbers to represent, thus occupying less memory space. Of course, the released memory comes at a price — quantized data inevitably loses precision.</p><p>The thing is, precision loss is not necessarily undesirable under certain circumstances. For instance, lower-precision results are totally fine for image processing tasks as long as the difference in visual effects is too minimal to be noticed by human eyes. In addition, quantization has been increasingly used in neural networks for better computation efficiency in recent years.</p><p>When it comes to physical simulation, where Taichi is frequently used to improve performance, a trade-off between precision and GPU memory is usually acceptable. The higher the resolution is, the better the visual effects will be. However, resolutions are subject to the limits of the GPU memory capacity. Therefore, it makes much sense to reduce GPU memory consumption, and to this end, Taichi enabled quantized data types. <em>With this feature, you can store data of arbitrarily low precision bits without changing the code for computation. In some cases, Taichi’s quantized data types can reduce the use of GPU memory by up to 8x.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XepqKEVWsQz4YLQ_w4c6vg.jpeg" /><figcaption>Game of Life: 20 billion cells; 7 GB of GPU memory (8x less after quantization)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*_NTYic-IMzU6xnqixH7I8g.gif" /><figcaption>Smoke simulation: 400 million voxels; 29 GB of GPU memory (1.6x less after quantization)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*FqEs4InBeSouJxfOO3NxAw.gif" /><figcaption>Elastic body simulation: 200 million particles; 17 GB of GPU memory (1.7x less after quantization)</figcaption></figure><h3>Write your first quantized Taichi program</h3><p>Let’s write your first quantized Taichi program together and see whether the new feature is as good as it sounds. In this section, we take a classic Taichi demo, the Euler equations, as an example, and slim it down by rewriting the data declaration part only.</p><p>Run the original program: examples/simulation/euler.py</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/1*Kv-sprnTAL7V_x1KUK17FA.gif" /></figure><p>Most of the Taichi fields in this program are defined in the following way:</p><pre>Q = ti.Vector.field(4, dtype=ti.f32, shape=(N, N))</pre><p>This example field is composed of ti.f32 elements, i.e., single-precision floating-point numbers. If we lower the precision by reducing the binary bits allocated to the four elements of a vector from 128 to 64, we can halve the GPU memory the field occupies.</p><p>It is time Taichi’s quantized data types came into play:</p><pre>float_8_8 = ti.types.quant.float(exp=8, frac=8)</pre><p>As we know, a floating-point number consists of an exponent, a mantissa (also known as a fraction or significand), and a sign. For example, a single-precision floating-point number contains an 8-bit exponent, a 23-bit mantissa, and a 1-bit sign, as defined by the IEEE 754 Standard. Taichi’s quantized data types allow you to customize the number of bits for the exponent and mantissa (The sign bit is made part of the mantissa for convenience of manipulation). The line of code above creates a float type float_8_8 with an 8-bit exponent (exp) and an 8-bit mantissa (frac). To halve the GPU memory consumption, we need to convert each element of a field to the float_8_8 type.</p><p>Quantized data types cannot be stored in a standalone fashion as they are not Taichi’s primitive types. To overcome this problem, Taichi provides an API ti.BitpackedFields for packing a group of quantized fields into the AOS (array-of-structure) layout. In other words, several quantized data types are combined and stored as one primitive type. After specifying the components of ti.BitpackedFields, you can place it under a random SNode as if each member field is placed individually.</p><pre>Q = ti.Vector.field(4, dtype=float_8_8)<br>bitpack = ti.BitpackedFields(max_num_bits=64)<br>bitpack.place(Q)<br>ti.root.dense(ti.ij, (N, N)).place(bitpack)</pre><p>That’s all we need to do to re-define the data types in this program. Run the revised program, and <em>we will get the following</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*VQZ3m9iF59UWvmNTiJZpLw.gif" /><figcaption>Left: Original program. Right: Quantized program</figcaption></figure><p>Unfortunately, we observe a significant difference in the output animations. This is because we have sacrificed too much precision by reducing the number of bits allocated to the mantissa from 24 to 8 although the exponent width remains unchanged. But let’s not jump to the hasty conclusion that the quantized data types do not work. Can we preserve more precision bits for the mantissa? Given that the field elements are physical quantities, some closely-tied ones, such as vector components, can share the same exponent, as specified by shared_exponent. In this way, we can assign more bits to the mantissa (and in this case, up to 14 bits).</p><pre>float_8_14 = ti.types.quant.float(exp=8, frac=14)<br>Q = ti.Vector.field(4, dtype=float_8_14)<br>bitpack = ti.BitpackedFields(max_num_bits=64)<br>bitpack.place(Q, shared_exponent=True)<br>ti.root.dense(ti.ij, (N, N)).place(bitpack)</pre><p><em>This time, we can hardly tell the difference between the two versions:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*2oc4oNiB1LCcidRoV7Nvvg.gif" /><figcaption>Left: Original program. Right: Second quantized program</figcaption></figure><p>So far, you have successfully modified a Taichi program with quantization, saving half of the GPU memory consumption and exerting little impact on the final visual effects. Hopefully, this neat example has demonstrated <em>how convenient and efficient it is to experiment on different data types while keeping the computational code intact</em>.</p><p>Here, we only introduced the basics of Taichi’s quantized data types. You can refer to our <a href="https://docs.taichi-lang.org/docs/quant">document</a> for detailed explanation, more examples, and advanced uses. You can start with Taichi examples (ti example) and share with us how you manage to minimize their GPU memory consumption without tampering with the output quality.</p><h3>Conclusion</h3><p>Now that you have some initial understanding of quantization and Taichi’s quantized data types, you may wonder how this idea of combining quantization and simulation came from and evolved. Well, the feature was first proposed by Taichi team in the paper <a href="https://yuanming.taichi.graphics/publication/2021-quantaichi/quantaichi.pdf"><em>QuanTaichi: A Compiler for Quantized Simulations</em></a>, published at SIGGRAPH 2021, and continuously restructured and refined before the final release of Taichi v1.1.0. Taichi always stands at the forefront of academic research and strives to apply the latest findings to real-life scenarios.</p><p>At the same time, we delightedly found that the academia recognizes the value of QuanTaichi and keeps exploring its potential. For example, <a href="https://hanke98.github.io/">Jiafeng Liu</a>, a PhD student and Taichi contributor, further improved the approach by designing a framework for automatic quantization scheme selection. His paper <a href="https://arxiv.org/abs/2207.04658"><em>Automatic Quantization for Physics-Based Simulation</em></a> has been accepted by SIGGRAPH 2022. We will continue to contribute to academic research and industrial applications.</p><p>Don’t forget to upgrade to the latest Taichi version before running the reference programs in this article:</p><pre>python3 -m pip install taichi --upgrade</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6ac9f38515e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/taichis-quantized-data-types-same-computational-code-optimized-gpu-memory-usage-c6ac9f38515e">Taichi’s Quantized Data Types: Same Computational Code, Optimized GPU Memory Usage</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>
        <item>
            <title><![CDATA[Can Taichi play a role in CFD?]]></title>
            <link>https://medium.com/parallel-programming-in-python/can-taichi-play-a-role-in-cfd-44a61e59027?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/44a61e59027</guid>
            <category><![CDATA[fluid-simulation]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[simulation]]></category>
            <dc:creator><![CDATA[Taichi Lang]]></dc:creator>
            <pubDate>Fri, 23 Dec 2022 04:16:18 GMT</pubDate>
            <atom:updated>2022-12-01T03:37:46.096Z</atom:updated>
            <content:encoded><![CDATA[<p>Author: <a href="https://github.com/houkensjtu">Qian Bao</a></p><p>Computational fluid dynamics (CFD) is a branch of fluid mechanics that endeavors to precisely reproduce the behavior of liquid/gas flows and their interaction with solid boundaries. It plays a vital role in such sectors as visual effects, virtual reality, and industrial design.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*qKNsf_-QoE2iH8vtRtgZ9g.gif" /><figcaption>Enlivened ocean simulation in the movie Moana</figcaption></figure><p>Although we are delightedly seeing an increasing number of impressive CFD projects implemented with Taichi (as presented in Taichi’s <a href="https://github.com/houkensjtu/taichi-fluid">CFD repo</a>), it remains confusing for outsiders as to how a programming language designed for computer graphics can facilitate high-performance computation and even write CFD programs directly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*mcBw7G1qSTecB_z4xppBdg.gif" /><figcaption>Selected CFD demos</figcaption></figure><p>Well, this blog will answer this question by explaining how Taichi fits in with the needs of CFD. And hopefully, you will find it worth a try in your next project.</p><h3>What CFD expects a programming language to do?</h3><p>Before touching upon how Taichi addresses numerical computation, we first need to figure out what a typical CFD simulation entails. The image below illustrates the four major steps of a CFD simulation based on the Eulerian method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*Bavp3_cvrv0ipH6Q_cxY8A.png" /></figure><p>Let me explain the whole process step-by-step.</p><p>First, pre-processing requires us to define the data structures (in most cases, 2D/3D arrays) that save the physical parameters of the flow field, such as velocity components (U and V), pressure (P), and density, and then set initial values for the flow variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i_CuXXLTb99MO3GPwYJoIQ.png" /></figure><p>To find the solution to the discretized Navier-Stokes equations is the most time-consuming part we need to deal with. Navier-Stoke equations are partial differential equations that describe the flow of incompressible fluids. Discretization means using the algebraic polynomials of discrete values to approximate the solutions to differential equations. For example, the Laplace operator of a scalar function can be denoted with the five-point discrete scheme:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*4CqZ-GuOT-nrhmKiTU7E2w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*WzsMh20luVV4mnRscvLc1w.png" /></figure><p>Now, we need to work out the solution to the discretized equations. Given that this is a linear system Ax=b in question, we can choose from either a direct solver or an iterative solver, depending on the size and sparsity of the matrix A, to derive the unknown vectors. One thing worth our attention here: <em>Both the discretization of differential equations and the solution to the linear system entail similar but mutually-independent computations of the vast amount of elements in the target field, which can be effectively accelerated if parallelized</em>. We will come back to this point later.</p><p>Finally, in the post-processing stage, we need to visualize the flow field data derived from the aforementioned steps into the more intuitive vector plots or contour plots. The image below right is visualized in Matplotlib, representing the velocity vector of the lid-driven cavity flows. Apart from Matplotlib, there are multiple tools available for visualization, including Taichi’s built-in GUI system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*ewS117kL_ow2uJr_X9bl0A.png" /></figure><p>Faced with the demanding computational complexity, an ideal programming language for CFD should be able to help with discretization and visualization efficiently. Now, let’s see what Taichi can do to save our efforts and improve the computation efficiency.</p><h3>Can Taichi meet the needs?</h3><p>Designed for computer graphics, Taichi is equipped with data structures friendly to large-scale numerical computation as well as and an automatic parallelism parallelization mechanism for performance speedup. At the same time, as a domain-specific language (DSL) embedded in Python, Taichi is integrated seamlessly with the Python ecosystem. So, we can leave the part that needs high-performance parallel computing to Taichi while keeping everything else in native Python code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iQHDGqUv4CbiyWoUCQV-2g.png" /><figcaption>Taichi can accelerate large-scale parallel computing in Step 2</figcaption></figure><p>Now, we take the same Eulerian method as an example.</p><h3>Data structures</h3><p>Taichi provides a data container field, which is essentially a multi-dimensional array of elements. To parallelize the solution process, we need to save the flow field data in a Taichi field. Luckily, field accepts scalars and vectors, which makes it straightforward to represent a flow field. For example, we can define a 640x480 scalar field of floating-point numbers as follows:</p><pre>f_2d = ti.field(float, shape=(640, 480))</pre><p>Similarly, the line of code below represents a 2D vector field of in the shape 640x480:</p><pre>v_2d = ti.Vector.field(n=2, dtype=float, shape=(640, 480))</pre><p>Other valid data structures include matrices and user-defined structs, as detailed in the <a href="https://docs.taichi-lang.org/docs/field">doc site</a>.</p><h3>Parallelization</h3><p>Suppose we want to apply the Laplace operator to the scalar field f_2d. We should first create a new field for storing the results:</p><pre>f_2d_grad2 = ti.field(float, shape=(640, 480))</pre><p>Then, we need to traverse the elements in the new field when calculating the Laplace operator. Taichi provides an easy-to-use struct-for statement to make traversing much more convenient:</p><pre>for i,j in f_2d_grad2:<br>    f_2d_grad2[i,j] = 4 * f_2d[i,j] - f_2d[i-1,j] \<br>                      - f_2d[i+1,j] - f_2d[i,j-1] - f_2d[i,j+1]</pre><p>However, the struct-for statement is a special syntax unique to Taichi and cannot be processed by Python. Therefore, we need to have Taichi take this over and accelerate the computation by wrapping the statement inside a Taichi kernel:</p><pre>@ti.kernel<br>def fgrad2():<br>    for i,j in f_2d_grad:<br>        f_2d_grad2[i,j] = 4 * f_2d[i,j] - f_2d[i-1,j] \<br>                          - f_2d[i+1,j] - f_2d[i,j-1] - f_2d[i,j+1]</pre><p>The decorator @ti.kernel serves as an entry point when Taichi kicks in and does the trick. We can understand its job as such: the decorator takes in and analyzes a function and returns an automatically parallelized version:</p><pre>fgrad2_ti = ti.kernel(fgrad2)</pre><p>When we call the function, the program hands over control from Python to Taichi and performs computation tasks on multiple cores in parallel, resulting in a considerable improvement in computational speed.</p><h3>Data visualization</h3><p>Taichi also has an elementary built-in GUI system that you can use as a visualization tool to quickly inspect the flow field dynamics when coding numerical computation tasks. For instance, the following concise lines generate a grayscale image to visualize the field f_2d. Note that gui.set_image can accept both Taichi fields and NumPy ndarrays:</p><pre>gui = ti.GUI(&#39;Title&#39;, (640, 480))<br>while gui.running:<br>    gui.set_image(f_2d)<br>    gui.show()</pre><p>Taichi’s GUI system can meet the most basic needs for visualization. You may still need other post-production software to achieve more complex rendering or animation effects. You can refer to the <a href="https://docs.taichi-lang.org/docs/gui_system">doc site</a> to check the complete features of the GUI system.</p><h3>Application of Taichi kernels to CFD</h3><h4>When to bring @ti.kernel into play</h4><p>For those unfamiliar with the term “kernel”: A kernel is a fraction of code designed for GPU computing. To run on GPU cores, a kernel is expected to contain a large amount of repetitive or similar but mutually-independent computation tasks. For example, when devising functions to traverse all pixels on the screen and update the color of each pixel, graphics engineers would designate such functions as kernels so that they can be recognized by the compiler and deployed to multiple cores for parallelization.</p><h4>Scenarios where @ti.kernel do not apply</h4><p>Obviously, not all loops can be parallelized. A typical exception is the Fibonacci sequence where each number is the sum of the two preceding ones. Hence, the calculation of a Fibonacci number cannot be dealt with independently. When it comes to CFD, the update of time increments is determined by the preceding time step and should thus be performed serially, whereas computation within the same iteration at the same time step is an ideal scenario for parallelization.</p><p>We will further elaborate on the application of Taichi kernels in the upcoming blogs on CFD.</p><h3>Recap</h3><p>The most computation-intensive step in CFD is the discretization of differential equations, which involves massive operations of 2D or 3D data arrays. Taichi’s data container makes the representation of a flow field intuitive and the <em>automatic parallelization mechanism significantly saves computational time</em>. In addition, the built-in GUI system offers a convenient visualization solution to track fluid dynamics with only a few lines of code.</p><p>Here’s hoping that Taichi can become a good companion for your CFD simulation projects. We look forward to seeing your fluid simulation demos, which may secure a place in our <a href="https://github.com/houkensjtu/taichi-fluid">CFD repo</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44a61e59027" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/can-taichi-play-a-role-in-cfd-44a61e59027">Can Taichi play a role in CFD?</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>
        <item>
            <title><![CDATA[How does Taichi Compare to CUB/CuPy/Numba in Numerical Computation?]]></title>
            <link>https://medium.com/parallel-programming-in-python/how-does-taichi-compare-to-cub-cupy-numba-in-numerical-computation-c7e058674e4a?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/c7e058674e4a</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[simulation]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-languages]]></category>
            <dc:creator><![CDATA[Taichi Lang]]></dc:creator>
            <pubDate>Fri, 23 Dec 2022 04:16:04 GMT</pubDate>
            <atom:updated>2022-12-01T03:51:42.613Z</atom:updated>
            <content:encoded><![CDATA[<p>Author: <a href="https://github.com/houkensjtu">Qian Bao</a> &amp; <a href="https://github.com/turbo0628">Haidong Lan</a></p><p>In the <a href="https://medium.com/@taichi-lang/can-taichi-play-a-role-in-cfd-44a61e59027">previous blog</a>, we learned that Taichi, a high-performance computing language embedded in Python, goes beyond a development tool for computer graphics and renderers but also comes in handy for numerical computation that involves massive operations on 2D and 3D arrays. Computational fluid dynamics (CFD) is a typical scenario where Taichi can play a part.</p><p>Now, the question is how much Taich can improve the performance of numerical computation programs. This blog applies Taichi and other acceleration solutions to some arithmetic operations frequently used in numerical computation and compares their performance and user-friendliness.</p><h3>An entry-level case: Performance comparison in summation operations</h3><p>A summation operation computes the sum of all elements in a given array. It is a common arithmetic operation in numerical computation.</p><p>This section takes a summation operation as an example to compare the computing performance of Taichi, CUB, Thrust, CuPy, and Numba.</p><p>Let’s start with a bit background information. Developed by Nvidia, both CUB and Thrust provide common utilities for parallel computing. Despite similarities they share in the usage, CUB was designed specially for Nvidia GPUs and resembles the underlying instructions of CUDA, while Thrust features a higher level of abstraction for compatibility with multiple platforms, hence a slower performance than CUB in most cases.</p><p>The common GPU acceleration solutions available to Python users include CuPy and Numba. The former provides an interface similar to NumPy, allowing users to call CuPy in the same way they call NumPy. Here, we choose the most performant CUB as the acceleration backend for the CuPy implementation. The Numba implementation performs the reduction operation in accordance with the instructions provided by its official documentation.</p><p>We also implement the summation operation in Taichi, and it is quite straightforward:</p><pre>@ti.kernel<br>def reduce_sum_kernel():<br>    sum = 0.0<br>    for i in f:<br>        sum += f[i]</pre><p>Having experimented with different data sizes and compared the performance of the various implementations mentioned above, we present the results in the graph below. The higher the column is, the closer the implementation approaches the upper limit of the hardware.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*f82ggZ2Ty0iDqWm0mMhoeg.png" /><figcaption>Hardware: i9–11900K + RTX 3080; OS: Ubuntu 20.04. The performance metric adopted in the test is the actual memory bandwidth achieved. A higher memory bandwidth indicates better performance.</figcaption></figure><p>The graph shows that the computing performance of all implementations is less than satisfactory when the data size is minimal. There are two reasons for the low performance: 1) small arrays cannot fully utilize the capacity of the GPU; 2) calling the libraries inevitably incurs overhead to varying degrees. As the data size increases, the proportion of fixed overhead lowers, and the performance improves significantly. Notably, the CUDA/CUB and CuPy implementations achieve impressive performance, both exceeding 90% of the peak bandwidth of the hardware. Taichi’s performance is comparable to the highly optimized CUB and CuPy versions and outruns Thrust at all data sizes by a large margin.</p><p>In fact, Taichi’s compiler relies on heavy underlying engineering to enable high performance. In the aforementioned code snippet, the summation is actually an atomic operation, which cannot be parallelized and is thus subject to limited operation efficiency. The most commonly used method of parallel computing optimization in vector summation is reduction, which is one of the must-have skills to learn parallel computing. Thanks to automatic reduction optimization implemented by its compiler, Taichi achieves a performance comparable to manually implemented CUB and way better than Numba. See the <a href="https://github.com/taichi-dev/taichi_benchmark">benchmark report</a>.</p><p>In the case of a simple arithmetic operation, Taichi can rival the highly optimized CUDA and CUB implementations and outperform Thrust and Numba effortlessly with very intuitive code. But can we trust Taichi in real-life numerical computation tasks involving more complex formulas? Now let’s move on to a more advanced case in CFD.</p><h3>An advanced case: Performance comparison in velocity field calculations</h3><p>When dealing with complex computing tasks, users find that their hands are tied not only by computing performance but also directly by coding efficiency. Here, to compare the computing performance and user-friendliness of different solutions, we take the velocity field calculations based on the marker-and-cell (MAC) method as an example. The MAC method entails calculating the changes of two 2D fluid velocity fields along the timeline as convection and viscosity are applied. We can denote the changes of each velocity field using the following formulas:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WcI8N_mW8tamL1SBoXlR1A.png" /></figure><p>Writing this formula with CUB is nothing but inefficient for lack of an API to support it. It is possible to write it in Thrust, which is a template library, but template programming makes debugging a nightmare. As for CuPy, it can implement complex operations through CUDA code only. When there is no readily available library, what we need is a high-performance programming language to implement this formula. Therefore, we limit the performance comparison scope within the set of Numba, CUDA, and Taichi.</p><p>To begin with, both Taichi and Numba are programming languages embedded in Python, allowing users to build algorithms by simply following Python’s syntax. However, Numba can utilize only the computing power of CPUs with Python code; to access the GPU, one is expected to understand the CUDA programming model and handle thread scheduling by oneself. In contrast, Taichi does not require any prior experience with CUDA. Users only need to specify the CUDA backend in the ti.init() call to offload the code to GPU.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*znrBPuY7Ff_SDGvbQkq_Ug.png" /><figcaption>Numba vs. Taichi: Taichi can apply the same code to CPUs and GPUs, but Numba needs to tailor functions for CPUs and GPUs separately.</figcaption></figure><p>When it comes to the native CUDA implementation, we finished writing the kernels in half an hour but spent almost two hours aligning the values. But this is not the end of the story. Calling CUDA code from Python also entails additional tooling code for compilation, interface encapsulation, memory allocation and deallocation, and CPU-GPU data transfer. The whole process still imposes a considerable burden on programming efficiency although there are various libraries available for simplification.</p><p>With all the programming convenience Taichi offers, how does it rank in terms of performance? We run each implementation 1,000 times consecutively and took the average time consumed per call as the performance metric. The results are presented below. The lower the column is, the better the performance is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2uNCM4XatYcnsCBh60plbw.png" /><figcaption>Hardware: i9–11900K + RTX 3080; OS: Ubuntu 20.04. The performance metric adopted in the test is the time consumed per call — the shorter the time is, the better the performance is. “Time” here refers to the wall-clock time, taking into account the overhead of function launches and device synchronization.</figcaption></figure><p>Apparently, the three GPU-accelerated implementations significantly outperform the Numba version on CPUs. When the edge length of the velocity fields reaches 2,048 or larger, Taichi takes only 1/3 of the time Numba (CUDA version) takes to finish the computation and runs even faster than the native CUDA implementation. The high performance can be attributed to faster memory access enabled by Taichi’s compiler, which automatically exploits hardware features. CUDA can achieve comparable performance after optimizations, provided that users are equipped with a deep understanding of its underlying hardware.</p><p>The source code of different implementations is available at <a href="https://github.com/houkensjtu/MAC-taichi/tree/main/benchmark">this repo</a>.</p><h3>Summary</h3><p>In this blog, we examine Taichi’s performance in vector summation and fluid field operations and compare Taichi with other common acceleration solutions. Our findings are as follows:</p><ul><li>In the case of simple summation operations, Taichi’s performance is on par with that of the mainstream highly-optimized GPU-accelerated libraries.</li><li>In the case of complex fluid field operations, Taichi outperforms Numba (CUDA version) by 3x-4x and slightly surpasses the manually implemented and semantically equivalent CUDA version</li><li>In terms of coding efficiency, Taichi provides a much more straightforward and easier coding experience than Numba (CUDA version) and CUDA do; it also allows seamless backend switching, saving the trouble of rewriting the code.</li></ul><p>Now that you have some knowledge about Taichi’s advantages in performance and user-friendliness, you must want to give it a try in your own numerical computing programs.</p><p>Our next blog will demonstrate how to write a CFD program within 99 lines of Taichi code. Stay in the loop!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7e058674e4a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/how-does-taichi-compare-to-cub-cupy-numba-in-numerical-computation-c7e058674e4a">How does Taichi Compare to CUB/CuPy/Numba in Numerical Computation?</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>
        <item>
            <title><![CDATA[Pythonic Supercomputing: Scaling Taichi Programs with MPI4Py]]></title>
            <link>https://medium.com/parallel-programming-in-python/pythonic-supercomputing-scaling-taichi-programs-with-mpi4py-6aeffc69bd17?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/6aeffc69bd17</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[supercomputer]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Taichi Lang]]></dc:creator>
            <pubDate>Fri, 23 Dec 2022 04:15:46 GMT</pubDate>
            <atom:updated>2022-12-07T14:26:27.861Z</atom:updated>
            <content:encoded><![CDATA[<p>author: <a href="https://github.com/turbo0628">Haidong Lan</a></p><p>Nvidia unveiled its Tesla V100 GPU accelerator, which has since become a must-have model for deep learning, at GTC (GPU Technology Conference) 2017 in Beijing. It was on the same occasion that Jensen Huang, Nvidia’s CEO, solemnly gave us the most sincere advice, which kept resonating in our heads for years to come:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*k7oCTgIzPTHuV4MRG9JChg.png" /></figure><p>And we all know what happened afterward. A few Internet giants piled tens of thousands of V100 chips up in their data centers. Artificial intelligence (AI) grows increasingly intelligent: facial and speech recognition becomes more accurate; machine translation delivers more humanlike results; AI gamers defeat professional players in the blink of an eye. Behind the scenes, massive R&amp;D resources and top-notch expertise have been channeled to the development of distributed machine learning frameworks, which automatically break down a heavy learning workload into smaller tasks to run on multiple graphics cards.</p><p>Can distributed frameworks be applied to sectors other than machine learning? Taichi community members often find that they need to deal with ultra-large grids for fluid or particle simulations, where the data volume far exceeds the capacity of a single GPU. A distributed solution that allows a Taichi program to run on multiple GPU cards or even multiple GPU machines would largely improve the efficiency of large-scale computation.</p><p>Well, it proves that parallel computing and distributed computing can be perfectly combined and neatly achieved in a few lines of code, and this blog demonstrates how. Spoiler alert: The secret lies in Taichi plus MPI4Py.</p><h3>A quick start with MPI4Py</h3><p>MPI (message passing interface) is a standardized communication interface initiated over two decades ago and has since been continuously improved. It enables thousands of computers to communicate and coordinate with each other, circulate data, and facilitate large-scale computations. Therefore, you cannot possibly bypass MPI as long as you use supercomputing. Implementing an MPI program from scratch is not easy. Luckily, MPI is available through a Python package, MPI4Py, and the following snippet shows a most basic MPI4Py program:</p><pre># mpi_hello.py<br>from mpi4py import MPI<br><br>comm = MPI.COMM_WORLD<br>print(f&quot;Hello from MPI worker {comm.rank}, the MPI world has {comm.size} peers.&quot;)</pre><p>This code snippet contains the most essential concept in MPI — MPI_COMM_WORLD, as introduced in Line 4. All worker processes created by MPI are uniquely identified with rank, no matter whether they are placed on the same device or not. For instance, rank=0 refers to the first worker, rank=1 refers to the second, and so on. The total number of workers is stored in comm.size. The print statement would be executed separately by each worker, printing their identity comm.rank in MPI_COMM_WORLD as well as the aggregate amount comm.size.</p><p>To run this program, add mpirun ahead of the python command as follows. -np specifies how many workers to fire up, and --hosts names the machines involved in distributed computing.</p><pre>mpirun -np 8 --hosts node1,node2 python mpi_hello.py</pre><p><em>You should see the following information if everything goes smoothly:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*9yUZi9-ZJfhsopK20Oa0Wg.png" /></figure><p>That’s all we need to know about MPI4Py basics to understand the following sections. For more information about the Python package, see <a href="https://mpi4py.readthedocs.io/en/stable/">this page</a>. Now, we can get down to a simple Taichi program and explore how it can be split into several smaller tasks and run on multiple machines via MPI4Py.</p><h3>A simple Taichi program</h3><p>Let’s implement a simple Poisson solver in Taichi. The Poisson solver solves the following Laplace’s equation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C_2Ti6iPuum8ZGDP4J2C3w.png" /></figure><p>The boundary condition is naturally given by <em>f(x,y) = 0</em>.</p><p>You do not have to be crystal clear about the rationale of this solver. It is essentially a Taichi kernel for stencil computing. It is more than straightforward to write such a kernel:</p><pre>x = ti.field(dtype=float, shape=(N + 2, N + 2))<br>xt = ti.field(dtype=float, shape=(N + 2, N + 2))<br><br>@ti.kernel                                                                                                                          <br>def substep():                                                                                                                  <br>    for i,j in ti.ndrange((1, N+1), (1, N+1)):                                                                            <br>        xt[i,j] = (-b[i,j]*dx**2+x[i+1,j]+x[i-1,j]+x[i,j+1]+x[i,j-1]) / 4.0                  <br>    for I in ti.grouped(x):                                                                                                    <br>        x[I] = xt[I]</pre><p>A Taichi field x of shape (N+2) x (N+2) is defined to represent <em>f</em>. + 2 provides padding to avoid erroneous out-of-bound indexing. The calculation of each point depends on its four adjacent neighbors, as the following image illustrates:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/1*G5_Xw2IStzOiD3huKxgexA.png" /></figure><p>We can gradually approximate the solution to <em>f</em> by calling substep repeatedly.</p><p>Now that we have a Taichi program, which currently relies on a single processor and a single device, we are ready to introduce MPI4Py and distribute this program to multiple machines.</p><h3>Original MPI version: Resolve data dependencies and achieve distributed computing</h3><p>The first step in distribution to multiple processors is data partitioning. There are two major considerations: the shape of data partitions and the data dependencies among partitions.</p><p>In our case, a simple approach to data partitioning is to divide the field x into four horizontally aligned subfields. The computation of internal cells in each subfield is independent of other subfields since the substep function only involves the four neighbors of a point. However, two adjacent subfields share a boundary, where data dependencies arise. We also reserve extra space for each subfield (grey areas in the image below) to store the border data shared with the adjacent subfield(s). MPI synchronizes border data among multiple machines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jCOfIEg_zbA08_CV8Ji_Rw.png" /></figure><p>After partitioning, MPI launches multiple workers. A subfield is only visible in the worker it is allocated to. We should modify the field declaration accordingly:</p><pre>n = N // 4<br>x = ti.field(dtype=float, shape=(N + 2, n + 2))</pre><p>x is no longer the complete field as defined in the last section. Instead, it now represents a subfield allocated to one of the four workers. In other words, x differs in different workers.</p><p>MPI4Py supports the use of NumPy’s ndarray as the buffer areas of data transmission as long as the NumPy arrays passed are contiguous in the memory. We can exchange data between Taichi fields and NumPy arrays using the functions to_numpy and from_numpy.</p><p>The following code snippet demonstrates how to <em>send the right border of a subfield to its right-hand worker:</em></p><pre>comm = MPI.COMM_WORLD<br><br>x_arr = x.to_numpy()<br>x_right = np.ascontiguousarray(x_arr[:, -2])<br><br>if comm.rank &lt; num_workers - 1:<br>    comm.Send(x_right, dest=comm.rank+1)</pre><p>Suppose the four subfields X0, X1, X2, and X3 correspond to Workers 0, 1, 2, and 3, respectively. comm.rank+1 denotes the worker to the right of the current subfield. The if statement ensures that only Workers 0, 1, and 2 send data to their right-hand neighbor, as Worker 3 is the rightmost one.</p><p>We also need to use recv and send in pairs; otherwise, a communication deadlock arises without warning when MPI cannot locate the data receiver. The following code snippet takes care of the receiving logic, i.e., Recv.</p><pre>shadow_left = np.zeros(N + 2)<br>if comm.rank &gt; 0:<br>    comm.Recv(shadow_left, source=comm.rank-1)<br><br># Fill in the left edge<br>x_arr[:, 0] = shadow_left<br>x.from_numpy(x_arr)</pre><p>shadow_left is a buffer area for storing the data received.</p><p>The receiving logic is the reversed version of data sending: each worker receives data from their <em>left-hand neighbor</em> and fills the data in the <em>left border</em> of its subfield. Mind that Worker 0 does not receive any data as it is the leftmost one, and the if statement above should ensure only Workers 1, 2, and 3 satisfy the data receiving condition.</p><p>Sending and receiving edge data in the opposite direction is similar to the process described above, so I won’t dive into details. The complete code can be found <a href="https://github.com/turbo0628/Taichi-MPI/tree/main/poisson">here</a>.</p><p>Now, our program can run on multiple machines, yet with unsatisfactory performance due to the following problems:</p><ol><li>Calling field.to_numpy() converts the entire field into a NumPy array, but it is the border data only that needs to be transmitted. Therefore, we can copy data on a smaller scale.</li><li>Currently, each subfield will not proceed to the next iteration until Recv receives data. But actually, they do not have to wait for data transfers as most of the computation happens internally.</li></ol><p>Let’s tackle the problems one by one.</p><h3>Optimization 01: Copy border data with Taichi</h3><p>In the original version, we first copy the entire field to a NumPy array and then slice the array to extract and transfer the border data. But we do not have to operate on the entire field; instead, we can accurately target the boundaries with several neatly-written Taichi kernels. The following code snippet shows how to fill in the border data in the grey areas:</p><pre>@ti.kernel<br>def fill_shadow(shadow_left : ti.types.ndarray(), <br>                shadow_right : ti.types.ndarray()):<br>    for i in ti.ndrange((0, N + 2)):<br>        x[i, 0] = shadow_left[i]<br>        x[i, n+1] = shadow_right[i]</pre><p>shadow_left and shadow_right are the NumPy buffers for receiving data. They can be passed directly to Taichi kernels, incurring no extra overhead apart from CPU-GPU data transfers – or no extra overhead at all if you are using the CPU backend where Taichi kernels operate on NumPy arrays directly.</p><h3>Optimization 02: Asynchronous communication</h3><p>Most stencil computing cases have only a small region subjected to data dependencies. In this Poisson solver, the inner cells of the subfields have no dependency on the border cells and thus play no part in communication. We can split substep into two functions to deal with them separately, i.e., substep_edge for computing border cells and substep_bulk for inner cells, as illustrated below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S65Hl4KpZZn9RdKPaIWGNg.png" /></figure><p>In this way, internal cells can be computed in parallel with communication, saving considerable overhead. To overlap communication with computation, we need to slightly change the implementation by replacing the blocking primitives Send and Recv with the non-blocking ones Isend and Irecv. The latter pair does not block the execution of subsequent statements until it encounters wait. We also need an additional parameter tag to label the data sent and received in each group of communications.</p><p>Accordingly, the communication code for data sending should be modified as follows:</p><pre>if comm.rank &lt; num_workers - 1:<br>    comm.Isend(edge_right, dest=comm.rank+1, tag=11)</pre><p>And the receiving part is updated as well:</p><pre>if comm.rank &gt; 0:<br>    comm.Irecv(shadow_left, source=comm.rank-1, tag=11).Wait()</pre><p>To reduce the total overhead, we call substep_bulk after data is sent and call substep_edge after the communication is done.</p><p>The computation and communication process is concluded as below:</p><pre>def step():<br>    mpi_send_edges() # Trigger communication<br>    substep_bulk() # Compute the bulk<br>    mpi_recv_edges() # Wait for communications<br>    substep_edge(shadow_left, shadow_right) # Compute the edges</pre><p>With this optimization, the computation time for substep_bulk and the MPI communication time overlap with each other, hence a more compact timeline and a decent performance gain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j388aXodWmp-CEttAgKkpw.png" /></figure><h3>Performance evaluation</h3><p>To evaluate and compare the performance of different implementations, I configured an MPI environment by connecting two GPU workstations head-to-head with the same router. One (node0) is equipped with one Nvidia RTX3080 GPU and Intel i9-11900k CPU, while the other (node1) has a GTX1080Ti and E3-1230v5. Both installed the Ubuntu 20.04 system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*J5Ei1DcS4Af-t_kshicstw.png" /></figure><p>I tested against the 8192 × 8192 fields on the connected machines and evaluated the CPU and GPU performance, respectively. With Taichi, we can switch between the CPU and GPU implementations effortlessly by simply specifying arch=ti.cpu or arch=ti.gpu upon initialization.</p><p>Ideally, distributed computing can achieve <em>linear scalability</em>, i.e., the performance gain in proportion to the number of machines added.</p><p>What is the gap between our optimized implementation and the ideal linear scalability, then?</p><ul><li>CPU implementations</li></ul><p>We can observe a significant difference in the CPU performance of the two computers, as indicated by the orange columns. As the fields are partitioned evenly, node0 needs to wait for node1 at every computing step. As the data shows, the performance of the fully optimized MPI version is roughly twice as fast as node1. Assuming that the two machines are mounted with the same CPU model, our MPI implementation can ideally achieve linear scalability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VxHzaPSo6q6M86Asr1Hf7Q.png" /></figure><ul><li>GPU implementations</li></ul><p>The original MPI implementation on the GPU backend even lags behind the CPU versions due to the extra overhead from CPU-GPU data transfers. After optimization with Taichi-aided data copy and asynchronous communication, the performance improved dramatically to almost twice as fast as node1 (171.2 / 89.68 = 1.91). The acceleration approaches the ideal scenario.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zcBDJeD1Vp-9YTVpV-EByw.png" /></figure><p>If we want to achieve linear scalability over hardware platforms with great performance differences, we need to re-partition the fields into uneven proportions. However, this is beyond the scope of this blog, and I will drop the discussion here.</p><h3>Conclusion</h3><p>In this blog, I demonstrate how to use Taichi and MPI4Py to implement parallel and distributed computing. The complete program takes only 100 or so lines of Python code, yet achieves satisfactory scalability. Hopefully, this blog can inspire those faced with large-scale numerical computations. Nevertheless, I touched upon the most basic case here. Real-life MPI programs can be much trickier, especially when it involves random data access or complex data dependencies that are hard to solve.</p><p>If you encounter any difficulties in leveraging MPI and Taichi or have better solutions, share them with us in the <a href="https://taichicommunity.slack.com/ssb/redirect#/shared-invite/email">slack channel</a> or our <a href="https://www.reddit.com/r/taichi_lang/">subreddit community</a>!</p><p>Reference code: <a href="https://github.com/turbo0628/Taichi-MPI/tree/main/poisson">https://github.com/turbo0628/Taichi-MPI/tree/main/poisson</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6aeffc69bd17" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/pythonic-supercomputing-scaling-taichi-programs-with-mpi4py-6aeffc69bd17">Pythonic Supercomputing: Scaling Taichi Programs with MPI4Py</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>
        <item>
            <title><![CDATA[How Taichi Fuels GPU-accelerated Image Processing: A Beginner to Expert Guide]]></title>
            <link>https://medium.com/parallel-programming-in-python/how-taichi-fuels-gpu-accelerated-image-processing-a-beginner-to-expert-guide-216295334956?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/216295334956</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[parallel-computing]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[image-processing]]></category>
            <dc:creator><![CDATA[Yuanming Hu]]></dc:creator>
            <pubDate>Mon, 14 Nov 2022 09:02:39 GMT</pubDate>
            <atom:updated>2022-11-14T09:02:39.516Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>How Taichi Fuels GPU-accelerated Image Processing: A Beginner to Expert Guide</strong></p><p>(<a href="https://github.com/neozhaoliang">neozhaoliang</a> also contributed to this article.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RD4GRdivqVAspHX1p77a7g.png" /></figure><p>We already proved that Taichi can effectively <a href="https://docs.taichi-lang.org/blog/accelerate-python-code-100x">accelerate Python</a> programs (at least by 50 times). Now, many of our readers are curious about whether Taichi can fuel specifically <em>image processing tasks</em>, work together with OpenCV (import cv2), and even process images in parallel on GPUs. Well, in this article we will strive to provide answers to these questions.</p><p>To make your reading experience less tedious, we will take the beauty filters and the HDR effect of the popular console game <em>Ghost of Tsushima</em> as examples to elaborate on image processing algorithms, including the Gaussian filter, the bilateral filter, and the bilateral grid, in ascending order of difficulty. You are recommended to write code as you read on, and we’re sure you will not close this page empty-handed whether you are familiar with image processing or not. We hope that this article can serve you well and look forward to your feedback, positive or negative :)</p><h3>Introduction</h3><p>Python is one of the most widely used languages for image processing. In computer vision and other rapidly evolving sectors, Python is considered the go-to option for many researchers when they conduct fast algorithm prototyping and verification, especially in scenarios like image pre-processing and model training. OpenCV, a popular image processing library, also provides a Python encapsulation so that users can write Python code to call underlying C++ algorithms and get performance gains.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*_eoZFhwR8E80iTdygIPsPA.jpeg" /></figure><p>However, real-life R&amp;D does not always go as expected. More often than not, an OpenCV interface alone is far from being a panacea: You will find Python less attractive when you have to implement by yourself some image processing algorithms that OpenCV does not supply. It is unbearably slow to run for loops in Python to iterate over the pixels of images, which are usually stored in memory as NumPy arrays. In addition, the overhead incurred by the Python interpreter constitutes an annoying performance bottleneck when real-time processing (such as camera feeds) or large amounts of data are involved.</p><p>Here’s when Taichi comes into play:</p><ul><li>The outermost for loops in a Taichi kernel are automatically parallelized, and users won’t be bothered with thread allocation or management.</li><li>Taichi’s just-in-time (JIT) compiler compiles the Taichi code to fast machine code, which is executed on the multi-core CPU or GPU backend. Users are freed from compilation and system adaptation pitfalls.</li><li>Taichi switches effortlessly between CPU and GPU execution.</li><li>Taichi saves the need to call separately-written C++/CUDA code in Python via ctypes or pybind 11 and thus enhances coding convenience and portability. Users can switch freely back and forth between OpenCV implementations and Taichi implementations, keeping everything within one Python program.</li></ul><p>With the features listed above, Taichi not only maximizes the simplicity of coding in Python, but also achieves high performance on par with C++/CUDA.</p><p>So much for the theory. Let’s get down to practice. This article is composed of three sections to explain how Taichi accelerates image processing in Python:</p><p>I. An entry-level case: Image transposition and bilinear interpolation</p><p>II. An intermediate case: Gaussian filtering and bilateral filtering</p><p>III. An advanced case: Bilateral grid and high dynamic range (HDR) tone mapping</p><p>We will demonstrate, step by step, how image processing algorithms evolve, and explore some of their fascinating applications. In the end, we will summarize things you need to keep in mind when using Taichi for image processing and also discuss Taichi’s current limitations for future improvement.</p><p>One final thing before the kick-start: Make sure you have installed the latest Taichi and opencv-python on your machine:</p><pre>pip3 install -U taichi opencv-python</pre><p>All source code used in this article is available at this repo: <a href="https://github.com/taichi-dev/image-processing-with-taichi">Image processing with Taichi</a></p><h3>An entry-level case: Image transposition and interpolation</h3><h4>Image transposition</h4><p>Let’s start with a basic example, image transposition, to familiarize you with the essential steps of image processing with Taichi.</p><p>Similar to matrix transposition, image transposition entails swapping the positions of the pixels at <em>(i, j)</em> and <em>(j, i)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1PabdVXud09tZu6e3mL8mg.png" /></figure><p>We first import the libraries we need. An indispensable step for each Taichi program is initiation via ti.init():</p><pre>import cv2<br>import numpy as np<br>import taichi as ti<br><br>ti.init()</pre><p>Then, read the cat image into memory:</p><pre>src = cv2.imread(&quot;./images/cat.jpg&quot;)<br>h, w, c = src.shape<br>dst = np.zeros((w, h, c), dtype=src.dtype)<br><br>img2d = ti.types.ndarray(element_dim=1) # Color image type</pre><p>Most of the image processing libraries available in Python assume that images exist in the form of NumPy arrays, and so does OpenCV. A (single-channel) grayscale image is represented with a 2D array of the shape (height, width); a (multi-channel) color image corresponds to a 3D array of the shape (height, width, channels). In the code snippet above, the image src read by OpenCV is a 3D NumPy array, and we subsequently declare dst, which is of the same data type as src yet with swapped height and width, for storing the transposed image.</p><p>Now, we involve the function transpose() to deal with the transposition. Looking exactly like a Python function, transpose() actually serves as a Taichi kernel here since it is decorated with ti.kernel:</p><pre>@ti.kernel<br>def transpose(src: img2d, dst: img2d):<br>    for i, j in ti.ndrange(h, w):<br>        dst[j, i] = src[i, j]</pre><p>This is a neat but important kernel. To write it properly, we need to understand the following:</p><ul><li>src and dst in Line 2 represent the input and output images, respectively. They are both type-annotated by img2d = ti.types.ndarray(element=1). Taichi allows you to pass a NumPy array as an argument to a kernel through ti.types.ndarray(). You can ignore the meaning of element_dimfor now, except that it enables us to manipulate the three channels of a pixel at the same time. The array is <em>passed by</em> <em>pointer</em> without generating an extra copy (but automatic CPU/GPU data transfer occurs on the GPU backend). In this way, any modification of the passed argument in the kernel affects the original array.</li><li>for i, j in ti.ndrange(h, w) is the outermost for loop in the kernel. It automatically iterates over all the elements in the array in parallel. Specifically, i traverses all the rows and j all the columns.</li></ul><p>Finally, call the kernel and save dst as an image:</p><pre>transpose(src, dst)<br>cv2.imwrite(&quot;cat_transpose.jpg&quot;, dst)</pre><p><em>And the output is the transposed image.</em></p><p>Source code: <a href="https://github.com/taichi-dev/image-processing-with-taichi/blob/main/image_transpose.py">image_transpose.py</a></p><h4>Bilinear interpolation</h4><p>Image transposition is friendly enough for beginners, isn’t it? Now we are ready to increase the difficulty level a bit.</p><p>Bilinear interpolation is a technique frequently used for image upsampling. Suppose the cat image we have has a size of 96x64 pixels only, and now we want to enlarge it five times into 480x320 pixels. Magnifying each pixel into a 5x5 mosaic may not be a good idea:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/96/1*u463k_pAQrx5zBVn52JyBQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*cJQWok9C08VShJGQN-526g.png" /><figcaption>upsampling by enlarging each pixel five times</figcaption></figure><p>For a pixel <em>(i, j)</em> in the output image, its corresponding position in the original image is <em>P=(i/5, j/5)</em>, which does not necessarily coincide with any pixel of the input. Rounding <em>P</em> up or down to the nearest pixel produces the mosaic effect as above.</p><p>Bilinear interpolation takes a different approach. It captures the four pixels around <em>P</em> and returns the weighted mean of their pixel values:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*6eVAnGIwDfLvkfoUoMNvag.png" /><figcaption>Image source: <a href="https://en.wikipedia.org/wiki/Bilinear_interpolation">Wikipedia</a></figcaption></figure><p>The four pixels surrounding <em>P=(x,y)</em> are:</p><p><em>Q</em>₁₁<em> = (x</em>₁<em>,y</em>₁<em>), Q</em>₁₂<em> = (x</em>₁<em>,y</em>₂<em>), Q</em>₂₁<em> = (x</em>₂<em>,y</em>₁<em>), Q</em>₂₂ <em>= (x</em>₂<em>,y</em>₂<em>)</em></p><p>They form a unit square, whose area (equivalent to the sum of the areas of the four rectangles) is 1. The weight of each pixel is the area of the rectangle in the same color as the pixel. For example, if <em>P</em> moves closer to the yellow pixel <em>Q</em>₁₂ in the upper left corner, the yellow rectangle at the bottom right will become larger, assigning a larger weight to <em>Q</em>₁₂.</p><p>We can now perform three 1D linear interpolations. We first adopt the weight <em>x — x</em>₁ to perform interpolations on the pairs <em>(Q</em>₁₁<em>,Q</em>₂₁<em>)</em> and <em>(Q</em>₁₂<em>,Q</em>₂₂<em>)</em>, respectively, and get the results <em>R</em>₁ and <em>R</em>₂. Then, interpolate <em>(R</em>₁<em>,R</em>₂<em>)</em> with the weight <em>y — y</em>₁.</p><pre>import taichi.math as tm<br><br>@ti.kernel<br>def bilinear_interp(src: img2d, dst: img2d):<br>    for I in ti.grouped(dst):<br>        x, y = I / scale<br>        x1, y1 = int(x), int(y)  # Bottom-left corner<br>        x2, y2 = min(x1 + 1, h - 1), min(y1 + 1, w - 1)  # Top-right corner<br>        Q11 = src[x1, y1]<br>        Q21 = src[x2, y1]<br>        Q12 = src[x1, y2]<br>        Q22 = src[x2, y2]<br>        R1 = tm.mix(Q11, Q21, x - x1)<br>        R2 = tm.mix(Q12, Q22, x - x1)<br>        dst[I] = ti.round(tm.mix(R1, R2, y - y1), ti.u8) # Round into uint8</pre><p>In the code snippet above, the 2D index I, which denotes the coordinate of a pixel in the output image dst, traverses all the pixels in dst. I/scale returns <em>(x,y)</em>, which is I&#39;s corresponding position in the input image src. <em>R1</em> represents the horizontal interpolation between the pixels <em>Q</em>₁₁<em> = (x</em>₁<em>,y</em>₁ and <em>Q</em>₂₁<em> = (x</em>₂<em>, y</em>₁<em>)</em>, and <em>R</em>₂ represents the horizontal interpolation between the pixels <em>Q</em>₁₂<em> = (x</em>₁<em>,y</em>₂<em>)</em> and <em>Q</em>₂₂<em> = (x</em>₂<em>, y</em>₂<em>)</em>. The final output pixel value is derived after finishing the vertical interpolation between <em>R</em>₁ and <em>R</em>₂.</p><p>tm.mix() above is the function that performs the 1D interpolation. It is provided by the taichi.math module and defined as follows:</p><pre>@ti.func<br>def mix(x, y, a): # also named &quot;lerp&quot; in other libraries<br>    return x * (1 - a) + y * a</pre><p><em>A comparison of the mosaic effect and bilinear interpolation:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*cJQWok9C08VShJGQN-526g.png" /><figcaption>Enlarging each pixel five times</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*UTYhS4R6e2uW4fXLtSL0yQ.png" /><figcaption>Bilinear interpolation</figcaption></figure><p><em>Obviously, the output image of bilinear interpolation is more natural.</em></p><p>Source code: <a href="https://github.com/taichi-dev/image-processing-with-taichi/blob/main/image_bilinear_inpterpolation.py">image_bilinear_inpterpolation.py.</a></p><h3>An intermediate case: Gaussian filtering and bilateral filtering</h3><h4>Gaussian filtering</h4><p>Gaussian filtering is one of the most widely used filtering algorithms in the field of image processing. It attenuates the high-frequency information to smoothen and blur an image. A Gaussian filter convolves a 2D image with a matrix named a Gaussian kernel, whose elements are derived from the sampling of the 2D Gaussian distribution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*uD64UvYcKOiBLoVGQpBkVw.png" /><figcaption>A 2D Gaussian convolution kernel; Image source: “Fast Bilateral Filtering for the Display of High-Dynamic-Range Images” by Durand and Dorsey, SIGGRAPH 2006</figcaption></figure><p>The probability density function of the 2D Gaussian distribution is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*i8rDsQBF46CSXw_VJ6fiAQ.png" /></figure><p>For a <em>(2k+1)×(2k+1)</em> Gaussian kernel <em>K</em>, its elements are derived from <em>G(x,y)</em> sampled at the points falling within <em>(i,j)｜-k ≤ i,j ≤ k</em>. For example, the following is a 3x3 Gaussian kernel:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*YSKC-UctcqiI6Zc0E7yU6w.png" /></figure><p>Given that</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*VWptoNhVJ1vdhvJT7kyOzA.png" /></figure><p>denotes the product of two 1D density functions, the Gaussian kernel is separable. <em>K</em> can be represented as the product of a 1D vector <em>v = (G</em>₁<em>(-k),G</em>₁<em>(-k + 1),…,G</em>₁<em>(k))</em>ᵀ and the transpose of the vector: <em>K = v × v</em>ᵀ.</p><p>Accordingly, the convolution between an image and the kernel <em>K</em> can be separated into two 1D convolution operations, i.e., convolution of each column using <em>v</em> and convolution of each row using <em>v</em>ᵀ (<a href="http://www.songho.ca/dsp/convolution/convolution.html#convolution_2d">this website</a> provides a proof). In this way, the 2D convolution <em>O(m×n×k²)</em> is simplified into <em>O(m×n×k)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*qGls1HRQo__DgVRM_thOSQ.png" /><figcaption>From left to right: the original image, the intermediate result of vertical filtering, and the final result of complete Gaussian filtering (<em>σ = 5.0</em>); Input image source: <a href="https://en.wikipedia.org/wiki/Bilateral_filter">Wikipedia</a></figcaption></figure><p>To write the program in Taichi, we should first create a 1D field (essentially a 1D data array) for storing the 1D Gaussian kernel:</p><pre>weights = ti.field(dtype=float, shape=1024, offset=-512)</pre><p>The field’s shape is set to 1,024, which would suffice to meet the needs of most scenarios. Note that offset=-512 means that the field index starts from -512 and ends with 511. The offset feature provided by Taichi helps simplify coding by ensuring the index range is symmetrical about the origin.</p><p>Initialize this field using @ti.func:</p><pre>@ti.func<br>def compute_weights(radius: int, sigma: float):<br>    total = 0.0<br>    ti.loop_config(serialize=True)<br>    for i in range(-radius, radius + 1):<br>        val = ti.exp(-0.5 * (i / sigma)**2)<br>        weights[i] = val<br>        total += val<br><br>    ti.loop_config(serialize=True)<br>    for i in range(-radius, radius + 1):<br>        weights[i] /= total</pre><p>The parameter radius controls the size of the Gaussian kernel - the element index of the kernel ranges between [-radius, radius]. sigma represents the variance of the Gaussian density function.</p><p>One thing worth attention is that ti.loop_config(serialize=True) disables the parallelization of the immediately following for loop. It is more efficient to serialize the for loops for non-intensive computation tasks to avoid the heavy thread overhead generated on CPUs/GPUs. We can safely ignore the coefficient <em>1/2πσ²</em> when computing each element of the Gaussian kernel, and use the variable total for the final normalization.</p><p>We now get down to the two 1D convolution operations. Declare a 1,024x1,024 vector field (essentially a 2D data array whose elements are RGB values) for storing the intermediate image after the first filtering:</p><pre>img = cv2.imread(&#39;./images/mountain.jpg&#39;)<br>img_blurred = ti.Vector.field(3, dtype=ti.u8, shape=(1024, 1024))</pre><p>Conduct the vertical and horizontal 1D filtering, consecutively:</p><pre>@ti.kernel<br>def gaussian_blur(img: img2d, sigma: float):<br>    img_blurred.fill(0)<br>    blur_radius = ti.ceil(sigma * 3, int) <br>    compute_weights(blur_radius, sigma)<br><br>    n, m = img.shape[0], img.shape[1]<br>    for i, j in ti.ndrange(n, m):<br>        l_begin, l_end = max(0, i - blur_radius), min(n, i + blur_radius + 1)<br>        total_rgb = tm.vec3(0.0)<br>        for l in range(l_begin, l_end):<br>            total_rgb += img[l, j] * weights[i - l]<br><br>        img_blurred[i, j] = total_rgb.cast(ti.u8)<br><br>    for i, j in ti.ndrange(n, m):<br>        l_begin, l_end = max(0, j - blur_radius), min(m, j + blur_radius + 1)<br>        total_rgb = tm.vec3(0.0)<br>        for l in range(l_begin, l_end):<br>            total_rgb += img_blurred[i, l] * weights[j - l]<br><br>        img[i, j] = total_rgb.cast(ti.u8)</pre><p>In the code snippet above, Lines 3–5 set the post-filtering image to 0 and initialize the Gaussian filter. The following two for loops, as defined by Lines 7–14 and Lines 16–22, respectively, are essentially the same, except that the first for loop saves the column filtering results in img_blurred and the second for loop saves results back to the input image img.</p><p>With preparation done, we can compute the product of the filter’s elements and the elements of img. It is as simple as that!</p><p>Source code: <a href="https://github.com/taichi-dev/image-processing-with-taichi/blob/main/gaussian_filter_separate.py">gaussian_filter_separate.py</a></p><h4>Bilateral Filtering</h4><p>Adopting fixed weights, Gaussian filtering effectively smoothens images but inevitably losses some details. As a result, image edges are usually blurred.</p><p>Can we <em>preserve the details on the edges</em> when smoothening an image? An idea is that an additional weighting factor can be introduced to reflect the difference in pixel values since Gaussian filtering only considers the distance between pixels. This is what bilateral filtering does.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nVgoZ3q5MUN31aaRi0uLCg.png" /><figcaption>Image source: <em>Fast Bilateral Filtering for the Display of High-Dynamic-Range Images</em> by Durand and Dorsey, SIGGRAPH 2006</figcaption></figure><p>As the image above shows, we create a 3D surface plot out of a 2D (single-channel) grayscale image, with the heights representing pixel values. Thanks to bilateral filtering, the output image has smooth slopes and preserves clear cliffs (i.e., the edges).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oA3rirVYam1QOPSK-FxpVg.png" /></figure><p><em>Gσ</em>ₛ refers to the distance-based Gaussian kernel, as explained in the last section, and <em>Gσ</em>ᵣ is a new Gaussian kernel determined by the difference in pixel values:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*7-DHEApWaRbz6DHH8-CFyw.png" /></figure><p><em>W</em>ₚ is the normalization coefficient. The weight carried by the pixel <em>(k,l)</em> for filtering the pixel <em>(i,j)</em> can be denoted as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l3oScfvWvEb0QAzajFCFFw.png" /></figure><p>The equation indicates that the bigger the difference in pixel values <em>|I(i,j) — I(k,l)|</em> is, the smaller the weight is allocated to <em>I(k,l)</em>. Therefore, the image edges are kept relatively intact.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*45uUYQBI6Wad4W4_S_ppmA.png" /><figcaption>Left: Original image. Right: Output image of bilateral filtering</figcaption></figure><p>The bilateral filter cannot be separated into two 1D convolution operations. So we have to write the 2D convolution patiently:</p><pre>img_filtered = ti.Vector.field(3, dtype=ti.u8, shape=(1024, 1024))<br><br>@ti.kernel<br>def bilateral_filter(img: img2d, sigma_s: ti.f32, sigma_r: ti.f32):<br>    n, m = img.shape[0], img.shape[1]<br><br>    blur_radius_s = ti.ceil(sigma_s * 3, int)<br><br>    for i, j in ti.ndrange(n, m):<br>        k_begin, k_end = max(0, i - blur_radius_s), min(n, i + blur_radius_s + 1)<br>        l_begin, l_end = max(0, j - blur_radius_s), min(m, j + blur_radius_s + 1)<br>        total_rgb = tm.vec3(0.0)<br>        total_weight = 0.0<br>        for k, l in ti.ndrange((k_begin, k_end), (l_begin, l_end)):<br>            dist = ((i - k)**2 + (j - l)**2) / sigma_s**2 + (img[i, j].cast(<br>                ti.f32) - img[k, l].cast(ti.f32)).norm_sqr() / sigma_r**2<br>            w = ti.exp(-0.5 * dist)<br>            total_rgb += img[k, l] * w<br>            total_weight += w<br><br>        img_filtered[i, j] = (total_rgb / total_weight).cast(ti.u8)<br><br>    for i, j in ti.ndrange(n, m):<br>        img[i, j] = img_filtered[i, j]</pre><p>Overall, the bilateral filtering process resembles Gaussian filtering. But there are some slight differences we should be aware of. The bilateral filtering involves one for loop only to process all the pixels in parallel; before normalization, we use total_weights to count the weights of the pixels covered by the Gaussian kernel and total_rgb to calculate the weighted sum of these pixel values.</p><h4>Application: Beauty filters… or actually bilateral filters</h4><p>Among all the diverse applications of bilateral filtering, the most typical ones should be image denoising and smoothing, with the latter often seen in <em>beauty filters</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RWa8yyEXy5ucUnZtZZPeOQ.jpeg" /><figcaption>Image source: Pixarbay</figcaption></figure><p>We can see that the bilateral filter smoothens the skin by removing local details and preserving the sharp edges. For contrast, we also apply a Gaussian filter with the same radius to the same original image, and it can barely beautify the face since all the edges are blurred.</p><p>Source code: <a href="https://github.com/taichi-dev/image-processing-with-taichi/blob/main/bilateral_filter.py">bilateral_filter.py</a></p><h3>An advanced case: Bilateral grid</h3><h4>Performance bottleneck of bilateral filtering</h4><p>Although bilateral filtering prevents image edges from being blurred, it has an unignorable performance bottleneck. Normally, we can optimize Gaussian filtering by separating a 2D filter into two 1D filters and applying the fast Fourier transform (FFT) to large convolution kernels. However, neither option works for a bilateral filter, which is inseparable and rules out FFT because of its dependence on the image content.</p><p>Can we retrofit a bilateral filter to make it separable? Well, the answer lies in the <em>bilateral grid</em>.</p><p>The bilateral grid was proposed in the <a href="https://people.csail.mit.edu/sparis/publi/2007/siggraph/Chen_07_Bilateral_Grid.pdf">SIGGRAPH 2007 paper by Chen et al.</a> It is basically an accelerated version of the bilateral filter. The method is brilliant in that it manages to simplify filtering by resorting to higher-dimensional spaces. All the following steps we have to go through to implement the bilateral grid may seem intimidating at first sight, but we assure you that the idea behind is straightforward.</p><p>A bilateral filter cannot be separated because the convolution kernels contain pixel values. But what if we create a third dimension for the independent convolution of pixel values? This is what the bilateral grid is about — involving higher dimensions to separate a bilateral filter.</p><p>Take a grayscale image as an example. The image is a 2D grid <em>I</em>, with each element storing a grayscale pixel value. We now transform the grid into a 3D one, whose third dimension equals the pixel value range [0, 255] in length. That is to say, the new grid has a shape of <em>w× h× 255</em>. The pixel value at the position <em>(i, j, k)</em> in the new grid is denoted as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*lcXNJsRYbdutn4fJ_DapgQ.png" /></figure><p>Apply a 3D Gaussian filter to this grid, and the result is a new 3D grid:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/298/1*KHzBWP83gyj42KKbvmQG-Q.png" /></figure><p><em>Γ(i,j) = z/w</em> is what we can derive from the bilateral filtering of the original grid <em>I</em>.</p><p>A useful technique: The grid <em>I</em> stores the 2-tuple <em>(I(i,j),1)</em> to record the weighted sum of pixel values, i.e., <em>∑ w</em>ᵢ<em>I(x</em>ᵢ<em>)</em>, into the first entry and the sum of weights, i.e., <em>∑ w</em>ᵢ, into the second entry during convolution. The division of the two results gives the normalized value.</p><p>The figure below is excerpted from the paper, illustrating how to upgrade a 1D image to 2D. The process involves three steps: create, process, and slice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eFK7MB0PhI-K0RIQ8i7ljw.png" /><figcaption>The bilateral grid “trio”: create, process, and slice. Image source: <a href="https://people.csail.mit.edu/sparis/publi/2007/siggraph/Chen_07_Bilateral_Grid.pdf">SIGGRAPH 2007 paper by Chen et al.</a></figcaption></figure><p>The leftmost image (a) in the figure shows the input 1D signal, where an evident break between the two segments represents the image edges. The second image on the left (b) was a 2D grid generated based on (a), converting the segments into two disconnected regions. The pixels on the tag end of the region above are far away from those on the starting point of the region below, hence little or even no weight they exert on each other during 2D Gaussian filtering. The edge information of the bilateral grid is preserved as such.</p><p>This figure chooses a 1D signal as an example for simplicity. In practice, we can input a 2D image and upgrade it to 3D.</p><p>Real-life scenarios can be tricky, though. A 1,024x1,024 (1MB) grayscale image would occupy 256MB of memory after being transformed into a 1,024x1,024x256 grid. It is also unnecessary to maintain such a large and precise grid since what we need is filtered results only. Therefore, we can scale down the generated grid proportionally through downsampling and restore it through oversampling after the filtering.</p><p>We provide a step-by-step guide below to filter a <em>grayscale</em> image with the bilateral grid.</p><h4>Step 1: Create a grid (downsampling and higher dimensions)</h4><p>To begin with, declare two 512x512x128 (32MB) vector fields, which store the 3D grid and its intermediate filtering results, respectively. To store the coefficients of filters, declare another field of the shape (2x512), where 2 stands for the two filters applied to the spatial space of the image and the range space of pixels, respectively.</p><pre>grid = ti.Vector.field(2, dtype=ti.f32, shape=(512, 512, 128))<br>grid_blurred = ti.Vector.field(2, dtype=ti.f32, shape=(512, 512, 128))<br>weights = ti.field(dtype=ti.f32, shape=(2, 512), offset=(0, -256))</pre><p>Executed inside a Taichi kernel, the following code snippet initializes grid. We leave out the initialization of weights here because it has been discussed in the Gaussian filtering part.</p><pre>grid.fill(0)<br>for i, j in ti.ndrange(img.shape[0], img.shape[1]):<br>    lum = img[i, j]<br>    grid[ti.round(i / s_s, ti.i32),<br>         ti.round(j / s_s, ti.i32),<br>         ti.round(lum / s_r, ti.i32)] += tm.vec2(lum, 1)<br><br>grid_blurred.fill(0)<br>grid_n = (img.shape[0] + s_s - 1) // s_s<br>grid_m = (img.shape[1] + s_s - 1) // s_s<br>grid_l = (256 + s_r - 1) // s_r</pre><p>s_s is the scale factor of the image resolution, and s_r is the scale factor of the range space [0, 255]. grid_blurred is initialized to 0.</p><p>We also compute the actual size of the scaled 3D grid, as denoted by grid_n, grid_m, and grid_l.</p><h4>Step 2: Process the grid (Gaussian blurring)</h4><p>We are now ready to apply a 3D Gaussian filter to the 3D grid grid. The code snippet below may look lengthy and daunting, but it is merely a repetition of the Gaussian filtering we have discussed in the first section, only with one more filter involved.</p><p>Note that the classic bilateral filter operator is separated into three filters. The following code is executed inside a Taichi kernel.</p><pre>blur_radius = ti.ceil(sigma_s * 3, int)<br>for i, j, k in ti.ndrange(grid_n, grid_m, grid_l):<br>    l_begin, l_end = max(0, i - blur_radius), min(grid_n, i + blur_radius + 1)<br>    total = tm.vec2(0, 0)<br>    for l in range(l_begin, l_end):<br>        total += grid[l, j, k] * weights[0, i - l]<br><br>    grid_blurred[i, j, k] = total<br><br>for i, j, k in ti.ndrange(grid_n, grid_m, grid_l):<br>    l_begin, l_end = max(0, j - blur_radius), min(grid_m, j + blur_radius + 1)<br>    total = tm.vec2(0, 0)<br>    for l in range(l_begin, l_end):<br>        total += grid_blurred[i, l, k] * weights[0, j - l]<br>    grid[i, j, k] = total<br><br>blur_radius = ti.ceil(sigma_r * 3, int)<br>for i, j, k in ti.ndrange(grid_n, grid_m, grid_l):<br>    l_begin, l_end = max(0, k - blur_radius), min(grid_l, k + blur_radius + 1)<br>    total = tm.vec2(0, 0)<br>    for l in range(l_begin, l_end):<br>        total += grid[i, j, l] * weights[1, k - l]<br><br>    grid_blurred[i, j, k] = total</pre><p>The three outermost for loops correspond to the three filters: the former two act on the spatial space using the weight weights[0] and the latter one on the range space using the weight weights[1].</p><p>We write the intermediate filtering results into grid_blurred and grid in turn, and the final results after three rounds of filtering are stored in grid_blurred.</p><h4>Step 3: Slice the grid (trilinear interpolation)</h4><p>Finally, we obtain the filtered image from the down-sampled grid grid_blurred through oversampling. This step is nothing but straightforward. Again, the code below is executed inside a Taichi kernel.</p><pre>for i, j in ti.ndrange(img.shape[0], img.shape[1]):<br>    lum = img[i, j]<br>    sample = sample_grid(i / s_s, j / s_s, lum / s_r)<br>    img[i, j] = ti.u8(sample[0] / sample[1])</pre><p>For the position [i, j, img[i,j]] in the pre-scaling 3D grid, we derive its value by passing the value of [i / s_s, j / s_s, lum / s_r] in the post-scaling grid into the interpolation function sample_grid. The result should be a 2-tuple <em>(z,w)</em>, where <em>z</em> represents the weighted sum of pixels and <em>w</em> the sum of weights. The division of <em>z</em> and <em>w</em> gives the filtered value.</p><p>Wait… Have we missed out something? You may wonder what the interpolation function sample_grid is. It is not invented out of nowhere but merely an upgrade of the bilinear interpolation that we have explained in the first section. It is trilinear in nature, and similar to bilinear interpolation, it calculates the weighted sum of the eight vertices of a unit cube. The function consists of two steps: bilinear interpolations of the top and bottom faces of the cube, respectively, followed by a linear interpolation operation on the results derived from the aforementioned step.</p><p>Source code: <a href="https://github.com/taichi-dev/image-processing-with-taichi/blob/main/bilateral_grid.py">bilateral_grid.py</a></p><p><strong>A quick recap:</strong> The higher-dimension approach replaces an inseparable 2D convolution with a separable 3D convolution. Performance-wise, it is recommended for the following reasons:</p><ul><li>The XY resolution of the 3D grid is much lower than that of the original image, which means that the number of the voxels of the 3D grid is not necessarily more than that of the pixels of the 2D image.</li><li>At the same time, separating the expensive 2D convolution dramatically reduces the computation workload.</li><li>Another attractive advantage of the approach is that it is GPU-friendly, especially when implemented with Taichi :-)</li></ul><h4>Application: Real-time local tone mapping (HDR effect)</h4><p>The <em>dynamic range</em>, i.e., the ratio between the maximum and minimum light intensities, that can be perceived by human eyes is <em>1:10⁹</em>. However, an ordinary display screen can only display images with a dynamic range limited to <em>1:100 ~ 1:1000</em>, while an advanced camera captures a range lower than what human eyes are capable of but better than display screens. As a result, displaying high dynamic range (HDR) pictures on low dynamic range (LDR) devices without any processing often leads to a loss of details in the shadows or in the highlights.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TM85iFj8ZDubud8EcHMGbA.png" /><figcaption>The dilemma about processing scenes with a great contrast between lights and shadows: A low brightness loses details in the shadows (e.g., branches in complete darkness), but a higher brightness causes overexposure of the sky.</figcaption></figure><p>We can solve the problem with a technique called tone mapping. Generally, there are two kinds of tone mapping: global tone mapping, which adjusts the brightness of all the pixels with the same function, and local tone mapping, which is context-based adjustment depending on individual pixels’ neighbors. Local tone mapping is usually considered the better option.</p><p>It all sounds terrific. But what is the relationship between local tone mapping and bilateral filtering? Bilateral filtering was introduced in the first place to <em>preserve the edges and blur the local details</em>, while local tone mapping serves the opposite purpose — <em>preserving the local details and blurring the edges</em>. To put it simply, local tone mapping can be regarded as a <em>reversed</em> version of bilateral filtering. Therefore, it can be used to compress HDR images as human eyes tend to pay more attention to local details.</p><p>Follow the steps below to implement local tone mapping with the bilateral grid:</p><ol><li>Calculate the log luminance of an RGB image (i.e., logarithm of the weighted sum of the RGB values) and name the new image <em>L</em>.</li><li>Apply bilateral filtering to <em>L</em> and get the blurred image, i.e., log luminance base: <em>B = bilateral_filter(L)</em>.</li><li>Calculate the luminance detail layer: <em>D = L − B</em>, which is the brightness part we want to preserve.</li><li>Compress B: <em>B’ = αB</em>, (<em>0 &lt; α &lt; 1</em>). B is the major cause of the wide dynamic range, and compressing it would not affect viewers’ perception.</li><li>Recalculate the log luminance of the adjusted image: <em>L’ = B’ + D + β</em>, where <em>β</em> is a constant standing for exposure compensation.</li><li>Compute the new image based on <em>L’</em>, ensuring that the RBG values of each pixel are in proportion to the original values and that the luminance is the same as <em>L’</em>.</li></ol><p><em>At last, we get a more natural picture where shadows and highlights are balanced and vividly presented.</em></p><p>Source code: <a href="https://github.com/taichi-dev/image-processing-with-taichi/blob/main/bilateral_grid_hdr.py">bilateral_grid_hdr.py</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*LXyTC9vHo294pRRCp7f0lg.jpeg" /><figcaption>Image captured on the MIT campus. Save and run the source code on your device. Play with the parameters to see how the effects change. You may see haloing at some point. The parameter “blend” controls tone mapping.</figcaption></figure><p>A well-known case where the bilateral grid is used for tone mapping is the adventure game <em>Ghost of Tsushima</em>, where it takes only 250 ƒus to process a scene for display on a 1080P PS4 screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gOgVIxDScE7q5t2oYYf7wQ.png" /><figcaption>Image source: “<em>Advances in Real-Time Rendering: Real-Time Samurai Cinema: Lighting, Atmosphere, and Tonemapping in Ghost of Tsushima”</em>, SIGGRAPH 2021 This <a href="https://www.youtube.com/watch?v=GOee6lcEbWg">Youtube video</a> elaborates on the post-processing pipelines of the game in great detail.</figcaption></figure><h3>Notes</h3><ul><li><strong>Storage and visualization:</strong> OpenCV stores image channels in the BGR format by default for some historical reasons, while Taichi’s visualization system adopts the RGB format. In addition, OpenCV’s visualization tool imshow() sets the upper left corner of an image as the origin (0, 0) with i representing the y-axis and j the x-axis; Taichi&#39;s gui.set_image creates a slightly different coordinate system by taking the bottom left corner as the origin with i representing the x-axis and j the y-axis. This article does not go into the details of the coordinate system because Taichi is mainly used for computation instead of visualization here.</li><li><strong>Debugging:</strong> You can activate debug mode by setting debug=True in the ti.init() call to automatically detect common errors like out-of-bound array accesses at the cost of slight performance loss. If you use a raw CUDA, these errors are harder to detect.</li><li><strong>Data types:</strong> Images are often stored as NumPy arrays of the data type np.uint8, which corresponds to ti.u8 in Taichi. Similarly, np.float32 corresponds to Taichi&#39;s ti.f32 or simply float.</li><li><strong>Performance:</strong> Taichi’s advantage in GPU-accelerated computation may not come into full play when it is used to process low-resolution images because the launch of GPU kernels, JIT compilation, and the overhead from pybind11 would offset any acceleration. You can feel its high performance more evidently in scenarios involving high-resolution images and complex computations.</li></ul><h3>Summary</h3><p>Is Taichi better positioned to accelerate image processing compared with other tools? The answer is affirmative. If you ever had experience with implementing classic algorithms like the bilateral grid in Python, you would agree that it used to be extremely laborious to achieve GPU-powered high performance.</p><p>At the same time, we admit that Taichi can do better in the following aspects:</p><ul><li>There are too many APIs with long names (such as ti.types.ndarray(element_dim=1)) for users to remember.</li><li>Users often have needs of deployment after prototyping, and Taichi expects to support deployment with an AOT system, which is still under development.</li><li>The overhead incurred by calling pybind11 for processing low-resolution images may compromise the performance.</li><li>Taichi outperforms other tools in customization but does not stand out in terms of the productivity of some simple and standard operations, such as Gaussian blurring, which can be conveniently implemented in Matlab/OpenCV with one line of code only. In the future, Taichi may incorporate these operations as well.</li></ul><p>Although Taichi is not perfect yet as an image processing tool, we cannot deny its irreplaceable value for users. As our open-source community keeps improving its features and user experience, Taichi will surely secure a place and gain increasing popularity in this sector.</p><p>All the source code and input images are available in <a href="https://github.com/taichi-dev/image-processing-with-taichi">this repo</a>.</p><p>It is already a lengthy article, and we have to leave out any rigorous evaluation of the programs, attempts at optimization, or ahead-of-time (AOT) deployment to mobile devices. Hopefully, we can touch upon these topics soon. If you are interested, subscribe to our monthly newsletter for the latest blogs and other useful information!</p><h3>References</h3><ul><li>MIT tutorial on bilateral filtering: <a href="https://people.csail.mit.edu/sparis/bf_course/">https://people.csail.mit.edu/sparis/bf_course/</a></li><li>Original paper that proposes the bilateral grid: <a href="https://people.csail.mit.edu/sparis/publi/2007/siggraph/Chen_07_Bilateral_Grid.pdf">https://people.csail.mit.edu/sparis/publi/2007/siggraph/Chen_07_Bilateral_Grid.pdf</a></li><li>Real-time post-processing of <em>Ghost of Tsushima</em>: <em>Advances in Real-Time Rendering: Real-Time Samurai Cinema: Lighting, Atmosphere, and Tonemapping in Ghost of Tsushima</em>, SIGGRAPH 2021</li><li>Taichi Lang documentation: <a href="https://docs.taichi-lang.org/">https://docs.taichi-lang.org/</a></li><li>Some of the images cited in this article are from Wikipedia</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=216295334956" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/how-taichi-fuels-gpu-accelerated-image-processing-a-beginner-to-expert-guide-216295334956">How Taichi Fuels GPU-accelerated Image Processing: A Beginner to Expert Guide</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>
        <item>
            <title><![CDATA[From molecular simulation to black hole rendering — Taichi Lang makes life easier for digital…]]></title>
            <link>https://medium.com/parallel-programming-in-python/from-molecular-simulation-to-black-hole-rendering-taichi-lang-makes-life-easier-for-digital-64d902fa190e?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/64d902fa190e</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[render]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[tech-blog]]></category>
            <dc:creator><![CDATA[Taichi Lang]]></dc:creator>
            <pubDate>Thu, 15 Sep 2022 02:27:03 GMT</pubDate>
            <atom:updated>2022-09-15T02:27:03.516Z</atom:updated>
            <content:encoded><![CDATA[<h3>From molecular simulation to black hole rendering — Taichi Lang makes life easier for digital content creators</h3><p>· 9 min read</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/0*XRuTOdsGkbEnDKpa.png" /></figure><p>CEO @ Taichi Graphics | Ph.D MIT | Tsinghua Alumni</p><p>It has been more than three years since I started working on a brand new programming language, Taichi-Lang, which is embedded in Python (but can perfectly run independently of Python) and designed for high-performance numerical computation. Two months ago, Taichi 1.0 was released, which is indeed a milestone for me personally and for our entire community. From an immature academic idea to an open-source project that has attracted hundreds of contributors, Taichi is committed to making graphics programming easier for content creators.</p><p>As I’m pondering over how to bring Taichi further and enlarge our creator community, I realize that many creators out there are looking for a handy tool yet remain unaware of Taichi. I decided that it definitely would help if I could explain Taichi’s attributes, showcase what our users are doing with Taichi, and envision what we aspire to achieve in the long run.</p><p>(BTW, before you start, you may want to scan through the ToC or subheadings quickly and pick whatever you are interested in most. I’m trying to be informative, but as long as you find certain parts of this article helpful to your creation, then this article serves its purpose quite well!)</p><ul><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#so--what-is-taichi-lang">What is Taichi-Lang</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#application-scenarios">Application scenarios</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#image-processing">Image processing</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#webgpu">WebGPU</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#webassembly">WebAssembly</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#robotics">Robotics</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#optics">Optics</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#visual-effects">Visual effects</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#numerical-computing-and-fluid-mechanics">Numerical computing and fluid mechanics</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#molecular-dynamics">Molecular dynamics</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#ahead-of-time-aot-run-independently-of-python">Ahead of Time (AOT)</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#rendering">Rendering</a></li><li><a href="https://docs.taichi-lang.org/blog/what-taichi-is-capable-of#where-we-are-heading">Where we are heading</a></li></ul><h3>So … What is Taichi-Lang?</h3><p>Initially positioned as a handy tool to support high-performance computer graphics, Taichi was designed based on the following three philosophies:</p><h3>Develop elegantly</h3><ol><li>Simple syntax: Python users can write Taichi code right away;</li><li>Naturally integrated into the Python ecosystem with easy interaction with packages like Numpy and PyTorch;</li><li>Automatic parallelization and differentiation spare you the implementation efforts;</li><li>Develop fancy computer graphics programs in considerably fewer lines of code.</li></ol><h3>Run rapidly</h3><ol><li>Born to harness parallelism in GPUs and multi-core CPUs;</li><li>Compiled Python just-in-time (JIT) to binary executable kernels;</li><li>Spatially sparse data structures: No wasted computation in empty space;</li><li>Quantized computation optimizes performance on mobile devices.</li></ol><h3>Deploy universally</h3><ol><li>Supports multiple backends, including x64 &amp; ARM CPUs, CUDA, Vulkan, Metal, and OpenGL Compute Shaders;</li><li>Ahead-of-time (AOT) compilation enables deployment on platforms without Python, including PCs, mobile devices, and even web browsers.</li></ol><p>Since its inception, Taichi has actually been applied to so many areas that we did not even imagine. Many of our users regard Taichi as <em>“a high-performance programming tool embedded in Python”</em> or <em>“a development system to deploy (mobile) compute shaders”</em>; and they represent the two major user groups of Taichi-Lang, depending on their reliance on Python (as discussed in the final part).</p><p>Still, it does not mean that reality has gone astray; instead, it proves our foresight when we designed the vision for Taichi:</p><blockquote><em>Deploy parallel computing programs universally with the same set of code unchanged and high performance unscathed.</em></blockquote><h3>Application scenarios</h3><p>Not long ago, I was delightedly informed that ETH Zürich, which is acclaimed as the best university in continental Europe, <a href="https://docs.taichi-lang.org/blog/eth-z%C3%BCrich-uses-taichi-lang-in-its-physically-based-simulation-source">used Taichi in a computer graphics course</a>. Over the past year, Taichi has witnessed its user base enlarge fourfold. I myself am often surprised at our users’ sophisticated use of Taichi and the creativity and vibrancy they injected into the community.</p><p>To have a better understanding of the scenarios where Taichi is (spontaneously) applied, we launched <a href="https://github.com/taichi-dev/awesome-taichi">taichi-dev/awesome-taichi</a> to collect and present top-notch Taichi-empowered projects. Most of the examples given below are available in this repo.</p><h3>Image processing</h3><p>Poisson Image Editing (Pérez et al) is a frequently cited paper. <a href="https://github.com/Trinkle23897/Fast-Poisson-Image-Editing/commits?author=Trinkle23897">Trinkle23897</a> and <a href="https://github.com/shawnchan2014">shawnchan2014</a> implemented this <a href="https://github.com/Trinkle23897/Fast-Poisson-Image-Editing">classic algorithm with Taichi as the backend</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/0*2_CRZCaNx2sTR0Jm.png" /><figcaption>Fast Poisson Image Editing: How to grow an eye in your palm…</figcaption></figure><p>Advantages of using Taichi for image processing include:</p><ol><li>Portability: Programmed with Taichi and run on AMDGPU, CUDA, and multi-threaded CPU;</li><li>JIT compilation: No need to compile CUDA or C++ files on users’ devices;</li><li>Development within Python: Just decorate the part of the code you want to parallelize with @ti.kernel.</li></ol><h3>WebGPU</h3><p><a href="https://github.com/AmesingFlank/taichi.js">Taichi.js</a> is a powerful project that adds a JS frontend to Taichi and compiles Taichi to WASM with Emscripten, allowing users to transform Javascript functions into WebGPU compute shaders for massive parallelization. If your browser supports WebGPU, you can try it out on <a href="https://taichi-js.com/playground">Playground | taichi.js</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A2ISn-Sz_nTFf_fA.png" /><figcaption>JS frontend compiles Taichi to WebGPU</figcaption></figure><h3>WebAssembly</h3><p><a href="https://zoo.taichi.graphics/">Taichi Zoo</a> is another option to compile and run Taichi code on web pages, though WebASM is slightly outperformed by WebGPU in performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6jk7xw3wy-DB1mFR.png" /></figure><h3>Robotics</h3><p>The following three projects all focus on differentiable physics. Relevant papers were accepted by the top machine learning conference ICLR.</p><ol><li><a href="https://github.com/taichi-dev/difftaichi">DiffTaichi</a>, which optimizes robots’ movements, is an early-stage trial of Taichi’s AutoDiff feature:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*IDYvbvKsiKHc5jgQ.gif" /></figure><p><a href="https://github.com/hzaskywalker/PlasticineLab">2. PlasticineLab</a>: A new differentiable physics benchmark, which includes a diverse collection of soft body manipulation tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/0*rDQV5LviXbWi7ohW.png" /></figure><p><a href="https://github.com/lester0866/CPDeform">3. CPDeform</a>: Contact Points Discovery for Soft-Body Manipulations with Differentiable Physics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/0*iGFgC8QA-1Vy5stD.png" /><figcaption>The updated version can control robots to shape objects at will</figcaption></figure><h3>Optics</h3><p><a href="https://github.com/yslib/Cameray">Cameray</a> is a project I personally find very intriguing. The author simulates SLR lenses with ray tracing, a feature enabled by Taichi, and offers a node editor as well.</p><p>(I tried to run the project but DearPyGui didn’t work properly! @<a href="https://github.com/yslib">yslib</a> Could you plz update the project if you see this article because I’m kind of a shutterbug and really want to try it 🤣)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ejBvKb1b96VmkMW9.png" /></figure><p>Hesitate to place an order for expensive SLR lenses? Experience the virtual ones first! You can even build a latest anti-dispersion model (though you have no choice but to stay in a Cornell box lol)</p><h3>Visual effects</h3><p>You can use Taichi to create various physics solvers. A typical project is MLS-MPM-based <a href="https://github.com/taichi-dev/taichi_elements">taichi_elements</a>, which enables simulations with one billion particles on a single GPU — the highest precision ever achieved by MPM on a single GPU! And all it takes is about 1,000 lines of code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*3C1guXGOHnQIOSwW.gif" /></figure><p>Another notable project is <a href="https://github.com/nepluno/pyasflip">PyASFLIP</a> by Raymond, showcasing a multi-scale model for coupling strands with shear-dependent liquid. You can refer to <a href="http://yunfei.work/asflip/">the paper</a> accepted by SIGGRAPH 2021 for details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iCfrvpZPodIEKxpI.png" /></figure><p>Deeply honored to find Taichi play a part in this project …</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JSuFM_3QpcM3MI7h.png" /></figure><h3>Numerical computing and fluid mechanics</h3><p><a href="https://github.com/hietwll/LBM_Taichi">LBM_Taichi</a> is one of the first computational fluid dynamics projects written in Taichi and still runs smoothly after two years.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*SMXXpKI8Vx5fWSnk.gif" /></figure><p>Another beautiful project I want to share with you here is <a href="https://github.com/takah29/2d-fluid-simulator">2d-fluid-simulator</a> contributed by a user from Japan, <a href="https://github.com/takah29">takah29</a>, based on the finite difference method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2aQw1l9xb67uylzB.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NLqsLcisKsPx14ag.jpeg" /><figcaption>velocity field</figcaption></figure><h3>Molecular dynamics</h3><p><a href="https://github.com/victoriacity">victoriacity</a> implemented a GPU-accelerated molecular dynamics project <a href="https://github.com/victoriacity/taichimd">Taichi MD</a>, as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*2IkkZ02aLW4UDgSE.gif" /></figure><p>Seems to be water molecules (if I didn’t get it wrong 🤣)</p><h3>Ahead of time (AOT): Run independently of Python</h3><p>AOT compilation is an important feature to achieve easy deployment across backends, making Taichi readily adaptable to all kinds of real-world industrial scenarios. Backed by the strong support from the OPPO US Research Center, we are still working on the feature; you can have a rough idea of what we are doing by checking out <a href="https://docs.taichi-lang.org/blog/taichi-aot-the-solution-for-deploying-kernels-in-mobile-devices">this article</a> or watching this demo:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/0*S0INGs1gDDzeotBy.gif" /></figure><h3>Rendering</h3><p>Renderers are not something we can avoid talking about since Taichi was designed for computer graphics at the very beginning. An impressive project is about the implementation of the classic <a href="https://github.com/bsavery/ray-tracing-one-weekend-taichi">Ray Tracing in One Weekend using Taichi</a>, by <a href="https://github.com/bsavery">bsavery</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GQpqnjsFDk__fy8X.png" /></figure><p>Ray Tracing in One Weekend, reproduced with Taichi</p><p>bsavery went on and explored more possibilities of Taichi in rendering: <a href="https://github.com/bsavery/BlenderPythonRenderer">Blender Python Renderer</a>, which is a GPU ray tracer embedded in Blender.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*8H6Y0gK1-TSOzMFZ.png" /></figure><p>Some users are quite ambitious — in a good way :). For example, <a href="https://github.com/theAfish">theAfish</a> simulates a black hole <a href="https://github.com/theAfish/BlackHoleRayMarching">accredition disk based on ray marching</a> (regardless of rotation in this case). General relativity is also involved here — what a bonus!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/0*sX1wH9nWZJByvMSM.png" /></figure><p>Don’t be intimidated by the Chinese README, though 😆</p><h3>Where we are heading</h3><p>As mentioned above, we recognize two major user groups of Taichi, depending on their reliance on Python.</p><p>Group A consists of hardcore Python users who want to take advantage of Taichi’s development efficiency and automatic parallelization. They are mostly students and researchers who often use Taichi together with other Python libraries, including Matplotlib, PyTorch, and OpenCV. Of course, they also care about portability and performance. For example, students at ETH Zürich need to ensure their CUDA programs can run on their classmates’ AMDGPU effortlessly; they also want to achieve high-level performance comparable to CUDA.</p><p>Group B users are interested in using Taichi independently of Python to fuel productivity. They are attracted by Taichi’s development efficiency, portability, and high performance and see its potential in settings like gaming, mobile applications, or browsers, where Python can hardly fit in. A lightweight Taichi that can be compiled easily into Metal, Vulkan, DirectX shader, and Neon/SSE would spare them a lot of effort. Particularly, Taichi’s AOT feature can relieve their burden of executing compute shaders in real-time with limited power consumption.</p><p>It is not hard to tell that the two groups have their needs overlapped to a large extent. To improve user experience, we will continue to optimize our CHI (Chi Hierarchical Instructions) IR, enhance modularization and reusability, and keep abreast of CUDA, Vulkan, and Metal developments. Specifically, we hope to further reduce the compile time for Group A users, and <a href="https://github.com/taichi-dev/taichi/issues/4401">CHI JIT Cache</a> is one such attempt. Meanwhile, we are proceeding with the AOT development, including the Foreign Function Interface (FFI) feature, so that Group B users can use Taichi more comfortably.</p><p>We are proud that Taichi has proved its value to creators over the past three or so years. At the same time, we are more than aware that it is still a toddler (if compared with C++/CUDA/Rustc…) and has a long way to go to fulfill its vision. As an open-source language, Taichi evolves every time a user shares their experience or a contributor pulls requests. So, <strong>if you are interested in Taichi</strong> after reading this article, you can:</p><p>pip install --upgrade taichi</p><p>Experience our classy Taichi demos: ti gallery</p><p>And join our developer community at <a href="https://github.com/taichi-dev/taichi">https://github.com/taichi-dev/taichi</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64d902fa190e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/from-molecular-simulation-to-black-hole-rendering-taichi-lang-makes-life-easier-for-digital-64d902fa190e">From molecular simulation to black hole rendering — Taichi Lang makes life easier for digital…</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>
        <item>
            <title><![CDATA[How I created the tranquil autumn air within 99 lines of Python code | Taichi Docs]]></title>
            <link>https://medium.com/parallel-programming-in-python/how-i-created-the-tranquil-autumn-air-within-99-lines-of-python-code-taichi-docs-fceecbfe6de7?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/fceecbfe6de7</guid>
            <category><![CDATA[taichi-lang]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[voxel]]></category>
            <dc:creator><![CDATA[Taichi Lang]]></dc:creator>
            <pubDate>Tue, 23 Aug 2022 12:30:47 GMT</pubDate>
            <atom:updated>2022-08-23T12:30:47.759Z</atom:updated>
            <content:encoded><![CDATA[<p>10 min read</p><p>Yuanming Hu, CEO @ Taichi Graphics | Ph.D MIT | Tsinghua Alumni</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*i3D9WR_puPu9rgdx" /></figure><p>On a Sunday afternoon about a couple of months ago, when <a href="https://github.com/k-ye">Ye</a> and I were on our way back from a long week of travel, we decided to do something to relax on the train ( to kill time). Since we happened to mention Minecraft and MagicaVoxel, we decided to do a Hackathon, where we use Taichi Lang to create a GPU path tracing voxel renderer. Soon, before we were back home, we had our prototype:</p><p>Taichi Lang is embedded in Python and it runs on any operating system and can easily interact with Python. As far as I know, apart from Taichi Lang, there’s no such tooling in the Python ecosystem for generating GPU path tracing voxel renders. With Taichi Lang, one can easily create such a renderer ( <a href="https://github.com/taichi-dev/voxel-challenge/blob/main/renderer.py">https://github.com/taichi-dev/voxel-challenge/blob/main/renderer.py</a>) in around 300 lines of code.</p><p>When finishing off the program, we became aware of something more serious: It is much easier for an experienced graphics developer to code a path tracing renderer than to come up with a scene setting for a beautiful image. Considering the huge amount of work involved in developing the UI, if we could only edit voxels one by one, it would take ages to get the intended results!</p><p>I’m not a mighty Minecraft developer, but, thankfully, I can program to batch process the voxels. Let’s get started!</p><h3>The theme</h3><p>Finding a beautiful theme is half success. Forest is a common theme in many great voxel artworks. Then I found from online the following picture: a quiet forest in the autumn air. Nice!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iJzCEHsiJ1ybrbEB" /></figure><p>It must be very interesting to reproduce a tranquil autumn theme with voxels and use ray tracing to render it!</p><blockquote><em>Note: So long as you have basic Python programming knowledge, you will be able to follow the rest of this post. Taichi Lang is a parallel programming language embedded in Python, allowing your program to be accelerated by your GPU. If you are unfamiliar with Taichi Lang, read this: </em><a href="https://docs.taichi-lang.org/docs/"><em>https://docs.taichi-lang.org/docs/</em></a><em>.</em></blockquote><h3>Program in Python and indulge in the 3D voxel world</h3><p>When finishing reading this post, you will be able to create your own 3D voxel world. The source code is in Python and compatible with almost all platforms. It runs fine on my Macbook (20 FPS) except for some noises, and the noises subside quickly after you pause. If you are using an ultimate GPU like RTX 3090, it will definitely run much more smoothly.</p><p>If you wish to try out the autumn theme for yourself and view the complete source code (91 lines of Python code in total), you can clone my repo at <a href="https://github.com/yuanming-hu/voxel-art">https://github.com/yuanming-hu/voxel-art</a>. If you wish to create your own voxel world, start by using our template repo <a href="https://github.com/taichi-dev/voxel-challenge/">https://github.com/taichi-dev/voxel-challenge/</a></p><p>Just so you know, Taichi Voxel Challenge 2022 is getting heated. If you are also a voxel art lover, you are very welcome to join! Let’s compare ‘voxels’ and motivate each other! For more information about how to participate, see the Voxel Challenge README at <a href="https://github.com/taichi-dev/community/blob/main/events/voxel-challenge/README.md">https://github.com/taichi-dev/community/blob/main/events/voxel-challenge/README.md</a></p><blockquote>It has been reported at<em> </em><a href="https://github.com/taichi-dev/taichi/issues/4891"><em>https://github.com/taichi-dev/taichi/issues/4891</em></a><em> </em>that Taichi Lang v1.0.1 has some compatibility issues with the Vulkan backend in some environments. The next Taichi Lang release will ship the fix. As a workaround, use Taichi Lang v1.0.0 instead:</blockquote><blockquote>pip install taichi==1.0.0</blockquote><h3>Using voxels to create a forest in the autumn air</h3><p>The autumn theme has three key elements: trees, forest floor covered by fallen leaves, and volumetric fog. Our renderer does not support volumetric fog, but we can simulate autumn sunset with a 45° directional light source ( set_directional_light()) and a yellowish light color ( set_background_color()) instead.</p><pre>from scene import Scene<br>import taichi as ti<br>from taichi.math import *<br><br>scene = Scene(voxel_edges=0, exposure=2) <em># Create a scene, specifying the voxel edge and exposure.</em><br>scene.set_floor(0, (1.0, 1.0, 1.0)) <em># Height of the floor</em><br>scene.set_background_color((0.5, 0.5, 0.4)) <em># Color of the sky</em><br>scene.set_directional_light((1, 1, -1), 0.2, (1, 0.8, 0.6)) <em># Direction and color of the light</em><br><br>@ti.kernel<br>def initialize_voxels():<br>    scene.set_voxel(vec3(0), 1, vec3(1)) <em># Add a (1, 1, 1) voxel at the position of (0, 0, 0)</em><br><br>initialize_voxels()<br><br>scene.finish()</pre><p>Then you get the following scene:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TdG9yQJi_1GNcbRl" /></figure><p>This is a small cube. I tuned the direction and color of the light to add a touch of the autumn air.</p><p>You may have noticed the black edges. Why we still have edges even if we have set voxel_edge to zero? This is due to the float-point precision. Well, they will not be noticed if you don&#39;t look close at them...</p><p><strong>Base floor</strong></p><p>Sectional drawing is the technique commonly used in many masterpiece voxel artworks. In other words, a cross section can help reveal things that cannot been seen otherwise. In our case, it is the base floor (earth of multiple layers):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*Xfuq0zwqftBFAv4I" /></figure><p>We can imitate the above cake to make a base floor of earth.</p><p>Now we start building the base floor layer by layer, and each layer is virtually a flat block. And we need a function for defining a flat block, which stretches from the point (pos[0], pos[1], pos[2]) and has a size of size[0] x size[1] x size[2]. We also need to add some noise ( color_noise) to its color to make it look more real.</p><p>The key here is the use of ti.ndrange. It can iterate over multiple ranges in one for loop.</p><p>Using ti.grouped, we pack the three loop variables i, j, and k into a vector I = (i, j, k). Using the create_block() method, we can draw a large block:</p><pre>@ti.kernel<br>def initialize_voxels():<br>    create_block(pos=ivec3(0, 0, 0),<br>                 size=ivec3(20, 40, 30),<br>                 color=vec3(0.3, 0.5, 0.3),<br>                 color_noise=vec3(0.1))</pre><p>There we go:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TM3FJg3X2wh8yjzo" /></figure><p>Then we can loop the create_block() method call for four times to draw a four-layered base floor:</p><pre>@ti.kernel<br>def initialize_voxels():<br>    for i in range(4):<br>        create_block(ivec3(-60, -(i + 1)**2 - 40, -60),<br>                     ivec3(120, 2 * i + 1, 120),<br>                     vec3(0.5 - i * 0.1) * vec3(1.0, 0.8, 0.6),<br>                     vec3(0.05 * (3 - i)))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0zegMUYQFafbLfaM" /></figure><p>The color is a bit lighter for the ground. Hmm, let’s add a darker surface to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fvU6AFtMUD4RD0yG" /></figure><p>Not too bad. By mimicking the layered cake, we have a base floor for holding the trees, fallen leaves, and fences.</p><p><strong>Tree and fallen leaves</strong></p><p>After finishing reading this session you will know how to create a forest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*82hvcNqcgkFMCguZ" /></figure><p>One tree does not make a forest, and we have to make it within 99 lines of code. So, first off, we need a function for creating a tree:</p><pre>@ti.func<br>def create_tree(pos, height, radius, color):<br>    ...</pre><p>The function takes four parameters:</p><ul><li>pos: Position of the tree.</li><li>height: Height of the tree.</li><li>radius: Radius of the tree crown</li><li>color: Color of the leaves.</li></ul><p>Let’s start with something easy, the tree trunk, and we can save some time by recycling the create_block() function:</p><pre>@ti.func<br>    def create_tree(pos, height, radius, color):<br>        create_block(pos, ivec3(3, height - radius * 0.5, 3), vec3(0.7), vec3(0.3))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*y1pVLZ4ahQgvX3Jo" /></figure><p>Now, let’s add some leaves to the bold, boring tree trunk. Here, we define a function called create_leaves(). In this function, we make the following three assumptions:</p><ul><li>The leaves of the tree are in the shape of a cylinder.</li><li>Each leaf has a probability of occurrence in the cylinder.</li><li>A leaf’s probability of occurrence is inversely proportional to its distance to the axis of the cylinder.</li></ul><pre>@ti.func<br>    def create_leaves(pos, radius, color):<br>        for I in ti.grouped(<br>                ti.ndrange((-radius, radius), (-radius, radius),<br>                           (-radius, +radius))):<br>            f = I / radius<br>            d = vec2(f[0], f[2]).norm() <em># Distance to the axis of the cylinder (tree crown)</em><br>            prob = max(0, 1 - d)**2<br>            if ti.random() &lt; prob:<br>                scene.set_voxel(pos + I, 1, color + (ti.random() - 0.5) * 0.2)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/0*YZ5KcCYn2q11OkcL" /></figure><p>Well, it doesn’t look very real, but we can use mathematics and noise to give it some fine tweak:</p><pre>@ti.func<br>    def create_leaves(pos, radius, color):<br>        for I in ti.grouped(<br>                ti.ndrange((-radius, radius), (-radius, radius),<br>                           (-radius, +radius))):<br>            f = I / radius<br>            h = 0.5 - max(f[1], -0.5) * 0.5<br>            d = vec2(f[0], f[2]).norm()<br>            prob = max(0, 1 - d)**2 * h  <em># xz mask</em><br>            prob *= h  <em># y mask</em><br>            <em># noise</em><br>            prob += ti.sin(f[0] * 5 + pos[0]) * 0.02<br>            prob += ti.sin(f[1] * 9 + pos[1]) * 0.01<br>            prob += ti.sin(f[2] * 10 + pos[2]) * 0.03<br>            if prob &lt; 0.1:<br>                prob = 0.0<br>            if ti.random() &lt; prob:<br>                scene.set_voxel(pos + I, 1, color + (ti.random() - 0.5) * 0.2)</pre><p>Though the function looks a bit complicated, the key point is to calculate a probability for each voxel in the ‘leaves’ region. After adding some details like noise, we are almost done with the tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/0*keohCae3f70AxA_-" /></figure><p>Autumn has always been synonymous with fallen leaves. Then, we intersperse the circular ground under the tree with some random voxels:</p><pre>for i, j in ti.ndrange((-radius, radius), (-radius, radius)):<br>            prob = max((radius - vec2(i, j).norm()) / radius, 0)<br>            prob = prob * prob<br>            if ti.random() &lt; prob * prob:<br>                scene.set_voxel(pos + ivec3(i, 1, j), 1,<br>                                color + ti.random() * vec3(0.1))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/0*v6ch_-RVSW2r2IMe" /></figure><p>After calling the create_tree() method a few times, we get a small forest. You probably need to fine-tweak the position, height, and color of each tree:</p><pre>create_tree(ivec3(-20, -40, 25), 65, 35, vec3(1.0, 0.3, 0.15))<br>        create_tree(ivec3(45, -40, -45), 15, 10, vec3(0.8, 0.4, 0.1))<br>        create_tree(ivec3(20, -40, 0), 45, 25, vec3(1.0, 0.4, 0.1))<br>        create_tree(ivec3(30, -40, -20), 25, 15, vec3(1.0, 0.4, 0.1))<br>        create_tree(ivec3(30, -40, 30), 45, 25, vec3(1.0, 0.4, 0.1))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uGvOjA93AJZocMkJ" /></figure><p><strong>Fences</strong></p><p>The last step is to create fences, which is to add a bar along each direction and insert small supporting sticks at regular intervals:</p><pre>@ti.func<br>def make_fence(start, direction, length):<br>    color = vec3(0.5, 0.3, 0.2)<br>    create_block(start, direction * length + ivec3(3, 2, 3), color, vec3(0.1))<br><br>    fence_dist = 3<br>    for i in range(length // fence_dist + 1):<br>        create_block(start + direction * i * fence_dist + ivec3(1, -3, 1), ivec3(1, 5, 1), color, vec3(0))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*B5bQ59RHPWLXzj4j" /></figure><p>Now we are done with the base floor, trees, fallen leaves, and the fences. Let&#39;s call it a day!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Jdky5Onj8xVhZjhy" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8ZQZGn75T8ejuXDa" /></figure><p>Press W/S/A/D/Q/E to enable panoramic mode and press P for screen capture. Alas! Haven&#39;t touched my camera for a long while, but at least I can turn to the 3D voxel world for solace. :-)</p><h3>Some great entries of Voxel Challenge 2022</h3><p>Before we move on to the next section, I&#39;d like to share with you some of the very interesting entries of Voxel Challenge 2022, all made within 99 lines of Python code. Do bear with me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KFZ-k1xNEZvzv8vV" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BNhQaVQIkMIqLUhd" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lIdgjBaPZ476e7E6" /></figure><p>Click <a href="https://bit.ly/3whe1zX">bit.ly/3whe1zX</a> for more interesting voxel artworks, along with their source code!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fceecbfe6de7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/how-i-created-the-tranquil-autumn-air-within-99-lines-of-python-code-taichi-docs-fceecbfe6de7">How I created the tranquil autumn air within 99 lines of Python code | Taichi Docs</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>
        <item>
            <title><![CDATA[Taichi Cookbook 001: Five practical tips on how to master Taichi, a handy parallel programming…]]></title>
            <link>https://medium.com/parallel-programming-in-python/taichi-cookbook-001-five-practical-tips-on-how-to-master-taichi-a-handy-parallel-programming-2d9b756dcbb?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/2d9b756dcbb</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[taichi-lang]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[colab]]></category>
            <category><![CDATA[parallel-programming]]></category>
            <dc:creator><![CDATA[Taichi Lang]]></dc:creator>
            <pubDate>Tue, 23 Aug 2022 12:14:36 GMT</pubDate>
            <atom:updated>2022-08-23T12:14:36.460Z</atom:updated>
            <content:encoded><![CDATA[<h3>Taichi Cookbook 001: Five practical tips on how to master Taichi, a handy parallel programming language embedded in Python</h3><p>Yuanming Hu</p><p>Hey guys! Welcome to my first Taichi cooking session!👋</p><p>From time to time, I hear our users ask questions like “how can I make my code cleaner and more straightforward” or “how can I further optimize the performance of my Taichi programs”. So, I decided to share some very practical tips I myself often use when coding with Taichi, as well as a new feature ti.dataclass. Hopefully, you can make good use of them the next time you ti.init.</p><p>We have released Taichi v1.0.4. Upgrade to the latest version and view classy demos:<br>pip install --upgrade taichi ti gallery</p><p><strong>You are recommended to run the following code snippets online at Colab:</strong> <a href="https://sourl.cn/GnGEEm">https://sourl.cn/GnGEEm</a> <strong>so you can get first-hand experience!</strong></p><h3>Tip 1: Auto-debug out-of-bound array accesses</h3><p>The array access violation issue is quite common during low-code programming (such as C++ and CUDA), and more often than not, a program would proceed regardless. You would not even realize it until you ended up with a wrong result. Even if, with a stroke of luck, you saw a segmentation fault triggered, you would find it hard to debug. Taichi solves this problem by providing an auto-debugging mode: just set debug=Truewhen initiating Taichi. For example:</p><pre>import taichi as ti</pre><pre>ti.init(arch=ti.cpu, debug=True)<br>f = ti.field(dtype=ti.i32, shape=(32, 32))</pre><pre>@ti.kernel<br>def test() -&gt; ti.i32:<br>    return f[0, 73]<br>    <br>print(test())</pre><p>And you will see an error appear:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SoZdQ33coelAPwpy" /><figcaption>Out-of-bound array access</figcaption></figure><p>To sum up:</p><ol><li>Bound checks are <em>not</em> available until you enable debug=True.</li><li><em>Only</em> ti.cpu and ti.cuda are supported (you should switch to CPU/CUDA for bounds checking if you are using other backends).</li><li>Program performance may worsen after debug=True is turned on.</li></ol><h3>Tip 2: Access a high-dimensional field by indexing integer vectors</h3><p>It can be cumbersome to use val[i, j, k, l]to access an element in a high-dimensional field. Is there an easier way to do that?</p><p>Well, we can index an integer vector directly (and conduct math operations based on such vectors) like this:</p><pre>import taichi as ti<br>import matplotlib.pyplot as plt<br>import math</pre><pre>ti.init(arch=ti.cpu)<br>n = 512<br>img = ti.field(dtype=ti.i32, shape=(n, n))<br>img_magnified = ti.field(dtype=ti.i32, shape=(n, n))</pre><pre>@ti.kernel<br>def paint():<br>    for I in ti.grouped(img):<br>        f = (I / n) * math.pi * 10<br>        img[I] = ti.sin(f[0]) + ti.cos(f[1])</pre><pre>        <br>paint()<br>plt.imshow(img.to_numpy())<br>plt.show()</pre><pre>@ti.kernel<br>def magnify():<br>    for I in ti.grouped(img_magnified):<br>        img_magnified[I] = img[I // 2]</pre><pre>    <br>    # equivalent to<br>    # for i, j in img_magnified:<br>    #     img_magnified[i, j] = img[i // 2, j // 2]</pre><pre>magnify()<br>    <br>plt.imshow(img_magnified.to_numpy())<br>plt.show()</pre><p>And run the program:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/0*6lcOL9ncW_wddUV8" /><figcaption>Access a high-dimensional field by indexing integer vectors</figcaption></figure><p>To sum up:</p><ol><li>for I in ti.grouped(img): Make sure you use ti.grouped to pack the index into ti.Vector.</li><li>If it is a floating-point vector, make sure you use I.cast(ti.i32) to cast it to an integer; otherwise, a warning would occur.</li><li>The point of this tip is that your code becomes dimension-independent. You can apply the same set of code for either 2D or 3D.</li></ol><h3>Tip 3: Serialize the outermost for loop</h3><p>By default, Taichi automatically parallelizes the for loop at the outermost scope, but sometimes some programs need to be serialized. In this case, you just need ti.loop_config(serialize=True):</p><pre>import taichi as ti</pre><pre>ti.init(arch=ti.cpu)<br>n = 1024<br>val = ti.field(dtype=ti.i32, shape=n)</pre><pre>val.fill(1)</pre><pre>@ti.kernel<br>def prefix_sum():<br>    ti.loop_config(serialize=True)<br>    for i in range(1, n):<br>        val[i] += val[i - 1]</pre><pre>prefix_sum()<br>print(val)</pre><p>And you will get the right result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/0*c6PaBnMyBkKzU-oz" /><figcaption>Serialize the outermost for loop</figcaption></figure><p>To sum up:</p><ol><li>ti.loop_config(serialize=True) decorates the outermost for loop that immediately follows it.</li><li>ti.loop_config works only for the <em>range-for</em> loop at the <em>outermost</em> scope.</li><li>Inner for loops are serialized by default.</li></ol><p>In addition, you can try warp-level intrinsics to accelerate prefix sum if you are using CUDA: <a href="https://github.com/taichi-dev/taichi/issues/4631">https://github.com/taichi-dev/taichi/issues/4631</a></p><h3>Tip 4: Interact with Python libraries, such as NumPy</h3><p>“I really want to convert the output to the data types supported by NumPy so I can paint with Matplotlib or develop deep learning models with PyTorch!”</p><p>Taichi provides a solution:</p><pre>import taichi as ti<br>import matplotlib.pyplot as plt</pre><pre>ti.init(arch=ti.cpu)<br>n = 8192<br>factors = ti.field(dtype=ti.i32, shape=n)</pre><pre>@ti.kernel<br>def number_of_factors():<br>    for i in range(1, n):<br>        counter = 0<br>        for j in range(1, int(ti.floor(ti.sqrt(i))) + 1):<br>            if i % j == 0:<br>                if j * j == i:<br>                    counter += 1<br>                else:<br>                    counter += 2<br>        factors[i] = counter</pre><pre>number_of_factors()</pre><pre>plt.plot(factors.to_numpy(), &#39;.&#39;)<br># for i in range(n):<br>#     print(i, factors[i])<br>plt.show()</pre><pre>factors.to_torch()</pre><p>I tried it out with Matplotlib and it went well:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/0*LSscCklg3eXLLx-5" /><figcaption>Interact with Python libraries, such as NumPy</figcaption></figure><h3>Tip 5: Analyze performance with Taichi Profiler</h3><p>“It takes a long time to run my program, but how can I figure out which Taichi kernel is the most time-consuming?”</p><p>Well begun is half done. It is crucial to locate the bottleneck before you start optimization, and Taichi’s Profiler can do that for you:</p><pre>import taichi as ti</pre><pre>ti.init(arch=ti.cpu, kernel_profiler=True)<br>f = ti.field(ti.i32, shape=(32, 32))<br></pre><pre>@ti.kernel<br>def foo():<br>    for i in range(400000):<br>        f[0, 0] += i<br>        <br>    for i in range(100000):<br>        f[0, 1] += i<br></pre><pre>@ti.kernel<br>def bar():<br>    for i in range(1000):<br>        a = f[0, 31]</pre><pre>for i in range(10):<br>    foo()<br>bar()</pre><pre>ti.sync()<br>ti.profiler.print_kernel_profiler_info()</pre><p>To give you an idea as to what the profiling report would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/0*P6BILSFw800HKmRm" /><figcaption>Profiling report</figcaption></figure><p>To sum up:</p><ol><li>A kernel that has been fully optimized by the compiler would not generate profiling records (the bar kernel mentioned above is a fully optimized one).</li><li>One kernel may generate multiple records of parallel for loops because they are divided into different tasks and assigned to separate devices.</li><li>Make sure you call ti.sync() before performance profiling if the program is running on GPU.</li><li>jit_evaluator_xxx can be ignored because it is automatically generated by the system.</li><li>Currently, kernel_profiler only supports CPU and CUDA (but you are very encouraged to make contributions and add more backends!).</li><li>You are recommended to run performance profiling several times to observe the minimum or average execution time.</li></ol><h3>Recent feature: ti.dataclass</h3><p>This new feature is contributed by <a href="https://github.com/bsavery">bsavery</a>. It resembles dataclasses.dataclass introduced in Python 3.10 but functions in Taichi kernels.</p><p>A simple example of how to use this feature:</p><pre>import taichi as ti<br>import taichi.math as tm<br>ti.init()</pre><pre>n_particles = 16</pre><pre>@ti.struct_class<br>class Particle:<br>    x: ti.types.vector(2, ti.f32) # position<br>    v: ti.types.vector(2, ti.f32) # velocity<br>    <br>    @ti.func<br>    def at(self, t):<br>        return self.x + self.v * t<br>    <br>    @ti.func<br>    def advance(self, dt):<br>        self.x = self.at(dt)</pre><pre>particles = Particle.field(shape=(n_particles, ))</pre><pre>dt = 0.1</pre><pre>@ti.kernel<br>def simulate():<br>    for i in particles:<br>        particles[i].x = tm.vec2(i, i)<br>        particles[i].v = tm.vec2(0, 100)</pre><pre>    for i in range(n_particles):<br>        particles[i].advance(5)</pre><pre>simulate()<br>for i in range(n_particles):<br>    print(particles[i].x)</pre><p>Result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/0*YMuybhhWMvwl8PPw" /><figcaption>ti.dataclass example</figcaption></figure><p><em>Hope you have fun with the following mpm99 demo written with this new feature! If the program does not support Colab, you can save it as a Python file (.py) and then run it locally, provided that you have installed the latest version of Taichi.</em></p><pre># MPM99 using ti.dataclass</pre><pre>import taichi as ti</pre><pre>ti.init(arch=ti.gpu)  # Try to run on GPU<br>quality = 1  # Use a larger value for higher-res simulations<br>n_particles, n_grid = 9000 * quality**2, 128 * quality<br>dx, inv_dx = 1 / n_grid, float(n_grid)<br>dt = 1e-4 / quality<br>p_vol, p_rho = (dx * 0.5)**2, 1<br>p_mass = p_vol * p_rho<br>E, nu = 0.1e4, 0.2  # Young&#39;s modulus and Poisson&#39;s ratio<br>mu_0, lambda_0 = E / (2 * (1 + nu)), E * nu / (<br>    (1 + nu) * (1 - 2 * nu))  # Lame parameters</pre><pre>@ti.dataclass<br>class Particle:<br>    x: ti.types.vector(2, ti.f32) # position<br>    v: ti.types.vector(2, ti.f32) # velocity<br>    C: ti.types.matrix(2, 2, ti.f32) # affine velocity field<br>    F: ti.types.matrix(2, 2, ti.f32) # deformation gradient<br>    Jp: ti.f32  # plastic deformation<br>    material: ti.i32  # material id</pre><pre>@ti.dataclass<br>class Grid:<br>    v: ti.types.vector(2, ti.f32) # grid node momentum/velocity<br>    m: ti.f32 # velocity</pre><pre>particles = Particle.field(shape=(n_particles, ))<br>grid = Grid.field(shape=(n_grid, n_grid))</pre><pre>@ti.kernel<br>def substep():<br>    for i, j in grid:<br>        grid.v[i, j] = [0, 0]<br>        grid.m[i, j] = 0<br>    for p in particles:  # Particle state update and scatter to grid (P2G)<br>        base = (particles.x[p] * inv_dx - 0.5).cast(int)<br>        fx = particles.x[p] * inv_dx - base.cast(float)<br>        # Quadratic kernels  [http://mpm.graphics   Eqn. 123, with x=fx, fx-1,fx-2]<br>        w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1)**2, 0.5 * (fx - 0.5)**2]<br>        particles[p].F = (ti.Matrix.identity(float, 2) +<br>                dt * particles[p].C) @ particles[p].F  # deformation gradient update<br>        h = ti.exp(<br>            10 *<br>            (1.0 -<br>             particles[p].Jp))  # Hardening coefficient: snow gets harder when compressed<br>        if particles[p].material == 1:  # jelly, make it softer<br>            h = 0.3<br>        mu, la = mu_0 * h, lambda_0 * h<br>        if particles[p].material == 0:  # liquid<br>            mu = 0.0<br>        U, sig, V = ti.svd(particles[p].F)<br>        J = 1.0<br>        for d in ti.static(range(2)):<br>            new_sig = sig[d, d]<br>            if particles[p].material == 2:  # Snow<br>                new_sig = ti.min(ti.max(sig[d, d], 1 - 2.5e-2),<br>                                 1 + 4.5e-3)  # Plasticity<br>            particles[p].Jp *= sig[d, d] / new_sig<br>            sig[d, d] = new_sig<br>            J *= new_sig<br>        if particles[p].material == 0:  # Reset deformation gradient to avoid numerical instability<br>            particles.F[p] = ti.Matrix.identity(float, 2) * ti.sqrt(J)<br>        elif particles[p].material == 2:<br>            particles.F[p] = U @ sig @ V.transpose(<br>            )  # Reconstruct elastic deformation gradient after plasticity<br>        stress = 2 * mu * (particles[p].F - U @ V.transpose()) @ particles[p].F.transpose(<br>        ) + ti.Matrix.identity(float, 2) * la * J * (J - 1)<br>        stress = (-dt * p_vol * 4 * inv_dx * inv_dx) * stress<br>        affine = stress + p_mass * particles.C[p]<br>        for i, j in ti.static(ti.ndrange(<br>                3, 3)):  # Loop over 3x3 grid node neighborhood<br>            offset = ti.Vector([i, j])<br>            dpos = (offset.cast(float) - fx) * dx<br>            weight = w[i][0] * w[j][1]<br>            grid.v[base + offset] += weight * (p_mass * particles.v[p] + affine @ dpos)<br>            grid.m[base + offset] += weight * p_mass<br>    for i, j in grid:<br>        if grid.m[i, j] &gt; 0:  # No need for epsilon here<br>            grid.v[i,<br>                   j] = (1 / grid.m[i, j]) * grid.v[i,<br>                                                    j]  # Momentum to velocity<br>            grid.v[i, j][1] -= dt * 50  # gravity<br>            if i &lt; 3 and grid.v[i, j][0] &lt; 0:<br>                grid.v[i, j][0] = 0  # Boundary conditions<br>            if i &gt; n_grid - 3 and grid.v[i, j][0] &gt; 0: grid.v[i, j][0] = 0<br>            if j &lt; 3 and grid.v[i, j][1] &lt; 0: grid.v[i, j][1] = 0<br>            if j &gt; n_grid - 3 and grid.v[i, j][1] &gt; 0: grid.v[i, j][1] = 0<br>    for p in particles.x:  # grid to particle (G2P)<br>        base = (particles.x[p] * inv_dx - 0.5).cast(int)<br>        fx = particles.x[p] * inv_dx - base.cast(float)<br>        w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1.0)**2, 0.5 * (fx - 0.5)**2]<br>        new_v = ti.Vector.zero(float, 2)<br>        new_C = ti.Matrix.zero(float, 2, 2)<br>        for i, j in ti.static(ti.ndrange(<br>                3, 3)):  # loop over 3x3 grid node neighborhood<br>            dpos = ti.Vector([i, j]).cast(float) - fx<br>            g_v = grid.v[base + ti.Vector([i, j])]<br>            weight = w[i][0] * w[j][1]<br>            new_v += weight * g_v<br>            new_C += 4 * inv_dx * weight * g_v.outer_product(dpos)<br>        particles.v[p], particles.C[p] = new_v, new_C<br>        particles.x[p] += dt * particles.v[p]  # advection<br></pre><pre>group_size = n_particles // 3<br></pre><pre>@ti.kernel<br>def initialize():<br>    for i in range(n_particles):<br>        particles[i].x = [<br>            ti.random() * 0.2 + 0.3 + 0.10 * (i // group_size),<br>            ti.random() * 0.2 + 0.05 + 0.32 * (i // group_size)<br>        ]<br>        particles[i].material = i // group_size  # 0: fluid 1: jelly 2: snow<br>        particles[i].v = ti.Matrix([0, 0])<br>        particles[i].F = ti.Matrix([[1, 0], [0, 1]])<br>        particles[i].Jp = 1<br></pre><pre>def main():<br>    initialize()<br>    gui = ti.GUI(&quot;Taichi MLS-MPM-99&quot;, res=512, background_color=0x112F41)<br>    for i in range(400):<br>        for s in range(int(2e-3 // dt)):<br>            substep()<br>        gui.circles(particles.x.to_numpy(),<br>                    radius=1.5,<br>                    palette=[0x068587, 0xED553B, 0xEEEEF0],<br>                    palette_indices=particles.material)<br>        gui.show()<br></pre><pre>if __name__ == &#39;__main__&#39;:<br>    main()</pre><p>If you encounter any problems when implementing the code above or if you have any advice to help us improve Taichi’s features, you are most welcome to DM me or contact our community team <a href="/service/mailto:community@taichi.graphics">community@taichi.graphics</a></p><p>And we look forward to your contribution or genuine opinions! Submit a PR or participate in discussions on GitHub: <a href="https://github.com/taichi-dev/taichi">https://github.com/taichi-dev/taichi</a></p><p>About Taichi Lang:<br>Taichi Lang is an open-source parallel programming language designed for high-performance numerical computation. It is embedded in Python and uses just-in-time (JIT) compiler frameworks (such as LLVM), to offload the compute-intensive Python code to the native GPU or CPU instructions.<br>View our GitHub project and become a contributor 👉🏻: <a href="https://github.com/taichi-dev/taichi">https://github.com/taichi-dev/taichi</a><br>To familiarize yourself with Taichi’s attributes or solve any technical issues, visit our doc site 👉🏻: <a href="https://www.taichi-lang.org/">https://www.taichi-lang.org/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d9b756dcbb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/taichi-cookbook-001-five-practical-tips-on-how-to-master-taichi-a-handy-parallel-programming-2d9b756dcbb">Taichi Cookbook 001: Five practical tips on how to master Taichi, a handy parallel programming…</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>
        <item>
            <title><![CDATA[Taichi v1.1.0 was released!]]></title>
            <link>https://medium.com/parallel-programming-in-python/taichi-v1-1-0-was-released-c38b4983a48c?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/c38b4983a48c</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[taichi-lang]]></category>
            <category><![CDATA[annoucements]]></category>
            <dc:creator><![CDATA[Taichi Lang]]></dc:creator>
            <pubDate>Tue, 23 Aug 2022 10:40:58 GMT</pubDate>
            <atom:updated>2022-08-23T10:40:58.543Z</atom:updated>
            <content:encoded><![CDATA[<h3>Highlights</h3><h3>New features</h3><h3>Quantized data types</h3><p>High-resolution simulations can deliver great visual quality, but are often limited by the capacity of the onboard GPU memory. This release adds quantized data types, allowing you to define your own integers, fixed-point numbers, or floating-point numbers of arbitrary number of bits that may strike a balance between your hardware limits and simulation effects. See <a href="https://docs.taichi-lang.org/docs/master/quant">Using quantized data types</a> for a comprehensive introduction.</p><h3>Offline cache</h3><p>A Taichi kernel is implicitly compiled the first time it is called. The compilation results are kept in an <em>online</em> in-memory cache to reduce the overhead in the subsequent function calls. As long as the kernel function is unchanged, it can be directly loaded and launched. The cache, however, is no longer available when the program terminates. Then, if you run the program again, Taichi has to re-compile all kernel functions and reconstruct the <em>online</em> in-memory cache. And the first launch of a Taichi function is always slow due to the compilation overhead.</p><p>To address this problem, this release adds the <em>offline</em> cache feature, which dumps the compilation cache to the disk for future runs. The first launch overhead can be drastically reduced in subsequent runs. Taichi now constructs and maintains an offline cache by default.</p><p>The following table shows the launch overhead of running <a href="https://github.com/taichi-dev/taichi/blob/master/python/taichi/examples/rendering/cornell_box.py">cornell_box</a> on the CUDA backend with and without offline cache:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*RXxY9Tik4XobBsMe_Gw6BA.png" /></figure><p>Note that, for now, the offline cache feature works <em>only</em> on the CPU and CUDA backends. If your code behaves abnormally, disable offline cache by setting the environment variable TI_OFFLINE_CACHE=0 or ti.init(offline_cache=False) and file an issue with us on <a href="https://github.com/taichi-dev/taichi/issues/new/choose">Taichi&#39;s GitHub repo</a>. See <a href="https://docs.taichi-lang.org/docs/performance#offline-cache">Offline cache</a> for more information.</p><h3>Forward-mode automatic differentiation</h3><p>Adds forward-mode automatic differentiation via ti.ad.FwdMode. Unlike the existing reverse-mode automatic differentiation, which computes vector-Jacobian product (vJp), forward-mode computes Jacobian-vector product (Jvp) when evaluating derivatives. Therefore, forward-mode automatic differentiation is much more efficient in situations where the number of a function&#39;s outputs is greater than its inputs. Read <a href="https://github.com/taichi-dev/taichi/blob/master/python/taichi/examples/autodiff/jacobian.py">this example</a>, which demonstrates Jacobian matrix computation in forward mode and reverse mode.</p><h3>SharedArray (experimental)</h3><p>GPU’s shared memory is a fast small memory that is visible within each thread block (or workgroup in Vulkan). It is widely used in scenarios where performance is a crucial concern. To give you access to your GPU’s shared memory, this release adds the SharedArray API under the namespace ti.simt.block.</p><p>The following diagram illustrates the performance benefits of Taichi&#39;s SharedArray. With SharedArray, Taichi Lang is comparable to or even outperforms the equivalent CUDA code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*QrHGnaHzQWB3VapH.png" /></figure><h3>Texture (experimental)</h3><p>Taichi now supports texture bilinear sampling and raw texel fetch on both Vulkan and OpenGL backends. This feature leverages the hardware texture unit and diminishes the need for manual composition of bilinear interpolation code in image processing tasks. This feature also provides an easy way for texture mapping in tasks such as rasterization or ray-tracing. On Vulkan backend, Taichi additionally supports image load and store. You can directly manipulate texels of an image and use this very image in subsequent texture mapping.</p><p>Note that the current texture and image APIs are in the early stages and subject to change. In the future we plan to support bindless textures to extend to tasks such as ray-tracing. We also plan to extend full texture support to all backends that support texture APIs.</p><p>Run ti example simple_texture to see an example of texture support!</p><h3>Improvements</h3><h3>GGUI</h3><ol><li>Supports fetching and storing the depth information of the current scene:</li></ol><p>1.1 In a Taichi field: ti.ui.Window.get_depth_buffer(field);</p><p>1.2 In a NumPy array: ti.ui.Window.get_depth_buffer_as_numpy().</p><p>2. Supports drawing 3D lines using Scene.lines(vertices, width).</p><p>3. Supports drawing mesh instances. You can pass a list of transformation matrices (ti.Matrix.field(4, 4, ti.f32, shape=N)) and call ti.ui.Scene.mesh_instance(vertices, transforms=TransformMatrixField) to put various mesh instances at different places.</p><p>4. Supports showing the wireframe of a mesh when calling Scene.mesh() or Scene.mesh_instance() by setting show_wireframe=True.</p><h3>Syntax</h3><ul><li>Taichi dataclass: Taichi now recommends using the @ti.dataclass decorator to define struct types, or even attach functions to them. See <a href="https://docs.taichi-lang.org/docs/master/odop#taichi-dataclasses">Taichi dataclasses</a> for more information.</li></ul><pre>@ti.dataclass<br>class Sphere:<br>  center: vec3<br>  radius: ti.f32<br>  @ti.func<br>  def area(self):<br>    # a function to run in taichi scope<br>    return 4 * math.pi * self.radius * self.radius<br>  def is_zero_sized(self):<br>    # a python scope function<br>    return self.radius == 0.0</pre><ul><li>As shown in the dataclass example above, vec2, vec3, and vec4 in the taichi.math module (same for ivec and uvec) can be directly used as type hints. The numeric precision of these types is determined by default_ip or default_fp in ti.init().</li><li>More flexible instantiation for a struct or dataclass:<br>In earlier releases, to instantiate a taichi.types.struct and taichi.dataclass, you have to explicitly put down a complete list of member-value pairs like:</li></ul><pre>ray = Ray(ro=vec3(0), rd=vec3(1, 0, 0), t=1.0)</pre><ul><li>As of this release, you are given more options. The positional arguments are passed to the struct members in the order they are defined; the keyword arguments set the corresponding struct members. Unspecified struct members are automatically set to zero. For example:</li></ul><pre># use positional arguments to set struct members in order<br>ray = Ray(vec3(0), vec3(1, 0, 0), 1.0)<br><br># ro is set to vec3(0) and t will be set to 0<br>ray = Ray(vec3(0), rd=vec3(1, 0, 0))<br><br># both ro and rd are set to vec3(0)<br>ray = Ray(t=1.0)<br><br># ro is set to vec3(1), rd=vec3(0) and t=0.0<br>ray = Ray(1)<br><br># all members are set to 0.<br>ray = Ray()</pre><ul><li>Supports calling fill() from both the Python scope and the Taichi scope.<br>In earlier releases, you can <em>only</em> call fill() from the Python scope, which is a method in the ScalarField or MatrixField class. As of this release, you can call this method from either the Python scope or the Taichi scope. See the following code snippet:</li></ul><pre>x = ti.field(int, shape=(10, 10))<br>x.fill(1)<br><br>@ti.kernel<br>def test():<br>    x.fill(-1)</pre><ul><li>More flexible initialization for customized matrix types:<br>As the following code snippet shows, matrix types created using taichi.types.matrix() or taichi.types.vector() can be initialized more flexibly: Taichi automatically combines the inputs and converts them to a matrix whose shape matches the shape of the target matrix type.</li></ul><pre># mat2 and vec3 are predefined types in the ti.math module<br>mat2 = ti.types.matrix(2, 2, float)<br>vec3 = ti.types.vector(3, float)<br><br>m = mat2(1)  # [[1., 1.], [1., 1.]]<br>m = mat2(1, 2, 3, 4)  # [[1., 2.], [3, 4.]]<br>m = mat2([1, 2], [3, 4])  # [[1., 2.], [3, 4.]]<br>m = mat2([1, 2, 3, 4])  # [[1., 2.], [3, 4.]]<br>v = vec3(1, 2, 3)<br>m = mat2(v, 4)  # [[1., 2.], [3, 4.]]</pre><ul><li>Makes ti.f32(x) syntax sugar for ti.cast(x, ti.f32), if x is neither a literal nor of a compound data type. Same for other primitive types such as ti.i32, ti.u8, or ti.f64.</li><li>More convenient axes order adjustment: A common way to improve the performance of a Taichi program is to <a href="https://docs.taichi-lang.org/docs/layout#row-major-versus-column-major">adjust the order of axes when laying out field data in the memory</a>. In earlier releases, this requires in-depth knowledge about the data definition language (the SNode system) and may become an extra burden in situations where sparse data structures are not required. As of this release, Taichi supports specifying the order of axes when defining a Taichi field.</li></ul><pre># Before<br>x = ti.field(ti.i32)<br>y = ti.field(ti.i32)<br>ti.root.dense(ti.i, M).dense(ti.j, N).place(x)  # row-major<br>ti.root.dense(ti.j, N).dense(ti.i, M).place(y)  # column-major<br># New syntax<br>x = ti.field(ti.i32, shape=(M, N), order=&#39;ij&#39;)<br>y = ti.field(ti.i32, shape=(M, N), order=&#39;ji&#39;)<br># SoA vs. AoS example<br>p = ti.Vector.field(3, ti.i32, shape=(M, N), order=&#39;ji&#39;, layout=ti.Layout.SOA)<br>q = ti.Vector.field(3, ti.i32, shape=(M, N), order=&#39;ji&#39;, layout=ti.Layout.AOS)</pre><h3>Important bug fixes</h3><ul><li>Fixed infinite loop when an integer pow() has a negative exponent (<a href="https://github.com/taichi-dev/taichi/pull/5275">#5275</a>)</li><li>Fixed numerical issues with matrix slicing (<a href="https://github.com/taichi-dev/taichi/issues/4677">#4677</a>)</li><li>Improved data type checks for ti.ndrange (<a href="https://github.com/taichi-dev/taichi/issues/4478">#4478</a>)</li></ul><h3>API changes</h3><h3>Added</h3><ul><li>ti.BitpackedFields</li><li>ti.from_paddle</li><li>ti.to_paddle</li><li>ti.FieldsBuilder.lazy_dual</li><li>ti.math module</li><li>ti.Texture</li><li>ti.ref</li><li>ti.dataclass</li><li>ti.simt.block.SharedArray</li></ul><h3>Moved</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*x4wF6eNFGoXkwJfo40Td9w.png" /></figure><h3>Deprecated</h3><ul><li>ti.ui.make_camera: Please construct cameras with ti.ui.Camera instead.</li></ul><h3>Deprecation notice</h3><h3>Python 3.6</h3><p>As announced in <a href="https://github.com/taichi-dev/taichi/releases/tag/v1.0.0">v1.0.0 release</a>, we no longer provide official python3.6 wheels through pypi. Users who need taichi with python3.6 may still build from source but its support is not guaranteed.</p><h3>Taichi_GLSL</h3><p>The taichi_glsl package on <a href="https://pypi.org/project/taichi-glsl/">pypi</a> will no longer be maintained as of this release. GLSL-related features will be implemented in the official taichi.math module, which includes data types and handy functions for daily math and shader development:</p><ul><li>Vector types: vec2, vec3, and vec4.</li><li>Matrix types: mat2,mat3, and mat4.</li><li>GLSL functions such as step(),clamp(), and smoothstep().</li></ul><h3>MacOS 10.14</h3><p>Official support for MacOS Mojave (10.14, released in 2018) will be dropped starting from v1.2.0. Please upgrade your MacOS if possible or let us know if you have any concerns.</p><h3>Full changelog:</h3><ul><li>[misc] Update version to v1.1.0 (by Ailing Zhang)</li><li>[test] Fix autodiff test for unsupported shift ptr (<a href="https://github.com/taichi-dev/taichi/pull/5723">#5723</a>) (by Mingrui Zhang)</li><li>[Doc] [type] Add introduction to quantized types (<a href="https://github.com/taichi-dev/taichi/pull/5705">#5705</a>) (by Yi Xu)</li><li>[autodiff] Clear all dual fields when exiting context manager (<a href="https://github.com/taichi-dev/taichi/pull/5716">#5716</a>) (by Mingrui Zhang)</li><li>[bug] Support indexing via np.integer for field (<a href="https://github.com/taichi-dev/taichi/pull/5712">#5712</a>) (by Ailing)</li><li>[Doc] Add docs for GGUI’s new features (<a href="https://github.com/taichi-dev/taichi/pull/5647">#5647</a>) (by Mocki)</li><li>[Doc] Add introduction to forward mode autodiff (<a href="https://github.com/taichi-dev/taichi/pull/5680">#5680</a>) (by Mingrui Zhang)</li><li>[autodiff] Fix AdStackAllocaStmt not correctly backup (<a href="https://github.com/taichi-dev/taichi/pull/5692">#5692</a>) (by Mingrui Zhang)</li><li>Fix shared array for all Vulkan versions. (<a href="https://github.com/taichi-dev/taichi/pull/5721">#5721</a>) (by Haidong Lan)</li><li>[misc] Rc v1.1.0 patch3 (<a href="https://github.com/taichi-dev/taichi/pull/5709">#5709</a>) (by Ailing)</li><li>[bug] RC v1.1.0 patch2 (<a href="https://github.com/taichi-dev/taichi/pull/5683">#5683</a>) (by Ailing)</li><li>[ci] Temporarily disable a M1 vulkan test (<a href="https://github.com/taichi-dev/taichi/pull/5703">#5703</a>) (by Proton)</li><li>[Doc] Add doc about offline cache (<a href="https://github.com/taichi-dev/taichi/pull/5646">#5646</a>) (<a href="https://github.com/taichi-dev/taichi/pull/5686">#5686</a>) (by Mingming Zhang)</li><li>[bug] Fix bug that kernel names are not correctly captured by the profiler (<a href="https://github.com/taichi-dev/taichi/pull/5651">#5651</a>) (<a href="https://github.com/taichi-dev/taichi/pull/5669">#5669</a>) (by Mingming Zhang)</li><li>[gui] GGUI scene APIs are broken (<a href="https://github.com/taichi-dev/taichi/pull/5658">#5658</a>) (<a href="https://github.com/taichi-dev/taichi/pull/5667">#5667</a>) (by PENGUINLIONG)</li><li>[release] v1.1.0 patch1 (<a href="https://github.com/taichi-dev/taichi/pull/5649">#5649</a>) (by Ailing)</li><li>[llvm] Compile serially when num_thread=0 (<a href="https://github.com/taichi-dev/taichi/pull/5631">#5631</a>) (by Lin Jiang)</li><li>[cuda] Reduce kernel profiler memory usage (<a href="https://github.com/taichi-dev/taichi/pull/5623">#5623</a>) (by Bo Qiao)</li><li>[doc] Add docstrings for texture related apis (by Ailing Zhang)</li><li>[Lang] Support from/to_image for textures and add tests (by Ailing Zhang)</li><li>[gui] Add wareframe mode for mesh &amp; mesh_instance, add slider_int for Window.GUI. (<a href="https://github.com/taichi-dev/taichi/pull/5576">#5576</a>) (by Mocki)</li><li>avoid redundant compilation (<a href="https://github.com/taichi-dev/taichi/pull/5607">#5607</a>) (by yixu)</li><li>[misc] Enable offline cache by default (<a href="https://github.com/taichi-dev/taichi/pull/5613">#5613</a>) (by Mingming Zhang)</li><li>[Lang] Add parameter ‘order’ to specify layout for scalar, vector, matrix fields (<a href="https://github.com/taichi-dev/taichi/pull/5617">#5617</a>) (by Yi Xu)</li><li>[autodiff] [example] Add an example for computing Jacobian matrix (<a href="https://github.com/taichi-dev/taichi/pull/5609">#5609</a>) (by Mingrui Zhang)</li><li>[ci] Add PR tag for dx12. (<a href="https://github.com/taichi-dev/taichi/pull/5614">#5614</a>) (by Xiang Li)</li><li>fix ti.ui.Space (<a href="https://github.com/taichi-dev/taichi/pull/5606">#5606</a>) (by yixu)</li><li>[ci] Build Android export core (<a href="https://github.com/taichi-dev/taichi/pull/5409">#5409</a>) (by Proton)</li><li>[type] Rename module quantized_types to quant (<a href="https://github.com/taichi-dev/taichi/pull/5608">#5608</a>) (by Yi Xu)</li><li>[llvm] [aot] Add unit tests for Dynamic SNodes with LLVM AOT (<a href="https://github.com/taichi-dev/taichi/pull/5594">#5594</a>) (by Zhanlue Yang)</li><li>[build] Forcing write file encoding in misc/make_changelog.py (<a href="https://github.com/taichi-dev/taichi/pull/5604">#5604</a>) (by Proton)</li><li>[llvm] [aot] Add unit tests for Bitmasked SNodes with LLVM AOT (<a href="https://github.com/taichi-dev/taichi/pull/5593">#5593</a>) (by Zhanlue Yang)</li><li>[GUI] Shifted to a more commonly supported type for set_image (<a href="https://github.com/taichi-dev/taichi/pull/5514">#5514</a>) (by PENGUINLIONG)</li><li>[gui] Fix snode offset (mesh disappearing bug) (<a href="https://github.com/taichi-dev/taichi/pull/5579">#5579</a>) (by Bob Cao)</li><li>[refactor] Redesign loading, dumping and cleaning of offline cache (<a href="https://github.com/taichi-dev/taichi/pull/5578">#5578</a>) (by Mingming Zhang)</li><li>[autodiff] [test] Add more complex for loop test cases for forward mode (<a href="https://github.com/taichi-dev/taichi/pull/5592">#5592</a>) (by Mingrui Zhang)</li><li>fix num_triangles (<a href="https://github.com/taichi-dev/taichi/pull/5602">#5602</a>) (by yixu)</li><li>[cuda] Decouple update from sync in kernel profiler (<a href="https://github.com/taichi-dev/taichi/pull/5589">#5589</a>) (by Bo Qiao)</li><li>Removed unnecessary tags to work around a crowdIn issue. (<a href="https://github.com/taichi-dev/taichi/pull/5590">#5590</a>) (by Vissidarte-Herman)</li><li>[Lang] Change vec2/3/4 from function calls to types (<a href="https://github.com/taichi-dev/taichi/pull/5556">#5556</a>) (by Zhao Liang)</li><li>[vulkan] Enable shared array support for vulkan backend (<a href="https://github.com/taichi-dev/taichi/pull/5583">#5583</a>) (by Haidong Lan)</li><li>[aot] Avoid reserved words when generate C# AOT bindings (<a href="https://github.com/taichi-dev/taichi/pull/5586">#5586</a>) (by Proton)</li><li>[ci] Update llvm15 prebuild binary. (<a href="https://github.com/taichi-dev/taichi/pull/5581">#5581</a>) (by Xiang Li)</li><li>[doc] Removed a redundant line break to see if it will fix a CrowdIn issue (<a href="https://github.com/taichi-dev/taichi/pull/5584">#5584</a>) (by Vissidarte-Herman)</li><li>[type] Refine SNode with quant 10/n: Add validity checks and simplify BitStructType (<a href="https://github.com/taichi-dev/taichi/pull/5573">#5573</a>) (by Yi Xu)</li><li>[autodiff] [refactor] Rename the parameters to param for forward mode (<a href="https://github.com/taichi-dev/taichi/pull/5582">#5582</a>) (by Mingrui Zhang)</li><li>[doc] Format fix to work around a crowdIn issue (<a href="https://github.com/taichi-dev/taichi/pull/5580">#5580</a>) (by Vissidarte-Herman)</li><li>Update syntax.md (<a href="https://github.com/taichi-dev/taichi/pull/5575">#5575</a>) (by Zhao Liang)</li><li>[doc] Added an mdx-code-block escape hatch syntaxt to workaround a CrowdIn … (<a href="https://github.com/taichi-dev/taichi/pull/5574">#5574</a>) (by Vissidarte-Herman)</li><li>[Doc] Update external.md (<a href="https://github.com/taichi-dev/taichi/pull/5547">#5547</a>) (by Zhao Liang)</li><li>[doc] Add introductions to ambient_elements in llvm_sparse_runtime.md (<a href="https://github.com/taichi-dev/taichi/pull/5567">#5567</a>) (by Zhanlue Yang)</li><li>[refactor] Unify ways to set external array args (<a href="https://github.com/taichi-dev/taichi/pull/5565">#5565</a>) (by Ailing)</li><li>[Lang] [type] Refine SNode with quant 9/n: Rename some parameters in quant APIs (<a href="https://github.com/taichi-dev/taichi/pull/5566">#5566</a>) (by Yi Xu)</li><li>[opt] Improved warning messages for statements (<a href="https://github.com/taichi-dev/taichi/pull/5564">#5564</a>) (by Zhanlue Yang)</li><li>[bug] Fix android build for taichi-aot-demo (<a href="https://github.com/taichi-dev/taichi/pull/5560">#5560</a>) (by Ailing)</li><li>[opt] Added llvm::SeparateConstOffsetFromGEPPass() for shared_memory optimizations (<a href="https://github.com/taichi-dev/taichi/pull/5494">#5494</a>) (by Zhanlue Yang)</li><li>[Lang] [type] Refine SNode with quant 8/n: Replace bit_struct with ti.BitpackedFields (<a href="https://github.com/taichi-dev/taichi/pull/5532">#5532</a>) (by Yi Xu)</li><li>[build] Enforce local-scoped symbols in static llvm libs (<a href="https://github.com/taichi-dev/taichi/pull/5553">#5553</a>) (by Bo Qiao)</li><li>[refactor] Unify ways to set ndarray args (<a href="https://github.com/taichi-dev/taichi/pull/5559">#5559</a>) (by Ailing)</li><li>[gui] [vulkan] Support for drawing mesh instances (<a href="https://github.com/taichi-dev/taichi/pull/5546">#5546</a>) (by Mocki)</li><li>[llvm] [aot] Added taichi_sparse unit test to C-API for CUDA backend (<a href="https://github.com/taichi-dev/taichi/pull/5531">#5531</a>) (by Zhanlue Yang)</li><li>Add glFinish to wait_idle (<a href="https://github.com/taichi-dev/taichi/pull/5538">#5538</a>) (by Bo Qiao)</li><li>[autodiff] Skip ConstStmt when generating alloca for dual (<a href="https://github.com/taichi-dev/taichi/pull/5554">#5554</a>) (by Mingrui Zhang)</li><li>[ci] Fix macOS nightly build (<a href="https://github.com/taichi-dev/taichi/pull/5552">#5552</a>) (by Proton)</li><li>Fix potential bug of lang::Program that could be double finalized (<a href="https://github.com/taichi-dev/taichi/pull/5550">#5550</a>) (by Mingming Zhang)</li><li>[Error] Raise error when using the struct for in python scope (<a href="https://github.com/taichi-dev/taichi/pull/5536">#5536</a>) (by Lin Jiang)</li><li>[bug] Fix calling make_aot_kernel failed when offline_cache=True (<a href="https://github.com/taichi-dev/taichi/pull/5537">#5537</a>) (by Mingming Zhang)</li><li>[ci] Move macOS 10.15 workloads to self-hosted runners (<a href="https://github.com/taichi-dev/taichi/pull/5539">#5539</a>) (by Proton)</li><li>[build] [refactor] Utilize find_cuda_toolkit and clean some target dependencies (<a href="https://github.com/taichi-dev/taichi/pull/5526">#5526</a>) (by Bo Qiao)</li><li>[autodiff] [test] Add more for-loop tests for forward mode (<a href="https://github.com/taichi-dev/taichi/pull/5525">#5525</a>) (by Mingrui Zhang)</li><li>[Lang] [bug] Ensure non-i32 compatibility in while statement conditions (<a href="https://github.com/taichi-dev/taichi/pull/5521">#5521</a>) (by daylily)</li><li>[Lang] Improve error message for ggui on opengl backend (<a href="https://github.com/taichi-dev/taichi/pull/5509">#5509</a>) (by Zhao Liang)</li><li>[aot] Support texture and rwtexture in cgraph (<a href="https://github.com/taichi-dev/taichi/pull/5528">#5528</a>) (by Ailing)</li><li>[llvm] Add parallel compilation to CUDA backend (<a href="https://github.com/taichi-dev/taichi/pull/5519">#5519</a>) (by Lin Jiang)</li><li>[type] [refactor] Decouple quant from SNode 9/n: Remove exponent handling from SNode (<a href="https://github.com/taichi-dev/taichi/pull/5510">#5510</a>) (by Yi Xu)</li><li>[Lang] Fix numpy and taichi operations problem (<a href="https://github.com/taichi-dev/taichi/pull/5506">#5506</a>) (by Zhao Liang)</li><li>[Vulkan] Added an interface to get accumulated on-device execution time (<a href="https://github.com/taichi-dev/taichi/pull/5488">#5488</a>) (by PENGUINLIONG)</li><li>[Async] [refactor] Remove AsyncTaichi (<a href="https://github.com/taichi-dev/taichi/pull/5523">#5523</a>) (by Lin Jiang)</li><li>[misc] Fix warning at GGUI canvas.circles (<a href="https://github.com/taichi-dev/taichi/issues/5424">#5424</a>) (<a href="https://github.com/taichi-dev/taichi/pull/5518">#5518</a>) (by Proton)</li><li>[gui] Support rendering lines from a part of VBO (<a href="https://github.com/taichi-dev/taichi/pull/5495">#5495</a>) (by Mocki)</li><li>[ir] Cast indices of ExternalPtrStmt to ti.i32 (<a href="https://github.com/taichi-dev/taichi/pull/5516">#5516</a>) (by Yi Xu)</li><li>[Lang] Support syntax sugar for ti.cast (<a href="https://github.com/taichi-dev/taichi/pull/5515">#5515</a>) (by Yi Xu)</li><li>[Lang] Better struct initialization (<a href="https://github.com/taichi-dev/taichi/pull/5481">#5481</a>) (by Zhao Liang)</li><li>[example] Make implicit_fem fallback to CPU when CUDA is not available (<a href="https://github.com/taichi-dev/taichi/pull/5512">#5512</a>) (by Yi Xu)</li><li>[Lang] Make MatrixType support more ways of initialization (<a href="https://github.com/taichi-dev/taichi/pull/5479">#5479</a>) (by Zhao Liang)</li><li>[Vulkan] Fixed depth texture validation error (<a href="https://github.com/taichi-dev/taichi/pull/5507">#5507</a>) (by PENGUINLIONG)</li><li>[bug] Fix vulkan source when build for android (<a href="https://github.com/taichi-dev/taichi/pull/5508">#5508</a>) (by Bo Qiao)</li><li>[refactor] [llvm] Rename CodeGenCPU/CUDA/WASM and CodeGenLLVMCPU/CUDA/WASM (<a href="https://github.com/taichi-dev/taichi/pull/5500">#5500</a>) (by Lin Jiang)</li><li>[bug] Let the arguments in ti.init override the environment variables (<a href="https://github.com/taichi-dev/taichi/pull/5497">#5497</a>) (by Lin Jiang)</li><li>[misc] Add debug logging and TI_AUTO_PROF for offline cache (<a href="https://github.com/taichi-dev/taichi/pull/5503">#5503</a>) (by Mingming Zhang)</li><li>[misc] ti.Tape -&gt; ti.ad.Tape (<a href="https://github.com/taichi-dev/taichi/pull/5501">#5501</a>) (by Zihua Wu)</li><li>[misc] Support jit offline cache for kernels that call real functions (<a href="https://github.com/taichi-dev/taichi/pull/5477">#5477</a>) (by Mingming Zhang)</li><li>[doc] Update cpp tests build doc (<a href="https://github.com/taichi-dev/taichi/pull/5493">#5493</a>) (by Bo Qiao)</li><li>[Lang] Support call field.fill in kernel functions (<a href="https://github.com/taichi-dev/taichi/pull/5486">#5486</a>) (by Zhao Liang)</li><li>[Lang] [bug] Make comparisons always return i32 (<a href="https://github.com/taichi-dev/taichi/pull/5487">#5487</a>) (by Yi Xu)</li><li>[gui] [vulkan] Support 3d-lines rendering (<a href="https://github.com/taichi-dev/taichi/pull/5492">#5492</a>) (by Mocki)</li><li>[autodiff] Switch off parts of store forwarding optimization for autodiff (<a href="https://github.com/taichi-dev/taichi/pull/5464">#5464</a>) (by Mingrui Zhang)</li><li>[llvm] [aot] Add LLVM to CAPI part 9: Added AOT field tests for LLVM backend in C-API (<a href="https://github.com/taichi-dev/taichi/pull/5461">#5461</a>) (by Zhanlue Yang)</li><li>[bug] [llvm] Fix GEP when allocating TLS buffer in struct for (<a href="https://github.com/taichi-dev/taichi/pull/5473">#5473</a>) (by Lin Jiang)</li><li>[gui] [vulkan] Modify some internal APIs (<a href="https://github.com/taichi-dev/taichi/pull/5484">#5484</a>) (by Mocki)</li><li>[Build] Remove TI_EMSCRIPTENED related code (<a href="https://github.com/taichi-dev/taichi/pull/5483">#5483</a>) (by Bo Qiao)</li><li>[type] [refactor] Decouple quant from SNode 8/n: Remove redundant handling of llvm15 in codegen_llvm_quant (<a href="https://github.com/taichi-dev/taichi/pull/5480">#5480</a>) (by Yi Xu)</li><li>[CUDA] Enable shared memory for CUDA (<a href="https://github.com/taichi-dev/taichi/pull/5429">#5429</a>) (by Haidong Lan)</li><li>[gui] [vulkan] A faster version of depth copy through ti.field/ti.ndarray (copy directly from vulkan to cuda/gpu/cpu) (<a href="https://github.com/taichi-dev/taichi/pull/5455">#5455</a>) (by Mocki)</li><li>[misc] Add missing members of XXXExpression and FrontendXXXStmt to result of ASTSerializer (<a href="https://github.com/taichi-dev/taichi/pull/5471">#5471</a>) (by Mingming Zhang)</li><li>[llvm] [aot] Added field tests for LLVM backend in CGraph (<a href="https://github.com/taichi-dev/taichi/pull/5458">#5458</a>) (by Zhanlue Yang)</li><li>[type] [refactor] Decouple quant from SNode 7/n: Rewrite BitStructStoreStmt codegen without SNode (<a href="https://github.com/taichi-dev/taichi/pull/5475">#5475</a>) (by Yi Xu)</li><li>[llvm] [aot] Add LLVM to CAPI part 8: Added CGraph tests for LLVM backend in C-API (<a href="https://github.com/taichi-dev/taichi/pull/5456">#5456</a>) (by Zhanlue Yang)</li><li>[build] [refactor] Rename taichi core and taichi python targets (<a href="https://github.com/taichi-dev/taichi/pull/5451">#5451</a>) (by Bo Qiao)</li><li>[llvm] [aot] Add LLVM to CAPI part 6: Handle Field initialization in C-API (<a href="https://github.com/taichi-dev/taichi/pull/5444">#5444</a>) (by Zhanlue Yang)</li><li>[llvm] [aot] Add LLVM to CAPI part 7: Added AOT kernel tests for LLVM backend in C-API (<a href="https://github.com/taichi-dev/taichi/pull/5447">#5447</a>) (by Zhanlue Yang)</li><li>[error] Throw proper error message when an Ndarray is passed in via ti.template (<a href="https://github.com/taichi-dev/taichi/pull/5457">#5457</a>) (by Ailing)</li><li>[type] [refactor] Decouple quant from SNode 6/n: Rewrite extract_quant_float() without SNode (<a href="https://github.com/taichi-dev/taichi/pull/5448">#5448</a>) (by Yi Xu)</li><li>[bug] Set SNode tree id to all SNodes (<a href="https://github.com/taichi-dev/taichi/pull/5454">#5454</a>) (by Lin Jiang)</li><li>[AOT] Support on-device event (<a href="https://github.com/taichi-dev/taichi/pull/5433">#5433</a>) (by PENGUINLIONG)</li><li>[llvm] [aot] Add LLVM to CAPI part 5: Added C-API tests for Vulkan and Cuda backend (<a href="https://github.com/taichi-dev/taichi/pull/5440">#5440</a>) (by Zhanlue Yang)</li><li>[llvm] [bug] Fixing the crash in release tests introduced by a typo in <a href="https://github.com/taichi-dev/taichi/pull/5381">#5381</a> where we need a deep copy of arglist. (<a href="https://github.com/taichi-dev/taichi/pull/5441">#5441</a>) (by Proton)</li><li>[llvm] [aot] Add LLVM to CAPI part 4: Enabled C-API tests on CI &amp; Added C-API tests for CPU backend (<a href="https://github.com/taichi-dev/taichi/pull/5435">#5435</a>) (by Zhanlue Yang)</li><li>[misc] Bump version to v1.0.5 (<a href="https://github.com/taichi-dev/taichi/pull/5437">#5437</a>) (by Proton)</li><li>[aot] Support specifying vk_api_version in CompileConfig (<a href="https://github.com/taichi-dev/taichi/pull/5419">#5419</a>) (by Ailing)</li><li>[Lang] Add append attribute to dynamic fields (<a href="https://github.com/taichi-dev/taichi/pull/5413">#5413</a>) (by Zhao Liang)</li><li>[Lang] Add inf and nan (<a href="https://github.com/taichi-dev/taichi/pull/5270">#5270</a>) (by Zhao Liang)</li><li>[Doc] Updated docsite structure (<a href="https://github.com/taichi-dev/taichi/pull/5416">#5416</a>) (by Vissidarte-Herman)</li><li>[ci] Run release tests (<a href="https://github.com/taichi-dev/taichi/pull/5327">#5327</a>) (by Proton)</li><li>[type] [refactor] Decouple quant from SNode 5/n: Rewrite load_quant_float() without SNode (<a href="https://github.com/taichi-dev/taichi/pull/5422">#5422</a>) (by Yi Xu)</li><li>[llvm] Allow using clang 15 for COMPILE_LLVM_RUNTIME (<a href="https://github.com/taichi-dev/taichi/pull/5381">#5381</a>) (by Xiang Li)</li><li>[opengl] Speedup compilation for Nvidia cards (<a href="https://github.com/taichi-dev/taichi/pull/5430">#5430</a>) (by Bob Cao)</li><li>[Bug] Fix infinite loop when exponent of integer pow is negative (<a href="https://github.com/taichi-dev/taichi/pull/5275">#5275</a>) (by Mike He)</li><li>[build] [refactor] Move spirv codegen and common targets (<a href="https://github.com/taichi-dev/taichi/pull/5415">#5415</a>) (by Bo Qiao)</li><li>[autodiff] Check not placed field.dual and add needs_dual (<a href="https://github.com/taichi-dev/taichi/pull/5412">#5412</a>) (by Mingrui Zhang)</li><li>[bug] Simplify scalar handling in cgraph and relax field_dim check (<a href="https://github.com/taichi-dev/taichi/pull/5411">#5411</a>) (by Ailing)</li><li>[gui] [vulkan] Surpport for getting depth information for python users. (<a href="https://github.com/taichi-dev/taichi/pull/5410">#5410</a>) (by Mocki)</li><li>[AOT] Adjusted C-API for nd-array type conformance (<a href="https://github.com/taichi-dev/taichi/pull/5417">#5417</a>) (by PENGUINLIONG)</li><li>[type] Decouple quant from SNode 4/n: Add exponent info to BitStructType (<a href="https://github.com/taichi-dev/taichi/pull/5407">#5407</a>) (by Yi Xu)</li><li>[llvm] Avoid creating new LLVM contexts when updating struct module (<a href="https://github.com/taichi-dev/taichi/pull/5397">#5397</a>) (by Lin Jiang)</li><li>[build] Enable C-API compilation on CI (<a href="https://github.com/taichi-dev/taichi/pull/5403">#5403</a>) (by Zhanlue Yang)</li><li>[Lang] Implement assignment by slicing (<a href="https://github.com/taichi-dev/taichi/pull/5369">#5369</a>) (by Mike He)</li><li>[llvm] [aot] Add LLVM to CAPI part 3: Adapted AOT interfaces for LLVM backend (<a href="https://github.com/taichi-dev/taichi/pull/5402">#5402</a>) (by Zhanlue Yang)</li><li>[AOT] Fixed Vulkan device import capability settings (<a href="https://github.com/taichi-dev/taichi/pull/5400">#5400</a>) (by PENGUINLIONG)</li><li>[llvm] [aot] Add LLVM to CAPI part 2: Adapted memory allocation interfaces for LLVM backend (<a href="https://github.com/taichi-dev/taichi/pull/5396">#5396</a>) (by Zhanlue Yang)</li><li>[autodiff] Add ternary operators for forward mode (<a href="https://github.com/taichi-dev/taichi/pull/5405">#5405</a>) (by Mingrui Zhang)</li><li>[llvm] [aot] Add LLVM to CAPI part 1: Implemented capi::LlvmRuntime class (<a href="https://github.com/taichi-dev/taichi/pull/5393">#5393</a>) (by Zhanlue Yang)</li><li>[ci] Add per test hard timeout limit (<a href="https://github.com/taichi-dev/taichi/pull/5384">#5384</a>) (by Proton)</li><li>[ci] Properly detect $DISPLAY (<a href="https://github.com/taichi-dev/taichi/pull/5398">#5398</a>) (by Proton)</li><li>[ci] Llvm15 clang10 ci (<a href="https://github.com/taichi-dev/taichi/pull/5368">#5368</a>) (by Xiang Li)</li><li>[llvm] [bug] Add stop grad to ASTSerializer (<a href="https://github.com/taichi-dev/taichi/pull/5401">#5401</a>) (by Lin Jiang)</li><li>[autodiff] Add test for ternary operators in reverse mode autodiff (<a href="https://github.com/taichi-dev/taichi/pull/5395">#5395</a>) (by Mingrui Zhang)</li><li>[llvm] [aot] Add numerical unit tests for LLVM-CGraph (<a href="https://github.com/taichi-dev/taichi/pull/5319">#5319</a>) (by Zhanlue Yang)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c38b4983a48c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/taichi-v1-1-0-was-released-c38b4983a48c">Taichi v1.1.0 was released!</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>
        <item>
            <title><![CDATA[Accelerate Python code 100x by import taichi as ti]]></title>
            <link>https://medium.com/parallel-programming-in-python/accelerate-python-code-100x-by-import-taichi-as-ti-e93f3426c925?source=rss----efb825834ab2---4</link>
            <guid isPermaLink="false">https://medium.com/p/e93f3426c925</guid>
            <category><![CDATA[taichi-lang]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Taichi Lang]]></dc:creator>
            <pubDate>Mon, 22 Aug 2022 09:03:23 GMT</pubDate>
            <atom:updated>2022-08-22T09:03:22.936Z</atom:updated>
            <content:encoded><![CDATA[<p>Author: Yuanming-Hu</p><p>Python has become the most popular language in many rapidly evolving sectors, such as deep learning and data sciences. Yet its easy readability comes at the cost of performance. Of course, we all complain about program performance from time to time, and Python should certainly not take all the blame. Still, it’s fair to say that Python’s nature as an interpreted language does not help, especially in computation-intensive scenarios (e.g., when there are multiple nested for loops).</p><p>If you were ever caught up in one of the following situations, then this article is definitely for you.</p><ul><li>It takes forever to run a massive for loop in my Python program…</li><li>My program has a bottleneck at some computation tasks. Rewriting the code in C++ and calling C++ with the ctypes module can work, but it’s not straightforward enough and risks compilation failures when the code is transplanted to other devices. It would be much better if I could keep all the work done in a Python script.</li><li>I am a loyal C++/Fortran user but would like to try out Python as it is gaining increasing popularity. However, rewriting code in Python is a nightmare — I feel the performance must be more than 100x slower than before!</li><li>I need to process lots of images, and OpenCV cannot give me what I want. So I have to write nested loops manually. This is not very enjoyable.</li></ul><p>If you can relate, then you might want to learn more about Taichi. For those who have not heard of Taichi: Taichi is a DSL embedded in Python but has its own compiler to take over the code decorated with @ti.kernel, achieving high-performance execution on all kinds of hardware, including CPU and GPU. One of the most notable advantages it delivers is speeding up Python code, hence no need to envy the performance of C++/CUDA any more. (See the <a href="https://docs.taichi-lang.org/blog/is-taichi-lang-comparable-to-or-even-faster-than-cuda">benchmark report</a>.)</p><p>The developers in the Taichi community put a lot of effort into improving Taichi’s compatibility with Python. So far, all Taichi features can function perfectly after you import taichi as ti; You can easily install Taichi via the pip install command and interact with other Python libraries, including NumPy, Matplotlib, and PyTorch.</p><p>I am not exaggerating when I say Taichi can be your solution. In this blog, I’m going to explain how Taichi manages to accelerate Python code by at least 50 times through three examples:</p><ul><li>Count the number of primes less than N</li><li>Find the longest common subsequence (LCS) via dynamic programming</li><li>Solve reaction-diffusion equations</li></ul><p>A shortcut to the source code of the three cases: <a href="https://github.com/taichi-dev/faster-python-with-taichi">https://github.com/taichi-dev/faster-python-with-taichi</a>.</p><h3>Count the number of primes</h3><p>I don’t want my first example to be intimidating: let’s write a small program that counts all the prime numbers less than a designated positive integer N. So long as you have some experience with Python, you can try to write your own version and see if it works.</p><p>Now, let me reveal a standard answer:</p><pre>&quot;&quot;&quot;Count the number of primes in range [1, n].<br>&quot;&quot;&quot;</pre><pre>def is_prime(n: int):<br>    result = True<br>    for k in range(2, int(n ** 0.5) + 1):<br>        if n % k == 0:<br>        result = False<br>        break<br>    return result<br>    <br>def count_primes(n: int) -&gt; int:<br>    count = 0<br>    for k in range(2, n):<br>        if is_prime(k):<br>        count += 1<br>        <br>    return count<br>    <br>print(count_primes(1000000))</pre><p>The logic behind this approach is intuitive. We first define a function is_prime to tell whether a positive integer N is a prime number - if yes, return 1; otherwise, return 0. Then we iterate over the integers from 2 to sqrt(n) and check whether they can divide N. Finally, collect the results. Done! Save the code above as a PY file (say, count_primes.py) and run the file with the command:</p><pre>time python count_primes.py</pre><p>This is what I got on my computer:</p><pre>78498</pre><pre>real        0m2.235s<br>user        0m2.235s<br>sys        0m0.000s</pre><p>When N is one million, it takes 2.235 seconds to get the final result. Maybe this is not a difficult task for your computer as well. But what about setting N to 10 million? I bet you have to wait for at least 30 seconds before you receive a response.</p><p>Now, let me show you the real magic. You do not need to modify the function body at all. Just import a Python library and add two decorators to the functions, and you have made all the difference:</p><pre>&quot;&quot;&quot;Count the number of primes below a given bound.<br>&quot;&quot;&quot;<br>import taichi as ti<br>ti.init()</pre><pre>@ti.func<br>def is_prime(n: int):<br>    result = True<br>    for k in range(2, int(n ** 0.5) + 1):<br>        if n % k == 0:<br>            result = False<br>            break<br>    return result</pre><pre>@ti.kernel<br>def count_primes(n: int) -&gt; int:<br>    count = 0<br>    for k in range(2, n):<br>        if is_prime(k):<br>            count += 1</pre><pre>    return count</pre><pre>print(count_primes(1000000))</pre><p>Again, run time python count_primes.py. This time, I saw the same answer but a much shorter processing time:</p><pre>78498</pre><pre>real        0m0.363s<br>user        0m0.546s<br>sys        0m0.179s</pre><p>Almost six times faster! I also changed N to 10 million, and Taichi takes 0.8s to compute, whereas Python uses 55s — a 70x speed-up! Taichi does not stop here. It also allows you to specify the backend to run the program, offering more options to optimize your program performance. For example, you can specify ti.init(arch=ti.gpu) when initiating Taichi. To share my first-hand data, <em>Taichi consumes 0.45s to count all the prime numbers</em> <em>less than</em> <em>10 million when running on</em> <em>GPU, a 120x improvement compared with Python</em>.</p><p>Well, some may argue that this example may be good enough for students or entry-level Python users but is not very convincing for well-versed engineers who are facing complex real-life scenarios. I agree. Now you can expect something trickier in the following two cases.</p><h3>Dynamic programming</h3><p>Dynamic programming is a handy algorithmic technique for optimization. The main idea of the approach is to sacrifice some storage for faster execution, i.e., storing the intermediate computation results to reduce execution time. Here, I take the longest common subsequence (LCS) problem as an example — a typical application case of dynamic programming from the book <em>Introduction to Algorithms</em>.</p><blockquote><em>(A small distraction 😆: I just love the book like many others! I bought a Chinese translation of </em>Introduction to Algorithms<em> long, long ago (when I was far from being a proper programmer), and the book mentioned that the four authors were from MET. I was so perplexed at the time, wondering how four artists could be so good at programming. Later I realized that I must have bought a pirate. Ten years later, I became a doctoral student at MIT, and one of the RQE (Research Qualifying Exam) committee members was Professor Leiserson, a renowned “MET” scholar who co-authored </em>Introduction to Algorithms.<em>)</em></blockquote><p>Back to the point.</p><p>A subsequence is a subset of a sequence, with the relative ordering of elements unchanged. For instance, [1, 2, 1] is a subsequence of [1, 2, 3, 1], but [3, 2] is not. Based on this concept, an LCS is defined as the longest subsequence common to all given sequences. It should be noted that there might be more than one LCS shared by given sequences, but the length of the LCS should be unique.</p><p>For sequence a:</p><pre>a = [0, 1, 0, 2, 4, 3, 1, 2, 1]</pre><p>and sequence b:</p><pre>b = [4, 0, 1, 4, 5, 3, 1, 2]</pre><p>Their LCS is:</p><pre>LCS(a, b) = [0, 1, 4, 3, 1, 2]</pre><p>LCS is widely applied to various scenarios where pattern comparison is involved. For example, LCS is used in the Linux diff or git diff tools to compare two texts; replacing numbers with ACGT, biologists can use LCS to compare genomic segments.</p><p>To find the LCS of two given sequences via dynamic programming, we can progressively find the length of the LCS of the first i elements of sequence a and the first j elements of sequence b. Then, increase i or j in turn and repeat the step till the final result is derived.</p><p>We use f[i, j] to refer to the length of this subsequence LCS((prefix(a, i), prefix(b, j). prefix(a, i) denotes the first i elements of sequence a, i.e., a[0], a[1], ..., a[i - 1]. So, we can get the following recurrence relation:</p><pre>f[i, j] = max(f[i - 1, j - 1] + (a[i - 1] == b[j - 1]),<br>              max(f[i - 1, j], f[i, j - 1]))</pre><p>The full solution can be coded as:</p><pre>for i in range(1, len_a + 1):<br>    for j in range(1, len_b + 1):<br>        f[i, j] = max(f[i - 1, j - 1] + (a[i - 1] == b[j - 1]),<br>                      max(f[i - 1, j], f[i, j - 1]))</pre><p>Now, accelerate it with Taichi:</p><pre>import taichi as ti<br>import numpy as np</pre><pre>ti.init(arch=ti.cpu)</pre><pre>benchmark = True</pre><pre>N = 15000</pre><pre>f = ti.field(dtype=ti.i32, shape=(N + 1, N + 1))</pre><pre>if benchmark:<br>    a_numpy = np.random.randint(0, 100, N, dtype=np.int32)<br>    b_numpy = np.random.randint(0, 100, N, dtype=np.int32)<br>else:<br>    a_numpy = np.array([0, 1, 0, 2, 4, 3, 1, 2, 1], dtype=np.int32)<br>    b_numpy = np.array([4, 0, 1, 4, 5, 3, 1, 2], dtype=np.int32)<br></pre><pre>@ti.kernel<br>def compute_lcs(a: ti.types.ndarray(), b: ti.types.ndarray()) -&gt; ti.i32:<br>    len_a, len_b = a.shape[0], b.shape[0]</pre><pre>    ti.loop_config(serialize=True) # To forbid automatic parallelism<br>    for i in range(1, len_a + 1):<br>        for j in range(1, len_b + 1):<br>            f[i, j] = max(f[i - 1, j - 1] + (a[i - 1] == b[j - 1]),<br>                          max(f[i - 1, j], f[i, j - 1]))</pre><pre>    return f[len_a, len_b]<br></pre><pre>print(compute_lcs(a_numpy, b_numpy))</pre><p>Save the code above as a PY file lcs.py and run it in the terminal:</p><pre>time python lcs.py</pre><p>The result on my computer:</p><pre>2721</pre><pre>real        0m1.409s<br>user        0m1.112s<br>sys        0m0.549s</pre><p>You can try to <a href="https://github.com/taichi-dev/faster-python-with-taichi/blob/main/lcs.py">implement the LCS solution using Taichi and Numpy</a>, respectively, to compare program performance. When N=15000, my Taichi program finishes in 0.9s, while the NumPy version takes 476s - a drastic disparity of over 500x! This is because NumPy specializes in array-based operations while Taichi features a higher level of granularity by directly manipulating elements in data containers.</p><h3>Reaction-diffusion equations</h3><p>Spots of cheetahs, stripes of zebras, dots and lines of pufferfish… The intriguing patterns that make creatures so distinguishable (or inconspicuous) make us wonder about the secrets of mother nature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7dpBT7O4xcZAJBli" /><figcaption>Turing patterns in nature</figcaption></figure><p>The elements in such patterns are irregularly distributed, yet not in a state of disorder at all. From the evolutionary perspective, they are a result of natural selection over the centuries. But is there an underlying rule that “draws” the patterns? Alan Turing (inventor of the Turing Machine) was the first to come up with a model to describe the phenomenon. In his paper <em>The Chemical Basis of Morphogenesis</em>, he introduces two chemical substances (U and V) to simulate the generation of the patterns. The relationship between U and V resembles that between prey and predator. They move on their own and interact with each other:</p><ol><li>Initially, U and V are randomly dispersed over a domain;</li><li>At each timestep, they diffuse into neighboring space;</li><li>When U and V meet, a proportion of U is swallowed by V. Consequently, the concentration of V increases;</li><li>To avoid U being eradicated by V, we add a certain percentage (f) of U and remove a certain percentage (k) of V at each timestep.</li></ol><p>The process described above can be summarized in the following reaction-diffusion equation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/0*Aj3o7IzP5WZIIZQ0" /></figure><p>There are four key parameters: Du (diffusion speed of U), Dv (diffusion speed of V), f (short for feed, which controls the addition of U), and k (short for kill, which controls the removal of V).</p><p>To simulate the process in Taichi, we first need to create a grid to cover the domain and use vec2 to represent the concentration of U and V in the grid. The numerical computation of the Laplace operator requires access to the neighboring grids. To avoid updating and reading data in the same loop, we should create two grids of the identical shape W × H × 2. Each time we access data from one grid, we write the updated data into the other and then switch grids. I design the data structure as such:</p><pre>W, H = 800, 600<br>uv = ti.Vector.field(2, float, shape=(2, W, H))</pre><p>In the beginning, we set the concentration of U in the grid to 1 everywhere and place V in 50 randomly-selected locations:</p><pre>import numpy as np</pre><pre>uv_grid = np.zeros((2, W, H, 2), dtype=np.float32)<br>uv_grid[0, :, :, 0] = 1.0<br>rand_rows = np.random.choice(range(W), 50)<br>rand_cols = np.random.choice(range(H), 50)<br>uv_grid[0, rand_rows, rand_cols, 1] = 1.0<br>uv.from_numpy(uv_grid)</pre><p>The actual computation can be done within 10 lines of code:</p><pre>@ti.kernel<br>def compute(phase: int):<br>    for i, j in ti.ndrange(W, H):<br>        cen = uv[phase, i, j]<br>        lapl = uv[phase, i + 1, j] + uv[phase, i, j + 1] + uv[phase, i - 1, j] + uv[phase, i, j - 1] - 4.0 * cen<br>        du = Du * lapl[0] - cen[0] * cen[1] * cen[1] + feed * (1 - cen[0])<br>        dv = Dv * lapl[1] + cen[0] * cen[1] * cen[1] - (feed + kill) * cen[1]<br>        val = cen + 0.5 * tm.vec2(du, dv)<br>        uv[1 - phase, i, j] = val</pre><p>We use an integer phase (either 0 or 1) to control which grid we read data from. Correspondingly, 1-phase directs to the other grid that accepts updated data.</p><p>The final step is to dye the substances according to the concentration of V. The final effects are striking:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/0*NS1NBa_iqlYwVvPq" /></figure><p>The interesting thing is that we can always end up with similar patterns despite that the initial concentration of V is randomly set.</p><p>We also <a href="https://github.com/taichi-dev/faster-python-with-taichi/blob/main/reaction_diffusion_taichi.py">provide two implementations based on Taichi and Numba, respectively</a>. Though both packages use their own compilers, the Taichi version chooses GPU as the backend, exceeding 300fps with ease; while Numba relies on CPU and only reaches about 30fps.</p><h3>Taichi vs. other Python packages</h3><p>So far, I have elaborated on three cases to prove that Taichi can effectively, if not dramatically, accelerate Python programs. Simply put, Taichi’s strength in high-performance computation boils down to its design philosophy:</p><ol><li>Taichi is a compiled language.</li><li>Taichi can automatically execute your code in parallel, while Python is usually single-threaded.</li><li>Taichi can run on both CPU and GPU, while Python runs on CPU.</li></ol><p>Of course, Python is an inclusive universe where many tools are available to expedite native Python code. It makes no sense to boast about Taichi if I safely ignore other Python libraries. Hence, I summarize the pros and cons of Taichi and similar packages as follows:</p><p><strong>Taichi vs. NumPy/JAX/PyTorch/TensorFlow:</strong> The latter group is highly dependent on array-based operations, and thus is friendly to users interested in data science and deep learning. However, they are much less flexible than Taichi when it comes to scientific computing. In the prime number case above, it would be less straightforward to complete the calculation with arrays. In terms of the granularity of math operations, Taichi compares more to C++ and CUDA as it can directly manipulate each iteration of the loops.</p><p><strong>Taichi vs. Cython:</strong> Cython is also used frequently to improve Python code performance. In fact, many modules in the official NumPy and SciPy code are written and compiled in Cython. On the flip side, readability is compromised, and Cython does not support switching to GPU (although it allows parallel computing to a certain degree).</p><p><strong>Taichi vs. Numba:</strong> As its name indicates, Numba is tailored for Numpy. Numba is recommended if your functions involve vectorization of Numpy arrays. Compared with Numba, Taichi enjoys the following advantages:</p><ul><li>Taichi supports multiple data types, including struct, dataclass, quant, and sparse, and allows you to adjust memory layout flexibly. This feature is extremely desirable when a program handles massive amounts of data. However, Numba only performs best when dealing with dense NumPy arrays.</li><li>Taichi can call different GPU backends for computation, making large-scale parallel programming (such as particle simulation or rendering) as easy as winking. But it would be hard even to imagine writing a renderer in Numba.</li></ul><p><strong>Taichi vs. PyPy:</strong> PyPy is a JIT compiler launched as early as in 2007. Similar to Taichi, PyPy also accelerates Python code via compilation. PyPy is attractive because users can keep Python code as it is without even moderate modification, which leaves restricted options for optimization, though. PyPy is alluringly painful in this sense. If you expect a greater leap in performance, Taichi can achieve the end perfectly. But you need to familiarize yourself with Taichi’s syntax and assumptions, which differ from Python’s slightly.</p><p><strong>Taichi vs. ctypes:</strong> ctypes allows users to call C functions from Python and run programs written in C++/CUDA in Python through a C-compatible API. However, ctypes further elevates the usage barrier: To write a satisfactory program, users need to command C, Python, CMake, CUDA, and even more languages. It is much more reassuring to keep everything in Python.</p><p>To sum up, there is no universal solution to all optimization problems. That’s partially why Python is fascinating. You can always find/create an easy-to-use tool that can precisely solve your problem at hand. In terms of scientific computing, Taichi is an ideal option within Python that can help you achieve performance comparable to C/C++.</p><p>If you are interested, join the Taichi community: <a href="https://github.com/taichi-dev/taichi">https://github.com/taichi-dev/taichi</a>. Any contribution &amp; feedback is welcome!</p><p>Install Taichi:</p><pre>pip install -U taichi</pre><p>And experience classy Taichi demos:</p><pre>ti gallery</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/0*SJxy4sujt_33V7pP" /></figure><p>About Taichi Lang:<br>Taichi Lang is an open-source parallel programming language designed for high-performance numerical computation. It is embedded in Python and uses just-in-time (JIT) compiler frameworks (such as LLVM), to offload the compute-intensive Python code to the native GPU or CPU instructions.<br>View our GitHub project and become a contributor:</p><p><a href="https://github.com/taichi-dev/taichi">GitHub - taichi-dev/taichi: Productive, portable, and performant GPU programming in Python.</a></p><p>To familiarize yourself with Taichi’s attributes or solve any technical issues, visit our doc site:</p><p><a href="https://www.taichi-lang.org/">https://www.taichi-lang.org/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e93f3426c925" width="1" height="1" alt=""><hr><p><a href="https://medium.com/parallel-programming-in-python/accelerate-python-code-100x-by-import-taichi-as-ti-e93f3426c925">Accelerate Python code 100x by import taichi as ti</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>