bheisler / RustaCUDA

Rusty wrapper for the CUDA Driver API
Apache License 2.0
758 stars 60 forks source link

Support for Vector Types #51

Open jzarnett opened 3 years ago

jzarnett commented 3 years ago

Would you consider adding built-in support for the vector types used in kernels like float4 and the other built-in vector types for char, short, int, long, longlong, float, double listed in the CUDA Documentation?

If I want to use them, I can make my own definition or import them from another library, but I cannot use them in a direct, convenient way because the types don't implement the DeviceCopy trait that would be needed for sending it over to the kernel.

My current workaround looks something like this, because of course I can't add traits to a type that isn't mine.

struct CudaFloat4(float4);
unsafe impl DeviceCopy for CudaFloat4 {}
impl Deref for CudaFloat4 {
    type Target = float4;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

Also, when I am creating one, there's a tiny bit of extra ceremony in that I have to add the 0: float4 { } around the definition, such as:

result.push(CudaFloat4 {
            0: float4 {
                x: rng.gen_range(0.0, SPACE),
                y: rng.gen_range(0.0, SPACE),
                z: rng.gen_range(0.0, SPACE),
                w: rng.gen_range(0.01, 100.0),
            }
        });

Although I'm not an expert in CUDA, I imagine these vector types are likely to be commonly used and it would be convenient for users of the library to have these supported directly in RustaCUDA. I do have a full (working) example with my workaround as above if more is needed. Thanks!

(Please pardon my ignorance if this is implemented somewhere in RustaCUDA and I've failed to find it.)

bheisler commented 3 years ago

Hey, thanks for the suggestion!

Honestly, I don't really use or work on RustaCUDA anymore. It's been a while since I did any GPGPU work.

That does seem like a useful feature though. If you're interested in sending a pull request, I'd be happy to help polish it up and get it merged in.