# cudaGraphicsMapResources() and cuCtxCreate() incompatible?

**URL:** <https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920>\
**Category:** CUDA Programming and Performance\
**Created:** [April 6, 2018, 7:34pm UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920 "2018-04-06T19:34:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SanBase](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SanBase](https://forums.developer.nvidia.com/u/SanBase)\
**Post date:** [April 6, 2018, 7:34pm UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/1 "2018-04-06T19:34:51Z")

</div>

Hi! I’m trying to create an application that generates DX11(actually DX12) texture using CUDA.  
The build-in version of the cuda program works, but I need to read and compile PTX files on the fly. For this, I should create a cuda context by cuCtxCreate(), because the functions cuModuleLoad() and cuModuleGetFunction() cannot work without it. But in this case, the application crashing on cudaGraphicsMapResources().

Diagnostic: “getLastCudaError() CUDA error : cudaGraphicsMapResources(3) failed : (33) invalid resource handle.”

The resource was created by DX11 before CUDA was initialized, that’s true, but the app perfectly works without cuCtxCreat. As I can guess, when I run cuCtxCreate(), the cuda starts new process and my DX11 resources and cuda modules are in different address spaces. So, what should I do in this situation?

Some technical details:  
cuda 9.1, the system includes AMD R9, Titan V and Titan X. The application running on Titan V.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 6, 2018, 8:00pm UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/2 "2018-04-06T20:00:45Z")

</div>

Perform your driver API context creation before you hit any runtime API calls in your code. The first runtime API call will then “pick up” the driver API context you have created, and use that as the context for the runtime API activity, as well.

The reverse does not work. cuCtxCreate() will not “pick up” a previously created context in use/created by the runtime API. In this reverse scenario, the context associated with the runtime API activity would be accessed in the driver API code via the so-called “Primary” context.

---

<div class="post-metadata">

**Author:** ![SanBase](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SanBase](https://forums.developer.nvidia.com/u/SanBase)\
**Post date:** [April 6, 2018, 8:15pm UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/3 "2018-04-06T20:15:59Z")

</div>

Do you mean that I would have called cuCtxCreate() immediately after the DХ11 device was created? I’m afraid that this is almost impossible, so this is actually DX12 app, and the DX11 resources is created by D3D11On12CreateDevice-\>CreateWrappedResource etc… The textures are created quite elsewhere in the program and much earlier than cuda starting to work.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 6, 2018, 8:57pm UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/4 "2018-04-06T20:57:58Z")

</div>

I was suggesting that before you call any cuda runtime API call in your program, you call your cuda driver API cuCtxCreate().

If you don’t know which calls belong to the cuda runtime API and which belong to the driver API, please review the documentation. Both APIs are documented.

