Skip to content
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

Feature request unsafe CudaViewMut & CudaViewMut constructors #295

Open
audioXD opened this issue Oct 19, 2024 · 1 comment
Open

Feature request unsafe CudaViewMut & CudaViewMut constructors #295

audioXD opened this issue Oct 19, 2024 · 1 comment

Comments

@audioXD
Copy link

audioXD commented Oct 19, 2024

I would really like it if one could construct a CudaViewMut and CudaViewMut

Like how one does for std::slice::from_raw_parts

pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]

Use case is creating a non owning safe View type.

Implementations

impl<'a, T> CudaViewMut<'a, T> {
    pub const unsafe fn from_raw_parts(ptr: sys::CUdeviceptr, len: usize) -> Self {
        Self {
            ptr,
            len,
            marker: PhantomData,
        }
    }
}

impl<'a, T> CudaView<'a, T> {
    pub const unsafe fn from_raw_parts(ptr: sys::CUdeviceptr, len: usize) -> Self {
        Self {
            ptr,
            len,
            marker: PhantomData,
        }
    }
}

Please define in the docs if the len is in bytes or if its in elements.

@CatsAreFluffy
Copy link
Contributor

Until those functions are added, you can emulate them using upgrade_device_ptr:

pub unsafe fn from_raw_parts<'a, T>(
    device: Arc<CudaDevice>,
    ptr: sys::CUdeviceptr,
    len: usize,
) -> CudaView<'a, T> {
    let slice = device.upgrade_device_ptr(ptr, len);
    let temp_view = slice.slice(..);
    // extend lifetime
    let view = std::mem::transmute::<CudaView<'_, T>, CudaView<'a, T>>(temp_view);
    // don't try to free the view
    slice.leak();
    view
}

This also requires a CudaDevice, but that shouldn't be a problem for most usecases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants