bheisler / RustaCUDA

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

Droping (module and streams) ... and ... (context and device) #35

Closed saona-raimundo closed 5 years ago

saona-raimundo commented 5 years ago

For Modules and streams, the problem is that DropResult type can not be used with "?". For example, if I add the following line

Stream::drop(stream)?;

after "stream.synchronize()?;" in the example in README.md, I got "the trait std::error::Error is not implemented for '(rustacuda::error::CudaError, rustacuda::prelude::Stream)' " I don't know how to implement this yet, sorry, Rust beginner. It is easy right?

In the other hand, when I want to finish the program and explicitly exit a context, it tries to deallocate cuda memory after dropping the context.

For example, if I add these lines:

match Context::drop(context) {
Ok(()) => println!("Successfully destroyed"),
Err((e, _ctx)) => {
    println!("Failed to destroy context: {:?}", e);
    // Do something with ctx
    },
}

at the end of the example in README.md and it does compile, and it does run, but fails at the very end: 1) context is dropped and "Successfully destroyed" is printed, but 2) then tries to deallocate some memory in CUDA (I assume some routine at the end of the program).

In terms of device, there is no method to drop a device. There should be one, right?

saona-raimundo commented 5 years ago

@bheisler , could you take a look at this, please?

I know initialization of cuda device and context can take time in applications, and I don't know when I would like to drop these yet, but, while learning CUDA, it seems to be good practice to drop them when you are finish.

bheisler commented 5 years ago

Well, no, you can't use the ? operator with DropResult. For most programs, you can just let your CUDA objects fall out of scope and let the compiler clean up afterwards. I would strongly recommend that you should learn about how the Drop trait works and how Rust uses it before using RustaCUDA if you haven't already.

Now, having said that, dropping resources in CUDA could potentially fail, because it can return error codes from previous failed asynchronous actions. I've never really understood what CUDA expects the program to do when this happens, but it could happen. Some programmers might want to handle these failures rather than panicking, so the explicit drop functions are available for that case. If you want to handle errors yourself, I assume that you know what you want your code to do with them.

saona-raimundo commented 5 years ago

Thanks!!

And sorry, I hadn't got to that chapter in The Rust book, my bad!