Skip to content

Commit

Permalink
feat: add CUDA support (#44)
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};

    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 authored Sep 22, 2021
1 parent 675e57c commit 705641f
Show file tree
Hide file tree
Showing 10 changed files with 1,670 additions and 446 deletions.
23 changes: 14 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2.1
executors:
default:
machine:
image: ubuntu-1604-cuda-10.1:201909-23
image: ubuntu-2004-cuda-11.2:202103-01
working_directory: ~/gpuci
resource_class: gpu.nvidia.medium

Expand All @@ -18,6 +18,14 @@ restore-cache: &restore-cache
- repo-source-{{ .Branch }}-{{ .Revision }}

commands:
set-env-path:
steps:
- run:
name: Set the PATH env variable
command: |
echo 'export PATH="$HOME:~/.cargo/bin:/usr/local/cuda-11.2/bin:$PATH"' | tee --append $BASH_ENV
source $BASH_ENV
test_target:
parameters:
target:
Expand All @@ -37,10 +45,9 @@ jobs:
steps:
- checkout
- run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- set-env-path
- run: echo $BASH_ENV
- run: echo $HOME
- run: source $BASH_ENV
- run: cargo --version
- run: rustc --version
- run:
Expand Down Expand Up @@ -71,8 +78,7 @@ jobs:
test_x86_64-unknown-linux-gnu:
executor: default
steps:
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- set-env-path
- run: sudo apt-get update -y
- run: apt-cache search opencl
- run: sudo apt install -y ocl-icd-opencl-dev
Expand All @@ -84,6 +90,7 @@ jobs:
steps:
- *restore-workspace
- *restore-cache
- set-env-path
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- run:
Expand All @@ -95,8 +102,7 @@ jobs:
steps:
- *restore-workspace
- *restore-cache
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- set-env-path
- run:
name: Run cargo clippy
command: cargo clippy --all-features --all-targets -- -D warnings
Expand All @@ -106,8 +112,7 @@ jobs:
steps:
- *restore-workspace
- *restore-cache
- run: echo 'export PATH="$HOME:~/.cargo/bin:$PATH"' >> $BASH_ENV
- run: source $BASH_ENV
- set-env-path
- run:
name: Run cargo release build
command: cargo build --release
Expand Down
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ 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 = { package = "fil-rustacuda", version = "0.1.3", optional = true }
Loading

0 comments on commit 705641f

Please sign in to comment.