You are on page 1of 4

Taichi: An Open-Source Computer Graphics Library

YUANMING HU, MIT CSAIL


arXiv:1804.09293v1 [cs.GR] 24 Apr 2018

Fig. 1. Results simulated and rendered using Taichi.


An ideal software system in computer graphics should be a combination of these frameworks, building prototypical systems from scratch is
innovative ideas, solid software engineering and rapid development. How- often necessary when modifying these libraries becomes even more
ever, in reality these requirements are seldom met simultaneously. In this expensive than starting over. Taichi is designed to be a reusable
paper, we present early results on an open-source library named Taichi infrastructure for the latter situation, by providing abstractions at
(http://taichi.graphics) which alleviates this practical issue by providing an different levels from vector/matrix arithmetics to scene configura-
accessible, portable, extensible, and high-performance infrastructure that is
tion and scripting. Compared with existing reusable components
reusable and tailored for computer graphics. As a case study, we share our
experience in building a novel physical simulation system using Taichi. such as Boost or Eigen, Taichi is tailored for computer graphics.
The domain-specific design has many benefits over those general
frameworks, as illustrated in Fig. 2 with a concrete example.
1 WHY NEW LIBRARY FOR COMPUTER GRAPHICS?
Single-precision 3D Matrix-Vector Multiplication Throughputs
2.5
(Intel i7-7700K@4.2G, 1-thread; data fit L1 $)

Computer graphics research has high standards on novelty and CPU intrinsics (vfmaddps)
quality, which lead to a trade-off between rapid prototyping and taichi::(Vector|Matrix)3f
good software engineering. The former allows researchers to code 2.0 Eigen::(Vector|Matrix)3f
quickly but inevitably leads to defects such as unsatisfactory per-
formance, poor portability and maintainability. As a result, many 1.5
G iters/s

projects are neither reused nor open-sourced, stopping other people


from experimenting, reproducing, and developing based on prior 1.0
art. The latter results in reusable codes for future projects, but slows
down research progress due to low-level engineering such as infras- 0.5
tructure building. Admittedly, for a researcher too much low-level
engineering may become an obstacle for high-level thinking.
0.0
Tight loop 4-way unrolled loop
Existing open-source projects are focused on certain functionality,
like rendering (e.g. Mitsuba [Jakob 2010], PBRT [Pharr et al. 2016],
Lightmetrica [Otsu 2015], POV-Ray [Buck and Collins 2004], etc.), ge- Fig. 2. Why do we need a tool tailored for graphics? Con-
ometry processing (libIGL [Jacobson et al. 2013], MeshLab [Cignoni sider 3D matrix-vector multiplication as an example. In Eigen single-
et al. 2008], CGAL [Fabri and Pion 2009], etc.), simulation (Bul- precision 3D vectors are stored compactly using 12 bytes. Compared
let [Coumans et al. 2013], ODE [Smith et al. 2005], ArcSim [Narain with 16-byte-aligned storage, this scheme saves some space, but offers
et al. 2004], VegaFEM [Sin et al. 2013], MantaFlow [Thuerey and unsatisfactory performance since additional permutation in SIMD
Pfaff 2017], Box2D [Catto 2011], PhysBAM [Dubey et al. 2011], etc.). registers is needed for vectorized computation. In graphics we care
more about throughput and latency instead of space, so Taichi’s vector
The aforementioned libraries have proven successful in their own system is designed in the latter way and is 1.8 − 2.3× faster.
applications. However, though some projects can more or less reuse
2 DESIGN GOALS time consumption of each part automatically, simplifying hotspot
analysis.
Accessibility. Taichi is open-source and easy-to-use by design. We
avoid unnecessary dependencies, especially hard-to-deploy ones
Debugging and Testing. C++ programs do not generate much useful
such as Boost. Installation can be done simply by executing a python
debugging information when it crashes. This makes debugging
script that automatically installs all dependencies. For beginners,
hard and is the reason why Taichi captures run-time errors and
it contains many working demos that showcase how this software
prints stack back-trace. On Linux, it additionally triggers gdb for
should be used at a high level.
debugging. Once a task finishes (or crashes), Taichi can send emails
to notify the user. Taichi uses Catch2 for testing and we aim to build
Portability. Taichi is cross-platform and supports Linux, OS X and a high-coverage test suite for the library.
Windows. Our aim is to make every project built on Taichi automat-
ically portable by providing an abstraction of the actual operating
systems. This makes open-sourcing projects much easier. Rendering. Though not designed to compete with existing render-
ing frameworks, Taichi provides demonstrative implementations
Reusability. Taichi contains common utilities for developing graph- of popular rendering algorithms, from basic path tracing to mod-
ics projects. Reusing them can significantly speed up development ern ones such as vertex connection and merging [Georgiev et al.
since the time spent on (re)inventing the wheels is saved. 2012]/unified path sampling [Hachisuka et al. 2012] with adaptive
Markov chain Monte Carlo [Šik et al. 2016].
Extensibility. With a plugin-based design, Taichi allows users to
quickly add their own implementations of existing or new interfaces. Simulation. Taichi is shipped with several physical simulators in-
The implementations (“units") will be automatically registered at cluding APIC [Jiang et al. 2015]/FLIP [Zhu and Bridson 2005] liq-
program load time and can be instantiated conveniently with a uid/smoke simulation with a multigrid-preconditioned conjugate
name identifier through the factory. gradient (MGPCG) [McAdams et al. 2010] Poisson solver for projec-
tion. We will also release a high-performance material point method
High-performance. We offer not only efficient (sometimes zero-cost) code, as detailed in section 4.
abstractions at different levels of Taichi, but also performance mon-
itoring tools such as scoped timers and profilers to make perfor- File IO support. Taichi supports reading and writing of popular file
mance engineering convenient. formats for computer graphics, including obj (via tinyobjloader) and
ply, jpg, png, bmp, ttf (via stb_image, stb_image_write, stb_truetype),
In addition, Taichi wraps common utilities provided by ffmpeg. It
3 COMPONENTS
can generate mp4 or gif videos from an array of images, either in
Here we present some representative components that can be memory or on disk.
reused for developing new computer graphics systems.
Scripting. Taichi has a hybrid design with a kernel part in C++14
(De)serialization. The serialization system allows marshaling inter- and an easy-to-use interface in Python 3. Such approach decouples
nal data structures in Taichi into bit streams. A typical application is inputs like demo set-up from the actual kernel computation code.
snapshotting. Many computer graphics programs take a long time pybind11 is used for binding C++ interface for Python.
to execute and it is necessary to take snapshots of current states. In
case of run-time error, the program can continue running from the In addition, we built Taichi based on Intel Thread Building Blocks
latest snapshot after the bug causing crashing is repaired. Other for multithreading. An efficiently vectorized linear algebra library
use cases include inter-program communication when there is no is also developed to suit computer graphics applications better,
shared memory. Our serialization library is header-only and can as mentioned in Figure 2. The Taichi developer team is in charge
be easily integrated into other projects for reading the serialized of maintaining and simplifying the deployment process of all the
Taichi data. aforementioned dependencies of Taichi.

Logging and Formatting. Appropriate logging is an effective way to 4 CASE STUDY: A PHYSICAL SIMULATION PROJECT
diagnose a long-running program. Taichi internally uses spdlog and
fmtlib. The former manages logging and the latter is a modern for- So far, we have used Taichi in five research projects. Published ones
matting library which unifies string formatting in C++ and Python. include [Hu and Fang 2017; Hu et al. 2018]. Here we summarize our
It is safer, easier to use and more portable than its alternatives like experience during the development of the SIGGRAPH 2018 paper
std::cout and printf. “A Moving Least Squares Material Point Method with Displacement
Discontinuity and Two-Way Rigid Body Coupling" [Hu et al. 2018],
Profiling. Though there are mature external profiling tools like gprof abbreviated as “MLSMPM-CPIC". Taichi is used as the backbone
or Intel VTune, using them needs additional manual operations. of the simulator development of this project. Visual results are
Taichi has an integrated scoped profiling system that records the displayed in Figure 3.

2
Fig. 3. Results in the MLSMPM-CPIC paper. Using Taichi as the code base significantly sped up the development of this project.

Productivity. All features mentioned in the previous section proved REFERENCES


useful. In fact, many of them are developed during research projects
Benedikt Bitterli. 2016. Rendering resources. (2016). https://benedikt-
like this one for productivity and future reusability. For example, bitterli.me/resources/.
the serialization system allows us to periodically save simulation David K Buck and Aaron A Collins. 2004. Pov-ray: the persistence of vision raytracer.
states to disk, and conveniently restart from these snapshots. It is (2004). http://www.povray.org/.
Erin Catto. 2011. Box2d: A 2d physics engine for games. (2011).
especially important for long-running simulations which can take Paolo Cignoni, Marco Callieri, Massimiliano Corsini, Matteo Dellepiane, Fabio Ganov-
hours or even days. Performance engineering is guided by the Taichi elli, and Guido Ranzuglia. 2008. Meshlab: an open-source mesh processing tool.. In
profiler, which shows a breakdown of time consumption and has Eurographics Italian Chapter Conference, Vol. 2008. 129–136.
Erwin Coumans et al. 2013. Bullet physics library. Open source: bulletphysics. org 15
led to significantly higher efficiency compared with the previous (2013), 49.
state-of-the-art [Tampubolon et al. 2017]. Pradeep Dubey, Pat Hanrahan, Ronald Fedkiw, Michael Lentine, and Craig Schroeder.
2011. Physbam: Physically based simulation. In ACM SIGGRAPH 2011 Courses.
ACM, 10.
Team Scalability. The python scripting system makes our develop- Andreas Fabri and Sylvain Pion. 2009. CGAL: The computational geometry algorithms
library. In Proceedings of the 17th ACM SIGSPATIAL international conference on
ment especially suitable for team working, because the algorithm de- advances in geographic information systems. ACM, 538–539.
velopment (in C++) and experimental validation (via python scripts) Iliyan Georgiev, Jaroslav Krivánek, Tomas Davidovic, and Philipp Slusallek. 2012. Light
transport simulation with vertex connection and merging. ACM Trans. Graph. 31, 6
are nicely decoupled: for most of the time only one or two core (2012), 192–1.
members need to develop the C++ simulation code, and all team Toshiya Hachisuka, Jacopo Pantaleoni, and Henrik Wann Jensen. 2012. A path space
members can help conducting experiments without getting involved extension for robust light transport simulation. ACM Transactions on Graphics
(TOG) 31, 6 (2012), 191.
into low-level details. Yuanming Hu and Yu Fang. 2017. An asynchronous material point method. In ACM
SIGGRAPH 2017 Posters. ACM, 60.
In general, the team is satisfied with this infrastructure and has Yuanming Hu, Yu Fang, Ziheng Ge, Ziyin Qu, Yixin Zhu, Andre Pradhana Tampubolon,
decided to build future projects on it. We believe the user experience and Chenfanfu Jiang. 2018. A Moving Least Squares Material Point Method with
Displacement Discontinuity and Two-Way Rigid Body Coupling. ACM Transactions
will continue to improve as we battle-test it in more projects. on Graphics (TOG), presented at SIGGRAPH 2018 37, 4 (2018).
Alec Jacobson, Daniele Panozzo, et al. 2013. libigl: A simple C++ geometry processing
library, 2016. (2013). https://github.com/libigl/libigl.
5 FUTURE WORK Wenzel Jakob. 2010. Mitsuba renderer. (2010). http://www.mitsuba-renderer.org.
Chenfanfu Jiang, Craig Schroeder, Andrew Selle, Joseph Teran, and Alexey Stomakhin.
2015. The affine particle-in-cell method. ACM Transactions on Graphics (TOG) 34, 4
Clearly, a lot more engineering efforts are needed to improve Taichi. (2015), 51.
Detailed documentation and better test coverage are two urgent Aleka McAdams, Eftychios Sifakis, and Joseph Teran. 2010. A parallel multigrid Pois-
son solver for fluids simulation on large grids. In Proceedings of the 2010 ACM
tasks. Improving and stabilizing the interface design, support for SIGGRAPH/Eurographics Symposium on Computer Animation. Eurographics Associ-
other open-source softwares like Blender are also very meaningful. ation, 65–74.
Rahul Narain, Armin Samii, Tobias Pfaff, and James F. O’Brien. 2004. ARCSim: Adaptive
Refining and Coarsening Simulator. (2004). http://www.povray.org/.
Hisanari Otsu. 2015. Lightmetrica: A modern, research-oriented renderer. (2015).
ACKNOWLEDGEMENTS http://lightmetrica.org/.
Matt Pharr, Wenzel Jakob, and Greg Humphreys. 2016. Physically based rendering:
The author would like to thank Toshiya Hachisuka and Seiichi From theory to implementation. Morgan Kaufmann.
Martin Šik, Hisanari Otsu, Toshiya Hachisuka, and Jaroslav Křivánek. 2016. Robust light
Koshizuka for hosting his internship at the University of Tokyo, transport simulation via metropolised bidirectional estimators. ACM Transactions
where he developed the initial version of Taichi. Chenfanfu Jiang on Graphics (TOG) 35, 6 (2016), 245.
Fun Shing Sin, Daniel Schroeder, and Jernej Barbič. 2013. Vega: Non-Linear FEM
provided helpful suggestions for developing Taichi and supported its Deformable Object Simulator. In Computer Graphics Forum, Vol. 32. Wiley Online
adoption in several projects. Other developers, especially Yu Fang, Library, 36–48.
also contributed to Taichi. Some demo scenes are from [Bitterli Russell Smith et al. 2005. Open dynamics engine. (2005).
Andre Pradhana Tampubolon, Theodore Gast, Gergely Klár, Chuyuan Fu, Joseph Teran,
2016]. Finally, thank all the open-source software developers for Chenfanfu Jiang, and Ken Museth. 2017. Multi-species simulation of porous sand
making their achievements freely available to everyone. and water mixtures. ACM Transactions on Graphics (TOG), presented at SIGGRAPH

3
2017 36, 4 (2017), 105.
Nils Thuerey and Tobias Pfaff. 2017. MantaFlow. (2017). http://mantaflow.com.
Yongning Zhu and Robert Bridson. 2005. Animating sand as a fluid. ACM Transactions
on Graphics (TOG) 24, 3 (2005), 965–972.

CHANGE LOG
April 24, 2018: initial version.

You might also like