# Failed on optix::Buffer::setDevicePointer()

**URL:** <https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059>\
**Category:** OptiX\
**Created:** [July 11, 2013, 11:37pm UTC](https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059 "2013-07-11T23:37:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leno](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@leno](https://forums.developer.nvidia.com/u/leno)\
**Post date:** [July 11, 2013, 11:37pm UTC](https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059/1 "2013-07-11T23:37:31Z")

</div>

I am trying to get CUDA OptiX interop working.  
I plan on allocating memory on CUDA and make OptiX Buffer use it.  
My code is as follows:

```auto
Buffer vertex_buffer = m_context->createBufferForCUDA(RT_BUFFER_INPUT, RT_FORMAT_FLOAT3, nverts);
float3 *d_ptr;
cudaMalloc((void **)&d_ptr, sizeof(float3)*nverts);
cudaMemcpy( d_ptr, verts, sizeof(float3)*nverts, cudaMemcpyHostToDevice);
vertex_buffer->setDevicePointer(0, (CUdeviceptr)d_ptr);

```

I got an uncaught exception at setDevicePointer(). The error message is as follows:

```auto
First-chance exception at 0x000007fefd92cacd in sample.exe: Microsoft C++ exception: cudaError at memory location 0x001fdb20..

```

I have two cards on my machine. One is low-end NVS 300 and the other is GTX 670.  
Since the major compute capability of those two cards doesn’t match, only GTX 670 should be used by OptiX. Even if I force device to be used by Buffer::setDevices(), it still doesn’t work.  
Could the device memory address converting between Driver and Runtime API and issue?  
I tried (CUdeviceptr)(uintptr\_t) to cast on d\_ptr, but still with no luck.  
Any one have any idea on what might go wrong?  
Thank you.

---

<div class="post-metadata">

**Author:** ![droettger](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/droettger/32/14043_2.png) [@droettger](https://forums.developer.nvidia.com/u/droettger)\
**Post date:** [July 12, 2013, 8:09am UTC](https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059/2 "2013-07-12T08:09:51Z")

</div>

Please check if all cuda\* calls returned successfully.  
When explicitly selecting the device with rtContextSetDevices() resp. its C++ wrapper (once directly after creating the context) did you match the optix\_device\_number parameter in the rtBufferSetDevicePointer() call?

Does it work if you only use the GTX 670?  
Assuming this is under Windows, you could test this by either disabling the NVS 300 inside the Windows Device Manager temporarily or by limiting the CUDA devices in the NVIDIA Control Panel maybe.

Related topic, this time using the OptiX buffer in CUDA:  
[url][rtBufferGetDevicePointer is reporting null pointer - OptiX - NVIDIA Developer Forums](https://devtalk.nvidia.com/default/topic/545340/optix/rtbuffergetdevicepointer-is-reporting-null-pointer/%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![leno](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@leno](https://forums.developer.nvidia.com/u/leno)\
**Post date:** [July 12, 2013, 5:32pm UTC](https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059/3 "2013-07-12T17:32:20Z")

</div>

Thank you Detlef for responding.  
I stripped out the error checking code while posting. All cuda\* calls returned cudaSuccess. Even rtBufferSetDevicePointer() return RT\_SUCCESS. It failed until OptiX launch call. The exception APIObj::checkError() caught was as follows:

```auto
Invalid context (Details: Function "_rtContextLaunch2D" caught exception: Cannot map CUDA interop buffers, [14614586])

```

The device number is matched. To confirm that, I would like to know if CUDA Device Number match OptiX Device Ordinal? I tried disabling NVS 300 in Windows Device Manager but still with no luck.  
The driver is up-to-date. Is there any other part I might be missing?  
Thanks.

---

<div class="post-metadata">

**Author:** ![leno](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@leno](https://forums.developer.nvidia.com/u/leno)\
**Post date:** [July 15, 2013, 9:58pm UTC](https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059/4 "2013-07-15T21:58:35Z")

</div>

Ok. I think I found the cause.  
I noticed that implicit casting didn’t behave what I expected. The d\_ptr returned by cudaMalloc was 0x0000000 **4** 00720000, but (CUdeviceptr)d\_ptr interpreted it as 0x0000000 **0** 00720000. After applying reinterpret\_cast on d\_ptr, it worked.  
Here is the working code:

```auto
Buffer vertex_buffer = m_context->createBufferForCUDA(RT_BUFFER_INPUT, RT_FORMAT_FLOAT3, nverts);
float3 *d_ptr;
cudaMalloc((void **)&d_ptr, sizeof(float3)*nverts);
cudaMemcpy( d_ptr, verts, sizeof(float3)*nverts, cudaMemcpyHostToDevice);
vertex_buffer->setDevicePointer(0, reinterpret_cast<CUdeviceptr>(d_ptr));

```

In the OptiX 3.0.0 Programming Guide, it said: “CUDA must be initialized using the CUDA runtime API; OptiX does not currently support interop with the CUDA driver API.” Why OptiX needs to interop with the Runtime API but still takes Driver API pointers as set/getDevicePointer input?

---

<div class="post-metadata">

**Author:** ![leno](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@leno](https://forums.developer.nvidia.com/u/leno)\
**Post date:** [July 16, 2013, 5:57pm UTC](https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059/5 "2013-07-16T17:57:34Z")

</div>

Now I have another problem when launching OptiX.  
I got the following exception:

```auto
Invalid context (Details: Function "_rtContextLaunch2D" caught exception: Cannot map CUDA interop buffers, [14614586])

```

I initiate CUDA as follows:

```auto
cudaSetDeviceFlags( cudaDeviceMapHost | cudaDeviceLmemResizeToMax );
cudaFree(0);

```

Is there any version/hardware restriction or known bug related to OptiX CUDA interop?  
Like what type of CUDA buffer it should be created to interact with OptiX…etc.  
Thank you!

---

<div class="post-metadata">

**Author:** ![droettger](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/droettger/32/14043_2.png) [@droettger](https://forums.developer.nvidia.com/u/droettger)\
**Post date:** [July 16, 2013, 6:10pm UTC](https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059/6 "2013-07-16T18:10:56Z")

</div>

Maybe try if float, float2, or float4 instead of float3 work better. float3 is slower to load than float4 anyways.  
I’m not sure about the OptiX to CUDA buffer interop, but for CUDA to OpenGL or D3D texture interop, 3-component texture formats aren’t supported. (Appendix A in the OptiX Programming Guide.)

---

<div class="post-metadata">

**Author:** ![leno](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@leno](https://forums.developer.nvidia.com/u/leno)\
**Post date:** [July 17, 2013, 2:41am UTC](https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059/7 "2013-07-17T02:41:33Z")

</div>

I am wondering why it could be an issue. The buffer format is defined as RT\_BUFFER\_INPUT and it is copied from Host to Device through CUDA Runtime API. Shouldn’t the issue only exist when OptiX needs to copy to the Host memory?  
By the way, before the “Invalid context” exception was caught, I also got the following two uncaught ones:

```auto
First-chance exception at 0x000007fefd63cacd in sample.exe: Microsoft C++ exception: optix::BasicException at memory location 0x0056c710..
First-chance exception at 0x000007fefd63cacd in sample.exe: Microsoft C++ exception: optix::Exception at memory location 0x0056d760..
Invalid context (Details: Function "_rtContextLaunch2D" caught exception: Cannot map CUDA interop buffers, [14614586])

```

Any clue?

---

<div class="post-metadata">

**Author:** ![TomNVIDIA](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/tomnvidia/32/14181_2.png) [@TomNVIDIA](https://forums.developer.nvidia.com/u/TomNVIDIA)\
**Post date:** [June 14, 2022, 9:12pm UTC](https://forums.developer.nvidia.com/t/failed-on-optix-setdevicepointer/30059/8 "2022-06-14T21:12:10Z")

</div>


