How to share device pointer across CUDA context?

Hi there,

I read this in the CUDA C Programming Guide:

Besides objects such as modules and texture or surface references, each context has its own distinct address space. As a result, CUdeviceptr values from different contexts reference different memory locations.

In my code, I have two CUDA contexts:

  1. The context for video decoding, mapping video frame and converting YUV to RGB.
  2. The CUDA Runtime primary context.

The problem is: How could I pass the device pointer from the 1st CUDA context to the primary context?

Thanks!

a typical method might be to use CUDA IPC. I personally have not tried it between a driver API setup and a runtime API setup. You could probably also try to enable peer context access.

Thanks Robert for the answer!

Actually, I don’t need to IPC nor across GPU devices. What I want to do is just passing a device pointer from a CUDA Context to CUDA primary Context within the same device and process.

You cannot pass a device pointer from one context directly to another with no other mechanism involved, and expect to make proper use of it. Even if they are on the same device/process. The reason for this is similar to the reason that you cannot do that with host processes, in fact the programming guide specifically states that a device context can be thought of as an analog of a host process. Even for the same device/process.

So you’ll need to use some explicit mechanism like one of the ones I indicated. Even for the same device/process.

It might be the case that if you only need to “share” a specific pointer, that overall the IPC method might be a more lightweight method. That is a fairly subjective statement in the absence of specific test conditions; exact situation will matter. However the IPC method and the peer method may both be using a similar “harmonization” mechanism at the lower-level memory management system.

1 Like

Thanks Robert for the suggestions!