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

Binding for struct that has pointer #294

Open
DoHoonKim8 opened this issue Oct 14, 2024 · 0 comments
Open

Binding for struct that has pointer #294

DoHoonKim8 opened this issue Oct 14, 2024 · 0 comments

Comments

@DoHoonKim8
Copy link

Hello, I am using this great library for my work and really appreciate to the author in advance.

I have encountered a problem recently when initializing the struct(that is generated by bindgen) that has pointer as fields in Rust side. For clarity, let's assume the following C structs(that are both binding types).

struct Bar {};

struct Foo {
   struct Bar* bar
};

bindgen would generate the following Rust structs.

#[repr(C)]
pub struct Bar {}

#[repr(C)]
pub struct Foo {
    pub bar: *mut Bar,
}

So now I tried to create Foo struct from Rust side and then pass to CUDA.
So I did the following step,

  1. malloc bar field and obtained CudaSlice<Bar>
  2. and tried to initialize Foo with *mut Bar but I realized the pointer value field is private in CudaSlice

I was thinking about how to overcome this issue, and just tried to add one additional method that interprets pointer value to raw pointer *T in Rust like below.

impl<T> CudaSlice<T> {
    ...

    pub fn cast_to_ptr(&self) -> *const T {
        self.cu_device_ptr as *const T
    }
}

This code seems a bit awkward, because the returned raw pointer actually does not point to T in host memory(it points to the device memory). So, I'm not sure whether this approach makes sense. Is there any other way to overcome this? @coreylowman

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

1 participant