Sharing device pointers between different threads on the same GPU

Is there any whatsoever to leave behind a device pointer allocated on the GPU to be used later by a different thread accessing the same GPU?

I dont think there is a way… Contexts are not shareable as far as I understand.

Device pointers mapping to pinned memory can be shared, I believe,… Not very sure though. REad the zero-copy thing on the progmming guide…

No, I don’ think you can do that ! i think the contexts are not sharable…

I hope this is what u mean ?

yup beat me by a min :rolleyes:

:-)

Could you keep the GPU pointer if you detached the thread context that it was declared in and then attached that context to another host thread? I’m still a little baffled by some of the context stuff…

It’s a bit clunky, but it’s definitely possible, maybe easier with the driver api (the SDK nvcuvid library has a useful C++ wrapper for this - see CCtxAutoLock in cuviddec.h). If you pair the CUcontext with a critical section, you can do something like:

typedef struct _cuda_lock
{
CUcontext ctx;
CRITICAL_SECTION csLock;
} cuda_lock;

void cudaEnter(cuda_lock *lock)
{
EnterCriticalSection(lock->csLock);
cuCtxPushCurrent(lock->ctx);
}

void cudaLeave(cuda_lock *lock)
{
CUcontext tmp;
cuCtxPopCurrent(&tmp);
LeaveCriticalSection(lock->csLock);
}

(can be easily modified with a count to allow multiple locks by the same thread, along with a auto-lock style C++ wrapper)

it is possible (been using it for a while now) but only with the driver api. You can then pop and push a context from thread to thread. your other option is to have a “cuda” thread meaning a special thread that dose all the work with the gpu and all the other threads use it (also have a version working like that :))