{"id":16886256,"url":"https://github.com/penguinliong/rtaichi","last_synced_at":"2025-07-10T06:08:11.943Z","repository":{"id":84049462,"uuid":"571123521","full_name":"PENGUINLIONG/rtaichi","owner":"PENGUINLIONG","description":"Taichi Frontend for Rust","archived":false,"fork":false,"pushed_at":"2022-12-04T02:47:42.000Z","size":84,"stargazers_count":47,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-04T05:40:08.270Z","etag":null,"topics":["rust","taichi"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PENGUINLIONG.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-27T08:35:02.000Z","updated_at":"2025-02-18T07:42:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"9b11111a-8aab-4586-b86a-dd18c2496429","html_url":"https://github.com/PENGUINLIONG/rtaichi","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/PENGUINLIONG/rtaichi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PENGUINLIONG%2Frtaichi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PENGUINLIONG%2Frtaichi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PENGUINLIONG%2Frtaichi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PENGUINLIONG%2Frtaichi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PENGUINLIONG","download_url":"https://codeload.github.com/PENGUINLIONG/rtaichi/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PENGUINLIONG%2Frtaichi/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264538158,"owners_count":23624427,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rust","taichi"],"created_at":"2024-10-13T16:38:52.179Z","updated_at":"2025-07-10T06:08:11.678Z","avatar_url":"https://github.com/PENGUINLIONG.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rTaichi: Taichi Frontend for Rust\n\n[![Crate](https://img.shields.io/crates/v/rtaichi)](https://crates.io/crates/rtaichi)\n\nThis repository lies the code for the Taichi Hackathon 2022 project \"rtaichi\".\n\nA project design documentation can be found [here](https://docs.qq.com/pdf/DT1JreFJTTVpha1BZ?u=08133394e0764e43a14513ecd1ef7a89) (Chinese).\n\n## Overview\n\n```rust\nuse rtaichi as ti;\n\n#[ti::kernel]\nfn fractal(t: f32, #[ti(ndim=2)] pixels: NdArray\u003ci32\u003e) {\n    for (i, j) in pixels { // Parallelized over all pixels\n        let c = [-0.8, ti::cos(t) * 0.2];\n        let z = [i / 8 - 1, j / 8 - 0.5] * 2;\n        let iterations = 0;\n        while z.norm() \u003c 20 \u0026\u0026 iterations \u003c 50 {\n            z = [z[0]*z[0] - z[1]*z[1], z[1] * z[0] * 2] + c;\n            iterations += 1;\n        }\n        pixels[(i, j)] = ti::select(1 - iterations * 0.02 \u003c 0.5, 1, 0);\n    }\n}\n\nfn main() {\n    let runtime = ti::init(ti::Arch::Vulkan).unwrap();\n    let arr = runtime.allocate_ndarray::\u003ci32\u003e()\n        .host_access(true)\n        .shape([16, 8])\n        .build()\n        .unwrap();\n\n    let mut t = 0.0;\n    loop {\n        fractal(t, \u0026arr).unwrap();\n        t += 0.3;\n    }\n}\n```\n\nThis project will utilize Rust's [`procedural macro`](https://doc.rust-lang.org/book/ch19-06-macros.html#procedural-macros-for-generating-code-from-attributes) mechanism to translate the Rust syntax tree of an attributed function into AOT python script. Because the lastest Taichi Runtime (TiRT) C-API supports creating AOT modules from Zip archives, we run the script to generate AOT modules in `.tcm`s and keep them in byte array literals. At runtime we use a singleton runtime to keep AOT module objects and help dispatching the kernels with runtime objects from the [`taichi-runtime`](https://crates.io/crates/taichi-runtime) crate.\n\nThe project is subdivided into three parts:\n- `rtaichi_attr`: Proc-macro crate to generate Taichi AOT module and to rewrite Rust syntax tree during compilation.\n- `rtaichi_attr_impl`: Implemenration of Taichi AOT module generation and Rust syntax tree rewrite, with unit tests.\n- `rtaichi`: A meta crate to re-export items in `rtaichi_attr` and `taichi-runtime` with a global thread-local runtime to realize CUDA-like development experience. The only crate for a user to depend on.\n\nBecause the translation is done on a syntactic level, you have access to all the math library calls as in Python. Here are some of the equivalent syntactic structures available in rTaichi.\n\n|Description|Taichi (Python)|rTaichi|\n|-|-|-|\n|Type cast|`ti.i32(x)`|`x as i32`|\n|Vector construction|`ti.Vector([1, 2, 3])`|`[1, 2, 3]`|\n|Qualified function call|`ti.math.acos(x)`|`ti::math::acos(x)`|\n|Member function call|`vec.norm()`|`vec.norm()`|\n|ND-array slicing|`arr[i, j]`|`arr[(i, j)]`|\n\n## How it works\n\nThe powerful [procedural macro](https://doc.rust-lang.org/reference/procedural-macros.html) allows us to implement compiler plugins in publishable crates to rewrite the token stream of a syntactic item. In this case, we used procedural macro attribute to rewrite the definition of a Rust function. We implemented [a simple stack machine](rtaichi_attr_impl\\src\\instr.rs) to translate Rust syntax tree into an internal IR. Then, in [`print.rs`](rtaichi_attr_impl\\src\\print.rs) a temporary Python script is generated to build an AOT module archive. After that, in [`macro_gen.rs`](rtaichi_attr_impl\\src\\macro_gen.rs) we generate a new Rust function body to launch the kernel. Concretely, it does the following:\n\n1. Lazily create a AOT module in the thread-local `ti::Runtime` singleton.\n2. Get the compute graph handle from it.\n3. Bind arguments.\n4. launch the kernel.\n5. Synchronize.\n\nWe enforce synchronization at the end of a launch at the moment but, in the future, it will be a feature that you can opt out.\n\n## Installation\n\nTo work with rTaichi, you need to build Taichi with the following CMake flags:\n\n- `TI_WITH_C_API=ON`\n- `Ti_WITH_VULKAN=ON`\n\nThen, set the following environment variable `TAICHI_C_API_INSTALL_DIR` to the installation directory. For example, if you have your Taichi repository cloned in `C:\\Users\\PENGUINLIONG\\Repositories\\taichi\\`, your C-API install directory might be `C:\\Users\\PENGUINLIONG\\Repositories\\taichi\\_skbuild\\win-amd64-3.8\\cmake-install\\c_api`.\n\nYou can run the [fractal](rtaichi\\examples\\fractal.rs) example to verify your installation.\n\n```bash\ncargo run --example fractal\n```\n\n## Compatibility\n\nCurrently we only tested rTaichi on macOS 12 (aarch64) and Windows 10 (x86_64). On Windows `cargo test` somehow hang on `ti_destroy_runtime` and I haven't figure out why at the moment. It seems some resource is not cleaned up in the right order.\n\n## License\n\nLicensed under either of \u003ca href=\"LICENSE-APACHE\"\u003eApache License, Version\n2.0\u003c/a\u003e or \u003ca href=\"LICENSE-MIT\"\u003eMIT license\u003c/a\u003e at your option.\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in this crate by you, as defined in the Apache-2.0 license, shall\nbe dual licensed as above, without any additional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpenguinliong%2Frtaichi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpenguinliong%2Frtaichi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpenguinliong%2Frtaichi/lists"}