-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add CUDA support #44
Conversation
This PR looks bigger than it is. For easier review:
|
It might also help to see how it will be used. Here's the WIP in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some preliminary thoughts, not through yet
src/cuda/mod.rs
Outdated
E: From<GPUError>, | ||
{ | ||
rustacuda::context::CurrentContext::set_current(&self.context).map_err(Into::into)?; | ||
let result = fun(self); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- what happens to the context if
fun
panics? - what happens if
stream.synchronize
returns an error, so the contex pop doesn't happen? - what happens when
pop
errors?
src/cuda/utils.rs
Outdated
Ok(u64::try_from(memory).expect("Platform must be <= 64-bit")) | ||
} | ||
|
||
/// Get a lost of all devices. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo, lost
Current state of this PR: It needs more work. The current API is unsafe, but not marked as such. rust-gpu-tools should be a save wrapper, which will need changes on the overall API. |
This PR is ready for another round of reviews. Things that changed since last time:
Things missing before this can be merged:
|
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); }
CUDA doesn't support an offset, hence it's removed from rust-gpu-tools
Use the latest CUDA image and cleanup the CI a bit.
This one is ready for another round of review. I'd squash it into a single commit once the review it is done. |
src/cuda/mod.rs
Outdated
@@ -0,0 +1,437 @@ | |||
//! The CUDA specific implementation of a [`Buffer`], [`Device`], [`Program`] and [`Kernel`]. | |||
//! | |||
//! The currenty operation mode is synchronuous, in order to have higher safety gurarantees. All |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
src/cuda/mod.rs
Outdated
|
||
/// Pop the current context. | ||
/// | ||
/// It panics it it cannot as it's an unrecoverable error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
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
andcuda
.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
define_closures()
helper macro.So your code would look like this: