bheisler / RustaCUDA

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

Support Creation and Management of CUDA Arrays in RustaCUDA #22

Closed AndrewGaspar closed 5 years ago

AndrewGaspar commented 5 years ago

Work towards addressing #10.

See the commit message below for precise list of what's currently implemented. This currently just implements the creation of "generic" CUDA Arrays. Before we merge,I'd like to add typed CUDA arrays that support copy-from-host using typed slices and (as an optional feature) ndarrays.

If you could, please look at the ArrayObject::from_descriptor routine. I try to exhaustively validate the preconditions of cuArray3DCreate_v2 (documentation here). Perhaps controversially, it panics when the preconditions are violated. The nice thing here is the user gets a pretty specific error message (did not set value "foo" to a valid value). The downside is that the user cannot handle the error. IMO, all of the precondition violations are essentially "unrecoverable". i.e., if you fall into this case, it's most likely a code bug and not something the user code is probably capable of handling anyway.

I did validate that all of these conditions are caught by CUDA and returned as CudaError::InvalidValue, so we could defer to CUDA to validate the parameters, though the error message is vague.

Initial commit message: The primary new type is ArrayObject, which represents a generic array with up to 3-dimensions.

An ArrayObject has a set of dimensions, an ArrayFormat, a number of channels (1, 2, or 4), and a set of ArrayObjectFlags

Adds tests that assert correct ArrayObject values are accepted and that bad ones are rejected with a panic.

bheisler commented 5 years ago

Awesome, thanks!

It might take me a while to review this in detail, but one idea did strike me as I read the description - you could use debug_assert! to enforce preconditions. That would let you give a nice error message while avoiding the overhead of the panic machinery at runtime. Come to think of it, I could probably use that elsewhere in this crate.

bheisler commented 5 years ago

Is this still in-progress?

AndrewGaspar commented 5 years ago

Yes, it is. I was spinning my wheels on figuring out the statically typed arrays and how they intersect with texture and surface binding. I think what I'll do is close out on the generic array object interface and we can do the statically typed CUDA Arrays + ndarray copy support as a separate issue and PR.

bheisler commented 5 years ago

Tests pass and the code and documentation looks good. Thanks!