[url][CUDA Runtime API :: CUDA Toolkit Documentation](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html%5B/url%5D)  
[url][CUDA Driver API :: CUDA Toolkit Documentation](http://docs.nvidia.com/cuda/cuda-driver-api/index.html%5B/url%5D)

note that the driver API also has driver-API native graphics interoperability calls:

[url][CUDA Driver API :: CUDA Toolkit Documentation](http://docs.nvidia.com/cuda/cuda-driver-api/group __CUDA__ GRAPHICS.html#group __CUDA__ GRAPHICS%5B/url%5D)  
[url][CUDA Driver API :: CUDA Toolkit Documentation](http://docs.nvidia.com/cuda/cuda-driver-api/group __CUDA__ D3D11.html#group __CUDA__ D3D11%5B/url%5D)

this might be a “cleaner” way to go.

Whether that will fix the issue in your actual program, I don’t know. I don’t have a test case to work with.

If this is “impossible” for whatever reason, then please disregard my comments.

---

<div class="post-metadata">

**Author:** ![SanBase](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SanBase](https://forums.developer.nvidia.com/u/SanBase)\
**Post date:** [April 6, 2018, 10:56pm UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/5 "2018-04-06T22:56:37Z")

</div>

> [@txbob](#):
>
> I was suggesting that before you call any cuda runtime API call in your program, you call your cuda driver API cuCtxCreate().

I run cuCtxCreate first. This is constructor of my CCUDA class:  
CCuda::CCuda()  
{  
int device;  
cudaD3D11GetDevice(&device, vAdapters[TITANV].pAdapter);

```
cudaSetDevice(device); // just in case
checkCudaErrors(cuDeviceGet(&cuDevice, device));

checkCudaErrors(cuCtxCreate(&m_Context, 0, cuDevice));

```

}

IMHO: The problem is not this. The problem is that the resources that I try to map belong to a different address space.

From Cuda programming guide:  
“A CUDA context is analogous to a CPU process. All resources and actions performed within the driver API are encapsulated inside a CUDA context, and the system automatically cleans up these resources when the context is destroyed. 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**.”

I double checked my code - I create the DX11 texture **after** cuCtxCreate():  
p\_CUDA = new CCuda();  
…  
hr = m\_Shared11Device-\>CreateTexture2D(&pDesc, NULL, &tmp\_texture);  
…  
cudaGraphicsD3D11RegisterResource(&m\_texture.cudaResource, tmp\_texture, cudaGraphicsRegisterFlagsNone);  
cudaMallocPitch(&c\_texture.cudaLinearMemory, &c\_texture.pitch, c\_texture.width \* c\_texture.byte\_size, c\_texture.height);  
…

```
ppResources = m_textures[i].cudaResource;
cudaGraphicsMapResources(1, &ppResources, stream); // crash here

```

I do not know CUDA well enough to solve this problem, therefore I ask the question here.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 6, 2018, 11:19pm UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/6 "2018-04-06T23:19:43Z")

</div>

> [@](#):
>
> I actually do it. This is constructor of my CCUDA class:  
> CCuda::CCuda()  
> {  
> int device;  
> cudaD3D11GetDevice(&device, vAdapters[TITANV].pAdapter);
> 
> ```
> cudaSetDevice(device); // just in case
> checkCudaErrors(cuDeviceGet(&cuDevice, device));
> 
> checkCudaErrors(cuCtxCreate(&m_Context, 0, cuDevice));
> 
> ```
> 
> }

Actually, you don’t.

This is a cuda runtime API call:

```auto
cudaD3D11GetDevice(&device, vAdapters[TITANV].pAdapter);
^^^^

```

so is this:

```auto
cudaSetDevice(device); // just in case
^^^^

```

They are both documented here:

[http://docs.nvidia.com/cuda/cuda-runtime-api/index.html](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)

this call, in the constructor, appears later:

```auto
checkCudaErrors(cuCtxCreate(&m_Context, 0, cuDevice));

```

so you actually have 2 CUDA runtime API calls preceding the call to cuCtxCreate(). That will definitely result in a “new”, separate, different context being created. You should not do that, if you want the runtime API to share the context created by cuCtxCreate().

> [@](#):
>
> IMHO: The problem is not this.

I have no idea what the problem in your application is. I was responding to this:

> [@](#):
>
> As I can guess, when I run cuCtxCreate(), the cuda starts new process and my DX11 resources and cuda modules are in different address spaces. So, what should I do in this situation?

That statement is correct. If you initialize the CUDA runtime API (for example via cudaSetDevice()), then call cuCtxCreate() from the driver API, the context created by the driver API will be in a “different address space”. If you don’t want that specific behavior, I’m quite confident in my previous statements about it.

[http://docs.nvidia.com/cuda/cuda-runtime-api/driver-vs-runtime-api.html#driver-vs-runtime-api](http://docs.nvidia.com/cuda/cuda-runtime-api/driver-vs-runtime-api.html#driver-vs-runtime-api)

But whether that is actually giving rise to the problem in your application, I can’t say. “invalid resource handle” could be due to a variety of things. Ultimately, I wouldn’t be able to make any such statement without a test case.

> [@](#):
>
> P.S.  
> From Cuda programming guide:  
> “A CUDA context is analogous to a CPU process. All resources and actions performed within the driver API are encapsulated inside a CUDA context, and the system automatically cleans up these resources when the context is destroyed. 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**.”

That is certainly true if we are talking about two separate contexts. But I have already indicated to you how to get the runtime API to share the context that has been previously created by the driver API. As near as I can tell, if your claim is that your CCuda constructor is enforcing this, then I wouldn’t agree with that. It doesn’t do what I think needs to be done.

If you’re having trouble with CUDA runtime API/CUDA driver API interoperability in your program, then you may wish to simply convert your program to use the CUDA driver API entirely. As I’ve already pointed out, there are corresponding graphics API interoperability routines (API) within the driver API, that correspond to the ones you have indicated so far in the runtime API.

---

<div class="post-metadata">

**Author:** ![SanBase](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SanBase](https://forums.developer.nvidia.com/u/SanBase)\
**Post date:** [April 6, 2018, 11:51pm UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/7 "2018-04-06T23:51:08Z")

</div>

The simplest question is:  
Is it possible to modify ~CUDA\Samples\2\_Graphics\simpleD3D11Texture to work with PTX?  
It would be sufficient for me to understand the principle that it is necessary to change in this sample to implement this.

Тhank you in advance.  
P.S.

> [@](#):
>
> cudaD3D11GetDevice(&device, vAdapters[TITANV].pAdapter);  
> ^^^^  
> so is this:  
> cudaSetDevice(device); // just in case  
> ^^^^  
> this call, in the constructor, appears later:  
> checkCudaErrors(cuCtxCreate(&m\_Context, 0, cuDevice));

Hmm… How I can run cuCtxCreate(&m\_Context,0,cuDevice) first, if I need to have cuDevice?

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 7, 2018, 12:18am UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/8 "2018-04-07T00:18:40Z")

</div>

> [@](#):
>
> The simplest question is:  
> Is it possible to modify ~CUDA\Samples\2\_Graphics\simpleD3D11Texture to work with PTX?

Yes, it should be possible.

The vectorAdd and vectorAddDrv sample code pair shows how a runtime API application may be realized using the driver API, including using PTX kernels. Apart from that, for the remainder of the simpleD3D11Texture app, there should be approximately a 1:1 correspondence between the cuda runtime API calls and their corresponding cuda driver API calls. This may require some study and effort on your part.

> [@](#):
>
> Hmm… How I can run cuCtxCreate(&m\_Context,0,cuDevice) first, if I need to have cuDevice?

perhaps by using the corresponding driver API call, to retrieve the D3D11 device, rather than by using a cuda runtime API call to do so:

[http://docs.nvidia.com/cuda/cuda-driver-api/group\_\_CUDA\_\_D3D11.html#group\_\_CUDA\_\_D3D11](http://docs.nvidia.com/cuda/cuda-driver-api/group __CUDA__ D3D11.html#group __CUDA__ D3D11)

```auto
CUresult cuD3D11GetDevice ( CUdevice* pCudaDevice, IDXGIAdapter* pAdapter )
Gets the CUDA device corresponding to a display adapter.

```

---

<div class="post-metadata">

**Author:** ![SanBase](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SanBase](https://forums.developer.nvidia.com/u/SanBase)\
**Post date:** [April 7, 2018, 12:52am UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/9 "2018-04-07T00:52:57Z")

</div>

> [@txbob](#):
>
> The vectorAdd and vectorAddDrv sample code pair shows how a runtime API application may be realized using the driver API, including using PTX kernels. Apart from that, for the remainder of the simpleD3D11Texture app, there should be approximately a 1:1 correspondence between the cuda runtime API calls and their corresponding cuda driver API calls. This may require some study and effort on your part.

Thank you! I started doing this job and almost converted my application into the driver API. Only one function have no analogues:  
**cudaMemcpy2DToArray** (out\_array, 0, 0, m\_textures[0].cudaLinearMemory, m\_textures[0].pitch,  
m\_textures[0].width \* m\_textures[0].byte\_size, m\_textures[0].height, cudaMemcpyDeviceToDevice);

(I borrowed this code from “simpleD3D11Texture”).  
Could you advise something?

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [April 7, 2018, 2:25am UTC](https://forums.developer.nvidia.com/t/cudagraphicsmapresources-and-cuctxcreate-incompatible/59920/10 "2018-04-07T02:25:54Z")

</div>

cuMemcpy2D can transfer to a destination that is a cuda Array type:

[url][CUDA Driver API :: CUDA Toolkit Documentation](http://docs.nvidia.com/cuda/cuda-driver-api/group __CUDA__ MEM.html#group __CUDA__ MEM_1g27f885b30c34cc20a663a671dbf6fc27%5B/url%5D)
