{"id":14992032,"url":"https://github.com/Lyn-liyuan/ndarray-cuda-matmul","last_synced_at":"2025-09-25T14:30:43.405Z","repository":{"id":249202422,"uuid":"793239627","full_name":"Lyn-liyuan/ndarray-cuda-matmul","owner":"Lyn-liyuan","description":"a high-performance computing solution designed to accelerate matrix operations using Nvidia's CUDA technology with Rust's ndarray data structure.","archived":false,"fork":false,"pushed_at":"2024-05-18T08:16:25.000Z","size":180,"stargazers_count":24,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-09-25T16:09:12.119Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lyn-liyuan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2024-04-28T19:57:27.000Z","updated_at":"2024-09-05T22:58:37.000Z","dependencies_parsed_at":"2024-07-19T08:16:29.812Z","dependency_job_id":"e68c1b93-97b0-4942-9874-57d292483aa2","html_url":"https://github.com/Lyn-liyuan/ndarray-cuda-matmul","commit_stats":null,"previous_names":["lyn-liyuan/ndarray-cuda-matmul"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lyn-liyuan%2Fndarray-cuda-matmul","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lyn-liyuan%2Fndarray-cuda-matmul/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lyn-liyuan%2Fndarray-cuda-matmul/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lyn-liyuan%2Fndarray-cuda-matmul/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lyn-liyuan","download_url":"https://codeload.github.com/Lyn-liyuan/ndarray-cuda-matmul/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234200157,"owners_count":18795139,"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":[],"created_at":"2024-09-24T15:00:39.975Z","updated_at":"2025-09-25T14:30:43.004Z","avatar_url":"https://github.com/Lyn-liyuan.png","language":"Rust","funding_links":[],"categories":["GPU Programming"],"sub_categories":[],"readme":"# Ndarray CUDA Matrix Operations\n\nWelcome to the Ndarray CUDA Matrix Operations library, a high-performance computing solution designed to accelerate matrix operations using Nvidia's CUDA technology with Rust's ndarray data structure. This library leverages the powerful cuBLAS library to perform efficient matrix multiplications on compatible Nvidia GPUs.\n\nFor ideas and methods of creating this library, please refer to:\n\n[Improving ndrray matrix computing performance with CUDA](https://medium.com/@yuanli13/improving-ndrray-matrix-computing-performance-with-cuda-cce63a749643)\n\n## Features\n- Seamless integration with Rust's ndarray crate.\n- High-performance matrix operations utilizing CUDA.\n- Support for one-dimensional and two-dimensional arrays.\n- Automatic memory management between host and device.\n- Simple and intuitive API mirroring that of ndarray.\n\n## Prerequisites\n\nTo use this library, you will need:\n- g++ 7 or above\n- rustc 1.77.2\n- cuda 10 or above\n\n## Usage\n\nFirst, ensure that you have initialized the CUDA environment by calling init_cublas() before any matrix operations, and call destory_cublas() to clean up resources upon completion:\n\n```Rust\nextern crate ndarray_cuda_matmul;\n\nuse ndarray_cuda_matmul::{init_cublas, destory_cublas};\n\nfn main() {\n    // Initialize cublas context\n    init_cublas();\n\n    // Your matrix operations here\n\n    // Clean up cublas context\n    destory_cublas();\n}\n```\n\nTo perform matrix multiplication, use the cuda_dot method provided by the trait CudaDot implemented for ndarray’s ArrayBase:\n\n```Rust\nuse ndarray::Array;\nuse ndarray_cuda_matmul::CudaDot;\n\nlet a = Array::from_shape_vec((m, k), vec![...]).unwrap();\nlet b = Array::from_shape_vec((k, n), vec![...]).unwrap();\n\nlet result = a.cuda_dot(\u0026b);\n\n```\nHere m, n, and k represent the dimensions of the matrices, and vec![...] should be replaced with your actual data.\n\nUsing the method of first copying the matrix into GPU memory, here's a code example\n\n```Rust\nlet a = array![[1.0_f32, 2.0_f32, 3.0_f32], [4.0_f32, 5.0_f32, 6.0_f32]];\nlet b = array![[1.0_f32, 2.0_f32], [3.0_f32, 4.0_f32], [5.0_f32, 6.0_f32]];\nlet c = array![[1.0f32,1.0f32],[1.0f32,1.0f32]];\ninit_cublas();\nlet out = a.to_device().dot(\u0026b.to_device()).dot(\u0026c.to_device()).to_host();\ndestory_cublas();\n```\nMatrix-scalar multiplication code example:\n\n```Rust\n    init_cublas();\n    let out = a\n        .to_device()\n        .dot(\u0026b.to_device())\n        .dot(\u0026c.to_device())\n        .mul_scalar(2.0_f32)\n        .to_host();\n    destory_cublas();\n```\n\nMatrix inversion code example:\n\n```Rust\nlet a = array![[1.0_f32, 2.0_f32, 3.0_f32, 4.0_f32],\n                [2.0_f32, 3.0_f32, 1.0_f32, 2.0_f32],\n                [1.0_f32, 1.0_f32, 1.0_f32, -1.0_f32],\n                [1.0_f32, 0.0_f32, -2.0_f32, -6.0_f32],\n            ];\n    \ninit_cublas();\nlet out = a.to_device().inv().to_host();\ndestory_cublas();\n```\nUsing run macro can simplify the code and write it like a mathematical expression. The following is an example of using run macro.\n\n```Rust\nfn least_squares_method()\n{\n    let x = array![[1f32, 1f32], [1f32, 2f32], [1f32, 3f32], [1f32, 4f32]];\n    let y = array![[6f32], [5f32], [7f32], [10f32]];\n    let bate_hat = run!(x,y =\u003e {\n        let x_t = x.t();\n        x_t.dot(x).inv().dot(\u0026x_t).dot(y)\n    }).to_host();\n    println!(\"{:?}\",bate_hat);\n}\n```\nThe example code implements the least squares method using the code that is most similar to the mathematical expression.\n\n$$(X^TX)^{-1}X^Ty$$\n\nV.S.\n```Rust\nx_t.dot(x).inv().dot(\u0026x_t).dot(y)\n```\n\n## Safety and Error Handling\n\nThis library uses unsafe code to interface with CUDA functions. It includes error handling that checks the status of each CUDA and cuBLAS call, ensuring that any errors are handled gracefully and reported appropriately.\n\n## Performance\n\nThe performance test was conducted using the following code, comparing the dot method provided by ndarray-linalg\n\n```Rust\nfn dot_with_ndarry() {\n    let a = Array::from_elem((H_SIZE, H_SIZE), 1.0_f32);\n    let b = Array::from_elem((H_SIZE, V_SIZE), 1.0_f32);\n    let start = Instant::now();\n    for _ in 0..100 {\n        let _ = a.dot(\u0026b);\n    }\n    println!(\"ndarray dot elapsed: {:.2?}\", start.elapsed());\n}\n\nfn dot_with_cuda() {\n    let a = Array::from_elem((H_SIZE, H_SIZE), 1.0_f32);\n    let b = Array::from_elem((H_SIZE, V_SIZE), 1.0_f32);\n    let start = Instant::now();\n    for _ in 0..100 {\n        let _ = a.cuda_dot(\u0026b);\n    }\n    println!(\"matmul elapsed: {:.2?}\", start.elapsed());\n}\n```\nComparing result:\n\n\n|Rows|columns|run times|ndarra-linalg|cuda_dot|\n|----|----|----|----|----|\n|64|64|100|2.27ms|9.89ms|\n|128|80|100|11.37ms|10.66ms|\n|768|128|100|438.01ms|57.86ms|\n|2048|1000|100|22800ms|323.30ms|\n\n## Contribution\n\nContributions to this library are welcome! Whether it's through reporting issues, proposing new features, improving documentation, or submitting pull requests, all forms of contribution are encouraged.\n\n## License\n\nThis library is distributed under the MIT license. \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLyn-liyuan%2Fndarray-cuda-matmul","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLyn-liyuan%2Fndarray-cuda-matmul","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLyn-liyuan%2Fndarray-cuda-matmul/lists"}