.NET & Cuda : cudaSetDevice fails with cudaErrorSetOnActiveProcess

Hi,

i’m porting a imaging library to Cuda 2.1 , the library is P/Invoked by a C# wrapper,
if i call cudaSetDevice it fails with cudaErrorSetOnActiveProcess.
With cuda 1.1 it works well.
Can somebody help me please?

cudaSetDevice was changed in 2.1 so if you call it on a thread with an already active context it returns an error.

You forgot to tell the solution you told me ;)

You can see if cuCtxGetDevice returns CUDA_SUCCESS. If it does, the thread already has an active context, and you should not use cudaSetDevice anymore.

I encountered the same problems calling CUDA more than once from matlab.

Thanks Riedijk, but which lib i have to link to use cuCtxGetDevice from 2.1?
Can’t find something from Mr. Google :)
If sucessed linking, how can i specify the device i would like to use?

libcuda.so/nvcuda.dll, and you don’t actually use the results except to check if it returned CUDA_SUCCESS. if it did, you already have a context and don’t need to call cudaSetDevice.

FYI

Creating a new thread for calling cuda code solves this issue in .net

CS:
public void InitCuda()
{
int rt=-1000;
Thread thread = new Thread(delegate() {rt = _initCuda();}); //your p/invoke
thread.Start();
thread.Join();
if(rt!=0) throw new Exception(“InitCuda:”+rt);
}