{"id":16837322,"url":"https://github.com/bheisler/rustacuda","last_synced_at":"2025-04-14T11:22:37.467Z","repository":{"id":33611087,"uuid":"158104357","full_name":"bheisler/RustaCUDA","owner":"bheisler","description":"Rusty wrapper for the CUDA Driver API","archived":false,"fork":false,"pushed_at":"2022-05-04T22:24:17.000Z","size":1254,"stargazers_count":777,"open_issues_count":32,"forks_count":57,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-07T02:19:22.602Z","etag":null,"topics":["cuda","cuda-api","gpu","rust"],"latest_commit_sha":null,"homepage":null,"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/bheisler.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-18T16:36:54.000Z","updated_at":"2025-03-31T00:04:39.000Z","dependencies_parsed_at":"2022-08-07T22:15:35.288Z","dependency_job_id":null,"html_url":"https://github.com/bheisler/RustaCUDA","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bheisler%2FRustaCUDA","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bheisler%2FRustaCUDA/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bheisler%2FRustaCUDA/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bheisler%2FRustaCUDA/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bheisler","download_url":"https://codeload.github.com/bheisler/RustaCUDA/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248869167,"owners_count":21174828,"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":["cuda","cuda-api","gpu","rust"],"created_at":"2024-10-13T12:16:58.148Z","updated_at":"2025-04-14T11:22:37.423Z","avatar_url":"https://github.com/bheisler.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eRustaCUDA\u003c/h1\u003e\r\n\r\n\u003cdiv align=\"center\"\u003eHigh-level Interface to \u003ca href=\"https://developer.nvidia.com/cuda-zone\"\u003eNVIDIA® CUDA™ Driver API\u003c/a\u003e in Rust\u003c/div\u003e\r\n\r\n\u003cdiv align=\"center\"\u003e\r\n    \u003ca href=\"https://bheisler.github.io/RustaCUDA/rustacuda/index.html\"\u003eAPI Documentation (master branch)\u003c/a\u003e\r\n\u003c/div\u003e\r\n\r\n\u003cdiv align=\"center\"\u003e\r\n\t\u003ca href=\"https://travis-ci.org/bheisler/RustaCUDA\"\u003e\r\n        \u003cimg src=\"https://travis-ci.org/bheisler/RustaCUDA.svg?branch=master\" alt=\"Travis-CI\"\u003e\r\n    \u003c/a\u003e\r\n\u003c/div\u003e\r\n\r\nRustaCUDA helps you bring GPU-acceleration to your projects by providing a flexible, easy-to-use\r\ninterface to the CUDA GPU computing toolkit. RustaCUDA makes it easy to manage GPU memory,\r\ntransfer data to and from the GPU, and load and launch compute kernels written in any language.\r\n\r\n## Table of Contents\r\n- [Table of Contents](#table-of-contents)\r\n  - [Goals](#goals)\r\n  - [Roadmap](#roadmap)\r\n  - [Quickstart](#quickstart)\r\n  - [Contributing](#contributing)\r\n  - [Maintenance](#maintenance)\r\n  - [License](#license)\r\n  - [Requirements](#requirements)\r\n  - [Related Projects](#related-projects)\r\n\r\n### Goals\r\n\r\n The primary design goals are:\r\n\r\n - __High-Level__: Using RustaCUDA should feel familiar and intuitive for Rust programmers.\r\n - __Easy-to-Use__: RustaCUDA should be well-documented and well-designed enough to help novice GPU programmers get started, while not limiting more experienced folks too much.\r\n - __Safe__: Many aspects of GPU-accelerated computing are difficult to reconcile with Rust's safety guarantees, but RustaCUDA should provide the safest interface that is reasonably practical.\r\n - __Fast__: RustaCUDA should aim to be as fast as possible, where it doesn't conflict with the other goals.\r\n\r\nRustaCUDA is intended to provide a programmer-friendly library for working with the host-side CUDA\r\nDriver API. It is not intended to assist in compiling Rust code to CUDA kernels (though see\r\n[rust-ptx-builder](https://github.com/denzp/rust-ptx-builder) for that) or to provide device-side\r\nutilities to be used within the kernels themselves.\r\n\r\nRustaCUDA is deliberately agnostic about how the kernels work or how they were compiled. This makes\r\nit possible to (for example) use C kernels compiled with `nvcc`.\r\n\r\n### Roadmap\r\n\r\nRustaCUDA currently supports a minimum viable subset of the CUDA API (essentially, the minimum\r\nnecessary to manage memory and launch basic kernels). This does not include:\r\n\r\n- Any asynchronous operation aside from kernel launches\r\n- Access to CUDA 1/2/3D arrays and texture memory\r\n- Multi-GPU support\r\n- Runtime linking\r\n- CUDA Graphs\r\n- And more!\r\n\r\nThese additional features will be developed later, as time permits and as necessary. If you need a\r\nfeature that is not yet supported, consider submitting a pull request!\r\n\r\n### Quickstart\r\n\r\nBefore using RustaCUDA, you must install the CUDA development libraries for your system. Version\r\n8.0 or newer is required. You must also have a CUDA-capable GPU installed with the appropriate\r\ndrivers.\r\n\r\nFirst, set the `CUDA_LIBRARY_PATH` environment variable to the location of your CUDA headers:\r\n\r\n```text\r\nexport CUDA_LIBRARY_PATH=\"C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v9.1\\lib\\x64\"\r\n```\r\n\r\nSome Ubuntu users have encountered linker errors when using CUDA_LIBRARY_PATH. If you see an error\r\nlike this:\r\n\r\n```text\r\n  = note: /usr/bin/ld: cannot find -lcudart                                                              \r\n          /usr/bin/ld: cannot find -lcublas                                                              \r\n          collect2: error: ld returned 1 exit status \r\n```\r\n\r\nUsing `LIBRARY_PATH` instead of `CUDA_LIBRARY_PATH` seems to help.\r\n\r\nNow, to start building a basic CUDA crate. Add the following to your `Cargo.toml`:\r\n\r\n```yaml\r\n[dependencies]\r\nrustacuda = \"0.1\"\r\nrustacuda_core = \"0.1\"\r\nrustacuda_derive = \"0.1\"\r\n```\r\n\r\nAnd this to your crate root:\r\n\r\n```rust\r\n#[macro_use]\r\nextern crate rustacuda;\r\n\r\n#[macro_use]\r\nextern crate rustacuda_derive;\r\nextern crate rustacuda_core;\r\n```\r\n\r\nNext, download the `resources/add.ptx` file from the RustaCUDA repository and place it in\r\nthe resources directory for your application.\r\n\r\nThe *examples/* directory contains sample code that helps getting started. \r\nTo execute the most simple example, (adding two numbers on GPU),\r\nplace this code to your `main.rs` file.\r\n\r\n```rust\r\n#[macro_use]\r\nextern crate rustacuda;\r\n\r\nuse rustacuda::prelude::*;\r\nuse rustacuda::memory::DeviceBox;\r\nuse std::error::Error;\r\nuse std::ffi::CString;\r\n\r\nfn main() -\u003e Result\u003c(), Box\u003cdyn Error\u003e\u003e {\r\n    // Initialize the CUDA API\r\n    rustacuda::init(CudaFlags::empty())?;\r\n    \r\n    // Get the first device\r\n    let device = Device::get_device(0)?;\r\n\r\n    // Create a context associated to this device\r\n    let context = Context::create_and_push(\r\n        ContextFlags::MAP_HOST | ContextFlags::SCHED_AUTO, device)?;\r\n\r\n    // Load the module containing the function we want to call\r\n    let module_data = CString::new(include_str!(\"../resources/add.ptx\"))?;\r\n    let module = Module::load_from_string(\u0026module_data)?;\r\n\r\n    // Create a stream to submit work to\r\n    let stream = Stream::new(StreamFlags::NON_BLOCKING, None)?;\r\n\r\n    // Allocate space on the device and copy numbers to it.\r\n    let mut x = DeviceBox::new(\u002610.0f32)?;\r\n    let mut y = DeviceBox::new(\u002620.0f32)?;\r\n    let mut result = DeviceBox::new(\u00260.0f32)?;\r\n\r\n    // Launching kernels is unsafe since Rust can't enforce safety - think of kernel launches\r\n    // as a foreign-function call. In this case, it is - this kernel is written in CUDA C.\r\n    unsafe {\r\n        // Launch the `sum` function with one block containing one thread on the given stream.\r\n        launch!(module.sum\u003c\u003c\u003c1, 1, 0, stream\u003e\u003e\u003e(\r\n            x.as_device_ptr(),\r\n            y.as_device_ptr(),\r\n            result.as_device_ptr(),\r\n            1 // Length\r\n        ))?;\r\n    }\r\n\r\n    // The kernel launch is asynchronous, so we wait for the kernel to finish executing\r\n    stream.synchronize()?;\r\n\r\n    // Copy the result back to the host\r\n    let mut result_host = 0.0f32;\r\n    result.copy_to(\u0026mut result_host)?;\r\n    \r\n    println!(\"Sum is {}\", result_host);\r\n\r\n    Ok(())\r\n}\r\n```\r\n\r\nIf everything is working, you should be able to run `cargo run` and see the output:\r\n\r\n```text\r\nSum is 30.0\r\n```\r\n\r\n### Contributing\r\n\r\nThanks for your interest! Contributions are welcome.\r\n\r\nIssues, feature requests, questions and bug reports should be reported via the issue tracker above.\r\nIn particular, because RustaCUDA aims to be well-documented, please report anything you find\r\nconfusing or incorrect in the documentation.\r\n\r\nCode or documentation improvements in the form of pull requests are also welcome. Please file or\r\ncomment on an issue to allow for discussion before doing a lot of work, though.\r\n\r\nFor more details, see the [CONTRIBUTING.md file](https://github.com/bheisler/rustaCUDA/blob/master/CONTRIBUTING.md).\r\n\r\n### Maintenance\r\n\r\nRustaCUDA is currently maintained by Brook Heisler (@bheisler).\r\n\r\n### License\r\n\r\nRustaCUDA is dual-licensed under the Apache 2.0 license and the MIT license.\r\n\r\n### Requirements\r\n\r\nRustaCUDA requires at least CUDA version 8 to be installed.\r\n\r\n### Related Projects\r\n\r\n- [accel](https://github.com/rust-accel/accel) is a full CUDA computing framework. Thanks to accel for creating and maintaining the `cuda-sys` FFI wrapper library.\r\n- [rust-ptx-builder](https://github.com/denzp/rust-ptx-builder) is a `build.rs` helper library which makes it easy to compile Rust crates into CUDA kernels.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbheisler%2Frustacuda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbheisler%2Frustacuda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbheisler%2Frustacuda/lists"}