bheisler / RustaCUDA

Rusty wrapper for the CUDA Driver API
Apache License 2.0
765 stars 58 forks source link

Context explanation and how to use rustacuda::quick_init()? #34

Closed saona-raimundo closed 5 years ago

saona-raimundo commented 5 years ago

I am trying to figure out the context declaration dynamic and some explanation in the documentation would be great.

1) In the main example (in README.md), when compiling the Rust code, context seems to be never used and the compiler suggest you to replace it for "_context", which also works. I assume it is called in the device somewhere?

2) rustacuda::quick_init() has no example in the documentation, so after a while I discovered that "let context = rustacuda::quick_init()?;" is how to use it.

3) It seems that the context has to be defined as a variable in main(), but I still don't know why :/

Thanks!!

bheisler commented 5 years ago

Hey, thanks for trying RustaCUDA.

The context simply has to exist. When it's created, it's placed on a stack of contexts in the device driver and all subsequent CUDA API calls use the top context on that stack.

If you don't assign the context to a variable, though, it will be immediately dropped as soon as the quick_init() function ends, and then there will be no context for the CUDA API to use. So, yes, let _context = rustacuda::quick_init(); is the right way to go, and it must be placed somewhere before you make any other calls to the CUDA API, but you also have to ensure that _context isn't destroyed until after you're finished. Placing that call early on in main is a quick and easy way to do that.

saona-raimundo commented 5 years ago

Thanks a lot!! :D