Problems when mix using CUDA runtime API and CUDA driver API

After I successfully called “cudaSetDevice( 0 )”, I called “cuLinkCreate( 6, options, values, &linkState )”.
Then I got the error string " context is destroyed ".

I’m not very clear about the management of contexts by cud runtime. When I call cudaSetDevice, a context will be created. But when will the context be destroyed? Are the runtime API and the driver API using the same context stack?

As the fallowing call order:

cudaSetDevice( 0 );
cudaSetDevice( 1 );
cudaSetDevice( 0 );

The first will create a context. When the second is called, will the first context will be destroyed? And when the third is called, will another context bind to device 0 be created?

No, nothing in the sequences you have shown destroys a cuda context.

driver API and runtime API do not necessarily share contexts, but they can.

create the context first (and make it current) in the driver API (before doing any runtime API calls) then use cudaSetDevice for the device that you just created the driver API context on. The runtime API should then pick up and use the driver API context.

http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#interoperability-between-runtime-and-driver-apis

There is quite a bit of discussion and sample usage of cuda contexts in the programming guide. Just search on “context”.