I am trying to use the same device pointer (to pass it into GPU kernels) on two different CPU threads so that I can have one thread running glutMainLoop. I have tried using the driver API context management functions (cuInit, cuDeviceGet, cuCtxCreate, then cuCtxPushCurrent, cuCtxPopCurrent when each thread runs) but have not yet gotten it to work. Should this method work for using the same device pointer across two threads, or is there another way to get this to work? Thanks.
I am trying to use the same device pointer (to pass it into GPU kernels) on two different CPU threads so that I can have one thread running glutMainLoop. I have tried using the driver API context management functions (cuInit, cuDeviceGet, cuCtxCreate, then cuCtxPushCurrent, cuCtxPopCurrent when each thread runs) but have not yet gotten it to work. Should this method work for using the same device pointer across two threads, or is there another way to get this to work? Thanks.
It is quite tricky to use cuInit, cuDeviceGet, cuCtxCreate, cuCtxPushCurrent, cuCtxPopCurrent, etc, but it does work.
You have to make sure that you share a single context across both threads. This is probably much more complicated when you are using openGL. I would take a look at this:
CUdevice device;
checkError(driver::cuDeviceGet(&device, id));
GLXContext openglContext = glXGetCurrentContext();
if(openglContext != 0)
{
checkError(driver::cuGLCtxCreate(&_context, flags, device));
_opengl = true;
}
else
{
checkError(driver::cuCtxCreate(&_context, flags, device));
}
The idea here is to create a CUDA context from an existing openGL context if it exists or to fall back on a regular context if it doesn’t.
It is quite tricky to use cuInit, cuDeviceGet, cuCtxCreate, cuCtxPushCurrent, cuCtxPopCurrent, etc, but it does work.
You have to make sure that you share a single context across both threads. This is probably much more complicated when you are using openGL. I would take a look at this:
CUdevice device;
checkError(driver::cuDeviceGet(&device, id));
GLXContext openglContext = glXGetCurrentContext();
if(openglContext != 0)
{
checkError(driver::cuGLCtxCreate(&_context, flags, device));
_opengl = true;
}
else
{
checkError(driver::cuCtxCreate(&_context, flags, device));
}
The idea here is to create a CUDA context from an existing openGL context if it exists or to fall back on a regular context if it doesn’t.
It is quite tricky to use cuInit, cuDeviceGet, cuCtxCreate, cuCtxPushCurrent, cuCtxPopCurrent, etc, but it does work.
You have to make sure that you share a single context across both threads. This is probably much more complicated when you are using openGL. I would take a look at this:
CUdevice device;
checkError(driver::cuDeviceGet(&device, id));
GLXContext openglContext = glXGetCurrentContext();
if(openglContext != 0)
{
checkError(driver::cuGLCtxCreate(&_context, flags, device));
_opengl = true;
}
else
{
checkError(driver::cuCtxCreate(&_context, flags, device));
}
The idea here is to create a CUDA context from an existing openGL context if it exists or to fall back on a regular context if it doesn’t.