Rust-GPU / Rust-CUDA

Ecosystem of libraries and tools for writing and executing fast GPU code fully in Rust.
Apache License 2.0
3.05k stars 118 forks source link

DeviceCopy vs. Copy #13

Closed tmrlvi closed 2 years ago

tmrlvi commented 2 years ago

In the Kernel ABI documentation we learn we can pass any struct that implements copy.

However, this seems not to work because cust still has assert_impl_devicecopy, that fails on compile (due to generics):

            fn assert_impl_devicecopy<T: $crate::memory::DeviceCopy>(_val: T) {}
            if false {
                $(
                    assert_impl_devicecopy($arg);
                )*
            }

(In functions.rs)

Is this an old remnant, or do we need to use DeviceCopy?

RDambrosio016 commented 2 years ago

DeviceCopy is a CPU thing, limiting kernel params to DeviceCopy does not make a lot of sense because things like &[u8] can be passed through kernels, except the codegen passes them as a pointer and a length. So it isn't quite feasible to limit kernel params to DeviceCopy. Also, to limit it to DeviceCopy, cuda_std would have to depend on cust, which is not possible. In the future, DeviceCopy will be a thing in its own crate, but it probably wont be used on the GPU even then.

tmrlvi commented 2 years ago

In this case, the lines that contain assert_impl_devicecopy should be removed from function.rs. Otherwise, code won't compile.

RDambrosio016 commented 2 years ago

DeviceCopy must be enforced on the CPU but not on the GPU if that makes sense, because things passed through the "ffi" interface are not necessarily the same as they are on the GPU end. Such as references, you cannot make a valid GPU reference without unified memory, but you can pass it using device pointers from the CPU.