Running cuda-enabled code from a separate thread


I am using Qt to build an interface to launch some of my cuda-enabled libraries from. As part of loading the GUI, I also have a gpuInit(); function called, so that this is not done when my cuda libraries are run so they are as responsive as possible.

As part of my code, my cuda enabled code is called in a differnt thread (only one thread actually making cuda calls), but this thread is different than the thread that called gpuInit();

Does this cause me to have two different contexts or something? How can I resolve this? Right now, my code is crashing on a cudaFree() call in my cuda code.


I mean to say cuInit(0) - my actual code that the first threa calls is the following:

CUresult res;

CUdevice dev;

CUcontext ctx;



cuCtxCreate(ctx, 0, dev);

After somre more testing, It seems like if I try to create the context like so in the host thread, and then try to

CUcontext ctx;

    CUresult res = cuCtxPopCurrent(&ctx);

In the worker thread, ctx never gets the right value. Does anyone know how you’re supposed to pass the context to the thread?

You should pop the context in the host thread where it is created, and then push it in the worker thread.

are you using the runtime? if you’re using the runtime, just use cudaSetDevice().

Tera, your suggestion seemed to work!