Skip to content

Commit

Permalink
feat: add CUDA support
Browse files Browse the repository at this point in the history
This library is now an abstraction for OpenCL and CUDA. It can be compiled
with support for either or both, via the feature flags `opencl` and `cuda`.

There is now a `Device`, which can point to an OpenCL and/or CUDA device.

You should be able to execute OpenCL and CUDA kernels with the same code
without modifications. You would pass in two closures with the same function
body into `Program::run()`.

To create two closures with the correct signature but the same body, you
can use the `program_closures()` helper macro.

So your code would look like this:

use rust_gpu_tools::{cuda, program_closures, Device, GPUError, Program};

    use rust_gpu_tools::{cuda, program_closures, Device, GPUError, Program};

    pub fn main() {
        let closures = program_closures!(|program, data: &[u8]| -> Result<Vec<u8>, GPUError> {
            let input = program.create_buffer_from_slice(data)?;
            let output = unsafe { program.create_buffer::<u8>(128)? };
            let kernel = program.create_kernel("foo", 24, 4)?;
            kernel.arg(&input).arg(&output).run()?;
            let mut out = vec![0u8; 128];
            program.read_into_buffer(&output, 0, &mut out)?;
            Ok(out)
        });

        let cuda_device = Device::all().first().unwrap().cuda_device().unwrap();
        let cuda_kernel_path = std::ffi::CString::new("/some/path").unwrap();
        let cuda_program = cuda::Program::from_binary(cuda_device, &cuda_kernel_path).unwrap();
        let program = Program::Cuda(cuda_program);
        let data = vec![5u8; 128];
        let results = program.run(closures, &data).unwrap();
        println!("results: {:?}", results);
    }
  • Loading branch information
vmx committed Sep 7, 2021
1 parent 675e57c commit 0418b37
Show file tree
Hide file tree
Showing 9 changed files with 1,646 additions and 406 deletions.
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ license = "MIT/Apache-2.0"
repository = "https://github.com/filecoin-project/rust-gpu-tools"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["opencl", "cuda"]
opencl = ["opencl3"]
cuda = ["rustacuda"]

[dependencies]
dirs = "2.0.2"
sha2 = "0.8.2"
thiserror = "1.0.10"
lazy_static = "1.2"
log = "0.4.11"
opencl3 = { version = "0.4.1", default-features = false, features = ["CL_VERSION_1_2"] }
hex = "0.4.3"

opencl3 = { version = "0.4.1", default-features = false, features = ["CL_VERSION_1_2"], optional = true }
#rustacuda = { version = "0.1.3", optional = true }
rustacuda = { git = "https://github.com/vmx/RustaCUDA", branch = "load-from-bytes", optional = true }
Loading

0 comments on commit 0418b37

Please sign in to comment.